Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(API): refactor template upload #1181

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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.
Expand Down
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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 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
Expand Down
23 changes: 12 additions & 11 deletions api/mysagw/identity/management/commands/upload_template.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from argparse import ArgumentTypeError
from pathlib import Path

from django.conf import settings
from django.core.management.base import BaseCommand
Expand All @@ -9,27 +8,28 @@


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(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)
return template

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",
"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):
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import io
from pathlib import Path

import pytest
from django.conf import settings
Expand All @@ -8,15 +7,13 @@

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", "no-existing-template.docx"], False),
(["identity-labels.docx"], True),
(["no-existing-template.docx"], False),
],
)
def test_upload_labels_template_command(requests_mock, args, success):
Expand Down Expand Up @@ -56,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:",
)
3 changes: 3 additions & 0 deletions api/mysagw/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading