From 13560b3b88c30240aaf75fbde17f5692caf1c1f5 Mon Sep 17 00:00:00 2001 From: Fabio Ambauen Date: Thu, 19 Dec 2024 16:22:34 +0100 Subject: [PATCH 1/3] chore(API): refactor template upload Since the refactoring of the build, the documented commands don't work anymore. Additionally the path to the templates changes now dynamically with the python version used. This commit puts all the templates into one global template directory. The command is refactored, so that it's sufficient to provide the file name. The path to the template will then be constructed in the command. --- README.md | 18 +++++++++--------- .../management/commands/upload_template.py | 8 ++++---- .../test_upload_labels_template_command.py | 5 +---- .../templates/accounting-cover.docx | Bin .../templates/acknowledgement-de.docx | Bin .../templates/acknowledgement-en.docx | Bin .../templates/acknowledgement-fr.docx | Bin .../{case => }/templates/application.docx | Bin .../templates/credit-approval-de.docx | Bin .../templates/credit-approval-en.docx | Bin .../templates/credit-approval-fr.docx | Bin .../templates/identity-labels.docx | Bin 12 files changed, 14 insertions(+), 17 deletions(-) rename api/mysagw/{accounting => }/templates/accounting-cover.docx (100%) rename api/mysagw/{case => }/templates/acknowledgement-de.docx (100%) rename api/mysagw/{case => }/templates/acknowledgement-en.docx (100%) rename api/mysagw/{case => }/templates/acknowledgement-fr.docx (100%) rename api/mysagw/{case => }/templates/application.docx (100%) rename api/mysagw/{case => }/templates/credit-approval-de.docx (100%) rename api/mysagw/{case => }/templates/credit-approval-en.docx (100%) rename api/mysagw/{case => }/templates/credit-approval-fr.docx (100%) rename api/mysagw/{identity => }/templates/identity-labels.docx (100%) diff --git a/README.md b/README.md index 224f0d6a7..df6e6a8ef 100644 --- a/README.md +++ b/README.md @@ -85,15 +85,15 @@ docker compose up -d # Wait for the database migrations to complete for the API and Caluma. make caluma-loadconfig # upload the templates to DMS -docker compose run --rm api poetry run ./manage.py upload_template -t mysagw/identity/templates/identity-labels.docx -docker compose run --rm api poetry run ./manage.py upload_template -t mysagw/accounting/templates/accounting-cover.docx -docker compose run --rm api poetry run ./manage.py upload_template -t mysagw/case/templates/acknowledgement-de.docx -docker compose run --rm api poetry run ./manage.py upload_template -t mysagw/case/templates/acknowledgement-fr.docx -docker compose run --rm api poetry run ./manage.py upload_template -t mysagw/case/templates/acknowledgement-en.docx -docker compose run --rm api poetry run ./manage.py upload_template -t mysagw/case/templates/credit-approval-de.docx -docker compose run --rm api poetry run ./manage.py upload_template -t mysagw/case/templates/credit-approval-fr.docx -docker compose run --rm api poetry run ./manage.py upload_template -t mysagw/case/templates/credit-approval-en.docx -docker compose run --rm api poetry run ./manage.py upload_template -t mysagw/case/templates/application.docx +docker compose run --rm api python manage.py upload_template -t identity-labels.docx +docker compose run --rm api python manage.py upload_template -t accounting-cover.docx +docker compose run --rm api python manage.py upload_template -t acknowledgement-de.docx +docker compose run --rm api python manage.py upload_template -t acknowledgement-fr.docx +docker compose run --rm api python manage.py upload_template -t acknowledgement-en.docx +docker compose run --rm api python manage.py upload_template -t credit-approval-de.docx +docker compose run --rm api python manage.py upload_template -t credit-approval-fr.docx +docker compose run --rm api python manage.py upload_template -t credit-approval-en.docx +docker compose run --rm api python manage.py upload_template -t application.docx ``` ## Contributing diff --git a/api/mysagw/identity/management/commands/upload_template.py b/api/mysagw/identity/management/commands/upload_template.py index b1137c625..074d08da1 100644 --- a/api/mysagw/identity/management/commands/upload_template.py +++ b/api/mysagw/identity/management/commands/upload_template.py @@ -16,7 +16,9 @@ def __init__(self, *args, **kwargs): self.client = DMSClient() def template_file(self, value): - template = Path(value) + template = ( + Path(__file__).parent.parent.parent.parent.absolute() / "templates" / value + ) if not template.exists() or not template.is_file: msg = f"Template not found: {template}" raise ArgumentTypeError(msg) @@ -26,9 +28,7 @@ def add_arguments(self, parser): parser.add_argument( "-t", "--template", - default=Path(__file__).parent.parent.parent.absolute() - / "templates" - / f"{settings.DOCUMENT_MERGE_SERVICE_LABELS_TEMPLATE_SLUG}.docx", + default=f"{settings.DOCUMENT_MERGE_SERVICE_LABELS_TEMPLATE_SLUG}.docx", type=self.template_file, ) diff --git a/api/mysagw/identity/tests/test_upload_labels_template_command.py b/api/mysagw/identity/tests/test_upload_labels_template_command.py index 87790309d..d0076d8c3 100644 --- a/api/mysagw/identity/tests/test_upload_labels_template_command.py +++ b/api/mysagw/identity/tests/test_upload_labels_template_command.py @@ -1,5 +1,4 @@ import io -from pathlib import Path import pytest from django.conf import settings @@ -8,14 +7,12 @@ from mysagw.utils import build_url -TEMPLATE_PATH = Path(__file__).parent.parent.absolute() / "templates" - @pytest.mark.parametrize( "args,success", [ ([], True), - (["-t", TEMPLATE_PATH / "identity-labels.docx"], True), + (["-t", "identity-labels.docx"], True), (["-t", "no-existing-template.docx"], False), ], ) diff --git a/api/mysagw/accounting/templates/accounting-cover.docx b/api/mysagw/templates/accounting-cover.docx similarity index 100% rename from api/mysagw/accounting/templates/accounting-cover.docx rename to api/mysagw/templates/accounting-cover.docx diff --git a/api/mysagw/case/templates/acknowledgement-de.docx b/api/mysagw/templates/acknowledgement-de.docx similarity index 100% rename from api/mysagw/case/templates/acknowledgement-de.docx rename to api/mysagw/templates/acknowledgement-de.docx diff --git a/api/mysagw/case/templates/acknowledgement-en.docx b/api/mysagw/templates/acknowledgement-en.docx similarity index 100% rename from api/mysagw/case/templates/acknowledgement-en.docx rename to api/mysagw/templates/acknowledgement-en.docx diff --git a/api/mysagw/case/templates/acknowledgement-fr.docx b/api/mysagw/templates/acknowledgement-fr.docx similarity index 100% rename from api/mysagw/case/templates/acknowledgement-fr.docx rename to api/mysagw/templates/acknowledgement-fr.docx diff --git a/api/mysagw/case/templates/application.docx b/api/mysagw/templates/application.docx similarity index 100% rename from api/mysagw/case/templates/application.docx rename to api/mysagw/templates/application.docx diff --git a/api/mysagw/case/templates/credit-approval-de.docx b/api/mysagw/templates/credit-approval-de.docx similarity index 100% rename from api/mysagw/case/templates/credit-approval-de.docx rename to api/mysagw/templates/credit-approval-de.docx diff --git a/api/mysagw/case/templates/credit-approval-en.docx b/api/mysagw/templates/credit-approval-en.docx similarity index 100% rename from api/mysagw/case/templates/credit-approval-en.docx rename to api/mysagw/templates/credit-approval-en.docx diff --git a/api/mysagw/case/templates/credit-approval-fr.docx b/api/mysagw/templates/credit-approval-fr.docx similarity index 100% rename from api/mysagw/case/templates/credit-approval-fr.docx rename to api/mysagw/templates/credit-approval-fr.docx diff --git a/api/mysagw/identity/templates/identity-labels.docx b/api/mysagw/templates/identity-labels.docx similarity index 100% rename from api/mysagw/identity/templates/identity-labels.docx rename to api/mysagw/templates/identity-labels.docx From bdb6154bbe2f2f2f995619bd40f31ebb192d3eb3 Mon Sep 17 00:00:00 2001 From: Fabio Ambauen Date: Fri, 20 Dec 2024 07:27:18 +0100 Subject: [PATCH 2/3] feat(API): allow for uploading multiple templates at once This commit refactors the `upload_template` command, so that is now uses positional arguments for templates and can handle multiple at once. --- README.md | 13 ++++------- .../management/commands/upload_template.py | 23 ++++++++++--------- .../test_upload_labels_template_command.py | 6 ++--- api/mysagw/settings.py | 3 +++ 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index df6e6a8ef..7b8dd0b23 100644 --- a/README.md +++ b/README.md @@ -85,15 +85,10 @@ docker compose up -d # Wait for the database migrations to complete for the API and Caluma. make caluma-loadconfig # upload the templates to DMS -docker compose run --rm api python manage.py upload_template -t identity-labels.docx -docker compose run --rm api python manage.py upload_template -t accounting-cover.docx -docker compose run --rm api python manage.py upload_template -t acknowledgement-de.docx -docker compose run --rm api python manage.py upload_template -t acknowledgement-fr.docx -docker compose run --rm api python manage.py upload_template -t acknowledgement-en.docx -docker compose run --rm api python manage.py upload_template -t credit-approval-de.docx -docker compose run --rm api python manage.py upload_template -t credit-approval-fr.docx -docker compose run --rm api python manage.py upload_template -t credit-approval-en.docx -docker compose run --rm api python manage.py upload_template -t application.docx +docker compose run --rm api python manage.py upload_template identity-labels.docx \ + accounting-cover.docx acknowledgement-de.docx acknowledgement-fr.docx \ + acknowledgement-en.docx credit-approval-de.docx credit-approval-fr.docx \ + credit-approval-en.docx application.docx ``` ## Contributing diff --git a/api/mysagw/identity/management/commands/upload_template.py b/api/mysagw/identity/management/commands/upload_template.py index 074d08da1..9869f7256 100644 --- a/api/mysagw/identity/management/commands/upload_template.py +++ b/api/mysagw/identity/management/commands/upload_template.py @@ -1,5 +1,4 @@ from argparse import ArgumentTypeError -from pathlib import Path from django.conf import settings from django.core.management.base import BaseCommand @@ -9,16 +8,14 @@ class Command(BaseCommand): - help = "Upload template for exports" + help = "Upload templates for exports" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.client = DMSClient() def template_file(self, value): - template = ( - Path(__file__).parent.parent.parent.parent.absolute() / "templates" / value - ) + template = settings.DOCUMENT_MERGE_SERVICE_TEMPLATE_DIR / value if not template.exists() or not template.is_file: msg = f"Template not found: {template}" raise ArgumentTypeError(msg) @@ -26,10 +23,13 @@ def template_file(self, value): def add_arguments(self, parser): parser.add_argument( - "-t", - "--template", - default=f"{settings.DOCUMENT_MERGE_SERVICE_LABELS_TEMPLATE_SLUG}.docx", + "templates", + default=[ + settings.DOCUMENT_MERGE_SERVICE_TEMPLATE_DIR + / f"{settings.DOCUMENT_MERGE_SERVICE_LABELS_TEMPLATE_SLUG}.docx" + ], type=self.template_file, + nargs="*", ) def _upload_template(self, template, update=False): @@ -51,6 +51,7 @@ def _upload_template(self, template, update=False): return True def handle(self, *args, **options): - template = options.get("template") - if self._upload_template(template) is False: - assert self._upload_template(template, update=True) is True + templates = options.get("templates") + for template in templates: + if self._upload_template(template) is False: + assert self._upload_template(template, update=True) is True diff --git a/api/mysagw/identity/tests/test_upload_labels_template_command.py b/api/mysagw/identity/tests/test_upload_labels_template_command.py index d0076d8c3..87be68bee 100644 --- a/api/mysagw/identity/tests/test_upload_labels_template_command.py +++ b/api/mysagw/identity/tests/test_upload_labels_template_command.py @@ -12,8 +12,8 @@ "args,success", [ ([], True), - (["-t", "identity-labels.docx"], True), - (["-t", "no-existing-template.docx"], False), + (["identity-labels.docx"], True), + (["no-existing-template.docx"], False), ], ) def test_upload_labels_template_command(requests_mock, args, success): @@ -53,5 +53,5 @@ def do_assertions(m): with pytest.raises(CommandError) as e: call_command("upload_template", *args) assert e.value.args[0].startswith( - "Error: argument -t/--template: Template not found:", + "Error: argument templates: Template not found:", ) diff --git a/api/mysagw/settings.py b/api/mysagw/settings.py index 825b87ded..b96766b12 100644 --- a/api/mysagw/settings.py +++ b/api/mysagw/settings.py @@ -168,11 +168,14 @@ def default(default_dev=env.NOTSET, default_prod=env.NOTSET): "DOCUMENT_MERGE_SERVICE_URL", default="http://dms:8000/api/v1", ) + DOCUMENT_MERGE_SERVICE_ENGINE = env.str( "DOCUMENT_MERGE_SERVICE_ENGINE", default="docx-template", ) +DOCUMENT_MERGE_SERVICE_TEMPLATE_DIR = Path(__file__).parent.resolve() / "templates" + DOCUMENT_MERGE_SERVICE_LABELS_TEMPLATE_SLUG = env.str( "DOCUMENT_MERGE_SERVICE_LABELS_TEMPLATE_SLUG", default="identity-labels", From 986f012886b04e51070d73c866e0c1bce1b0112f Mon Sep 17 00:00:00 2001 From: Fabio Ambauen Date: Fri, 20 Dec 2024 07:28:50 +0100 Subject: [PATCH 3/3] chore: improve dev docs --- CONTRIBUTING.md | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7cc40d10a..b1376ee51 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,8 +1,9 @@ # Contributing -Contributions to mysagw are very welcome! Best have a look at the open [issues](https://github.com/adfinis-sygroup/mysagw) -and open a [GitHub pull request](https://github.com/adfinis-sygroup/mysagw/compare). See instructions below how to setup development -environment. Before writing any code, best discuss your proposed change in a GitHub issue to see if the proposed change makes sense for the project. +Contributions to mySAGW are very welcome! Best have a look at the open [issues](https://github.com/adfinis-sygroup/mysagw) +and open a [GitHub pull request](https://github.com/adfinis-sygroup/mysagw/compare). See instructions below how to set up development +environment. Before writing any code, best discuss your proposed change in a GitHub +issue to see if the proposed change makes sense for the project. ## Setup development environment @@ -11,14 +12,23 @@ environment. Before writing any code, best discuss your proposed change in a Git To work on mysagw you first need to clone ```bash -git clone https://github.com/adfinis-sygroup/mysagw.git -cd mysagw +git clone https://github.com/adfinis/mySAGW.git +cd mySAGW +``` + +### Build the dev containers + +By default, it pulls images for prod, that don't contain any tooling needed during dev. +In order to have those tools available, you need to build those containers first: + +```bash +docker compose build --pull api caluma ``` ### Open Shell Once it is cloned you can easily open a shell in the docker container to -open an development environment. +open a development environment. ```bash # needed for permission handling @@ -43,8 +53,7 @@ ruff format . pytest # create migrations ./manage.py makemigrations -# install debugger or other temporary dependencies -pip install --user pdbpp +# you can use pip to install other temporary dependencies ``` Writing of code can still happen outside the docker container of course.