Skip to content

Commit

Permalink
feat: add backed for tracking agreement and implement DPA
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed Nov 14, 2024
1 parent 634997c commit f96cdd3
Show file tree
Hide file tree
Showing 11 changed files with 744 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
venv
/node_modules/
/invoices/
/agreements/
/.coverage
/junit.xml
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ python-dateutil==2.9.0.post0
qrcode==8.0
requests==2.32.3
sentry-sdk==2.18.0
unidecode==1.3.8
weasyprint==62.3
weblate-language-data==2024.14
weblate-schemas==2024.2
Expand Down
43 changes: 43 additions & 0 deletions weblate_web/legal/migrations/0001_initial.py
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",
),
),
],
),
]
104 changes: 103 additions & 1 deletion weblate_web/legal/models.py
Original file line number Diff line number Diff line change
@@ -1 +1,103 @@
# 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, cast

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}"

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 kind_name(self) -> str:
return cast(AgreementKind, self.kind).name

@property
def filename(self) -> str:
"""PDF filename."""
return f"Weblate_{self.kind_name}_{self.customer.short_filename}_{self.shortdate}_{self.pk}.pdf"

@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(),
},
)
Loading

0 comments on commit f96cdd3

Please sign in to comment.