1515from api .utils import CountryValidator , RegionValidator
1616from deployments .models import EmergencyProject , Personnel , PersonnelDeployment
1717from dref .models import Dref , DrefFinalReport , DrefOperationalUpdate
18- from eap .models import FullEAP , SimplifiedEAP
18+ from eap .models import EAPRegistration , FullEAP , SimplifiedEAP
1919from lang .models import String
2020from lang .serializers import ModelSerializer
2121from local_units .models import DelegationOffice
@@ -2544,6 +2544,11 @@ class ExportSerializer(serializers.ModelSerializer):
25442544 status_display = serializers .CharField (source = "get_status_display" , read_only = True )
25452545 # NOTE: is_pga is used to determine if the export contains PGA or not
25462546 is_pga = serializers .BooleanField (default = False , required = False , write_only = True )
2547+ # NOTE: diff is used to determine if the export is requested for diff view or not
2548+ # Currently only used for EAP exports
2549+ diff = serializers .BooleanField (default = False , required = False , write_only = True )
2550+ # NOTE: Version of a EAP export being requested, only applicable for full and simplified EAP exports
2551+ version = serializers .IntegerField (required = False , write_only = True )
25472552
25482553 class Meta :
25492554 model = Export
@@ -2559,6 +2564,7 @@ def create(self, validated_data):
25592564 export_id = validated_data .get ("export_id" )
25602565 export_type = validated_data .get ("export_type" )
25612566 country_id = validated_data .get ("per_country" )
2567+ version = validated_data .pop ("version" , None )
25622568 if export_type == Export .ExportType .DREF :
25632569 title = Dref .objects .filter (id = export_id ).first ().title
25642570 elif export_type == Export .ExportType .OPS_UPDATE :
@@ -2569,19 +2575,62 @@ def create(self, validated_data):
25692575 overview = Overview .objects .filter (id = export_id ).first ()
25702576 title = f"{ overview .country .name } -preparedness-{ overview .get_phase_display ()} "
25712577 elif export_type == Export .ExportType .SIMPLIFIED_EAP :
2572- simplified_eap = SimplifiedEAP .objects .filter (id = export_id ).first ()
2578+ if version :
2579+ simplified_eap = SimplifiedEAP .objects .filter (
2580+ eap_registration = export_id ,
2581+ version = version ,
2582+ ).first ()
2583+ if not simplified_eap :
2584+ raise serializers .ValidationError ("No Simplified EAP found for the given EAP Registration ID and version" )
2585+ else :
2586+ eap_registration = EAPRegistration .objects .filter (id = export_id ).first ()
2587+ if not eap_registration :
2588+ raise serializers .ValidationError ("No EAP Registration found for the given ID" )
2589+
2590+ simplified_eap = eap_registration .latest_simplified_eap
2591+ if not simplified_eap :
2592+ serializers .ValidationError ("No Simplified EAP found for the given EAP Registration ID" )
2593+
25732594 title = (
25742595 f"{ simplified_eap .eap_registration .national_society .name } -{ simplified_eap .eap_registration .disaster_type .name } "
25752596 )
25762597 elif export_type == Export .ExportType .FULL_EAP :
2577- full_eap = FullEAP .objects .filter (id = export_id ).first ()
2598+ if version :
2599+ full_eap = FullEAP .objects .filter (
2600+ eap_registration = export_id ,
2601+ version = version ,
2602+ ).first ()
2603+ if not full_eap :
2604+ raise serializers .ValidationError ("No Full EAP found for the given EAP Registration ID and version" )
2605+ else :
2606+ eap_registration = EAPRegistration .objects .filter (id = export_id ).first ()
2607+ if not eap_registration :
2608+ raise serializers .ValidationError ("No EAP Registration found for the given ID" )
2609+
2610+ full_eap = eap_registration .latest_full_eap
2611+ if not full_eap :
2612+ serializers .ValidationError ("No Full EAP found for the given EAP Registration ID" )
2613+
25782614 title = f"{ full_eap .eap_registration .national_society .name } -{ full_eap .eap_registration .disaster_type .name } "
25792615 else :
25802616 title = "Export"
25812617 user = self .context ["request" ].user
25822618
25832619 if export_type == Export .ExportType .PER :
25842620 validated_data ["url" ] = f"{ settings .GO_WEB_INTERNAL_URL } /countries/{ country_id } /{ export_type } /{ export_id } /export/"
2621+
2622+ elif export_type in [
2623+ Export .ExportType .SIMPLIFIED_EAP ,
2624+ Export .ExportType .FULL_EAP ,
2625+ ]:
2626+ validated_data ["url" ] = f"{ settings .GO_WEB_INTERNAL_URL } /eap/{ export_id } /{ export_type } /export/"
2627+ # NOTE: EAP exports with diff view only for EAPs exports
2628+ if version :
2629+ validated_data ["url" ] += f"?version={ version } "
2630+ diff = validated_data .pop ("diff" )
2631+ if diff :
2632+ validated_data ["url" ] += "&diff=true" if version else "?diff=true"
2633+
25852634 else :
25862635 validated_data ["url" ] = f"{ settings .GO_WEB_INTERNAL_URL } /{ export_type } /{ export_id } /export/"
25872636
0 commit comments