Skip to content

Commit

Permalink
refactor(apis_relations): Property field, methods
Browse files Browse the repository at this point in the history
Focus/favour "name_forward" field in
prep for upcoming field deprecation:
- require non-empty string value
- swap with "deprecated_name" field in:
  .. Property methods
  .. apis_entities
  .. apis_relations
  .. utils
  • Loading branch information
koeaw committed Feb 26, 2024
1 parent da7fd6a commit 5cf61f9
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 22 deletions.
4 changes: 2 additions & 2 deletions apis_core/apis_entities/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


def related_property(queryset, name, value):
p = Property.objects.get(deprecated_name=value)
p = Property.objects.get(name_forward=value)
queryset = queryset.filter(triple_set_from_subj__prop=p).distinct()
return queryset

Expand All @@ -37,7 +37,7 @@ class AbstractEntityFilterSet(GenericFilterSet):
method=related_entity_name, label="Related entity"
)
related_property = django_filters.ModelChoiceFilter(
queryset=Property.objects.all().order_by("deprecated_name"),
queryset=Property.objects.all().order_by("name_forward"),
label="Related Property",
method=related_property,
)
Expand Down
4 changes: 3 additions & 1 deletion apis_core/apis_metainfo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ class RootObject(GenericModel, models.Model):
deprecated_name.system_check_deprecated_details = {
"msg": "RootObject's field for name is being deprecated.",
"hint": "Create a new field in all affected model classes and "
"transfer the current contents of the 'deprecated_name' CharField.",
"transfer the current contents of the 'deprecated_name' CharField. "
"To interact with Property instances, use only the 'name_forward' "
"and 'name_reverse' fields going forward, not 'deprecated_name'.",
"id": "apis_core.W001",
}

Expand Down
4 changes: 2 additions & 2 deletions apis_core/apis_relations/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ def load_subj_obj_prop(
):
# the more important function here when writing data from an user input via an ajax call into this form.
# Because here the direction of the property is respected. Hence the subject and object position of the
# triple and the property deprecated_name or name_reverse are loaded correctly here.
# triple and the property name_forward or name_reverse are loaded correctly here.

if property_direction == PropertyAutocomplete.SELF_SUBJ_OTHER_OBJ_STR:

triple_subj = entity_instance_self
triple_obj = entity_instance_other
property_direction_name = property_instance.deprecated_name
property_direction_name = property_instance.name_forward

elif property_direction == PropertyAutocomplete.SELF_OBJ_OTHER_SUBJ_STR:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def handle(self, *args, **options):
# below msg. purposefully only mentions "labels".
self.stdout.write("The following labels now exist:")
for p in props:
self.stdout.write(self.style.SUCCESS(f"{p.deprecated_name}"), ending="")
self.stdout.write(self.style.SUCCESS(f"{p.name_forward}"), ending="")
self.stdout.write(f" ... {p.name_reverse}")
else:
# construct_properties() starts out by deleting all existing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.10 on 2024-02-13 20:56

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("apis_relations", "0004_auto_20230124_1201"),
]

operations = [
migrations.AlterField(
model_name="property",
name="name_forward",
field=models.CharField(
help_text='Inverse relation like: "is sub-class of" vs. "is super-class of".',
max_length=255,
verbose_name="Name forward",
),
),
]
15 changes: 6 additions & 9 deletions apis_core/apis_relations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,12 @@ class Meta:
property_class_uri = models.CharField(
max_length=255, verbose_name="Property Class URI", blank=True
)
# TODO RDF: Redundancy between name_forward and name, solve this.

name_forward = models.CharField(
max_length=255,
verbose_name="Name forward",
help_text='Inverse relation like: "is sub-class of" vs. "is super-class of".',
blank=True,
)
# TODO RDF: Maybe rename name to name_subj_to_obj and name_reverse to name_obj_to_subj
name_reverse = models.CharField(
max_length=255,
verbose_name="Name reverse",
Expand All @@ -81,17 +79,16 @@ class Meta:
)

def __str__(self):
return self.deprecated_name
return self.name_forward

def save(self, *args, **kwargs):
if self.name_reverse == "":
self.name_reverse = f"{self.deprecated_name} [INVERSE]"
self.name_reverse = f"{self.name_forward} [INVERSE]"

self.deprecated_name = unicodedata.normalize("NFC", self.deprecated_name)
self.name_reverse = unicodedata.normalize("NFC", self.name_reverse)
self.name_forward = unicodedata.normalize("NFC", str(self.name_forward))
self.name_reverse = unicodedata.normalize("NFC", str(self.name_reverse))

# TODO RDF: Temporary hack, remove this once better solution is found
self.name_forward = self.deprecated_name
self.deprecated_name = self.name_forward

if "update_fields" in kwargs and (
include_fields := {"name_forward", "name_reverse", "deprecated_name"}
Expand Down
4 changes: 1 addition & 3 deletions apis_core/apis_relations/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ def __init__(self, data, *args, **kwargs):
),
other_prop=Case(
# **kwargs pattern is needed here as the key-value pairs change with each relation class and entity instance.
When(
**{"subj__pk": entity_pk_self, "then": "prop__deprecated_name"}
),
When(**{"subj__pk": entity_pk_self, "then": "prop__name_forward"}),
When(**{"obj__pk": entity_pk_self, "then": "prop__name_reverse"}),
),
)
Expand Down
4 changes: 2 additions & 2 deletions apis_core/utils/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def get_autocomplete_property_choices(
rbc_self_subj_other_obj = Property.objects.filter(
subj_class=model_self_contenttype,
obj_class=model_other_contenttype,
deprecated_name__icontains=search_name_str,
name_forward__icontains=search_name_str,
)
rbc_self_obj_other_subj = Property.objects.filter(
subj_class=model_other_contenttype,
Expand All @@ -228,7 +228,7 @@ def get_autocomplete_property_choices(
{
# misuse of the id item as explained above
"id": f"id:{rbc.pk}__direction:{PropertyAutocomplete.SELF_SUBJ_OTHER_OBJ_STR}",
"text": rbc.deprecated_name,
"text": rbc.name_forward,
}
)
for rbc in rbc_self_obj_other_subj:
Expand Down
4 changes: 2 additions & 2 deletions apis_core/utils/filtermethods.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ def related_property_name(queryset, name, value):
lookup, value = construct_lookup(value)

queryset = queryset.filter(
Q(**{f"triple_set_from_obj__prop__deprecated_name{lookup}": value})
Q(**{f"triple_set_from_obj__prop__name_forward{lookup}": value})
| Q(**{f"triple_set_from_obj__prop__name_reverse{lookup}": value})
| Q(**{f"triple_set_from_subj__prop__deprecated_name{lookup}": value})
| Q(**{f"triple_set_from_subj__prop__name_forward{lookup}": value})
| Q(**{f"triple_set_from_subj__prop__name_reverse{lookup}": value})
).distinct()

Expand Down

0 comments on commit 5cf61f9

Please sign in to comment.