Skip to content

Commit

Permalink
Prevent multiple prefLabels in same language #147
Browse files Browse the repository at this point in the history
  • Loading branch information
johnatawnclementawn committed Jan 15, 2025
1 parent 557a2a0 commit f71bacb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
32 changes: 32 additions & 0 deletions arches_lingo/serializers.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion arches_lingo/src/arches_lingo/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down

0 comments on commit f71bacb

Please sign in to comment.