|
1 | 1 | import logging |
2 | 2 |
|
| 3 | +from django.core.exceptions import ValidationError |
3 | 4 | from rest_framework import serializers |
4 | 5 | from rest_framework.exceptions import ValidationError as DRFValidationError |
5 | 6 | from rest_framework.fields import CharField, SerializerMethodField |
|
26 | 27 | logger = logging.getLogger(__name__) |
27 | 28 |
|
28 | 29 |
|
| 30 | +def reformat_serialized_civ_data(*, serialized_civ_data): |
| 31 | + """Takes serialized CIV data and returns list of CIVData objects.""" |
| 32 | + |
| 33 | + possible_keys = [ |
| 34 | + "image", |
| 35 | + "value", |
| 36 | + "file", |
| 37 | + "user_upload", |
| 38 | + "upload_session", |
| 39 | + ("image_name", "user_uploads"), |
| 40 | + ] |
| 41 | + |
| 42 | + civ_data_objects = [] |
| 43 | + for civ in serialized_civ_data: |
| 44 | + interface = civ["interface"] |
| 45 | + |
| 46 | + keys = set(civ.keys()) - {"interface"} |
| 47 | + |
| 48 | + if not keys: |
| 49 | + raise serializers.ValidationError( |
| 50 | + f"You must provide at least one of {possible_keys}" |
| 51 | + ) |
| 52 | + elif keys == {"image_name", "user_uploads"}: |
| 53 | + value = DICOMUploadWithName( |
| 54 | + name=civ["image_name"], |
| 55 | + user_uploads=civ["user_uploads"], |
| 56 | + ) |
| 57 | + elif len(keys) > 1: |
| 58 | + raise serializers.ValidationError( |
| 59 | + f"You can only provide one of {possible_keys} for each interface." |
| 60 | + ) |
| 61 | + else: |
| 62 | + value = civ[list(keys)[0]] |
| 63 | + |
| 64 | + try: |
| 65 | + civ_data_objects.append( |
| 66 | + CIVData(interface_slug=interface.slug, value=value) |
| 67 | + ) |
| 68 | + except ValidationError as e: |
| 69 | + raise serializers.ValidationError(e) |
| 70 | + |
| 71 | + return civ_data_objects |
| 72 | + |
| 73 | + |
29 | 74 | class ComponentInterfaceSerializer(serializers.ModelSerializer): |
30 | 75 | kind = serializers.CharField(source="get_kind_display", read_only=True) |
31 | 76 | super_kind = SerializerMethodField() |
@@ -276,31 +321,10 @@ def update(self, instance, validated_data): |
276 | 321 |
|
277 | 322 | request = self.context["request"] |
278 | 323 |
|
279 | | - civ_data_objects = [] |
280 | | - |
281 | | - for value in values: |
282 | | - interface = value["interface"] |
283 | | - upload_session = value.get("upload_session") |
284 | | - user_upload = value.get("user_upload") |
285 | | - image = value.get("image") |
286 | | - user_uploads = value.get("user_uploads") |
287 | | - image_name = value.get("image_name") |
288 | | - value = value.get("value") |
289 | | - dicom_upload_with_name = ( |
290 | | - DICOMUploadWithName(name=image_name, user_uploads=user_uploads) |
291 | | - if user_uploads and image_name |
292 | | - else None |
293 | | - ) |
294 | | - civ_data_objects.append( |
295 | | - CIVData( |
296 | | - interface_slug=interface.slug, |
297 | | - value=upload_session |
298 | | - or user_upload |
299 | | - or image |
300 | | - or dicom_upload_with_name |
301 | | - or value, |
302 | | - ) |
303 | | - ) |
| 324 | + civ_data_objects = reformat_serialized_civ_data( |
| 325 | + serialized_civ_data=values |
| 326 | + ) |
| 327 | + |
304 | 328 | try: |
305 | 329 | instance.validate_civ_data_objects_and_execute_linked_task( |
306 | 330 | civ_data_objects=civ_data_objects, user=request.user |
|
0 commit comments