Skip to content

Commit

Permalink
Translator: fill nationality of translators in letter template
Browse files Browse the repository at this point in the history
TYPE: Bugfix
LINK: ogc-1805
  • Loading branch information
Tschuppi81 authored Aug 29, 2024
1 parent fae0306 commit 011c922
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/onegov/translator_directory/generate_docx.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
from onegov.org.models import GeneralFileCollection, GeneralFile
from onegov.ticket import Ticket, TicketCollection
from onegov.translator_directory import _
from onegov.translator_directory.utils import country_code_to_name
from docxtpl import DocxTemplate, InlineImage # type:ignore[import-untyped]


from typing import Any, IO, NamedTuple, TYPE_CHECKING

if TYPE_CHECKING:
from collections.abc import Iterator
from onegov.translator_directory.models.translator import Translator
Expand All @@ -30,11 +32,15 @@ def fill_docx_with_variables(
- The rendered docx file (bytes).
"""

mapping = country_code_to_name(request.locale)
nationalities = ', '.join(mapping[n] for n in t.nationalities) if (
t.nationalities) else ''

docx_template = DocxTemplate(original_docx)
template_variables = {
'translator_last_name': t.last_name,
'translator_first_name': t.first_name,
'translator_nationalities': t.nationalities,
'translator_nationality': nationalities,
'translator_address': t.address,
'translator_city': t.city,
'translator_zip_code': t.zip_code,
Expand Down
6 changes: 5 additions & 1 deletion src/onegov/translator_directory/views/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@
AddTranslatorLayout, TranslatorCollectionLayout, TranslatorLayout,
EditTranslatorLayout, ReportTranslatorChangesLayout, MailTemplatesLayout)
from onegov.translator_directory.models.translator import Translator
from onegov.translator_directory.utils import country_code_to_name

from uuid import uuid4
from xlsxwriter import Workbook # type:ignore[import-untyped]
from docx.image.exceptions import UnrecognizedImageError


from typing import TYPE_CHECKING

if TYPE_CHECKING:
from datetime import date, datetime
from collections.abc import Iterable
Expand Down Expand Up @@ -180,9 +183,10 @@ def format_gender(gender: 'Gender | None') -> str:
return request.translate(GENDERS[gender])

def format_nationalities(nationalities: list[str] | None) -> str:
mapping = country_code_to_name(request.locale)
if not nationalities:
return ''
return ', '.join(nationalities)
return ', '.join(mapping[n] for n in nationalities)

worksheet = workbook.add_worksheet()
worksheet.name = request.translate(_("Translator directory"))
Expand Down
Binary file modified tests/onegov/translator_directory/fixtures/Vorlage.docx
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/onegov/translator_directory/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
self_employed=False,
gender=list(GENDERS.keys())[0],
date_of_birth=date.today(),
nationalities=['Schweiz'],
nationalities=['CH', 'AT'],
address='Downing Street 5',
zip_code='4000',
city='Luzern',
Expand Down
8 changes: 8 additions & 0 deletions tests/onegov/translator_directory/test_mail_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
parse_from_filename,
)
from onegov.translator_directory.models.translator import Translator
from onegov.translator_directory.utils import country_code_to_name
from onegov.translator_directory.views.translator import (
fill_docx_with_variables,
)
Expand All @@ -28,6 +29,12 @@ def test_read_write_cycle():
request = Bunch(locale='en', app=Bunch(version='1.0', sentry_dsn=None))

layout = Layout(model=object(), request=request)

mapping = country_code_to_name(request.locale)
nationalities = (
', '.join(mapping[n] for n in translator.nationalities)) if (
translator.nationalities) else ''

variables_to_fill = {
'current_date': layout.format_date(utcnow(), 'date'),
'translator_date_of_birth': layout.format_date(
Expand All @@ -43,6 +50,7 @@ def test_read_write_cycle():
'translator_address': translator.address,
'translator_zip_code': translator.zip_code,
'translator_city': translator.city,
'translator_nationality': nationalities,
'translator_admission': '',
}

Expand Down
2 changes: 1 addition & 1 deletion tests/onegov/translator_directory/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def test_view_export_translators(client):
assert sheet.cell(2, 6).value == 'Hugo'
assert sheet.cell(2, 7).value == 'Männlich'
assert sheet.cell(2, 8).value == data['date_of_birth'].isoformat()
assert sheet.cell(2, 9).value == 'Schweiz'
assert sheet.cell(2, 9).value == 'Schweiz, Österreich'
# assert sheet.cell(2, 10).value == '{"lon":null,"zoom":null,"lat":null}'
assert sheet.cell(2, 11).value == 'Downing Street 5'
assert sheet.cell(2, 12).value == '4000'
Expand Down

0 comments on commit 011c922

Please sign in to comment.