Skip to content

Commit 40af6ae

Browse files
authored
Merge pull request #1623 from IFRCGo/hotfix-dref-final-report
Add missing file in dref final report serializer
2 parents 8e0143c + bf52a8f commit 40af6ae

File tree

4 files changed

+100
-18
lines changed

4 files changed

+100
-18
lines changed

deployments/snapshots/snap_tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@
308308
{
309309
'budget_amount_total': 5920000,
310310
'id': 21,
311-
'iso3': 'WNq',
312-
'name': 'country-FWJNPIGxPkqKRLeNzbOHsgUSwRJXvxmDTodTYrnDAnhHuxKzFb',
311+
'iso3': 'hBW',
312+
'name': 'country-hkFWJNPIGxPkqKRLeNzbOHsgUSwRJXvxmDTodTYrnDAnhHuxKz',
313313
'ongoing_projects': 1,
314314
'operation_types': [
315315
0
@@ -324,7 +324,7 @@
324324
'primary_sector_display': 'CEA'
325325
}
326326
],
327-
'society_name': 'society-name-qJTgrVtaadVUrcYCxAcjpXhdUBtXcCXMFkLAHVklUJGQvVYtjD',
327+
'society_name': 'society-name-NqqJTgrVtaadVUrcYCxAcjpXhdUBtXcCXMFkLAHVklUJGQvVYt',
328328
'target_total': 0
329329
},
330330
{
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Generated by Django 3.2.16 on 2022-12-27 05:01
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('dref', '0048_auto_20221213_0402'),
11+
]
12+
13+
operations = [
14+
migrations.RemoveField(
15+
model_name='dreffinalreport',
16+
name='budget_file',
17+
),
18+
migrations.AddField(
19+
model_name='dreffinalreport',
20+
name='financial_report',
21+
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='financial_report_dref_final_report', to='dref.dreffile', verbose_name='financial report'),
22+
),
23+
migrations.AddField(
24+
model_name='dreffinalreport',
25+
name='financial_report_description',
26+
field=models.TextField(blank=True, null=True, verbose_name='Financial Report Description'),
27+
),
28+
migrations.AddField(
29+
model_name='dreffinalreport',
30+
name='has_national_society_conducted',
31+
field=models.BooleanField(blank=True, null=True, verbose_name='Has national society conducted any intervention'),
32+
),
33+
migrations.AddField(
34+
model_name='dreffinalreport',
35+
name='national_society_conducted_description',
36+
field=models.TextField(blank=True, null=True, verbose_name='National Society conducted description'),
37+
),
38+
migrations.AddField(
39+
model_name='dreffinalreport',
40+
name='num_assisted',
41+
field=models.IntegerField(blank=True, null=True, verbose_name='number of assisted'),
42+
),
43+
migrations.AddField(
44+
model_name='plannedintervention',
45+
name='person_assisted',
46+
field=models.IntegerField(blank=True, null=True, verbose_name='person assisted'),
47+
),
48+
]

