Skip to content

Commit

Permalink
nit re #10541, rename tables
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Feb 22, 2024
1 parent a5d11ac commit cacff79
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 33 deletions.
35 changes: 21 additions & 14 deletions arches/app/models/migrations/10541_controlled_list_manager.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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)),
Expand All @@ -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"
),
Expand All @@ -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",
),
]
4 changes: 2 additions & 2 deletions arches/app/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"],
Expand Down
28 changes: 17 additions & 11 deletions arches/app/views/controlled_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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"]
)

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"]),
Expand All @@ -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)
12 changes: 6 additions & 6 deletions tests/views/controlled_lists_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from arches.app.models.models import (
ControlledList,
ControlledListItem,
ControlledListItemLabel,
DValueType,
Label,
Language,
)
from arches.app.views.controlled_lists import serialize
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit cacff79

Please sign in to comment.