1111from rest_framework import serializers
1212
1313# from api.utils import pdf_exporter
14- from api .tasks import generate_url
15- from api .utils import CountryValidator , RegionValidator
14+ from api .tasks import generate_export_pdf
15+ from api .utils import CountryValidator , RegionValidator , generate_eap_export_url
1616from deployments .models import EmergencyProject , Personnel , PersonnelDeployment
1717from dref .models import Dref , DrefFinalReport , DrefOperationalUpdate
18+ from eap .models import EAPRegistration , FullEAP , SimplifiedEAP
1819from lang .models import String
1920from lang .serializers import ModelSerializer
2021from local_units .models import DelegationOffice
@@ -371,12 +372,14 @@ class Admin2Serializer(GeoSerializerMixin, ModelSerializer):
371372 bbox = serializers .SerializerMethodField ()
372373 centroid = serializers .SerializerMethodField ()
373374 district_id = serializers .IntegerField (source = "admin1.id" , read_only = True )
375+ district_name = serializers .CharField (source = "admin1.name" , read_only = True )
374376
375377 class Meta :
376378 model = Admin2
377379 fields = (
378380 "id" ,
379381 "district_id" ,
382+ "district_name" ,
380383 "name" ,
381384 "code" ,
382385 "bbox" ,
@@ -387,10 +390,11 @@ class Meta:
387390
388391class MiniAdmin2Serializer (ModelSerializer ):
389392 district_id = serializers .IntegerField (source = "admin1.id" , read_only = True )
393+ district_name = serializers .CharField (source = "admin1.name" , read_only = True )
390394
391395 class Meta :
392396 model = Admin2
393- fields = ("id" , "name" , "code" , "district_id" )
397+ fields = ("id" , "name" , "code" , "district_id" , "district_name" )
394398
395399
396400class MiniDistrictSerializer (ModelSerializer ):
@@ -2545,6 +2549,13 @@ class ExportSerializer(serializers.ModelSerializer):
25452549 status_display = serializers .CharField (source = "get_status_display" , read_only = True )
25462550 # NOTE: is_pga is used to determine if the export contains PGA or not
25472551 is_pga = serializers .BooleanField (default = False , required = False , write_only = True )
2552+ # NOTE: diff is used to determine if the export is requested for diff view or not
2553+ # Currently only used for EAP exports
2554+ diff = serializers .BooleanField (default = False , required = False , write_only = True , help_text = "Only applicable for EAP exports" )
2555+ # NOTE: Version of a EAP export being requested, only applicable for full and simplified EAP exports
2556+ version = serializers .IntegerField (required = False , write_only = True , help_text = "Only applicable for EAP exports" )
2557+ # NOTE: Only for FUll eap export
2558+ summary = serializers .BooleanField (default = False , required = False , write_only = True , help_text = "Only applicable for FUll EAP" )
25482559
25492560 class Meta :
25502561 model = Export
@@ -2556,10 +2567,12 @@ def validate_pdf_file(self, pdf_file):
25562567 return pdf_file
25572568
25582569 def create (self , validated_data ):
2559- language = django_get_language ()
25602570 export_id = validated_data .get ("export_id" )
25612571 export_type = validated_data .get ("export_type" )
25622572 country_id = validated_data .get ("per_country" )
2573+ version = validated_data .pop ("version" , None )
2574+ diff = validated_data .pop ("diff" , False )
2575+ summary = validated_data .pop ("summary" , False )
25632576 if export_type == Export .ExportType .DREF :
25642577 title = Dref .objects .filter (id = export_id ).first ().title
25652578 elif export_type == Export .ExportType .OPS_UPDATE :
@@ -2569,17 +2582,67 @@ def create(self, validated_data):
25692582 elif export_type == Export .ExportType .PER :
25702583 overview = Overview .objects .filter (id = export_id ).first ()
25712584 title = f"{ overview .country .name } -preparedness-{ overview .get_phase_display ()} "
2585+ elif export_type == Export .ExportType .SIMPLIFIED_EAP :
2586+ if version :
2587+ simplified_eap = SimplifiedEAP .objects .filter (
2588+ eap_registration = export_id ,
2589+ version = version ,
2590+ ).first ()
2591+ if not simplified_eap :
2592+ raise serializers .ValidationError ("No Simplified EAP found for the given EAP Registration ID and version" )
2593+ else :
2594+ eap_registration = EAPRegistration .objects .filter (id = export_id ).first ()
2595+ if not eap_registration :
2596+ raise serializers .ValidationError ("No EAP Registration found for the given ID" )
2597+
2598+ simplified_eap = eap_registration .latest_simplified_eap
2599+ if not simplified_eap :
2600+ serializers .ValidationError ("No Simplified EAP found for the given EAP Registration ID" )
2601+
2602+ title = (
2603+ f"{ simplified_eap .eap_registration .national_society .name } -{ simplified_eap .eap_registration .disaster_type .name } "
2604+ )
2605+ elif export_type == Export .ExportType .FULL_EAP :
2606+ if version :
2607+ full_eap = FullEAP .objects .filter (
2608+ eap_registration = export_id ,
2609+ version = version ,
2610+ ).first ()
2611+ if not full_eap :
2612+ raise serializers .ValidationError ("No Full EAP found for the given EAP Registration ID and version" )
2613+ else :
2614+ eap_registration = EAPRegistration .objects .filter (id = export_id ).first ()
2615+ if not eap_registration :
2616+ raise serializers .ValidationError ("No EAP Registration found for the given ID" )
2617+
2618+ full_eap = eap_registration .latest_full_eap
2619+ if not full_eap :
2620+ serializers .ValidationError ("No Full EAP found for the given EAP Registration ID" )
2621+
2622+ title = f"{ full_eap .eap_registration .national_society .name } -{ full_eap .eap_registration .disaster_type .name } "
25722623 else :
25732624 title = "Export"
25742625 user = self .context ["request" ].user
25752626
25762627 if export_type == Export .ExportType .PER :
25772628 validated_data ["url" ] = f"{ settings .GO_WEB_INTERNAL_URL } /countries/{ country_id } /{ export_type } /{ export_id } /export/"
2629+
2630+ elif export_type in [
2631+ Export .ExportType .SIMPLIFIED_EAP ,
2632+ Export .ExportType .FULL_EAP ,
2633+ ]:
2634+ validated_data ["url" ] = generate_eap_export_url (
2635+ registration_id = export_id ,
2636+ version = version ,
2637+ diff = diff ,
2638+ summary = summary ,
2639+ )
2640+
25782641 else :
25792642 validated_data ["url" ] = f"{ settings .GO_WEB_INTERNAL_URL } /{ export_type } /{ export_id } /export/"
25802643
25812644 # Adding is_pga to the url
2582- is_pga = validated_data .pop ("is_pga" )
2645+ is_pga = validated_data .pop ("is_pga" , False )
25832646 if is_pga :
25842647 validated_data ["url" ] += "?is_pga=true"
25852648 validated_data ["requested_by" ] = user
@@ -2589,7 +2652,8 @@ def create(self, validated_data):
25892652 export .requested_at = timezone .now ()
25902653 export .save (update_fields = ["status" , "requested_at" ])
25912654
2592- transaction .on_commit (lambda : generate_url .delay (export .url , export .id , user .id , title , language ))
2655+ language = django_get_language ()
2656+ transaction .on_commit (lambda : generate_export_pdf .delay (export .id , title , language ))
25932657 return export
25942658
25952659 def update (self , instance , validated_data ):
0 commit comments