Skip to content

Commit

Permalink
De-hardcode "Mozilla" in the codebase (mozilla#3458)
Browse files Browse the repository at this point in the history
1. De-hardcode non-transactionoal email footer text

2. De-hardcode .TBX header title and description
* Use multi-line f-strings
* Add unit tests
  • Loading branch information
mathjazz authored Nov 28, 2024
1 parent f751a51 commit 7935bcf
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 105 deletions.
10 changes: 10 additions & 0 deletions docs/admin/deployment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ you create:
It allows to explain what type of communication to expect and to link to
deployment-specific privacy notices among other things.

``EMAIL_COMMUNICATIONS_FOOTER_PRE_TEXT``
Optional. Text to be shown in the footer of the non-transactional emails sent
using the Messaging Center, just above the unsubscribe text.

``ENABLE_BUGS_TAB``
Optional. Enables Bugs tab on team pages, which pulls team data from
bugzilla.mozilla.org. Specific for Mozilla deployments.
Expand Down Expand Up @@ -296,6 +300,12 @@ you create:
Optional. Set your `SYSTRAN Translate API key` to use machine translation
by SYSTRAN.

``TBX_DESCRIPTION``
Optional. Description to be used in the header of the Terminology (.TBX) files.

``TBX_TITLE``
Optional. Title to be used in the header of the Terminology (.TBX) files.

``THROTTLE_ENABLED``
Optional. Enables traffic throttling based on IP address (default: ``False``).

Expand Down
7 changes: 5 additions & 2 deletions pontoon/messaging/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import logging
import uuid

from urllib.parse import urljoin

from guardian.decorators import permission_required_or_403
from notifications.signals import notify

Expand Down Expand Up @@ -306,9 +308,10 @@ def send_message(request):
log.info(f"Notifications sent to {len(recipients)} users.")

if is_email:
unsubscribe_url = urljoin(settings.SITE_URL, f"unsubscribe/{uuid}")
footer = (
"""<br><br>
You’re receiving this email as a contributor to Mozilla localization on Pontoon. <br>To no longer receive emails like these, unsubscribe here: <a href="https://pontoon.mozilla.org/unsubscribe/{ uuid }">Unsubscribe</a>.
f"""<br><br>
{ settings.EMAIL_COMMUNICATIONS_FOOTER_PRE_TEXT }<br>To no longer receive emails like these, unsubscribe here: <a href="{ unsubscribe_url }">Unsubscribe</a>.
"""
if not is_transactional
else ""
Expand Down
7 changes: 7 additions & 0 deletions pontoon/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ def _default_from_email():
EMAIL_CONSENT_MAIN_TEXT = os.environ.get("EMAIL_CONSENT_MAIN_TEXT", "")
EMAIL_CONSENT_PRIVACY_NOTICE = os.environ.get("EMAIL_CONSENT_PRIVACY_NOTICE", "")
EMAIL_COMMUNICATIONS_HELP_TEXT = os.environ.get("EMAIL_COMMUNICATIONS_HELP_TEXT", "")
EMAIL_COMMUNICATIONS_FOOTER_PRE_TEXT = os.environ.get(
"EMAIL_COMMUNICATIONS_FOOTER_PRE_TEXT", ""
)

# Log emails to console if the SendGrid credentials are missing.
if EMAIL_HOST_USER and EMAIL_HOST_PASSWORD:
Expand Down Expand Up @@ -1137,3 +1140,7 @@ def account_username(user):
)

DEFAULT_AUTO_FIELD = "django.db.models.AutoField"

# Used in the header of the Terminology (.TBX) files.
TBX_TITLE = os.environ.get("TBX_TITLE", "Pontoon Terminology")
TBX_DESCRIPTION = os.environ.get("TBX_DESCRIPTION", "Terms localized in Pontoon")
136 changes: 136 additions & 0 deletions pontoon/terminology/tests/test_tbx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import xml.etree.ElementTree as ET

from unittest.mock import MagicMock

import pytest

from pontoon.terminology.utils import build_tbx_v2_file, build_tbx_v3_file


@pytest.fixture
def mock_term_translations():
term_mock = MagicMock()
term_mock.pk = 1
term_mock.text = "Sample Term"
term_mock.part_of_speech = "noun"
term_mock.definition = "Sample Definition"
term_mock.usage = "Sample Usage"

translation_mock = MagicMock()
translation_mock.term = term_mock
translation_mock.text = "Translation"

return [translation_mock]


def test_build_tbx_v2_file(settings, mock_term_translations):
settings.TBX_TITLE = "Sample Title"
settings.TBX_DESCRIPTION = "Sample Description"
locale = "fr-FR"

result = "".join(build_tbx_v2_file(mock_term_translations, locale))

# Fail if the result is not a valid XML
ET.fromstring(result)

# Construct the expected XML
expected = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE martif SYSTEM "TBXcoreStructV02.dtd">
<martif type="TBX" xml:lang="en-US">
<martifHeader>
<fileDesc>
<titleStmt>
<title>Sample Title</title>
</titleStmt>
<sourceDesc>
<p>Sample Description</p>
</sourceDesc>
</fileDesc>
<encodingDesc>
<p type="XCSURI">TBXXCSV02.xcs</p>
</encodingDesc>
</martifHeader>
<text>
<body>
<termEntry id="c1">
<descrip type="context">Sample Usage</descrip>
<langSet xml:lang="en-US">
<ntig>
<termGrp>
<term>Sample Term</term>
<termNote type="partOfSpeech">noun</termNote>
</termGrp>
</ntig>
<descripGrp>
<descrip type="definition">Sample Definition</descrip>
</descripGrp>
</langSet>
<langSet xml:lang="fr-FR">
<ntig>
<termGrp>
<term>Translation</term>
</termGrp>
</ntig>
</langSet>
</termEntry>
</body>
</text>
</martif>"""

# Assert that the generated result matches the expected XML
assert result.strip() == expected.strip()


def test_build_tbx_v3_file(settings, mock_term_translations):
settings.TBX_TITLE = "Sample Title"
settings.TBX_DESCRIPTION = "Sample Description"
locale = "fr-FR"

result = "".join(build_tbx_v3_file(mock_term_translations, locale))

# Fail if the result is not a valid XML
ET.fromstring(result)

# Construct the expected XML
expected = """<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="https://raw.githubusercontent.com/LTAC-Global/TBX-Basic_dialect/master/DCA/TBXcoreStructV03_TBX-Basic_integrated.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?>
<?xml-model href="https://raw.githubusercontent.com/LTAC-Global/TBX-Basic_dialect/master/DCA/TBX-Basic_DCA.sch" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>
<tbx style="dca" type="TBX-Basic" xml:lang="en" xmlns="urn:iso:std:iso:30042:ed-2">
<tbxHeader>
<fileDesc>
<titleStmt>
<title>Sample Title</title>
</titleStmt>
<sourceDesc>
<p>Sample Description</p>
</sourceDesc>
</fileDesc>
<encodingDesc>
<p type="XCSURI">TBXXCSV02.xcs</p>
</encodingDesc>
</tbxHeader>
<text>
<body>
<conceptEntry id="c1">
<langSec xml:lang="en-US">
<termSec>
<term>Sample Term</term>
<termNote type="partOfSpeech">noun</termNote>
<descripGrp>
<descrip type="definition">Sample Definition</descrip>
<descrip type="context">Sample Usage</descrip>
</descripGrp>
</termSec>
</langSec>
<langSec xml:lang="fr-FR">
<termSec>
<term>Translation</term>
</termSec>
</langSec>
</conceptEntry>
</body>
</text>
</tbx>"""

# Assert that the generated result matches the expected XML
assert result.strip() == expected.strip()
Loading

0 comments on commit 7935bcf

Please sign in to comment.