Skip to content

Commit 74367b7

Browse files
Added caption for file upload
1 parent 7343ef7 commit 74367b7

File tree

8 files changed

+105
-21
lines changed

8 files changed

+105
-21
lines changed

api/drf_views.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -919,14 +919,18 @@ def create_event(self, report):
919919

920920
def create_eap_activation(self, data, fieldreport):
921921
eap = EAP.objects.filter(id=data.pop('eap', None)).first()
922-
documents = EAPDocument.objects.filter(id__in=data.pop('documents', None))
922+
documents_data = data.pop('documents', None)
923923
eap_activation = EAPActivation.objects.create(
924924
eap=eap,
925925
field_report=fieldreport,
926926
**data
927927
)
928-
if documents:
929-
for document in documents:
928+
if documents_data:
929+
for data in documents_data:
930+
document = EAPDocument.objects.filter(id=data['id']).first()
931+
document.caption = data['caption']
932+
document.save(update_fields=['caption'])
933+
# save m2m
930934
eap_activation.documents.add(document)
931935
return eap_activation
932936

@@ -999,13 +1003,18 @@ def update_eap_activation(self, data, fieldreport):
9991003
from eap.serializers import EAPActivationSerializer
10001004

10011005
eap_id = data.pop('eap', None)
1002-
document_ids = data.pop('documents', None)
1006+
documents_data = data.pop('documents', None)
10031007
instance = EAPActivation.objects.get(field_report=fieldreport)
10041008
eap_activation = EAPActivationSerializer().update(instance, data)
10051009
instance.eap = EAP.objects.filter(id=eap_id).first()
1006-
if document_ids:
1007-
documents = EAPDocument.objects.filter(id__in=document_ids)
1008-
for document in documents:
1010+
1011+
instance.documents.clear()
1012+
if documents_data:
1013+
for data in documents_data:
1014+
document = EAPDocument.objects.filter(id=data['id']).first()
1015+
document.caption = data['caption']
1016+
document.save(update_fields=['caption'])
1017+
# save m2m
10091018
instance.documents.add(document)
10101019
instance.save(update_fields=['eap'])
10111020

api/serializers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from lang.serializers import ModelSerializer
99
from lang.models import String
1010

11+
1112
from .models import (
1213
DisasterType,
1314
ExternalPartner,
@@ -1062,8 +1063,6 @@ class Meta:
10621063

10631064

10641065
class DetailFieldReportSerializer(FieldReportEnumDisplayMixin, ModelSerializer):
1065-
from eap.serializers import EAPActivationSerializer
1066-
10671066
user = UserSerializer()
10681067
dtype = DisasterTypeSerializer()
10691068
contacts = FieldReportContactSerializer(many=True)
@@ -1078,6 +1077,8 @@ class DetailFieldReportSerializer(FieldReportEnumDisplayMixin, ModelSerializer):
10781077

10791078
@staticmethod
10801079
def get_eap_activation(obj):
1080+
from eap.serializers import EAPActivationSerializer
1081+
10811082
eap_activation = EAPActivation.objects.get(field_report=obj)
10821083
eap_activation_data = EAPActivationSerializer(eap_activation).data
10831084
return eap_activation_data

api/test_views.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ 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-
'documents': [document1.id],
118+
'documents': [
119+
{
120+
"id": document1.id,
121+
"caption": "test eap"
122+
}
123+
],
119124
'originator_name': 'test name',
120125
'originator_title': 'test originator title',
121126
'originator_email': '[email protected]',
@@ -181,7 +186,16 @@ def test_create_and_update(self):
181186
'eap': eap2.id,
182187
'description': 'test eap description updated',
183188
'trigger_met_date': '2022-11-11 01:00',
184-
'documents': [document2.id],
189+
'documents': [
190+
{
191+
"id": document1.id,
192+
"caption": "test eap updated"
193+
},
194+
{
195+
"id": document2.id,
196+
"caption": "test eap updated 2"
197+
}
198+
],
185199
'originator_name': 'test name',
186200
'originator_title': 'test originator title',
187201
'originator_email': '[email protected]',
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Django 2.2.28 on 2022-08-11 08:36
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('eap', '0008_auto_20220804_0505'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='eapdocument',
15+
name='caption',
16+
field=models.CharField(blank=True, max_length=225, null=True),
17+
),
18+
migrations.AlterField(
19+
model_name='earlyaction',
20+
name='sector',
21+
field=models.IntegerField(choices=[(0, 'Shelter, Housing And Settlements'), (1, 'Livelihoods'), (2, 'Multi-purpose Cash'), (3, 'Health And Care'), (4, 'Water, Sanitation And Hygiene'), (5, 'Protection, Gender And Inclusion'), (6, 'Education'), (7, 'Migration'), (8, 'Risk Reduction, Climate Adaptation And Recovery'), (9, 'Community Engagement And Accountability'), (10, 'Environment Sustainability'), (11, 'Shelter Cluster Coordination')], verbose_name='sector'),
22+
),
23+
]

eap/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from django.utils.translation import ugettext_lazy as _
44

