@ -9,6 +9,7 @@ import shutil
from django . contrib . auth . models import User
from django . contrib . gis . db import models
from django . db . models import QuerySet
from django . utils . timezone import localtime
from django . utils . translation import gettext_lazy as _
@ -94,8 +95,39 @@ class RevocationDocument(AbstractDocument):
@property
def intervention ( self ) :
"""
Shortcut for opening the related intervention
Returns :
intervention ( 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 ) :
"""
@ -326,6 +358,21 @@ class Intervention(BaseObject):
tooltip = _ ( " Recorded on {} by {} " ) . format ( on , self . recorded . user )
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 ) :
"""
@ -353,12 +400,7 @@ class InterventionDocument(AbstractDocument):
Returns :
"""
revoc_docs = RevocationDocument . objects . filter (
instance = self . instance . legal . revocation
)
other_intervention_docs = InterventionDocument . objects . filter (
instance = self . instance
)
revoc_docs , other_intervention_docs = self . instance . get_documents ( )
folder_path = None
if revoc_docs . count ( ) == 0 and other_intervention_docs . count ( ) == 1 :