Skip to content

Commit 3625320

Browse files
Added create, update, get api for EAP final report
1 parent 332e631 commit 3625320

File tree

10 files changed

+406
-50
lines changed

10 files changed

+406
-50
lines changed

api/drf_views.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -910,13 +910,15 @@ def create_event(self, report):
910910

911911
def create_eap_activation(self, data, fieldreport):
912912
eap = EAP.objects.filter(id=data.pop('eap', None)).first()
913-
document = EAPDocument.objects.filter(id=data.pop('document', None)).first()
913+
documents = EAPDocument.objects.filter(id__in=data.pop('documents', None))
914914
eap_activation = EAPActivation.objects.create(
915915
eap=eap,
916916
field_report=fieldreport,
917-
document=document,
918917
**data
919918
)
919+
if documents:
920+
for document in documents:
921+
eap_activation.documents.add(document)
920922
return eap_activation
921923

922924
def create(self, request, *args, **kwargs):
@@ -977,7 +979,6 @@ def create(self, request, *args, **kwargs):
977979
return Response({'id': fieldreport.id}, status=HTTP_201_CREATED)
978980

979981

980-
from eap.serializers import EAPActivationSerializer # It is imported here to avoid circular import issue
981982
class UpdateFieldReport(UpdateAPIView, GenericFieldReportView):
982983
authentication_classes = (TokenAuthentication,)
983984
permission_classes = (IsAuthenticated,)
@@ -986,13 +987,19 @@ class UpdateFieldReport(UpdateAPIView, GenericFieldReportView):
986987

987988
# function for updating eap-activate in field report
988989
def update_eap_activation(self, data, fieldreport):
990+
from eap.serializers import EAPActivationSerializer
991+
989992
eap_id = data.pop('eap', None)
990-
document_id = data.pop('document', None)
993+
document_ids = data.pop('documents', None)
991994
instance = EAPActivation.objects.get(field_report=fieldreport)
992995
eap_activation = EAPActivationSerializer().update(instance, data)
993-
instance.document = EAPDocument.objects.filter(id=document_id).first()
994996
instance.eap = EAP.objects.filter(id=eap_id).first()
995-
instance.save(update_fields=['document', 'eap'])
997+
if document_ids:
998+
documents = EAPDocument.objects.filter(id__in=document_ids)
999+
for document in documents:
1000+
instance.documents.add(document)
1001+
instance.save(update_fields=['eap'])
1002+
9961003
return eap_activation
9971004

9981005
def partial_update(self, request, *args, **kwargs):
@@ -1066,6 +1073,7 @@ class GoHistoricalViewSet(viewsets.ReadOnlyModelViewSet):
10661073
def get_queryset(self):
10671074
return Event.objects.filter(appeals__isnull=False)
10681075

1076+
10691077
class CountryOfFieldReportToReviewViewset(viewsets.ReadOnlyModelViewSet):
10701078
queryset = CountryOfFieldReportToReview.objects.order_by('country')
10711079
serializer_class = CountryOfFieldReportToReviewSerializer

api/test_views.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_create_and_update(self):
115115
'eap': eap1.id,
116116
'description': 'test eap description',
117117
'trigger_met_date': '2022-11-11 00:00',
118-
'document': document1.id,
118+
'documents': [document1.id],
119119
'originator_name': 'test name',
120120
'originator_title': 'test originator title',
121121
'originator_email': '[email protected]',
@@ -162,7 +162,6 @@ def test_create_and_update(self):
162162
eap_activation_obj = EAPActivation.objects.get(field_report=created)
163163
self.assertEqual(eap_activation_obj.title, 'eap activation title')
164164
self.assertEqual(eap_activation_obj.eap.id, eap1.id)
165-
self.assertEqual(eap_activation_obj.document.id, document1.id)
166165
self.assertEqual(eap_activation_obj.field_report.id, created.id)
167166

168167
# created an emergency automatically
@@ -182,7 +181,7 @@ def test_create_and_update(self):
182181
'eap': eap2.id,
183182
'description': 'test eap description updated',
184183
'trigger_met_date': '2022-11-11 01:00',
185-
'document': document2.id,
184+
'documents': [document2.id],
186185
'originator_name': 'test name',
187186
'originator_title': 'test originator title',
188187
'originator_email': '[email protected]',
@@ -226,7 +225,6 @@ def test_create_and_update(self):
226225
eap_activation_obj = EAPActivation.objects.get(field_report=updated)
227226
self.assertEqual(eap_activation_obj.title, 'eap activation title updated')
228227
self.assertEqual(eap_activation_obj.eap.id, eap2.id)
229-
self.assertEqual(eap_activation_obj.document.id, document2.id)
230228
self.assertEqual(eap_activation_obj.field_report.id, updated.id)
231229

232230
body['summary'] = 'test [updated again]'

