From cacff79bd1f08acc2c991ae2f03dafe21a856fa5 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Tue, 20 Feb 2024 14:59:48 -0500 Subject: [PATCH] nit re #10541, rename tables --- .../10541_controlled_list_manager.py | 35 +++++++++++-------- arches/app/models/models.py | 4 +-- arches/app/views/controlled_lists.py | 28 +++++++++------ tests/views/controlled_lists_tests.py | 12 +++---- 4 files changed, 46 insertions(+), 33 deletions(-) diff --git a/arches/app/models/migrations/10541_controlled_list_manager.py b/arches/app/models/migrations/10541_controlled_list_manager.py index 34c3b841bb2..d1447456ee1 100644 --- a/arches/app/models/migrations/10541_controlled_list_manager.py +++ b/arches/app/models/migrations/10541_controlled_list_manager.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.9 on 2024-01-25 14:50 +# Generated by Django 4.2.10 on 2024-02-20 13:44 import uuid import django.core.validators @@ -44,9 +44,6 @@ class Migration(migrations.Migration): ("name", models.CharField(max_length=127)), ("dynamic", models.BooleanField(default=False)), ], - options={ - "db_table": "controlled_lists", - }, ), migrations.CreateModel( name="ControlledListItem", @@ -63,16 +60,17 @@ class Migration(migrations.Migration): ( "parent", models.ForeignKey( - null=True, blank=True, on_delete=django.db.models.deletion.CASCADE, related_name="children", to="models.controlledlistitem" + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="children", + to="models.controlledlistitem", ), ), ], - options={ - "db_table": "controlled_list_items", - }, ), migrations.CreateModel( - name="Label", + name="ControlledListItemLabel", fields=[ ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), ("value", models.CharField(max_length=1024)), @@ -98,18 +96,15 @@ class Migration(migrations.Migration): ), ), ], - options={ - "db_table": "controlled_list_labels", - }, ), migrations.AddConstraint( - model_name="label", + model_name="controlledlistitemlabel", constraint=models.UniqueConstraint( fields=("item", "value", "value_type", "language"), name="unique_item_value_valuetype_language" ), ), migrations.AddConstraint( - model_name="label", + model_name="controlledlistitemlabel", constraint=models.UniqueConstraint( condition=models.Q(("value_type", "prefLabel")), fields=("item", "language"), name="unique_item_preflabel_language" ), @@ -120,4 +115,16 @@ class Migration(migrations.Migration): deferrable=django.db.models.constraints.Deferrable["DEFERRED"], fields=("list", "sortorder"), name="unique_list_sortorder" ), ), + migrations.AlterModelTable( + name="controlledlist", + table="controlled_lists", + ), + migrations.AlterModelTable( + name="controlledlistitem", + table="controlled_list_items", + ), + migrations.AlterModelTable( + name="controlledlistitemlabel", + table="controlled_list_item_labels", + ), ] diff --git a/arches/app/models/models.py b/arches/app/models/models.py index 934a6adf70b..1f2707cc34f 100644 --- a/arches/app/models/models.py +++ b/arches/app/models/models.py @@ -1880,7 +1880,7 @@ def clean(self): raise ValidationError(_("At least one preferred label is required.")) -class Label(models.Model): +class ControlledListItemLabel(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) item = models.ForeignKey( ControlledListItem, @@ -1900,7 +1900,7 @@ class Label(models.Model): value = models.CharField(max_length=1024, null=False) class Meta: - db_table = "controlled_list_labels" + db_table = "controlled_list_item_labels" constraints = [ models.UniqueConstraint( fields=["item", "value", "value_type", "language"], diff --git a/arches/app/views/controlled_lists.py b/arches/app/views/controlled_lists.py index 84339e566fa..7a74a640f58 100644 --- a/arches/app/views/controlled_lists.py +++ b/arches/app/views/controlled_lists.py @@ -8,8 +8,12 @@ from django.utils.decorators import method_decorator from django.utils.translation import get_language, gettext as _ - -from arches.app.models.models import ControlledList, ControlledListItem, Label, Language +from arches.app.models.models import ( + ControlledList, + ControlledListItem, + ControlledListItemLabel, + Language, +) from arches.app.utils.betterJSONSerializer import JSONDeserializer from arches.app.utils.decorators import group_required from arches.app.utils.response import JSONErrorResponse, JSONResponse @@ -44,7 +48,7 @@ def serialize(obj, depth_map=None): "parent_id": str(obj.parent_id) if obj.parent_id else None, "depth": depth_map[obj.id], } - case Label(): + case ControlledListItemLabel(): return { "id": str(obj.id), "valuetype": obj.value_type_id, @@ -86,7 +90,9 @@ def handle_items(itemDicts): for label in labels: label["language_id"] = label.pop("language") label["value_type_id"] = label.pop("valuetype") - labels_to_save.append(Label(item_id=item_to_save.id, **label)) + labels_to_save.append( + ControlledListItemLabel(item_id=item_to_save.id, **label) + ) # Consider skipping uniqueness checks and just letting IntegrityError # bubble up. But doing Django validation provides a localized error. @@ -96,7 +102,7 @@ def handle_items(itemDicts): ControlledListItem.objects.bulk_update( items_to_save, fields=["uri", "sortorder", "parent"] ) - Label.objects.bulk_update( + ControlledListItemLabel.objects.bulk_update( labels_to_save, fields=["value", "value_type", "language"] ) @@ -212,7 +218,7 @@ def add_new_item(self, request): parent_id=data.get("parent_id", None), ) item.save() - label = Label( + label = ControlledListItemLabel( item=item, value=_("New Label: ") + datetime.now().isoformat(), value_type_id="prefLabel", @@ -263,7 +269,7 @@ class LabelView(View): def add_new_label(self, request): data = JSONDeserializer().deserialize(request.body) - label = Label( + label = ControlledListItemLabel( item_id=data["itemId"], value_type_id=data["valuetype"], language=Language.objects.get(code=data["language"]), @@ -288,21 +294,21 @@ def post(self, request, **kwargs): data = JSONDeserializer().deserialize(request.body) try: - Label.objects.filter(pk=label_id).update( + ControlledListItemLabel.objects.filter(pk=label_id).update( value=data["value"], language_id=data["language"] ) - except Label.DoesNotExist: + except ControlledListItemLabel.DoesNotExist: return JSONErrorResponse(status=404) except IntegrityError as e: return JSONErrorResponse(message=" ".join(e.args), status=400) except: return JSONErrorResponse() - return JSONResponse(serialize(Label.objects.get(pk=label_id))) + return JSONResponse(serialize(ControlledListItemLabel.objects.get(pk=label_id))) def delete(self, request, **kwargs): label_id = kwargs.get("id") - objs_deleted, _ = Label.objects.filter(pk=label_id).delete() + objs_deleted, _ = ControlledListItemLabel.objects.filter(pk=label_id).delete() if not objs_deleted: return JSONErrorResponse(status=404) return JSONResponse(status=204) diff --git a/tests/views/controlled_lists_tests.py b/tests/views/controlled_lists_tests.py index 9f1294787d2..656b0cc8ae7 100644 --- a/tests/views/controlled_lists_tests.py +++ b/tests/views/controlled_lists_tests.py @@ -8,8 +8,8 @@ from arches.app.models.models import ( ControlledList, ControlledListItem, + ControlledListItemLabel, DValueType, - Label, Language, ) from arches.app.views.controlled_lists import serialize @@ -62,9 +62,9 @@ def setUpTestData(cls): child.save() # Create a prefLabel and altLabel per item. (20) - Label.objects.bulk_create( + ControlledListItemLabel.objects.bulk_create( [ - Label( + ControlledListItemLabel( value=f"label{num}-pref", language=cls.first_language, value_type=cls.pref_label, @@ -73,7 +73,7 @@ def setUpTestData(cls): for num in range(5) ] + [ - Label( + ControlledListItemLabel( value=f"label{num}-alt", language=cls.first_language, value_type=cls.alt_label, @@ -82,7 +82,7 @@ def setUpTestData(cls): for num in range(5) ] + [ - Label( + ControlledListItemLabel( value=f"label{num}-pref", language=cls.first_language, value_type=cls.pref_label, @@ -91,7 +91,7 @@ def setUpTestData(cls): for num in range(5) ] + [ - Label( + ControlledListItemLabel( value=f"label{num}-alt", language=cls.first_language, value_type=cls.alt_label,