#18 File upload in certain folders

* refactors documents and file upload to be distributed into different subfolders, depending on the type of document (InterventionDocument, RevocationDocument, ...)
* refactors Document model into AbstractDocument
* subclasses RevocationDocument, InterventionDocument, COmpensationDocument, EmaDocument and EcoAccountDocument from AbstractDocument to provide proper functionality for each
* adds new specialized routes for each new document type (opening, removing)
* drops generic get and remove routes for documents
This commit is contained in:
mipel
2021-09-01 16:24:49 +02:00
parent 17d574e2a5
commit f64a11cb50
25 changed files with 437 additions and 109 deletions
+20 -7
View File
@@ -21,11 +21,11 @@ from django.shortcuts import render
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from compensation.models import EcoAccount
from ema.models import Ema
from intervention.models import Intervention
from compensation.models import EcoAccount, Compensation, EcoAccountDocument, CompensationDocument
from ema.models import Ema, EmaDocument
from intervention.models import Intervention, Revocation, RevocationDocument, InterventionDocument
from konova.contexts import BaseContext
from konova.models import Document, BaseObject
from konova.models import BaseObject
from konova.utils.message_templates import FORM_INVALID
from user.models import UserActionLogEntry, UserAction
@@ -307,7 +307,7 @@ class NewDocumentForm(BaseModalForm):
attrs={
"class": "w-75"
}
)
),
)
comment = forms.CharField(
required=False,
@@ -322,6 +322,13 @@ class NewDocumentForm(BaseModalForm):
}
)
)
document_instance_map = {
Intervention: InterventionDocument,
Compensation: CompensationDocument,
EcoAccount: EcoAccountDocument,
Revocation: RevocationDocument,
Ema: EmaDocument,
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@@ -331,6 +338,12 @@ class NewDocumentForm(BaseModalForm):
self.form_attrs = {
"enctype": "multipart/form-data", # important for file upload
}
self.document_type = self.document_instance_map.get(
self.instance.__class__,
None
)
if not self.document_type:
raise NotImplementedError("Unsupported document type for {}".format(self.instance.__class__))
def save(self):
with transaction.atomic():
@@ -338,14 +351,14 @@ class NewDocumentForm(BaseModalForm):
user=self.user,
action=UserAction.CREATED,
)
doc = Document.objects.create(
doc = self.document_type.objects.create(
created=action,
title=self.cleaned_data["title"],
comment=self.cleaned_data["comment"],
file=self.cleaned_data["file"],
date_of_creation=self.cleaned_data["creation_date"],
instance=self.instance,
)
self.instance.documents.add(doc)
edited_action = UserActionLogEntry.objects.create(
user=self.user,