11import json
2- import datetime
32from django .utils import timezone
43from rest_framework import serializers
54from django .contrib .auth .models import User
5251 MainContact
5352)
5453from notifications .models import Subscription
55- from deployments .models import Personnel
5654
5755
5856class GeoSerializerMixin :
@@ -92,7 +90,8 @@ class Meta:
9290class RegionGeoSerializer (EnumSupportSerializerMixin , ModelSerializer ):
9391 bbox = serializers .SerializerMethodField ()
9492
95- def get_bbox (self , region ):
93+ @staticmethod
94+ def get_bbox (region ):
9695 return region .bbox and json .loads (region .bbox .geojson )
9796
9897 class Meta :
@@ -129,10 +128,12 @@ class CountryGeoSerializer(EnumSupportSerializerMixin, ModelSerializer):
129128 centroid = serializers .SerializerMethodField ()
130129 record_type_display = serializers .CharField (source = 'get_record_type_display' , read_only = True )
131130
132- def get_bbox (self , country ):
131+ @staticmethod
132+ def get_bbox (country ):
133133 return country .bbox and json .loads (country .bbox .geojson )
134134
135- def get_centroid (self , country ):
135+ @staticmethod
136+ def get_centroid (country ):
136137 return country .centroid and json .loads (country .centroid .geojson )
137138
138139 class Meta :
@@ -168,12 +169,25 @@ class Meta:
168169 model = District
169170 fields = ('name' , 'code' , 'is_deprecated' ,)
170171
172+
171173class MicroCountrySerializer (ModelSerializer ):
172174 class Meta :
173175 model = Country
174176 fields = ('id' , 'name' , 'iso' , 'iso3' , 'society_name' )
175177
176178
179+ class NanoCountrySerializer (ModelSerializer ):
180+ region = serializers .SerializerMethodField ()
181+
182+ @staticmethod
183+ def get_region (obj ):
184+ return obj .region .label if obj .region else None
185+
186+ class Meta :
187+ model = Country
188+ fields = ('iso3' , 'name' , 'society_name' , 'region' )
189+
190+
177191class RegoCountrySerializer (ModelSerializer ):
178192 class Meta :
179193 model = Country
@@ -219,7 +233,19 @@ class MiniDistrictGeoSerializer(GeoSerializerMixin, ModelSerializer):
219233 country_iso = serializers .CharField (source = 'country.iso' , read_only = True )
220234 country_iso3 = serializers .CharField (source = 'country.iso3' , read_only = True )
221235
236+ @staticmethod
237+ def get_bbox (district ):
238+ if district .bbox :
239+ return json .loads (district .bbox .geojson )
240+ else :
241+ return None
222242
243+ @staticmethod
244+ def get_centroid (district ):
245+ if district .centroid :
246+ return json .loads (district .centroid .geojson )
247+ else :
248+ return None
223249
224250 class Meta :
225251 model = District
@@ -330,10 +356,12 @@ class RegionRelationSerializer(EnumSupportSerializerMixin, ModelSerializer):
330356 national_society_count = serializers .SerializerMethodField ()
331357 country_cluster_count = serializers .SerializerMethodField ()
332358
333- def get_national_society_count (self , obj ):
359+ @staticmethod
360+ def get_national_society_count (obj ):
334361 return obj .get_national_society_count ()
335362
336- def get_country_cluster_count (self , obj ):
363+ @staticmethod
364+ def get_country_cluster_count (obj ):
337365 return obj .get_country_cluster_count ()
338366
339367 class Meta :
@@ -442,15 +470,15 @@ class Meta:
442470class MiniEventSerializer (ModelSerializer ):
443471 class Meta :
444472 model = Event
445- fields = ('name' , 'dtype' , 'id' , 'slug' , 'parent_event' ,)
473+ fields = ('name' , 'dtype' , 'id' , 'slug' , 'parent_event' , 'emergency_response_contact_email' )
446474
447475
448476class ListMiniEventSerializer (ModelSerializer ):
449477 dtype = DisasterTypeSerializer (read_only = True )
450478
451479 class Meta :
452480 model = Event
453- fields = ('id' , 'name' , 'slug' , 'dtype' , 'auto_generated_source' )
481+ fields = ('id' , 'name' , 'slug' , 'dtype' , 'auto_generated_source' , 'emergency_response_contact_email' )
454482
455483
456484class ListEventSerializer (EnumSupportSerializerMixin , ModelSerializer ):
@@ -466,6 +494,7 @@ class Meta:
466494 'name' , 'dtype' , 'countries' , 'summary' , 'num_affected' , 'ifrc_severity_level' , 'ifrc_severity_level_display' ,
467495 'glide' , 'disaster_start_date' , 'created_at' , 'auto_generated' , 'appeals' , 'is_featured' , 'is_featured_region' ,
468496 'field_reports' , 'updated_at' , 'id' , 'slug' , 'parent_event' , 'tab_one_title' , 'tab_two_title' , 'tab_three_title' ,
497+ 'emergency_response_contact_email' ,
469498 )
470499
471500
@@ -476,13 +505,16 @@ class ListEventTableauSerializer(EnumSupportSerializerMixin, serializers.ModelSe
476505 dtype = DisasterTypeSerializer ()
477506 ifrc_severity_level_display = serializers .CharField (source = 'get_ifrc_severity_level_display' , read_only = True )
478507
479- def get_countries (self , obj ):
508+ @staticmethod
509+ def get_countries (obj ):
480510 return get_merged_items_by_fields (obj .countries .all (), ['id' , 'name' ])
481511
482- def get_field_reports (self , obj ):
512+ @staticmethod
513+ def get_field_reports (obj ):
483514 return get_merged_items_by_fields (obj .field_reports .all (), ['id' ])
484515
485- def get_appeals (self , obj ):
516+ @staticmethod
517+ def get_appeals (obj ):
486518 return get_merged_items_by_fields (obj .appeals .all (), ['id' ])
487519
488520 class Meta :
@@ -501,13 +533,16 @@ class ListEventCsvSerializer(EnumSupportSerializerMixin, serializers.ModelSerial
501533 dtype = DisasterTypeSerializer ()
502534 ifrc_severity_level_display = serializers .CharField (source = 'get_ifrc_severity_level_display' , read_only = True )
503535
504- def get_countries (self , obj ):
536+ @staticmethod
537+ def get_countries (obj ):
505538 return get_merged_items_by_fields (obj .countries .all (), ['id' , 'name' , 'iso' , 'iso3' , 'society_name' ])
506539
507- def get_field_reports (self , obj ):
540+ @staticmethod
541+ def get_field_reports (obj ):
508542 return get_merged_items_by_fields (obj .field_reports .all (), ['id' ])
509543
510- def get_appeals (self , obj ):
544+ @staticmethod
545+ def get_appeals (obj ):
511546 return get_merged_items_by_fields (obj .appeals .all (), ['id' ])
512547
513548 class Meta :
@@ -526,19 +561,23 @@ class ListEventForPersonnelCsvSerializer(EnumSupportSerializerMixin, serializers
526561 dtype_name = serializers .SerializerMethodField ()
527562
528563 # NOTE: prefetched at deployments/drf_views.py::PersonnelViewset::get_queryset
529- def get_countries (self , obj ):
564+ @staticmethod
565+ def get_countries (obj ):
530566 fields = ['id' , 'name' , 'iso' , 'iso3' , 'society_name' ]
531567 return get_merged_items_by_fields (obj .countries .all (), fields )
532568
533- def get_field_reports (self , obj ):
569+ @staticmethod
570+ def get_field_reports (obj ):
534571 fields = ['id' ]
535572 return get_merged_items_by_fields (obj .field_reports .all (), fields )
536573
537- def get_appeals (self , obj ):
574+ @staticmethod
575+ def get_appeals (obj ):
538576 fields = ['id' , 'status' ]
539577 return get_merged_items_by_fields (obj .appeals .all (), fields )
540578
541- def get_dtype_name (self , obj ):
579+ @staticmethod
580+ def get_dtype_name (obj ):
542581 return obj .dtype and obj .dtype .name
543582
544583 class Meta :
@@ -550,6 +589,24 @@ class Meta:
550589 )
551590
552591
592+ class SmallEventForPersonnelCsvSerializer (EnumSupportSerializerMixin , serializers .ModelSerializer ):
593+ countries = serializers .SerializerMethodField ()
594+ dtype_name = serializers .SerializerMethodField ()
595+
596+ @staticmethod
597+ def get_countries (obj ):
598+ fields = ['name' , 'iso3' , 'society_name' , 'region' ]
599+ return get_merged_items_by_fields (obj .countries .all (), fields )
600+
601+ @staticmethod
602+ def get_dtype_name (obj ):
603+ return obj .dtype and obj .dtype .name
604+
605+ class Meta :
606+ model = Event
607+ fields = ('name' , 'dtype_name' , 'countries' , 'ifrc_severity_level' , 'glide' , 'id' )
608+
609+
553610class ListEventDeploymentsSerializer (serializers .Serializer ):
554611 id = serializers .IntegerField ()
555612 type = serializers .CharField ()
@@ -560,7 +617,8 @@ class DeploymentsByEventSerializer(ModelSerializer):
560617 organizations_from = serializers .SerializerMethodField ()
561618 personnel_count = serializers .IntegerField ()
562619
563- def get_organizations_from (self , obj ):
620+ @staticmethod
621+ def get_organizations_from (obj ):
564622 deployments = [d for d in obj .personneldeployment_set .all ()]
565623 personnels = []
566624 for d in deployments :
@@ -650,7 +708,6 @@ class AppealHistoryTableauSerializer(EnumSupportSerializerMixin, serializers.Mod
650708 country = MiniCountrySerializer ()
651709 dtype = DisasterTypeSerializer ()
652710 region = RegionSerializer ()
653- event = MiniEventSerializer ()
654711 atype_display = serializers .CharField (source = 'get_atype_display' , read_only = True )
655712 status_display = serializers .CharField (source = 'get_status_display' , read_only = True )
656713 code = serializers .CharField (source = 'appeal.code' , read_only = True )
@@ -773,7 +830,8 @@ def update(self, instance, validated_data):
773830 instance .save ()
774831 return instance
775832
776- def get_is_ifrc_admin (self , obj ):
833+ @staticmethod
834+ def get_is_ifrc_admin (obj ):
777835 return obj .groups .filter (name__iexact = "IFRC Admins" ).exists ()
778836
779837
@@ -794,19 +852,22 @@ class Meta:
794852 'is_admin_for_countries' , 'is_admin_for_regions' , 'lang_permissions'
795853 )
796854
797- def get_is_admin_for_countries (self , user ):
855+ @staticmethod
856+ def get_is_admin_for_countries (user ):
798857 return set ([
799858 int (permission [18 :]) for permission in user .get_all_permissions ()
800859 if ('api.country_admin_' in permission and permission [18 :].isdigit ())
801860 ])
802861
803- def get_is_admin_for_regions (self , user ):
862+ @staticmethod
863+ def get_is_admin_for_regions (user ):
804864 return set ([
805865 int (permission [17 :]) for permission in user .get_all_permissions ()
806866 if ('api.region_admin_' in permission and permission [17 :].isdigit ())
807867 ])
808868
809- def get_lang_permissions (self , user ):
869+ @staticmethod
870+ def get_lang_permissions (user ):
810871 return String .get_user_permissions_per_language (user )
811872
812873
@@ -889,16 +950,20 @@ class ListFieldReportTableauSerializer(FieldReportEnumDisplayMixin, ModelSeriali
889950 event = MiniEventSerializer ()
890951 actions_taken = serializers .SerializerMethodField ('get_actions_taken_for_organization' )
891952
892- def get_countries (self , obj ):
953+ @staticmethod
954+ def get_countries (obj ):
893955 return get_merged_items_by_fields (obj .countries .all (), ['id' , 'name' ])
894956
895- def get_districts (self , obj ):
957+ @staticmethod
958+ def get_districts (obj ):
896959 return get_merged_items_by_fields (obj .districts .all (), ['id' , 'name' ])
897960
898- def get_regions (self , obj ):
961+ @staticmethod
962+ def get_regions (obj ):
899963 return get_merged_items_by_fields (obj .regions .all (), ['id' , 'region_name' ])
900964
901- def get_actions_taken_for_organization (self , obj ):
965+ @staticmethod
966+ def get_actions_taken_for_organization (obj ):
902967 actions_data = {}
903968 actions_taken = obj .actions_taken .all ()
904969 for action in actions_taken :
@@ -927,16 +992,20 @@ class ListFieldReportCsvSerializer(FieldReportEnumDisplayMixin, ModelSerializer)
927992 event = MiniEventSerializer ()
928993 actions_taken = serializers .SerializerMethodField ('get_actions_taken_for_organization' )
929994
930- def get_countries (self , obj ):
995+ @staticmethod
996+ def get_countries (obj ):
931997 return get_merged_items_by_fields (obj .countries .all (), ['id' , 'name' , 'iso' , 'iso3' , 'society_name' ])
932998
933- def get_districts (self , obj ):
999+ @staticmethod
1000+ def get_districts (obj ):
9341001 return get_merged_items_by_fields (obj .districts .all (), ['id' , 'name' ])
9351002
936- def get_regions (self , obj ):
1003+ @staticmethod
1004+ def get_regions (obj ):
9371005 return get_merged_items_by_fields (obj .regions .all (), ['id' , 'region_name' ])
9381006
939- def get_actions_taken_for_organization (self , obj ):
1007+ @staticmethod
1008+ def get_actions_taken_for_organization (obj ):
9401009 actions_data = {}
9411010 actions_taken = obj .actions_taken .all ()
9421011 for action in actions_taken :
0 commit comments