Represents a file from a Wagtail/Hypha Stream Form block.
Aside: with imports in their usual place, there could be circular imports. Deferring or delaying import to methods
resolves the issue.
def__init__(self,instance,field,*args,filename=None,storage=default_storage,**kwargs):super().__init__(*args,**kwargs)# Field is the wagtail field that the file was uploaded toself.field=field# Instance is the parent model object that created this file objectself.instance=instanceself.storage=storageself.filename=filenameorself.basenameself._committed=False
defdelete(self,save=True):ifnotself:return# Only close the file if it's already open, which we know by the# presence of self._fileifhasattr(self,"_file"):self.close()delself.fileself.storage.delete(self.name)self.name=Noneself._committed=False
defget_entity_id(self):fromhypha.apply.funds.modelsimportApplicationRevisionfromhypha.apply.projects.modelsimportReportVersionentity_id=self.instance.pkifisinstance(self.instance,ApplicationRevision):entity_id=self.instance.submission.pkelifisinstance(self.instance,ReportVersion):# Reports are project documents.entity_id=self.instance.report.project.pkreturnentity_id