@@ -184,6 +184,7 @@ class DrefSerializer(
184184 ALLOWED_ASSESSMENT_REPORT_EXTENSIONS = ["pdf" , "docx" , "pptx" ]
185185 MAX_OPERATION_TIMEFRAME = 30
186186 ASSESSMENT_REPORT_MAX_OPERATION_TIMEFRAME = 2
187+ DREF_UPDATE_ERROR_MESSAGE = "OBSOLETE_PAYLOAD"
187188 national_society_actions = NationalSocietyActionSerializer (many = True , required = False )
188189 needs_identified = IdentifiedNeedSerializer (many = True , required = False )
189190 planned_interventions = PlannedInterventionSerializer (many = True , required = False )
@@ -308,6 +309,11 @@ def validate_operation_timeframe(self, operation_timeframe):
308309 )
309310 return operation_timeframe
310311
312+ def validate_modified_at (self , modified_at ):
313+ if self .instance and self .instance .modified_at is None :
314+ raise serializers .ValidationError ('Modified At can\' t be None' )
315+ return modified_at
316+
311317 def create (self , validated_data ):
312318 validated_data ['created_by' ] = self .context ['request' ].user
313319 is_assessment_report = validated_data .get ('is_assessment_report' )
@@ -349,7 +355,9 @@ def create(self, validated_data):
349355 def update (self , instance , validated_data ):
350356 validated_data ['modified_by' ] = self .context ['request' ].user
351357 is_assessment_report = validated_data .get ('is_assessment_report' )
352- modified_at = validated_data .get ('modified_at' )
358+ modified_at = validated_data .pop ('modified_at' , None )
359+ if modified_at is None :
360+ raise serializers .ValidationError ('Modified At is required!' )
353361 if is_assessment_report :
354362 # Previous Operations
355363 validated_data ['lessons_learned' ] = None
@@ -382,13 +390,16 @@ def update(self, instance, validated_data):
382390 if u .email not in {t .email for t in instance .users .iterator ()}}
383391 else :
384392 to = None
385- if modified_at and instance .modified_at and modified_at > instance .modified_at :
386- dref = super ().update (instance , validated_data )
387- if to :
388- transaction .on_commit (
389- lambda : send_dref_email .delay (dref .id , list (to ), 'Updated' )
390- )
391- return dref
393+ if modified_at and instance .modified_at and modified_at < instance .modified_at :
394+ raise serializers .ValidationError (
395+ gettext (f'Input Payload in { self .DREF_UPDATE_ERROR_MESSAGE } ' )
396+ )
397+ dref = super ().update (instance , validated_data )
398+ if to :
399+ transaction .on_commit (
400+ lambda : send_dref_email .delay (dref .id , list (to ), 'Updated' )
401+ )
402+ return dref
392403 return instance
393404
394405
0 commit comments