Skip to content

Commit

Permalink
refactor(apis_vocabularies)!: drop apis_vocabularies
Browse files Browse the repository at this point in the history
the apis_vocabularies app was removed- it is not being used right now
and it will be reindtroduced in #441.

BREAKING CHANGE: projects will have to remove
`apis_core.apis_vocabularies` from the list of INSTALLED_APPS

Closes: #514
  • Loading branch information
b1rger committed Dec 20, 2023
1 parent 59ed073 commit ba2eba2
Show file tree
Hide file tree
Showing 30 changed files with 23 additions and 1,038 deletions.
36 changes: 2 additions & 34 deletions apis_core/api_routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,32 +82,6 @@ def add_uri(self, obj):
)
)

@extend_schema_field(OpenApiTypes.OBJECT)
def add_type(self, obj):
lst_type = ["kind", "type", "collection_type", "relation_type"]
lst_kind = [
x
for x in obj._meta.fields
if x.name in lst_type and "apis_vocabularies" in str(x.related_model)
]
if len(lst_kind):
pk_obj = getattr(obj, f"{lst_kind[0].name}_id")
if pk_obj is not None:
obj_type = getattr(obj, str(lst_kind[0].name))
res = {
"id": pk_obj,
"url": self.context["view"].request.build_absolute_uri(
reverse(
f"apis:apis_api:{lst_kind[0].related_model.__name__.lower()}-detail",
kwargs={"pk": pk_obj},
)
),
"type": obj_type.__class__.__name__,
"label": str(obj_type),
"parent_class": getattr(obj, "parent_class_id", None),
}
return res

class Meta:
model = TempEntityClass
fields = ["id", "label", "url"]
Expand Down Expand Up @@ -138,10 +112,6 @@ class Meta(ApisBaseSerializer.Meta):
]


class VocabsBaseSerializer(LabelSerializer, EntitySerializer):
pass


class RelatedTripleSerializer(ApisBaseSerializer):
relation_type = serializers.SerializerMethodField(
method_name="add_related_property"
Expand Down Expand Up @@ -196,9 +166,7 @@ def generic_serializer_creation_factory():
lst_cont = caching.get_all_contenttype_classes()
not_allowed_filter_fields = [
"useradded",
"vocab_name",
"parent_class",
"vocab",
"entity",
"autofield",
]
Expand Down Expand Up @@ -272,7 +240,7 @@ def __init__(self, *args, **kwargs):
"ManyToManyField",
"ForeignKey",
"InheritanceForeignKey",
] and "apis_vocabularies" not in str(f.related_model):
]:
self.fields[f.name] = ApisBaseSerializer(
many=ck_many, read_only=True
)
Expand Down Expand Up @@ -308,7 +276,7 @@ def __init__(self, *args, **kwargs):
elif f.__class__.__name__ in [
"ManyToManyField",
"ForeignKey",
] and "apis_vocabularies" not in str(f.related_model):
]:
self.fields[f.name] = ApisBaseSerializer(
many=ck_many, read_only=True
)
Expand Down
45 changes: 0 additions & 45 deletions apis_core/apis_entities/autocomplete3.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from django.db.models import Q

from apis_core.apis_metainfo.models import Uri, Collection
from apis_core.apis_vocabularies.models import VocabsBaseClass
from apis_core.utils import caching
from apis_core.utils.caching import get_autocomplete_property_choices
from apis_core.utils.settings import get_entity_settings_by_modelname
Expand Down Expand Up @@ -377,50 +376,6 @@ def get(self, request, *args, **kwargs):
)


# TODO RDF: Check if this should be removed or adapted
class GenericVocabulariesAutocomplete(autocomplete.Select2ListView):
def get(self, request, *args, **kwargs):
page_size = 20
offset = (int(self.request.GET.get("page", 1)) - 1) * page_size
more = False
vocab = self.kwargs["vocab"]
direct = self.kwargs["direct"]
q = self.q
vocab_model = ContentType.objects.get(
app_label="apis_vocabularies", model=vocab
).model_class()
if direct == "normal":
if vocab_model.__bases__[0] == VocabsBaseClass:
choices = [
{"id": x.pk, "text": x.label}
for x in vocab_model.objects.filter(name__icontains=q).order_by(
"parent_class__name", "name"
)[offset : offset + page_size]
]
else:
choices = [
{"id": x.pk, "text": x.label}
for x in vocab_model.objects.filter(
Q(name__icontains=q) | Q(name_reverse__icontains=q)
).order_by("parent_class__name", "name")[
offset : offset + page_size
]
]
elif direct == "reverse":
choices = [
{"id": x.pk, "text": x.label_reverse}
for x in vocab_model.objects.filter(
Q(name__icontains=q) | Q(name_reverse__icontains=q)
).order_by("parent_class__name", "name")[offset : offset + page_size]
]
if len(choices) == page_size:
more = True
return http.HttpResponse(
json.dumps({"results": choices + [], "pagination": {"more": more}}),
content_type="application/json",
)


