Skip to content

Commit

Permalink
Added caption for file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Rup-Narayan-Rajbanshi committed Sep 13, 2022
1 parent 8aa916e commit 4909a62
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 21 deletions.
23 changes: 16 additions & 7 deletions api/drf_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,14 +911,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

Expand Down Expand Up @@ -991,13 +995,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'])

Expand Down
5 changes: 3 additions & 2 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from lang.serializers import ModelSerializer
from lang.models import String


from .models import (
DisasterType,
ExternalPartner,
Expand Down Expand Up @@ -1062,8 +1063,6 @@ class Meta:


class DetailFieldReportSerializer(FieldReportEnumDisplayMixin, ModelSerializer):
from eap.serializers import EAPActivationSerializer

user = UserSerializer()
dtype = DisasterTypeSerializer()
contacts = FieldReportContactSerializer(many=True)
Expand All @@ -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
Expand Down
18 changes: 16 additions & 2 deletions api/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': '[email protected]',
Expand Down Expand Up @@ -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': '[email protected]',
Expand Down
23 changes: 23 additions & 0 deletions eap/migrations/0009_auto_20220811_0836.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
2 changes: 1 addition & 1 deletion eap/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 15 additions & 4 deletions eap/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
34 changes: 30 additions & 4 deletions eap/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[email protected]",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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'] = [
{
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 4909a62

Please sign in to comment.