From 74367b77fba8daea0cf5e32c3bd3ea7a124ad115 Mon Sep 17 00:00:00 2001 From: rup Date: Tue, 13 Sep 2022 15:11:09 +0545 Subject: [PATCH] Added caption for file upload --- api/drf_views.py | 23 ++++++++++----- api/serializers.py | 5 ++-- api/test_views.py | 18 ++++++++++-- eap/migrations/0009_auto_20220811_0836.py | 23 +++++++++++++++ eap/models.py | 2 +- eap/serializers.py | 19 ++++++++++--- eap/test_views.py | 34 ++++++++++++++++++++--- main/settings.py | 2 +- 8 files changed, 105 insertions(+), 21 deletions(-) create mode 100644 eap/migrations/0009_auto_20220811_0836.py diff --git a/api/drf_views.py b/api/drf_views.py index 99cf93763..84a146810 100644 --- a/api/drf_views.py +++ b/api/drf_views.py @@ -919,14 +919,18 @@ def create_event(self, report): def create_eap_activation(self, data, fieldreport): eap = EAP.objects.filter(id=data.pop('eap', None)).first() - documents = EAPDocument.objects.filter(id__in=data.pop('documents', None)) + documents_data = data.pop('documents', None) eap_activation = EAPActivation.objects.create( eap=eap, field_report=fieldreport, **data ) - if documents: - for document in documents: + if documents_data: + for data in documents_data: + document = EAPDocument.objects.filter(id=data['id']).first() + document.caption = data['caption'] + document.save(update_fields=['caption']) + # save m2m eap_activation.documents.add(document) return eap_activation @@ -999,13 +1003,18 @@ def update_eap_activation(self, data, fieldreport): from eap.serializers import EAPActivationSerializer eap_id = data.pop('eap', None) - document_ids = data.pop('documents', None) + documents_data = data.pop('documents', None) instance = EAPActivation.objects.get(field_report=fieldreport) eap_activation = EAPActivationSerializer().update(instance, data) instance.eap = EAP.objects.filter(id=eap_id).first() - if document_ids: - documents = EAPDocument.objects.filter(id__in=document_ids) - for document in documents: + + instance.documents.clear() + if documents_data: + for data in documents_data: + document = EAPDocument.objects.filter(id=data['id']).first() + document.caption = data['caption'] + document.save(update_fields=['caption']) + # save m2m instance.documents.add(document) instance.save(update_fields=['eap']) diff --git a/api/serializers.py b/api/serializers.py index 6f2f8ceaa..40500e185 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -8,6 +8,7 @@ from lang.serializers import ModelSerializer from lang.models import String + from .models import ( DisasterType, ExternalPartner, @@ -1062,8 +1063,6 @@ class Meta: class DetailFieldReportSerializer(FieldReportEnumDisplayMixin, ModelSerializer): - from eap.serializers import EAPActivationSerializer - user = UserSerializer() dtype = DisasterTypeSerializer() contacts = FieldReportContactSerializer(many=True) @@ -1078,6 +1077,8 @@ class DetailFieldReportSerializer(FieldReportEnumDisplayMixin, ModelSerializer): @staticmethod def get_eap_activation(obj): + from eap.serializers import EAPActivationSerializer + eap_activation = EAPActivation.objects.get(field_report=obj) eap_activation_data = EAPActivationSerializer(eap_activation).data return eap_activation_data diff --git a/api/test_views.py b/api/test_views.py index ee2dabe04..8ebcf37ee 100644 --- a/api/test_views.py +++ b/api/test_views.py @@ -115,7 +115,12 @@ def test_create_and_update(self): 'eap': eap1.id, 'description': 'test eap description', 'trigger_met_date': '2022-11-11 00:00', - 'documents': [document1.id], + 'documents': [ + { + "id": document1.id, + "caption": "test eap" + } + ], 'originator_name': 'test name', 'originator_title': 'test originator title', 'originator_email': 'test@email.com', @@ -181,7 +186,16 @@ def test_create_and_update(self): 'eap': eap2.id, 'description': 'test eap description updated', 'trigger_met_date': '2022-11-11 01:00', - 'documents': [document2.id], + 'documents': [ + { + "id": document1.id, + "caption": "test eap updated" + }, + { + "id": document2.id, + "caption": "test eap updated 2" + } + ], 'originator_name': 'test name', 'originator_title': 'test originator title', 'originator_email': 'test@email.com', diff --git a/eap/migrations/0009_auto_20220811_0836.py b/eap/migrations/0009_auto_20220811_0836.py new file mode 100644 index 000000000..161abfa60 --- /dev/null +++ b/eap/migrations/0009_auto_20220811_0836.py @@ -0,0 +1,23 @@ +# Generated by Django 2.2.28 on 2022-08-11 08:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('eap', '0008_auto_20220804_0505'), + ] + + operations = [ + migrations.AddField( + model_name='eapdocument', + name='caption', + field=models.CharField(blank=True, max_length=225, null=True), + ), + migrations.AlterField( + model_name='earlyaction', + name='sector', + 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'), + ), + ] diff --git a/eap/models.py b/eap/models.py index a7d86eacc..faca72af6 100644 --- a/eap/models.py +++ b/eap/models.py @@ -3,7 +3,6 @@ from django.utils.translation import ugettext_lazy as _ from main.enums import TextChoices, IntegerChoices -from deployments.models import Sectors from api.models import ( Country, District, @@ -67,6 +66,7 @@ def __str__(self): class EAPDocument(models.Model): file = models.FileField(null=True, blank=True) + caption = models.CharField(max_length=225, null=True, blank=True) created_by = models.ForeignKey( settings.AUTH_USER_MODEL, verbose_name=_('Created by'), related_name='document_created_by', null=True, blank=True, on_delete=models.SET_NULL, diff --git a/eap/serializers.py b/eap/serializers.py index dec67339b..20099c57c 100644 --- a/eap/serializers.py +++ b/eap/serializers.py @@ -94,7 +94,7 @@ class EAPDocumentSerializer(serializers.ModelSerializer): class Meta: model = EAPDocument - fields = ['id', 'file'] + fields = ['id', 'file', 'caption', ] def create(self, validated_data): validated_data['created_by'] = self.context['request'].user @@ -114,7 +114,7 @@ class EAPSerializer( created_by_details = UserNameSerializer(source='created_by', read_only=True) modified_by_details = UserNameSerializer(source='modified_by', read_only=True) hazard_type_details = DisasterTypeSerializer(source='disaster_type', read_only=True) - documents_details = EAPDocumentSerializer(source='documents', many=True, read_only=True, required=False) + documents = EAPDocumentSerializer(many=True, required=False) status_display = serializers.CharField(source='get_status_display', read_only=True) class Meta: @@ -142,7 +142,18 @@ def update(self, instance, validated_data): class EAPActivationSerializer(serializers.ModelSerializer): - document_detail = EAPDocumentSerializer(source='document', read_only=True) + document_details = serializers.SerializerMethodField('get_eap_documents') + + @staticmethod + def get_eap_documents(obj): + eap_documents = obj.documents.all() + return [ + { + 'id': document.id, + 'file': document.file.url, + 'caption': document.caption + } for document in eap_documents + ] class Meta: model = EAPActivation @@ -184,7 +195,7 @@ class EAPActivationReportSerializer( operational_plans = OperationalPlanSerializer(many=True) created_by_details = UserNameSerializer(source='created_by', read_only=True) modified_by_details = UserNameSerializer(source='modified_by', read_only=True) - document_details = EAPDocumentSerializer(source='documents', read_only=True, many=True, required=False) + documents = EAPDocumentSerializer(many=True, required=False) ifrc_financial_report_details = EAPDocumentSerializer(source='ifrc_financial_report', read_only=True) class Meta: diff --git a/eap/test_views.py b/eap/test_views.py index 14f384126..fa75af8fe 100644 --- a/eap/test_views.py +++ b/eap/test_views.py @@ -76,7 +76,16 @@ def setUp(self): "early_action_budget": 2000, "trigger_statement": "test", "overview": "test", - "documents": [self.document1.id], + "documents": [ + { + "id": self.document1.id, + "caption": "test caption" + }, + { + "id": self.document2.id, + "caption": "test caption 2" + }, + ], "originator_name": "eap name", "originator_title": "eap title", "originator_email": "eap@gmail.com", @@ -168,7 +177,16 @@ def setUp(self): "number_of_people_reached": 1000, "description": "test eap activation report", "overall_objectives": "test eap activation report", - "documents": [self.document1.id, self.document2.id], + "documents": [ + { + "id": self.document1.id, + "caption": "test caption" + }, + { + "id": self.document2.id, + "caption": "test caption 2" + }, + ], "challenges_and_lesson": "test eap activation report", "general_lesson_and_recomendations": "test eap activation report", "ifrc_financial_report": self.document1.id, @@ -315,7 +333,16 @@ def test_create_and_update_eap_activation_report(self): # update eap_report data = self.eap_act_report_body data['description'] = 'updated description' - data['documents'] = [self.document1.id] + data['documents'] = [ + { + "id": self.document2.id, + "caption": "test caption updated" + }, + { + "id": self.document1.id, + "caption": "test caption updated" + } + ] data['ifrc_financial_report'] = self.document2.id data['operational_plans'] = [ { @@ -356,7 +383,6 @@ def test_create_and_update_eap_activation_report(self): self.assertEqual(updated.created_by.id, self.user.id) self.assertEqual(final_report_updated_resp['eap_activation'], self.eap_activation.id) self.assertEqual(final_report_updated_resp['ifrc_financial_report'], self.document2.id) - self.assertEqual(len(final_report_updated_resp['documents']), 1) self.assertEqual(len(final_report_updated_resp['operational_plans']), 1) self.assertEqual(len(final_report_updated_resp['operational_plans'][0]['early_actions_achievements']), 2) self.assertEqual(len(final_report_updated_resp['operational_plans'][0]['indicators']), 2) diff --git a/main/settings.py b/main/settings.py index 0e3933d11..82328901a 100644 --- a/main/settings.py +++ b/main/settings.py @@ -177,7 +177,7 @@ 'DEFAULT_RENDERER_CLASSES': ( 'rest_framework.renderers.JSONRenderer', # 'rest_framework.renderers.BrowsableAPIRenderer', # it is comment out to reduce load time in browsable api - 'main.utils.BrowsableAPIRendererWithRawForms', # it is added to reduce load time in browsable api + 'main.utils.BrowsableAPIRendererWithRawForms', # it is added to remove html form and reduce load time in browsable api 'rest_framework_csv.renderers.PaginatedCSVRenderer', ), 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',