dref/models.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ class Title(models.TextChoices):
154154
title = models.CharField(max_length=255, verbose_name=_('title'), choices=Title.choices)
155155
description = models.TextField(verbose_name=_('description'), blank=True, null=True)
156156
person_targeted = models.IntegerField(verbose_name=_('person targeted'), null=True, blank=True)
157+
person_assisted = models.IntegerField(verbose_name=_('person assisted'), null=True, blank=True)
157158
budget = models.IntegerField(verbose_name=_('budget'), blank=True, null=True)
158159
male = models.IntegerField(verbose_name=_('male'), blank=True, null=True)
159160
female = models.IntegerField(verbose_name=_('female'), blank=True, null=True)
@@ -1553,11 +1554,27 @@ class DrefFinalReport(models.Model):
15531554
verbose_name=_('Did National Society'),
15541555
null=True, blank=True
15551556
)
1556-
budget_file = models.ForeignKey(
1557+
financial_report = models.ForeignKey(
15571558
'DrefFile', on_delete=models.SET_NULL,
15581559
null=True, blank=True,
1559-
verbose_name=_('budget file'),
1560-
related_name='budget_file_dref_final_report'
1560+
verbose_name=_('financial report'),
1561+
related_name='financial_report_dref_final_report'
1562+
)
1563+
num_assisted = models.IntegerField(
1564+
verbose_name=_('number of assisted'),
1565+
blank=True, null=True
1566+
)
1567+
has_national_society_conducted = models.BooleanField(
1568+
verbose_name=_('Has national society conducted any intervention'),
1569+
null=True, blank=True
1570+
)
1571+
national_society_conducted_description = models.TextField(
1572+
verbose_name=_('National Society conducted description'),
1573+
null=True, blank=True
1574+
)
1575+
financial_report_description = models.TextField(
1576+
verbose_name=_('Financial Report Description'),
1577+
null=True, blank=True
15611578
)
15621579

15631580
class Meta:

dref/serializers.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,14 @@ class DrefOperationalUpdateSerializer(
412412
budget_file_details = DrefFileSerializer(source='budget_file', read_only=True)
413413
country_details = MiniCountrySerializer(source='country', read_only=True)
414414
district_details = MiniDistrictSerializer(source='district', read_only=True, many=True)
415-
assessment_report_file = DrefFileSerializer(source='assessment_report', required=False, allow_null=True)
415+
assessment_report_details = DrefFileSerializer(source='assessment_report', read_only=True)
416416
risk_security = RiskSecuritySerializer(many=True, required=False)
417417
modified_at = serializers.DateTimeField(required=False)
418418

419419
class Meta:
420420
model = DrefOperationalUpdate
421421
read_only_fields = ('operational_update_number', )
422-
exclude = ('images', 'photos', 'event_map', 'assessment_report', 'cover_image')
422+
exclude = ('images', 'photos', 'event_map', 'cover_image')
423423

424424
def validate(self, data):
425425
dref = data.get('dref')
@@ -516,6 +516,7 @@ def create(self, validated_data):
516516
validated_data['anticipatory_actions'] = dref.anticipatory_actions
517517
validated_data['event_scope'] = dref.event_scope
518518
validated_data['budget_file'] = dref.budget_file
519+
validated_data['assessment_report'] = dref.assessment_report
519520
validated_data['country'] = dref.country
520521
validated_data['risk_security_concern'] = dref.risk_security_concern
521522
validated_data['is_assessment_report'] = dref.is_assessment_report
@@ -535,7 +536,6 @@ def create(self, validated_data):
535536
operational_update = super().create(validated_data)
536537
# XXX: Copy files from DREF (Only nested serialized fields)
537538
nested_serialized_file_fields = [
538-
'assessment_report',
539539
'cover_image',
540540
'event_map',
541541
]
@@ -613,7 +613,6 @@ def create(self, validated_data):
613613
validated_data['event_description'] = dref_operational_update.event_description
614614
validated_data['anticipatory_actions'] = dref_operational_update.anticipatory_actions
615615
validated_data['event_scope'] = dref_operational_update.event_scope
616-
validated_data['cover_image'] = dref_operational_update.cover_image
617616
validated_data['budget_file'] = dref_operational_update.budget_file
618617
validated_data['assessment_report'] = dref_operational_update.assessment_report
619618
validated_data['country'] = dref_operational_update.country
@@ -633,6 +632,16 @@ def create(self, validated_data):
633632
validated_data['communication'] = dref_operational_update.communication
634633
validated_data['people_in_need'] = dref_operational_update.people_in_need
635634
operational_update = super().create(validated_data)
635+
# XXX: Copy files from DREF (Only nested serialized fields)
636+
nested_serialized_file_fields = [
637+
'cover_image',
638+
'event_map',
639+
]
640+
for file_field in nested_serialized_file_fields:
641+
dref_file = getattr(dref, file_field, None)
642+
if dref_file:
643+
setattr(operational_update, file_field, dref_file.clone(self.context['request'].user))
644+
operational_update.save(update_fields=nested_serialized_file_fields)
636645
# XXX COPY M2M fields
637646
operational_update.planned_interventions.add(*dref_operational_update.planned_interventions.all())
638647
operational_update.images.add(*dref_operational_update.images.all())
@@ -709,18 +718,17 @@ class DrefFinalReportSerializer(
709718
photos_file = DrefFileSerializer(source='photos', many=True, required=False, allow_null=True)
710719
country_details = MiniCountrySerializer(source='country', read_only=True)
711720
district_details = MiniDistrictSerializer(source='district', read_only=True, many=True)
712-
assessment_report_file = DrefFileSerializer(source='assessment_report', required=False, allow_null=True)
721+
assessment_report_details = DrefFileSerializer(source='assessment_report', read_only=True)
713722
risk_security = RiskSecuritySerializer(many=True, required=False)
714723
modified_at = serializers.DateTimeField(required=False)
715-
budget_file_details = DrefFileSerializer(source='budget_file', read_only=True)
724+
financial_report_details = DrefFileSerializer(source='financial_report', read_only=True)
716725

717726
class Meta:
718727
model = DrefFinalReport
719728
exclude = (
720729
'images',
721730
'photos',
722731
'event_map',
723-
'assessment_report',
724732
'cover_image',
725733
)
726734

@@ -762,6 +770,7 @@ def create(self, validated_data):
762770
dref = validated_data['dref']
763771
dref_operational_update = DrefOperationalUpdate.objects.filter(
764772
dref=dref,
773+
is_published=True
765774
).order_by('-operational_update_number').first()
766775
if dref_operational_update:
767776
validated_data['title'] = dref_operational_update.title
@@ -823,7 +832,6 @@ def create(self, validated_data):
823832
validated_data['event_description'] = dref_operational_update.event_description
824833
validated_data['anticipatory_actions'] = dref_operational_update.anticipatory_actions
825834
validated_data['event_scope'] = dref_operational_update.event_scope
826-
validated_data['assessment_report'] = dref_operational_update.assessment_report
827835
validated_data['country'] = dref_operational_update.country
828836
validated_data['risk_security_concern'] = dref_operational_update.risk_security_concern
829837
validated_data['is_assessment_report'] = dref_operational_update.is_assessment_report
@@ -832,11 +840,10 @@ def create(self, validated_data):
832840
validated_data['event_date'] = dref_operational_update.event_date
833841
validated_data['people_in_need'] = dref_operational_update.people_in_need
834842
validated_data['ns_respond_date'] = dref_operational_update.ns_respond_date
835-
validated_data['budget_file'] = dref_operational_update.budget_file
843+
validated_data['assessment_report'] = dref_operational_update.assessment_report
836844
dref_final_report = super().create(validated_data)
837845
# XXX: Copy files from DREF (Only nested serialized fields)
838846
nested_serialized_file_fields = [
839-
'assessment_report',
840847
'cover_image',
841848
'event_map',
842849
]
@@ -922,9 +929,19 @@ def create(self, validated_data):
922929
validated_data['people_in_need'] = dref.people_in_need
923930
validated_data['event_text'] = dref.event_text
924931
validated_data['ns_respond_date'] = dref.ns_respond_date
925-
validated_data['budget_file'] = dref.budget_file
926-
validated_data['did_national_society'] = dref.did_national_society
932+
validated_data['cover_image'] = dref.cover_image
933+
validated_data['event_map'] = dref.event_map
927934
dref_final_report = super().create(validated_data)
935+
# XXX: Copy files from DREF (Only nested serialized fields)
936+
nested_serialized_file_fields = [
937+
'cover_image',
938+
'event_map',
939+
]
940+
for file_field in nested_serialized_file_fields:
941+
dref_file = getattr(dref, file_field, None)
942+
if dref_file:
943+
setattr(dref_final_report, file_field, dref_file.clone(self.context['request'].user))
944+
dref_final_report.save(update_fields=nested_serialized_file_fields)
928945
dref_final_report.planned_interventions.add(*dref.planned_interventions.all())
929946
dref_final_report.needs_identified.add(*dref.needs_identified.all())
930947
dref_final_report.district.add(*dref.district.all())

0 commit comments

Comments
 (0)