From f71bacbb8118422dbd1166e1883154f122eb80f5 Mon Sep 17 00:00:00 2001 From: Johnathan Clementi Date: Tue, 14 Jan 2025 20:16:53 -0500 Subject: [PATCH] Prevent multiple prefLabels in same language #147 --- arches_lingo/serializers.py | 32 ++++++++++++++++++++++++++++ arches_lingo/src/arches_lingo/api.ts | 5 ++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/arches_lingo/serializers.py b/arches_lingo/serializers.py index 26bf149a..f86358ee 100644 --- a/arches_lingo/serializers.py +++ b/arches_lingo/serializers.py @@ -1,3 +1,7 @@ +from rest_framework.exceptions import ValidationError + +from arches_references.models import ListItem + from arches.app.models.models import ResourceInstance, TileModel from arches.app.models.serializers import ArchesModelSerializer, ArchesTileSerializer @@ -49,6 +53,34 @@ class Meta: root_node = "appellative_status" fields = "__all__" + def validate(self, data): + data = super().validate(data) + graph_slug = self.Meta.graph_slug + PREF_LABEL_LIST_ITEM = ListItem.objects.get(list_item_values__value="prefLabel") + + if data: + new_label_language = data["appellative_status_ascribed_name_language"][0] + new_label_type = data["appellative_status_ascribed_relation"][0] + + resource_instance_id = self.instance.resourceinstance.pk + resource_instance = self.instance.resourceinstance.__class__.as_model( + graph_slug, resource_ids=[resource_instance_id] + ).get(pk=resource_instance_id) + current_labels = resource_instance.appellative_status + for label in current_labels: + if ( + data["tileid"] != label.tileid + and new_label_type["uri"] == PREF_LABEL_LIST_ITEM.uri + and label.appellative_status_ascribed_relation[0]["uri"] + == PREF_LABEL_LIST_ITEM.uri + and label.appellative_status_ascribed_name_language[0]["uri"] + == new_label_language["uri"] + ): + raise ValidationError( + "A preferred label with the same language already exists for this scheme." + ) + return data + class SchemeNoteSerializer(ArchesModelSerializer): class Meta: diff --git a/arches_lingo/src/arches_lingo/api.ts b/arches_lingo/src/arches_lingo/api.ts index 61f1aead..5cf0ea51 100644 --- a/arches_lingo/src/arches_lingo/api.ts +++ b/arches_lingo/src/arches_lingo/api.ts @@ -137,7 +137,10 @@ export const updateSchemeLabel = async ( }, ); const parsed = await response.json(); - if (!response.ok) throw new Error(parsed.message || response.statusText); + if (!response.ok) + throw new Error( + parsed.non_field_errors || parsed.message || response.statusText, + ); return parsed; };