55
from main.enums import TextChoices, IntegerChoices
6-
from deployments.models import Sectors
76
from api.models import (
87
Country,
98
District,
@@ -67,6 +66,7 @@ def __str__(self):
6766

6867
class EAPDocument(models.Model):
6968
file = models.FileField(null=True, blank=True)
69+
caption = models.CharField(max_length=225, null=True, blank=True)
7070
created_by = models.ForeignKey(
7171
settings.AUTH_USER_MODEL, verbose_name=_('Created by'), related_name='document_created_by',
7272
null=True, blank=True, on_delete=models.SET_NULL,

eap/serializers.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class EAPDocumentSerializer(serializers.ModelSerializer):
9494

9595
class Meta:
9696
model = EAPDocument
97-
fields = ['id', 'file']
97+
fields = ['id', 'file', 'caption', ]
9898

9999
def create(self, validated_data):
100100
validated_data['created_by'] = self.context['request'].user
@@ -114,7 +114,7 @@ class EAPSerializer(
114114
created_by_details = UserNameSerializer(source='created_by', read_only=True)
115115
modified_by_details = UserNameSerializer(source='modified_by', read_only=True)
116116
hazard_type_details = DisasterTypeSerializer(source='disaster_type', read_only=True)
117-
documents_details = EAPDocumentSerializer(source='documents', many=True, read_only=True, required=False)
117+
documents = EAPDocumentSerializer(many=True, required=False)
118118
status_display = serializers.CharField(source='get_status_display', read_only=True)
119119

120120
class Meta:
@@ -142,7 +142,18 @@ def update(self, instance, validated_data):
142142

143143

144144
class EAPActivationSerializer(serializers.ModelSerializer):
145-
document_detail = EAPDocumentSerializer(source='document', read_only=True)
145+
document_details = serializers.SerializerMethodField('get_eap_documents')
146+
147+
@staticmethod
148+
def get_eap_documents(obj):
149+
eap_documents = obj.documents.all()
150+
return [
151+
{
152+
'id': document.id,
153+
'file': document.file.url,
154+
'caption': document.caption
155+
} for document in eap_documents
156+
]
146157

147158
class Meta:
148159
model = EAPActivation
@@ -184,7 +195,7 @@ class EAPActivationReportSerializer(
184195
operational_plans = OperationalPlanSerializer(many=True)
185196
created_by_details = UserNameSerializer(source='created_by', read_only=True)
186197
modified_by_details = UserNameSerializer(source='modified_by', read_only=True)
187-
document_details = EAPDocumentSerializer(source='documents', read_only=True, many=True, required=False)
198+
documents = EAPDocumentSerializer(many=True, required=False)
188199
ifrc_financial_report_details = EAPDocumentSerializer(source='ifrc_financial_report', read_only=True)
189200

190201
class Meta:

eap/test_views.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,16 @@ def setUp(self):
7676
"early_action_budget": 2000,
7777
"trigger_statement": "test",
7878
"overview": "test",
79-
"documents": [self.document1.id],
79+
"documents": [
80+
{
81+
"id": self.document1.id,
82+
"caption": "test caption"
83+
},
84+
{
85+
"id": self.document2.id,
86+
"caption": "test caption 2"
87+
},
88+
],
8089
"originator_name": "eap name",
8190
"originator_title": "eap title",
8291
"originator_email": "[email protected]",
@@ -168,7 +177,16 @@ def setUp(self):
168177
"number_of_people_reached": 1000,
169178
"description": "test eap activation report",
170179
"overall_objectives": "test eap activation report",
171-
"documents": [self.document1.id, self.document2.id],
180+
"documents": [
181+
{
182+
"id": self.document1.id,
183+
"caption": "test caption"
184+
},
185+
{
186+
"id": self.document2.id,
187+
"caption": "test caption 2"
188+
},
189+
],
172190
"challenges_and_lesson": "test eap activation report",
173191
"general_lesson_and_recomendations": "test eap activation report",
174192
"ifrc_financial_report": self.document1.id,
@@ -315,7 +333,16 @@ def test_create_and_update_eap_activation_report(self):
315333
# update eap_report
316334
data = self.eap_act_report_body
317335
data['description'] = 'updated description'
318-
data['documents'] = [self.document1.id]
336+
data['documents'] = [
337+
{
338+
"id": self.document2.id,
339+
"caption": "test caption updated"
340+
},
341+
{
342+
"id": self.document1.id,
343+
"caption": "test caption updated"
344+
}
345+
]
319346
data['ifrc_financial_report'] = self.document2.id
320347
data['operational_plans'] = [
321348
{
@@ -356,7 +383,6 @@ def test_create_and_update_eap_activation_report(self):
356383
self.assertEqual(updated.created_by.id, self.user.id)
357384
self.assertEqual(final_report_updated_resp['eap_activation'], self.eap_activation.id)
358385
self.assertEqual(final_report_updated_resp['ifrc_financial_report'], self.document2.id)
359-
self.assertEqual(len(final_report_updated_resp['documents']), 1)
360386
self.assertEqual(len(final_report_updated_resp['operational_plans']), 1)
361387
self.assertEqual(len(final_report_updated_resp['operational_plans'][0]['early_actions_achievements']), 2)
362388
self.assertEqual(len(final_report_updated_resp['operational_plans'][0]['indicators']), 2)

main/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
'DEFAULT_RENDERER_CLASSES': (
178178
'rest_framework.renderers.JSONRenderer',
179179
# 'rest_framework.renderers.BrowsableAPIRenderer', # it is comment out to reduce load time in browsable api
180-
'main.utils.BrowsableAPIRendererWithRawForms', # it is added to reduce load time in browsable api
180+
'main.utils.BrowsableAPIRendererWithRawForms', # it is added to remove html form and reduce load time in browsable api
181181
'rest_framework_csv.renderers.PaginatedCSVRenderer',
182182
),
183183
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',

0 commit comments

Comments
 (0)