Skip to content

Commit d064974

Browse files
Merge pull request #1858 from IFRCGo/hot-fix/dref-final-report
Rename incorrect name fields
2 parents 3a109f7 + 1edd526 commit d064974

File tree

7 files changed

+91
-11
lines changed

7 files changed

+91
-11
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## Unreleased
88

9+
## 1.1.484
10+
11+
### Added
12+
- Hotfix for DREF final reports
13+
914
## 1.1.483
1015

1116
### Added
@@ -2262,7 +2267,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
22622267

22632268
## 0.1.20
22642269

2265-
[Unreleased]: https://github.com/IFRCGo/go-api/compare/1.1.483...HEAD
2270+
[Unreleased]: https://github.com/IFRCGo/go-api/compare/1.1.484...HEAD
2271+
[1.1.484]: https://github.com/IFRCGo/go-api/compare/1.1.483...1.1.484
22662272
[1.1.483]: https://github.com/IFRCGo/go-api/compare/1.1.482...1.1.483
22672273
[1.1.482]: https://github.com/IFRCGo/go-api/compare/1.1.481...1.1.482
22682274
[1.1.481]: https://github.com/IFRCGo/go-api/compare/1.1.480...1.1.481
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+
]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Generated by Django 3.2.20 on 2023-07-28 04:05
2+
3+
from django.db import migrations, models
4+
import django.utils.timezone
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('dref', '0064_dreffinalreport_financial_report_preview'),
11+
]
12+
13+
operations = [
14+
migrations.AlterField(
15+
model_name='dref',
16+
name='modified_at',
17+
field=models.DateTimeField(default=django.utils.timezone.now, null=True, verbose_name='modified at'),
18+
),
19+
migrations.AlterField(
20+
model_name='dreffinalreport',
21+
name='modified_at',
22+
field=models.DateTimeField(default=django.utils.timezone.now, null=True, verbose_name='modified at'),
23+
),
24+
migrations.AlterField(
25+
model_name='drefoperationalupdate',
26+
name='modified_at',
27+
field=models.DateTimeField(default=django.utils.timezone.now, null=True, verbose_name='modified at'),
28+
),
29+
]

dref/models.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from django.templatetags.static import static
1111
from django.core.exceptions import ValidationError
1212
from django.contrib.postgres.aggregates import ArrayAgg
13+
from django.utils import timezone
1314

1415
from api.models import Country, DisasterType, District, FieldReport
1516

@@ -224,7 +225,7 @@ class Status(models.IntegerChoices):
224225
COMPLETED = 1, _("Completed")
225226

