@@ -1150,7 +1150,7 @@ class Meta(BaseAssignmentSerializer.Meta):
11501150
11511151class IdentificationTaskSerializer (serializers .ModelSerializer ):
11521152 class IdentificationTaskReviewSerializer (serializers .ModelSerializer ):
1153- type = serializers .ChoiceField (source = 'review_type' ,choices = IdentificationTask .Review .choices )
1153+ action = serializers .ChoiceField (source = 'review_type' ,choices = IdentificationTask .Review .choices )
11541154
11551155 def to_representation (self , instance ):
11561156 if self .allow_null and instance .review_type is None :
@@ -1160,7 +1160,7 @@ def to_representation(self, instance):
11601160 class Meta :
11611161 model = IdentificationTask
11621162 fields = (
1163- "type " ,
1163+ "action " ,
11641164 "created_at"
11651165 )
11661166 extra_kwargs = {
@@ -1172,7 +1172,7 @@ class IdentificationTaskResultSerializer(serializers.ModelSerializer):
11721172 confidence = serializers .FloatField (min_value = 0 , max_value = 1 , read_only = True )
11731173 confidence_label = serializers .SerializerMethodField ()
11741174 is_high_confidence = serializers .SerializerMethodField ()
1175- source = serializers .ChoiceField (source = 'result_source' ,choices = IdentificationTask .ResultSource .choices )
1175+ source = serializers .ChoiceField (source = 'result_source' ,read_only = True , choices = IdentificationTask .ResultSource .choices )
11761176
11771177 def get_confidence_label (self , obj ) -> str :
11781178 return obj .confidence_label
@@ -1213,7 +1213,8 @@ class Meta(BaseAssignmentSerializer.Meta):
12131213 fields = ("user" , "annotation_id" ,) + BaseAssignmentSerializer .Meta .fields
12141214
12151215 observation = SimplifiedObservationWithPhotosSerializer (source = 'report' , read_only = True )
1216- public_photo = SimplePhotoSerializer (source = 'photo' , required = True )
1216+ public_photo_uuid = serializers .UUIDField (source = 'photo__uuid' , write_only = True )
1217+ public_photo = SimplePhotoSerializer (source = 'photo' , read_only = True )
12171218 review = IdentificationTaskReviewSerializer (source = '*' , allow_null = True , read_only = True )
12181219 result = IdentificationTaskResultSerializer (source = '*' , read_only = True , allow_null = True )
12191220 assignments = UserAssignmentSerializer (many = True , read_only = True )
@@ -1222,6 +1223,7 @@ class Meta:
12221223 model = IdentificationTask
12231224 fields = (
12241225 'observation' ,
1226+ 'public_photo_uuid' ,
12251227 'public_photo' ,
12261228 'assignments' ,
12271229 'status' ,
@@ -1235,13 +1237,98 @@ class Meta:
12351237 'updated_at'
12361238 )
12371239 extra_kwargs = {
1238- 'status' : {'default' : IdentificationTask .Status .OPEN },
1240+ 'status' : {'default' : IdentificationTask .Status .OPEN , 'read_only' : True },
12391241 'public_note' : {'allow_null' : True , 'allow_blank' : True },
12401242 'num_annotations' : {'source' : 'total_finished_annotations' ,'min_value' : 0 },
12411243 'created_at' : {'read_only' : True },
12421244 'updated_at' : {'read_only' : True },
12431245 }
12441246
1247+ class CreateReviewSerializer (serializers .ModelSerializer ):
1248+ action = serializers .HiddenField (default = None )
1249+
1250+ def validate (self , data ):
1251+ del data ['review_type' ]
1252+ data ['validation_complete' ] = True
1253+ data ['simplified_annotation' ] = True
1254+ data ['report' ] = self .context .get ('report' )
1255+
1256+ return data
1257+
1258+ def create (self , validated_data ):
1259+ report = validated_data .pop ('report' )
1260+
1261+ # TODO: Create a Review model for this.
1262+ ExpertReportAnnotation .objects .update_or_create (
1263+ user = self .context .get ('request' ).user ,
1264+ report = report ,
1265+ defaults = validated_data
1266+ )
1267+
1268+ identification_task = report .identification_task
1269+ identification_task .refresh_from_db ()
1270+ return identification_task
1271+
1272+ class Meta :
1273+ model = IdentificationTask
1274+ fields = (
1275+ 'action' ,
1276+ )
1277+ extra_kwargs = {
1278+ 'action' : {'source' : 'review_type' ,'read_only' : False },
1279+ }
1280+
1281+ class CreateAgreeReviewSerializer (CreateReviewSerializer ):
1282+ action = serializers .ChoiceField (source = 'review_type' , choices = [IdentificationTask .Review .AGREE .value ], default = IdentificationTask .Review .AGREE .value )
1283+
1284+ def validate (self , data ):
1285+ data = super ().validate (data )
1286+
1287+ data ['revise' ] = False
1288+ data ['status' ] = ExpertReportAnnotation .STATUS_HIDDEN if not data ['report' ].identification_task .is_safe else ExpertReportAnnotation .STATUS_PUBLIC
1289+
1290+ return data
1291+
1292+ class Meta (CreateReviewSerializer .Meta ):
1293+ fields = (
1294+ 'action' ,
1295+ )
1296+
1297+ class CreateOverwriteReviewSerializer (CreateReviewSerializer ):
1298+ action = serializers .ChoiceField (source = 'review_type' , choices = [IdentificationTask .Review .OVERWRITE .value ], default = IdentificationTask .Review .OVERWRITE .value )
1299+
1300+ public_photo_uuid = serializers .UUIDField (source = 'photo__uuid' , write_only = True )
1301+ result = AnnotationSerializer .AnnotationClassificationSerializer (source = '*' , required = True , allow_null = True )
1302+
1303+ def validate (self , data ):
1304+ data = super ().validate (data )
1305+
1306+ data ['revise' ] = True
1307+ data ['status' ] = ExpertReportAnnotation .STATUS_HIDDEN if not data .pop ('is_safe' ) else ExpertReportAnnotation .STATUS_PUBLIC
1308+ data ['simplified_annotation' ] = False
1309+ data ['edited_user_notes' ] = data .pop ('public_note' , None )
1310+
1311+ if public_photo_uuid := data .pop ('photo__uuid' , None ):
1312+ try :
1313+ data ['best_photo' ] = Photo .objects .get (report = data ['report' ], uuid = public_photo_uuid )
1314+ except Photo .DoesNotExist :
1315+ raise serializers .ValidationError ("The photo does not does not exist or does not belong to the observation." )
1316+
1317+ return data
1318+
1319+ class Meta (CreateReviewSerializer .Meta ):
1320+ fields = (
1321+ 'action' ,
1322+ 'public_photo_uuid' ,
1323+ 'is_safe' ,
1324+ 'public_note' ,
1325+ 'result' ,
1326+ )
1327+ extra_kwargs = {
1328+ 'is_safe' : {'read_only' : False },
1329+ 'public_note' : {'allow_null' : True , 'allow_blank' : False , 'read_only' : False }
1330+ }
1331+
12451332class ObservationSerializer (BaseReportWithPhotosSerializer ):
12461333 class IdentificationSerializer (serializers .ModelSerializer ):
12471334 photo = SimplePhotoSerializer (required = True )
0 commit comments