@@ -1262,6 +1262,9 @@ class DrefFinalReport(models.Model):
12621262 verbose_name = _ ("financial report" ),
12631263 related_name = "financial_report_dref_final_report" ,
12641264 )
1265+ financial_report_preview = models .FileField (
1266+ verbose_name = _ ("financial preview" ), null = True , blank = True , upload_to = "dref/images/"
1267+ )
12651268 num_assisted = models .IntegerField (verbose_name = _ ("number of assisted" ), blank = True , null = True )
12661269 has_national_society_conducted = models .BooleanField (
12671270 verbose_name = _ ("Has national society conducted any intervention" ), null = True , blank = True
@@ -1275,13 +1278,27 @@ class DrefFinalReport(models.Model):
12751278 null = True ,
12761279 blank = True ,
12771280 )
1281+ __financial_report_id = None
12781282
12791283 class Meta :
12801284 verbose_name = _ ("Dref Final Report" )
12811285 verbose_name_plural = _ ("Dref Final Reports" )
12821286
12831287 def save (self , * args , ** kwargs ):
1288+ if self .financial_report_id and self .financial_report_id != self .__financial_report_id :
1289+ pages = convert_from_bytes (self .financial_report .file .read ())
1290+ if len (pages ) > 0 :
1291+ financial_report_preview = pages [0 ] # get first page
1292+ filename = f'preview_{ self .financial_report .file .name .split ("/" )[0 ]} .png'
1293+ temp_image = open (os .path .join ("/tmp" , filename ), "wb" )
1294+ financial_report_preview .save (temp_image , "PNG" )
1295+ thumb_data = open (os .path .join ("/tmp" , filename ), "rb" )
1296+ self .financial_report_preview .save (filename , thumb_data , save = False )
1297+ else :
1298+ raise ValidationError ({"financial_report" : "Sorry cannot generate preview for empty pdf" })
1299+
12841300 self .status = Dref .Status .COMPLETED if self .is_published else Dref .Status .IN_PROGRESS
1301+ self .__financial_report_id = self .financial_report_id
12851302 super ().save (* args , ** kwargs )
12861303
12871304 @staticmethod
0 commit comments