-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add backed for tracking agreement and implement DPA
- Loading branch information
Showing
9 changed files
with
702 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,6 @@ | |
venv | ||
/node_modules/ | ||
/invoices/ | ||
/agreements/ | ||
/.coverage | ||
/junit.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Generated by Django 5.1.2 on 2024-11-14 11:26 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
initial = True | ||
|
||
dependencies = [ | ||
("payments", "0037_alter_customer_vat"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Agreement", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("signed", models.DateTimeField(auto_now_add=True)), | ||
( | ||
"kind", | ||
models.IntegerField( | ||
choices=[(1, "Data Processing Agreement")], default=1 | ||
), | ||
), | ||
( | ||
"customer", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.PROTECT, | ||
to="payments.customer", | ||
), | ||
), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,99 @@ | ||
# Create your models here. | ||
# | ||
# Copyright © Michal Čihař <[email protected]> | ||
# | ||
# This file is part of Weblate <https://weblate.org/> | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
# | ||
from __future__ import annotations | ||
|
||
from shutil import copyfile | ||
from typing import TYPE_CHECKING | ||
|
||
from django.conf import settings | ||
from django.db import models | ||
from django.template.loader import render_to_string | ||
from django.utils.translation import override | ||
|
||
from weblate_web.pdf import render_pdf | ||
|
||
if TYPE_CHECKING: | ||
from pathlib import Path | ||
|
||
|
||
class AgreementKind(models.IntegerChoices): | ||
DPA = 1, "Data Processing Agreement" | ||
|
||
|
||
class Agreement(models.Model): | ||
customer = models.ForeignKey("payments.Customer", on_delete=models.deletion.PROTECT) | ||
signed = models.DateTimeField(auto_now_add=True) | ||
kind = models.IntegerField(choices=AgreementKind, default=AgreementKind.DPA) | ||
|
||
def __str__(self) -> str: | ||
return f"{self.kind.name} {self.customer.name} {self.shortdate}" | ||
Check failure on line 45 in weblate_web/legal/models.py GitHub Actions / mypy
|
||
|
||
def save( # type: ignore[override] | ||
self, | ||
*, | ||
force_insert: bool = False, | ||
force_update: bool = False, | ||
using=None, | ||
update_fields=None, | ||
): | ||
super().save( | ||
force_insert=force_insert, | ||
force_update=force_update, | ||
using=using, | ||
update_fields=update_fields, | ||
) | ||
self.generate_files() | ||
|
||
@property | ||
def shortdate(self) -> str: | ||
return self.signed.date().isoformat() | ||
|
||
@property | ||
def filename(self) -> str: | ||
"""PDF filename.""" | ||
return f"Weblate_{self.kind.name}_{self.shortdate}_{self.pk}.pdf" | ||
Check failure on line 70 in weblate_web/legal/models.py GitHub Actions / mypy
|
||
|
||
@property | ||
def path(self) -> Path: | ||
"""PDF path object.""" | ||
return settings.AGREEMENTS_PATH / self.filename | ||
|
||
def generate_files(self) -> None: | ||
self.generate_pdf() | ||
if settings.AGREEMENTS_COPY_PATH: | ||
copyfile(self.path, settings.AGREEMENTS_COPY_PATH / self.filename) | ||
|
||
def generate_pdf(self) -> None: | ||
# Create directory to store agreements | ||
settings.AGREEMENTS_PATH.mkdir(exist_ok=True) | ||
render_pdf( | ||
html=self.render_html(), | ||
output=settings.AGREEMENTS_PATH / self.filename, | ||
) | ||
|
||
def render_html(self) -> str: | ||
with override("en_GB"): | ||
return render_to_string( | ||
"pdf/dpa.html", | ||
{ | ||
"customer": self.customer, | ||
"signed": self.signed, | ||
"title": self.get_kind_display(), | ||
}, | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,25 @@ | ||
# Create your tests here. | ||
from weblate_web.payments.models import Customer | ||
from weblate_web.tests import UserTestCase | ||
|
||
from .models import Agreement, AgreementKind | ||
|
||
|
||
class InvoiceTestCase(UserTestCase): | ||
def create_customer(self, *, vat: str = "") -> Customer: | ||
return Customer.objects.create( | ||
name="Zkušební zákazník", | ||
address="Street 42", | ||
city="City", | ||
postcode="424242", | ||
country="cz", | ||
user_id=-1, | ||
vat=vat, | ||
) | ||
|
||
def test_agreement(self): | ||
agreement = Agreement.objects.create( | ||
customer=self.create_customer(), kind=AgreementKind.DPA | ||
) | ||
self.assertTrue(agreement.path.exists()) | ||
self.assertIn("DPA", str(agreement)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters