Skip to content

Commit 4d33ec6

Browse files
thenav56k9845
authored andcommitted
Add financial_report preview
1 parent b820c2a commit 4d33ec6

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 3.2.20 on 2023-07-27 12:27
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('dref', '0063_merge_20230628_1022'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='dreffinalreport',
15+
name='financial_report_preview',
16+
field=models.FileField(blank=True, null=True, upload_to='dref/images/', verbose_name='financial preview'),
17+
),
18+
]

dref/models.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

dref/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ class DrefFinalReportSerializer(NestedUpdateMixin, NestedCreateMixin, ModelSeria
916916

917917
class Meta:
918918
model = DrefFinalReport
919-
read_only_fields = ("modified_by", "created_by")
919+
read_only_fields = ("modified_by", "created_by", "financial_report_preview")
920920
exclude = (
921921
"images",
922922
"photos",

0 commit comments

Comments
 (0)