#18 File upload in certain folders

* removes 'revocation' folder if RevocationDocument is removed from an intervention and removes the whole intervention document folder as well, if no other documents for this intervention exist
* adds further documentation
* refactors getting of related documents into Intervention model
pull/21/head
mipel 3 years ago
parent cd3eb8099d
commit 9dbea71af5

@ -9,6 +9,7 @@ import shutil
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.gis.db import models from django.contrib.gis.db import models
from django.db.models import QuerySet
from django.utils.timezone import localtime from django.utils.timezone import localtime
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -94,8 +95,39 @@ class RevocationDocument(AbstractDocument):
@property @property
def intervention(self): def intervention(self):
"""
Shortcut for opening the related intervention
Returns:
intervention (Intervention)
"""
return self.instance.legaldata.intervention return self.instance.legaldata.intervention
def delete(self, *args, **kwargs):
"""
Custom delete functionality for RevocationDocuments.
Removes the folder from the file system if there are no further documents for this entry.
Args:
*args ():
**kwargs ():
Returns:
"""
revoc_docs, other_intervention_docs = self.intervention.get_documents()
# Remove the file itself
super().delete(*args, **kwargs)
# Always remove 'revocation' folder
folder_path = self.file.path.split("/")
shutil.rmtree("/".join(folder_path[:-1]))
if other_intervention_docs.count() == 0:
# If there are no further documents for the intervention, we can simply remove the whole folder as well!
shutil.rmtree("/".join(folder_path[:-2]))
class LegalData(UuidModel): class LegalData(UuidModel):
""" """
@ -326,6 +358,21 @@ class Intervention(BaseObject):
tooltip = _("Recorded on {} by {}").format(on, self.recorded.user) tooltip = _("Recorded on {} by {}").format(on, self.recorded.user)
return tooltip return tooltip
def get_documents(self) -> (QuerySet, QuerySet):
""" Getter for all documents of an intervention
Returns:
revoc_docs (QuerySet): The queryset of a revocation document
regular_docs (QuerySet): The queryset of regular other documents
"""
revoc_docs = RevocationDocument.objects.filter(
instance=self.legal.revocation
)
regular_docs = InterventionDocument.objects.filter(
instance=self
)
return revoc_docs, regular_docs
class InterventionDocument(AbstractDocument): class InterventionDocument(AbstractDocument):
""" """
@ -353,12 +400,7 @@ class InterventionDocument(AbstractDocument):
Returns: Returns:
""" """
revoc_docs = RevocationDocument.objects.filter( revoc_docs, other_intervention_docs = self.instance.get_documents()
instance=self.instance.legal.revocation
)
other_intervention_docs = InterventionDocument.objects.filter(
instance=self.instance
)
folder_path = None folder_path = None
if revoc_docs.count() == 0 and other_intervention_docs.count() == 1: if revoc_docs.count() == 0 and other_intervention_docs.count() == 1:

Loading…
Cancel
Save