# TODO RDF: Check if this should be removed or adapted
class GenericNetworkEntitiesAutocomplete(autocomplete.Select2ListView):
def get(self, request, *args, **kwargs):
Expand Down
34 changes: 0 additions & 34 deletions apis_core/apis_entities/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,43 +95,9 @@ def __init__(self, *args, **kwargs):
"apis_entities",
"apis_metainfo",
"apis_relations",
"apis_vocabularies",
],
model=model_uri,
)
if (
len(matching_content_type) == 1
and matching_content_type[0].app_label.lower()
== "apis_vocabularies"
):
self.fields[f].widget = widget1(
url=reverse(
"apis:apis_vocabularies:generic_vocabularies_autocomplete",
kwargs={"vocab": model_uri, "direct": "normal"},
),
attrs=attrs,
)
if self.instance:
res = []
if isinstance(self.fields[f], ModelMultipleChoiceField):
try:
for x in getattr(self.instance, f).all():
res.append((x.pk, x.label))
except ValueError:
pass
self.fields[f].initial = res
self.fields[f].choices = res
else:
try:
res = getattr(self.instance, f)
if res is not None:
self.fields[f].initial = (res.pk, res.label)
self.fields[f].choices = [
(res.pk, res.label),
]
except ValueError:
res = ""

main_fields.append(f)

def sort_fields_list(field_names, entity_name):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

from apis_core.apis_entities.api_renderers import EntityToCIDOC
from apis_core.apis_entities.serializers_generic import EntitySerializer
from apis_core.apis_vocabularies.api_renderers import VocabToSkos
from apis_core.apis_vocabularies.serializers import GenericVocabsSerializer
from apis_core.utils import caching

map_ct = {
Expand Down
6 changes: 0 additions & 6 deletions apis_core/apis_entities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,6 @@ def prepare_fields_dict(fields_list, vocabs, vocabs_m2m):
res = dict()
for f in fields_list:
res[f["name"]] = getattr(models, f["field_type"])(**f["attributes"])
for v in vocabs:
res[v] = models.ForeignKey(
f"apis_vocabularies.{v}", blank=True, null=True, on_delete=models.SET_NULL
)
for v2 in vocabs_m2m:
res[v2] = models.ManyToManyField(f"apis_vocabularies.{v2}", blank=True)
return res


Expand Down
8 changes: 0 additions & 8 deletions apis_core/apis_entities/serializers_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,11 @@ def __init__(
"FloatField",
]:
self.fields[f.name] = getattr(serializers, field_name)()
elif field_name in ["ForeignKey", "ManyToMany"]:
if str(f.related_model.__module__).endswith("apis_vocabularies.models"):
many = False
if f.many_to_many or f.one_to_many:
many = True
self.fields[f.name] = VocabsSerializer(many=many)
for f in inst._meta.many_to_many:
if f.name.endswith("relationtype_set"):
continue
elif f.name == "collection":
self.fields["collection"] = CollectionSerializer(many=True)
elif str(f.related_model.__module__).endswith("apis_vocabularies.models"):
self.fields[f.name] = VocabsSerializer(many=True)
self.fields["entity_type"] = serializers.SerializerMethodField(
method_name="add_entity_type"
)
Expand Down
25 changes: 1 addition & 24 deletions apis_core/apis_metainfo/migrations/0002_auto_20220201_1241.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,11 @@ class Migration(migrations.Migration):

dependencies = [
("apis_metainfo", "0001_initial"),
("apis_vocabularies", "0001_initial"),
("auth", "0012_alter_user_first_name_max_length"),
("contenttypes", "0002_remove_content_type_name"),
]

operations = [
migrations.AddField(
model_name="text",
name="kind",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="apis_vocabularies.texttype",
),
),
migrations.AddField(
model_name="text",
name="source",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="apis_metainfo.source",
),
),
migrations.AddField(
model_name="rootobject",
name="self_content_type",
Expand All @@ -49,11 +28,9 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name="collection",
name="collection_type",
field=models.ForeignKey(
field=models.IntegerField(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="apis_vocabularies.collectiontype",
),
),
migrations.AddField(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.8 on 2023-12-20 15:46

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("apis_metainfo", "0008_alter_rootobject_self_contenttype"),
]

operations = [
migrations.RemoveField(
model_name="collection",
name="collection_type",
),
]
6 changes: 0 additions & 6 deletions apis_core/apis_metainfo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from apis_core.apis_metainfo import signals

# from apis_core.apis_entities.serializers_generic import EntitySerializer
# from apis_core.apis_vocabularies.models import CollectionType, LabelType, TextType

path_ac_settings = getattr(settings, "APIS_AUTOCOMPLETE_SETTINGS", False)
if path_ac_settings:
Expand Down Expand Up @@ -112,13 +111,8 @@ def duplicate(self):
class Collection(models.Model):
"""Allows to group entities and relation."""

from apis_core.apis_vocabularies.models import CollectionType

name = models.CharField(max_length=255)
description = models.TextField(blank=True)
collection_type = models.ForeignKey(
CollectionType, blank=True, null=True, on_delete=models.SET_NULL
)
groups_allowed = models.ManyToManyField(Group)
parent_class = models.ForeignKey(
"self", blank=True, null=True, on_delete=models.CASCADE
Expand Down
19 changes: 0 additions & 19 deletions apis_core/apis_relations/rel_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,25 +197,6 @@ def __init__(self, *args, **kwargs):
),
),
)
elif (
ContentType.objects.filter(
app_label="apis_vocabularies", model=current_model_name
).count()
> 0
):
self.filters[x] = django_filters.ModelMultipleChoiceFilter(
field_name=x,
queryset=current_qs,
widget=autocomplete.ModelSelect2Multiple(
url=reverse(
"apis:apis_vocabularies:generic_vocabularies_autocomplete",
kwargs={
"vocab": current_model_name,
"direct": "normal",
},
),
),
)
if type(self.filters[x].field).__name__ == "DateField":
self.filters[x] = django_filters.DateFromToRangeFilter(
field_name=x,
Expand Down
Empty file.
Loading

0 comments on commit ba2eba2

Please sign in to comment.