226227
created_at = models.DateTimeField(verbose_name=_("created at"), auto_now_add=True)
227-
modified_at = models.DateTimeField(verbose_name=_("modified at"), auto_now=True, blank=True)
228+
modified_at = models.DateTimeField(verbose_name=_("modified at"), default=timezone.now, null=True)
228229
created_by = models.ForeignKey(
229230
settings.AUTH_USER_MODEL,
230231
verbose_name=_("created by"),
@@ -657,9 +658,10 @@ def clone(self, user):
657658
return clone
658659

659660

661+
@reversion.register()
660662
class DrefOperationalUpdate(models.Model):
661663
created_at = models.DateTimeField(verbose_name=_("created at"), auto_now_add=True)
662-
modified_at = models.DateTimeField(verbose_name=_("modified at"), auto_now=True, blank=True)
664+
modified_at = models.DateTimeField(verbose_name=_("modified at"), default=timezone.now, null=True)
663665
created_by = models.ForeignKey(
664666
settings.AUTH_USER_MODEL,
665667
verbose_name=_("created by"),
@@ -1014,9 +1016,10 @@ def get_for(user):
10141016
return DrefOperationalUpdate.objects.filter(id__in=union_query.values("id")).distinct()
10151017

10161018

1019+
@reversion.register()
10171020
class DrefFinalReport(models.Model):
10181021
created_at = models.DateTimeField(verbose_name=_("created at"), auto_now_add=True)
1019-
modified_at = models.DateTimeField(verbose_name=_("modified at"), auto_now=True)
1022+
modified_at = models.DateTimeField(verbose_name=_("modified at"), default=timezone.now, null=True)
10201023
created_by = models.ForeignKey(
10211024
settings.AUTH_USER_MODEL,
10221025
verbose_name=_("created by"),
@@ -1260,6 +1263,9 @@ class DrefFinalReport(models.Model):
12601263
verbose_name=_("financial report"),
12611264
related_name="financial_report_dref_final_report",
12621265
)
1266+
financial_report_preview = models.FileField(
1267+
verbose_name=_("financial preview"), null=True, blank=True, upload_to="dref/images/"
1268+
)
12631269
num_assisted = models.IntegerField(verbose_name=_("number of assisted"), blank=True, null=True)
12641270
has_national_society_conducted = models.BooleanField(
12651271
verbose_name=_("Has national society conducted any intervention"), null=True, blank=True
@@ -1273,13 +1279,27 @@ class DrefFinalReport(models.Model):
12731279
null=True,
12741280
blank=True,
12751281
)
1282+
__financial_report_id = None
12761283

12771284
class Meta:
12781285
verbose_name = _("Dref Final Report")
12791286
verbose_name_plural = _("Dref Final Reports")
12801287

12811288
def save(self, *args, **kwargs):
1289+
if self.financial_report_id and self.financial_report_id != self.__financial_report_id:
1290+
pages = convert_from_bytes(self.financial_report.file.read())
1291+
if len(pages) > 0:
1292+
financial_report_preview = pages[0] # get first page
1293+
filename = f'preview_{self.financial_report.file.name.split("/")[0]}.png'
1294+
temp_image = open(os.path.join("/tmp", filename), "wb")
1295+
financial_report_preview.save(temp_image, "PNG")
1296+
thumb_data = open(os.path.join("/tmp", filename), "rb")
1297+
self.financial_report_preview.save(filename, thumb_data, save=False)
1298+
else:
1299+
raise ValidationError({"financial_report": "Sorry cannot generate preview for empty pdf"})
1300+
12821301
self.status = Dref.Status.COMPLETED if self.is_published else Dref.Status.IN_PROGRESS
1302+
self.__financial_report_id = self.financial_report_id
12831303
super().save(*args, **kwargs)
12841304

12851305
@staticmethod

dref/serializers.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ def update(self, instance, validated_data):
518518
to = None
519519
if modified_at and instance.modified_at and modified_at < instance.modified_at:
520520
raise serializers.ValidationError({"modified_at": settings.DREF_OP_UPDATE_FINAL_REPORT_UPDATE_ERROR_MESSAGE})
521+
validated_data["modified_at"] = modified_at
521522
dref = super().update(instance, validated_data)
522523
if to:
523524
transaction.on_commit(lambda: send_dref_email.delay(dref.id, list(to), "Updated"))
@@ -549,7 +550,7 @@ class DrefOperationalUpdateSerializer(NestedUpdateMixin, NestedCreateMixin, Mode
549550

550551
class Meta:
551552
model = DrefOperationalUpdate
552-
read_only_fields = ("operational_update_number",)
553+
read_only_fields = ("operational_update_number", "modified_by", "created_by")
553554
exclude = ("images", "photos", "event_map", "cover_image")
554555

555556
def validate(self, data):
@@ -574,7 +575,7 @@ def validate(self, data):
574575
return data
575576

576577
def validate_appeal_code(self, appeal_code):
577-
if appeal_code != self.instance.appeal_code:
578+
if appeal_code and appeal_code != self.instance.appeal_code:
578579
raise serializers.ValidationError("Can't edit MDR Code")
579580
return appeal_code
580581

@@ -590,6 +591,7 @@ def create(self, validated_data):
590591
dref_operational_update = (
591592
DrefOperationalUpdate.objects.filter(dref=dref).order_by("-operational_update_number").first()
592593
)
594+
validated_data["created_by"] = self.context["request"].user
593595
if not dref_operational_update:
594596
validated_data["title"] = dref.title
595597
validated_data["title_prefix"] = dref.title_prefix
@@ -807,7 +809,7 @@ def create(self, validated_data):
807809
return operational_update
808810

809811
def update(self, instance, validated_data):
810-
validated_data["updated_by"] = self.context["request"].user
812+
validated_data["modified_by"] = self.context["request"].user
811813
# changing_timeframe_operation = validated_data.get(
812814
# "changing_timeframe_operation", instance.changing_timeframe_operation
813815
# )
@@ -887,6 +889,7 @@ def update(self, instance, validated_data):
887889

888890
if modified_at and instance.modified_at and modified_at < instance.modified_at:
889891
raise serializers.ValidationError({"modified_at": settings.DREF_OP_UPDATE_FINAL_REPORT_UPDATE_ERROR_MESSAGE})
892+
validated_data["modified_at"] = modified_at
890893
return super().update(instance, validated_data)
891894

892895

@@ -911,9 +914,11 @@ class DrefFinalReportSerializer(NestedUpdateMixin, NestedCreateMixin, ModelSeria
911914
financial_report_details = DrefFileSerializer(source="financial_report", read_only=True)
912915
created_by_details = UserNameSerializer(source="created_by", read_only=True)
913916
users_details = UserNameSerializer(source="users", many=True, read_only=True)
917+
modified_by_details = UserNameSerializer(source="modified_by", read_only=True)
914918

915919
class Meta:
916920
model = DrefFinalReport
921+
read_only_fields = ("modified_by", "created_by", "financial_report_preview")
917922
exclude = (
918923
"images",
919924
"photos",
@@ -955,6 +960,7 @@ def create(self, validated_data):
955960
dref_operational_update = (
956961
DrefOperationalUpdate.objects.filter(dref=dref, is_published=True).order_by("-operational_update_number").first()
957962
)
963+
validated_data["created_by"] = self.context["request"].user
958964
if dref_operational_update:
959965
validated_data["title"] = dref_operational_update.title
960966
validated_data["title_prefix"] = dref_operational_update.title_prefix
@@ -1150,8 +1156,8 @@ def update(self, instance, validated_data):
11501156
raise serializers.ValidationError({"modified_at": "Modified At is required!"})
11511157
if modified_at and instance.modified_at and modified_at < instance.modified_at:
11521158
raise serializers.ValidationError({"modified_at": settings.DREF_OP_UPDATE_FINAL_REPORT_UPDATE_ERROR_MESSAGE})
1153-
1154-
validated_data["updated_by"] = self.context["request"].user
1159+
validated_data["modified_at"] = modified_at
1160+
validated_data["modified_by"] = self.context["request"].user
11551161
return super().update(instance, validated_data)
11561162

11571163

dref/test_views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ def test_dref_is_published(self, mock_now):
402402
Test for dref if is_published = True
403403
"""
404404

405-
initial_now = datetime(2011, 11, 11)
405+
initial_now = datetime(2023, 7, 28)
406406
mock_now.return_value = initial_now
407407

408408
region = Region.objects.create(name=RegionName.AFRICA)
@@ -430,6 +430,7 @@ def test_dref_is_published(self, mock_now):
430430
)
431431
url = f"/api/v2/dref/{not_published_dref.id}/"
432432
self.client.force_authenticate(self.user)
433+
data["modified_at"] = initial_now + timedelta(days=10)
433434
response = self.client.patch(url, data)
434435
self.assert_200(response)
435436

main/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
from .celery import app as celery_app
44

55
__all__ = ['celery_app']
6-
__version__ = '1.1.483'
6+
__version__ = '1.1.484'

0 commit comments

Comments
 (0)