Compare commits

..

5 Commits

Author SHA1 Message Date
mipel
1edc1edc98 #18 File upload in certain folders
* removes document file folder if the last EmaDocument is removed from an Ema
2021-09-17 09:24:31 +02:00
mipel
e7b6b4dd8d #18 File upload in certain folders
* removes document file folder if the last EcoAccountDocument is removed from an eco account
2021-09-17 09:20:20 +02:00
mipel
799ce8d72c #18 File upload in certain folders
* removes document file folder if the last CompensationDocument is removed from a compensation
2021-09-17 09:16:55 +02:00
mipel
9dbea71af5 #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
2021-09-17 09:05:43 +02:00
mipel
cd3eb8099d #18 File upload in certain folders
* removes folder if last InterventionDocument is removed from an intervention and no RevocationDocument exists as well
2021-09-16 09:12:32 +02:00
3 changed files with 206 additions and 2 deletions

View File

@ -5,10 +5,12 @@ Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 17.11.20 Created on: 17.11.20
""" """
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.core.validators import MinValueValidator from django.core.validators import MinValueValidator
from django.db.models import Sum from django.db.models import Sum, QuerySet
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from codelist.models import KonovaCode from codelist.models import KonovaCode
@ -197,6 +199,17 @@ class Compensation(AbstractCompensation):
y, y,
) )
def get_documents(self) -> QuerySet:
""" Getter for all documents of a compensation
Returns:
docs (QuerySet): The queryset of all documents
"""
docs = CompensationDocument.objects.filter(
instance=self
)
return docs
class CompensationDocument(AbstractDocument): class CompensationDocument(AbstractDocument):
""" """
@ -212,6 +225,35 @@ class CompensationDocument(AbstractDocument):
max_length=1000, max_length=1000,
) )
def delete(self, *args, **kwargs):
"""
Custom delete functionality for CompensationDocuments.
Removes the folder from the file system if there are no further documents for this entry.
Args:
*args ():
**kwargs ():
Returns:
"""
comp_docs = self.instance.get_documents()
folder_path = None
if comp_docs.count() == 1:
# The only file left for this compensation is the one which is currently processed and will be deleted
# Make sure that the compensation folder itself is deleted as well, not only the file
# Therefore take the folder path from the file path
folder_path = self.file.path.split("/")[:-1]
folder_path = "/".join(folder_path)
# Remove the file itself
super().delete(*args, **kwargs)
# If a folder path has been set, we need to delete the whole folder!
if folder_path is not None:
shutil.rmtree(folder_path)
class EcoAccount(AbstractCompensation): class EcoAccount(AbstractCompensation):
""" """
@ -313,6 +355,17 @@ class EcoAccount(AbstractCompensation):
return ret_msgs return ret_msgs
def get_documents(self) -> QuerySet:
""" Getter for all documents of an EcoAccount
Returns:
docs (QuerySet): The queryset of all documents
"""
docs = EcoAccountDocument.objects.filter(
instance=self
)
return docs
class EcoAccountDocument(AbstractDocument): class EcoAccountDocument(AbstractDocument):
""" """
@ -328,6 +381,35 @@ class EcoAccountDocument(AbstractDocument):
max_length=1000, max_length=1000,
) )
def delete(self, *args, **kwargs):
"""
Custom delete functionality for EcoAccountDocuments.
Removes the folder from the file system if there are no further documents for this entry.
Args:
*args ():
**kwargs ():
Returns:
"""
acc_docs = self.instance.get_documents()
folder_path = None
if acc_docs.count() == 1:
# The only file left for this eco account is the one which is currently processed and will be deleted
# Make sure that the compensation folder itself is deleted as well, not only the file
# Therefore take the folder path from the file path
folder_path = self.file.path.split("/")[:-1]
folder_path = "/".join(folder_path)
# Remove the file itself
super().delete(*args, **kwargs)
# If a folder path has been set, we need to delete the whole folder!
if folder_path is not None:
shutil.rmtree(folder_path)
class EcoAccountDeduction(BaseResource): class EcoAccountDeduction(BaseResource):
""" """

View File

@ -1,5 +1,8 @@
import shutil
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.db import models from django.db import models
from django.db.models import QuerySet
from compensation.models import AbstractCompensation from compensation.models import AbstractCompensation
from konova.models import AbstractDocument, generate_document_file_upload_path from konova.models import AbstractDocument, generate_document_file_upload_path
@ -86,6 +89,17 @@ class Ema(AbstractCompensation):
return ret_msgs return ret_msgs
def get_documents(self) -> QuerySet:
""" Getter for all documents of an EMA
Returns:
docs (QuerySet): The queryset of all documents
"""
docs = EmaDocument.objects.filter(
instance=self
)
return docs
class EmaDocument(AbstractDocument): class EmaDocument(AbstractDocument):
""" """
@ -100,3 +114,32 @@ class EmaDocument(AbstractDocument):
upload_to=generate_document_file_upload_path, upload_to=generate_document_file_upload_path,
max_length=1000, max_length=1000,
) )
def delete(self, *args, **kwargs):
"""
Custom delete functionality for EcoAccountDocuments.
Removes the folder from the file system if there are no further documents for this entry.
Args:
*args ():
**kwargs ():
Returns:
"""
ema_docs = self.instance.get_documents()
folder_path = None
if ema_docs.count() == 1:
# The only file left for this EMA is the one which is currently processed and will be deleted
# Make sure that the compensation folder itself is deleted as well, not only the file
# Therefore take the folder path from the file path
folder_path = self.file.path.split("/")[:-1]
folder_path = "/".join(folder_path)
# Remove the file itself
super().delete(*args, **kwargs)
# If a folder path has been set, we need to delete the whole folder!
if folder_path is not None:
shutil.rmtree(folder_path)

View File

@ -5,8 +5,11 @@ Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 17.11.20 Created on: 17.11.20
""" """
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 _
@ -92,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):
""" """
@ -324,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):
""" """
@ -338,3 +387,33 @@ class InterventionDocument(AbstractDocument):
upload_to=generate_document_file_upload_path, upload_to=generate_document_file_upload_path,
max_length=1000, max_length=1000,
) )
def delete(self, *args, **kwargs):
"""
Custom delete functionality for InterventionDocuments.
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.instance.get_documents()
folder_path = None
if revoc_docs.count() == 0 and other_intervention_docs.count() == 1:
# The only file left for this intervention is the one which is currently processed and will be deleted
# Make sure that the intervention folder itself is deleted as well, not only the file
# Therefore take the folder path from the file path
folder_path = self.file.path.split("/")[:-1]
folder_path = "/".join(folder_path)
# Remove the file itself
super().delete(*args, **kwargs)
# If a folder path has been set, we need to delete the whole folder!
if folder_path is not None:
shutil.rmtree(folder_path)