eap/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ class EAPDocumentAdmin(admin.ModelAdmin):
3030
class EAPAdmin(admin.ModelAdmin):
3131
list_display = ('eap_number', 'country', 'status', 'operational_timeframe',)
3232
inlines = [ReferenceAdminInline, PartnerAdminInline]
33-
autocomplete_fields = ('country', 'district', 'disaster_type', 'created_by', 'modified_by')
33+
autocomplete_fields = ('country', 'districts', 'disaster_type', 'created_by', 'modified_by')

eap/factories.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
disaster_type,
1010
country,
1111
district,
12+
field_report,
1213
)
1314

1415
from .models import (
1516
EAP,
1617
EAPDocument,
18+
EAPActivation,
1719
)
1820

1921

@@ -34,10 +36,9 @@ class EAPFactory(factory.django.DjangoModelFactory):
3436
class Meta:
3537
model = EAP
3638

37-
district = factory.SubFactory(district.DistrictFactory)
39+
# districts = factory.SubFactory(district.DistrictFactory)
3840
country = factory.SubFactory(country.CountryFactory)
3941
disaster_type = factory.SubFactory(disaster_type.DisasterTypeFactory)
40-
document = factory.SubFactory(EAPDocumentFactory)
4142
eap_number = fuzzy.FuzzyText(length=20)
4243
approval_date = fuzzy.FuzzyDateTime(datetime.datetime(2008, 1, 1, tzinfo=timezone.utc))
4344
status = fuzzy.FuzzyChoice(EAP.Status)
@@ -74,3 +75,46 @@ def early_actions(self, create, extracted, **kwargs):
7475
if extracted:
7576
for early_action in extracted:
7677
self.early_actions.add(early_action)
78+
79+
@factory.post_generation
80+
def documents(self, create, extracted, **kwargs):
81+
if not create:
82+
return
83+
84+
if extracted:
85+
for document in extracted:
86+
self.documents.add(document)
87+
88+
89+
class EAPActivationFactory(factory.django.DjangoModelFactory):
90+
class Meta:
91+
model = EAPActivation
92+
93+
title = fuzzy.FuzzyText(length=20)
94+
field_report = factory.SubFactory(field_report.FieldReportFactory)
95+
eap = factory.SubFactory(EAPFactory)
96+
trigger_met_date = fuzzy.FuzzyDateTime(datetime.datetime(2008, 1, 1, tzinfo=timezone.utc))
97+
description = fuzzy.FuzzyText(length=50)
98+
99+
originator_name = fuzzy.FuzzyText(length=50)
100+
description = fuzzy.FuzzyText(length=50)
101+
originator_email = fuzzy.FuzzyText(length=50)
102+
103+
nsc_name_operational = fuzzy.FuzzyText(length=50)
104+
nsc_title_operational = fuzzy.FuzzyText(length=50)
105+
nsc_email_operational = fuzzy.FuzzyText(length=50)
106+
107+
nsc_name_secretary = fuzzy.FuzzyText(length=50)
108+
nsc_title_secretary = fuzzy.FuzzyText(length=50)
109+
nsc_email_secretary = fuzzy.FuzzyText(length=50)
110+
111+
@factory.post_generation
112+
def documents(self, create, extracted, **kwargs):
113+
if not create:
114+
return
115+
116+
if extracted:
117+
for document in extracted:
118+
self.documents.add(document)
119+
120+
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Generated by Django 2.2.28 on 2022-08-02 08:35
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
import django.db.models.deletion
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('api', '0157_auto_20220721_0754'),
12+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
13+
('eap', '0005_eapactivation'),
14+
]
15+
16+
operations = [
17+
migrations.CreateModel(
18+
name='EAPOperationalPlan',
19+
fields=[
20+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
21+
('budget', models.IntegerField(blank=True, null=True, verbose_name='Budget per sector (CHF)')),
22+
('value', models.IntegerField(blank=True, null=True, verbose_name='value')),
23+
('no_of_people_reached', models.IntegerField(blank=True, null=True, verbose_name='People Reached')),
24+
('readiness_activities_achievements', models.TextField(blank=True, null=True, verbose_name='Readiness Activities Achievements')),
25+
('prepo_activities_achievements', models.TextField(blank=True, null=True, verbose_name='Pre-positioning Activities Achievements')),
26+
('client_id', models.CharField(blank=True, max_length=50, null=True)),
27+
],
28+
options={
29+
'verbose_name': 'EAP Operational Plan',
30+
'verbose_name_plural': 'EAP Operational Plans',
31+
},
32+
),
33+
migrations.RemoveField(
34+
model_name='eap',
35+
name='district',
36+
),
37+
migrations.RemoveField(
38+
model_name='eap',
39+
name='document',
40+
),
41+
migrations.RemoveField(
42+
model_name='eapactivation',
43+
name='document',
44+
),
45+
migrations.AddField(
46+
model_name='action',
47+
name='client_id',
48+
field=models.CharField(blank=True, max_length=50, null=True),
49+
),
50+
migrations.AddField(
51+
model_name='eap',
52+
name='districts',
53+
field=models.ManyToManyField(related_name='eap_district', to='api.District', verbose_name='districts'),
54+
),
55+
migrations.AddField(
56+
model_name='eap',
57+
name='documents',
58+
field=models.ManyToManyField(blank=True, related_name='eap_document', to='eap.EAPDocument', verbose_name='EAP Documents'),
59+
),
60+
migrations.AddField(
61+
model_name='eapactivation',
62+
name='documents',
63+
field=models.ManyToManyField(blank=True, related_name='eap_activation_document', to='eap.EAPDocument', verbose_name='EAP Activation Documents'),
64+
),
65+
migrations.AddField(
66+
model_name='eapdocument',
67+
name='client_id',
68+
field=models.CharField(blank=True, max_length=50, null=True),
69+
),
70+
migrations.AddField(
71+
model_name='eappartner',
72+
name='client_id',
73+
field=models.CharField(blank=True, max_length=50, null=True),
74+
),
75+
migrations.AddField(
76+
model_name='eapreference',
77+
name='client_id',
78+
field=models.CharField(blank=True, max_length=50, null=True),
79+
),
80+
migrations.AddField(
81+
model_name='earlyaction',
82+
name='client_id',
83+
field=models.CharField(blank=True, max_length=50, null=True),
84+
),
85+
migrations.AddField(
86+
model_name='earlyactionindicator',
87+
name='client_id',
88+
field=models.CharField(blank=True, max_length=50, null=True),
89+
),
90+
migrations.AddField(
91+
model_name='prioritizedrisk',
92+
name='client_id',
93+
field=models.CharField(blank=True, max_length=50, null=True),
94+
),
95+
migrations.CreateModel(
96+
name='OperationalPlanIndicator',
97+
fields=[
98+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
99+
('indicator_value', models.IntegerField(blank=True, null=True)),
100+
('indicator', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='eap.EarlyActionIndicator')),
101+
('operational_plan', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='operational_plan_indicator', to='eap.EAPOperationalPlan', verbose_name='Operational Plan')),
102+
],
103+
options={
104+
'verbose_name': 'Operational Indicator',
105+
'verbose_name_plural': 'Operational Indicators',
106+
},
107+
),
108+
migrations.AddField(
109+
model_name='eapoperationalplan',
110+
name='early_action',
111+
field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='eap.EarlyAction'),
112+
),
113+
migrations.CreateModel(
114+
name='EAPActivationReport',
115+
fields=[
116+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
117+
('number_of_people_reached', models.IntegerField(verbose_name='Number Of People Reached')),
118+
('description', models.TextField(verbose_name='Description of Event & Overview of Implementation')),
119+
('overall_objectives', models.TextField(verbose_name='Overall Objective of the Intervention')),
120+
('challenges_and_lesson', models.TextField(verbose_name='Challenges & Lesson Learned per Sector')),
121+
('general_lesson_and_recomendations', models.TextField(verbose_name='General Lessons Learned and Recomendations')),
122+
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Created at')),
123+
('modified_at', models.DateTimeField(auto_now=True, verbose_name='Updated at')),
124+
('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='eap_act_report_created_by', to=settings.AUTH_USER_MODEL, verbose_name='Created by')),
125+
('documents', models.ManyToManyField(blank=True, related_name='eap_act_reports', to='eap.EAPDocument', verbose_name='EAP Activation Report Document')),
126+
('eap_activation', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='eap_activation_report', to='eap.EAPActivation', verbose_name='EAP Activation Report')),
127+
('ifrc_financial_report', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='eap_activation_ifrc_report', to='eap.EAPDocument', verbose_name='IFRC Financial Report')),
128+
('modified_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='eap_act_modified_by', to=settings.AUTH_USER_MODEL, verbose_name='Modified by')),
129+
('operational_plans', models.ManyToManyField(blank=True, to='eap.EAPOperationalPlan', verbose_name='Operational Plans')),
130+
],
131+
),
132+
migrations.CreateModel(
133+
name='ActionAchievements',
134+
fields=[
135+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
136+
('early_act_achievement', models.TextField(blank=True, null=True, verbose_name='Early Actions Achievements')),
137+
('client_id', models.CharField(blank=True, max_length=50, null=True)),
138+
('action', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='eap.Action')),
139+
('operational_plan', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='action_achievement', to='eap.EAPOperationalPlan', verbose_name='Action Achievement')),
140+
],
141+
options={
142+
'verbose_name': 'Action Achievement',
143+
'verbose_name_plural': 'Action Achievements',
144+
},
145+
),
146+
]

0 commit comments

Comments
 (0)