From be3300fbadbe1cd54fe1aa0f5796e773d1281995 Mon Sep 17 00:00:00 2001 From: KK Date: Wed, 14 Feb 2024 06:06:20 +0100 Subject: [PATCH] refactor(apis_relations): usage of duplicated Property fields 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 --- apis_core/apis_entities/filtersets.py | 4 ++-- apis_core/apis_metainfo/models.py | 4 +++- apis_core/apis_relations/forms.py | 4 ++-- .../commands/create_relationships.py | 2 +- .../0006_alter_property_name_forward.py | 22 +++++++++++++++++++ apis_core/apis_relations/models.py | 15 +++++-------- apis_core/apis_relations/tables.py | 4 +--- apis_core/utils/caching.py | 4 ++-- apis_core/utils/filtermethods.py | 4 ++-- 9 files changed, 41 insertions(+), 22 deletions(-) create mode 100644 apis_core/apis_relations/migrations/0006_alter_property_name_forward.py diff --git a/apis_core/apis_entities/filtersets.py b/apis_core/apis_entities/filtersets.py index 175590521..fbed98d9f 100644 --- a/apis_core/apis_entities/filtersets.py +++ b/apis_core/apis_entities/filtersets.py @@ -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 @@ -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, ) diff --git a/apis_core/apis_metainfo/models.py b/apis_core/apis_metainfo/models.py index f828b8906..a409f992c 100644 --- a/apis_core/apis_metainfo/models.py +++ b/apis_core/apis_metainfo/models.py @@ -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", } diff --git a/apis_core/apis_relations/forms.py b/apis_core/apis_relations/forms.py index b9b052956..028b278e3 100644 --- a/apis_core/apis_relations/forms.py +++ b/apis_core/apis_relations/forms.py @@ -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: diff --git a/apis_core/apis_relations/management/commands/create_relationships.py b/apis_core/apis_relations/management/commands/create_relationships.py index d27e81dff..8f27f4138 100644 --- a/apis_core/apis_relations/management/commands/create_relationships.py +++ b/apis_core/apis_relations/management/commands/create_relationships.py @@ -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 diff --git a/apis_core/apis_relations/migrations/0006_alter_property_name_forward.py b/apis_core/apis_relations/migrations/0006_alter_property_name_forward.py new file mode 100644 index 000000000..2ec053d18 --- /dev/null +++ b/apis_core/apis_relations/migrations/0006_alter_property_name_forward.py @@ -0,0 +1,22 @@ +# Generated by Django 4.2.10 on 2024-02-27 18:00 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("apis_relations", "0005_alter_property_obj_class_alter_property_subj_class"), + ] + + 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", + ), + ), + ] diff --git a/apis_core/apis_relations/models.py b/apis_core/apis_relations/models.py index f252c58f1..16da60545 100644 --- a/apis_core/apis_relations/models.py +++ b/apis_core/apis_relations/models.py @@ -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", @@ -79,17 +77,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"} diff --git a/apis_core/apis_relations/tables.py b/apis_core/apis_relations/tables.py index 6f885b6de..f6352f560 100644 --- a/apis_core/apis_relations/tables.py +++ b/apis_core/apis_relations/tables.py @@ -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"}), ), ) diff --git a/apis_core/utils/caching.py b/apis_core/utils/caching.py index 28b63840e..4100c4081 100644 --- a/apis_core/utils/caching.py +++ b/apis_core/utils/caching.py @@ -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, @@ -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: diff --git a/apis_core/utils/filtermethods.py b/apis_core/utils/filtermethods.py index 3cde77099..670df9e80 100644 --- a/apis_core/utils/filtermethods.py +++ b/apis_core/utils/filtermethods.py @@ -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()