Skip to content

Commit

Permalink
WIP: drop TempEntityClass
Browse files Browse the repository at this point in the history
Closes: #6
  • Loading branch information
b1rger committed Dec 20, 2023
1 parent 59ed073 commit a16dab5
Show file tree
Hide file tree
Showing 12 changed files with 5 additions and 294 deletions.
2 changes: 0 additions & 2 deletions apis_core/api_routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
)
from drf_spectacular.types import OpenApiTypes
from django_filters import rest_framework as filters
from apis_core.apis_entities.models import TempEntityClass
from .api_renderers import NetJsonRenderer
from .apis_relations.models import Triple, Property
from apis_core.utils import caching
Expand Down Expand Up @@ -109,7 +108,6 @@ def add_type(self, obj):
return res

class Meta:
model = TempEntityClass
fields = ["id", "label", "url"]


Expand Down
14 changes: 1 addition & 13 deletions apis_core/apis_entities/detail_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,8 @@ def get(self, request, *args, **kwargs):
iiif_info_json = self.instance.name

relevant_fields = []
# those are fields from TempEntityClass, this exclude list can be removed once TempEntityClass is dropped
exclude_fields = [
"start_date",
"start_start_date",
"start_end_date",
"end_date",
"end_start_date",
"end_end_date",
"tempentityclass_ptr",
"rootobject_ptr",
"collection",
]
entity_settings = get_entity_settings_by_modelname(self.entity)
exclude_fields.extend(entity_settings.get("detail_view_exclude", []))
exclude_fields = entity_settings.get("detail_view_exclude", [])
for field, value in model_to_dict(self.instance).items():
if field not in exclude_fields:
relevant_fields.append((self.instance._meta.get_field(field), value))
Expand Down
2 changes: 0 additions & 2 deletions apis_core/apis_entities/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.db.models import Q, JSONField

from apis_core.apis_metainfo.models import Collection
from apis_core.apis_entities.models import TempEntityClass
from apis_core.utils import caching
from apis_core.utils.settings import get_entity_settings_by_modelname
from apis_core.utils.filtermethods import (
Expand Down Expand Up @@ -58,7 +57,6 @@ class GenericEntityListFilter(django_filters.FilterSet):
end_date = django_filters.DateFromToRangeFilter()

class Meta:
model = TempEntityClass
# exclude all hardcoded fields or nothing, however this exclude is only defined here as a temporary measure in
# order to load all filters of all model fields by default so that they are available in the first place.
# Later those which are not referenced in the settings file will be removed again
Expand Down
64 changes: 0 additions & 64 deletions apis_core/apis_entities/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,70 +158,6 @@ def sort_fields_list(field_names, entity_name):
crispy_main_fields.append(Field(field))

self.helper.layout = Layout(crispy_main_fields)
# backwards compatibility:
# those fields are part of TempEntityClass - this
# block can probably be removed when TempEntityClass
# is gone from apis_entities
# for now we at least check if they exist
if "status" in self.fields:
self.fields["status"].required = False
if "collection" in self.fields:
self.fields["collection"].required = False
if "start_date_written" in self.fields:
self.fields["start_date_written"].required = False
if "end_date_written" in self.fields:
self.fields["end_date_written"].required = False

instance = getattr(self, "instance", None)
if instance != None:

# same as above, part of TempEntityClass
if "start_date_written" in self.fields:
if instance.start_date_written:
self.fields[
"start_date_written"
].help_text = DateParser.get_date_help_text_from_dates(
single_date=instance.start_date,
single_start_date=instance.start_start_date,
single_end_date=instance.start_end_date,
single_date_written=instance.start_date_written,
)
else:
self.fields[
"start_date_written"
].help_text = DateParser.get_date_help_text_default()

# same as above, part of TempEntityClass
if "end_date_written" in self.fields:
if instance.end_date_written:
self.fields[
"end_date_written"
].help_text = DateParser.get_date_help_text_from_dates(
single_date=instance.end_date,
single_start_date=instance.end_start_date,
single_end_date=instance.end_end_date,
single_date_written=instance.end_date_written,
)
else:
self.fields[
"end_date_written"
].help_text = DateParser.get_date_help_text_default()

def save(self, *args, **kwargs):
obj = super(GenericEntitiesForm, self).save(*args, **kwargs)

# backwards compatibility
# collection is a field of TempEntityClass - this block
# can be removed when TempEntityClass is gone from apis_entities
# for now we at least check if the attribute exist
if hasattr(obj, "collection"):
if obj.collection.all().count() == 0:
col_name = getattr(
settings, "APIS_DEFAULT_COLLECTION", "manually created entity"
)
col, created = Collection.objects.get_or_create(name=col_name)
obj.collection.add(col)
return obj

return GenericEntitiesForm

Expand Down
66 changes: 2 additions & 64 deletions apis_core/apis_entities/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Generated by Django 3.1.14 on 2022-02-01 12:41

from django.db import migrations, models
import django.db.models.deletion
from django.db import migrations


class Migration(migrations.Migration):
Expand All @@ -12,65 +11,4 @@ class Migration(migrations.Migration):
("apis_metainfo", "0001_initial"),
]

operations = [
migrations.CreateModel(
name="TempEntityClass",
fields=[
(
"rootobject_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="apis_metainfo.rootobject",
),
),
(
"review",
models.BooleanField(
default=False,
help_text="Should be set to True, if the data record holds up quality standards.",
),
),
("start_date", models.DateField(blank=True, null=True)),
("start_start_date", models.DateField(blank=True, null=True)),
("start_end_date", models.DateField(blank=True, null=True)),
("end_date", models.DateField(blank=True, null=True)),
("end_start_date", models.DateField(blank=True, null=True)),
("end_end_date", models.DateField(blank=True, null=True)),
(
"start_date_written",
models.CharField(
blank=True, max_length=255, null=True, verbose_name="Start"
),
),
(
"end_date_written",
models.CharField(
blank=True, max_length=255, null=True, verbose_name="End"
),
),
("status", models.CharField(max_length=100)),
("references", models.TextField(blank=True, null=True)),
("notes", models.TextField(blank=True, null=True)),
("published", models.BooleanField(default=False)),
("collection", models.ManyToManyField(to="apis_metainfo.Collection")),
(
"source",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="apis_metainfo.source",
),
),
("text", models.ManyToManyField(blank=True, to="apis_metainfo.Text")),
],
options={
"abstract": False,
},
bases=("apis_metainfo.rootobject",),
),
]
operations = []
97 changes: 0 additions & 97 deletions apis_core/apis_entities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,103 +235,6 @@ def get_serialization(self):
return EntitySerializer(self).data


@reversion.register(follow=["rootobject_ptr"])
class TempEntityClass(AbstractEntity):
"""
Base class to bind common attributes to many classes.
The common attributes are:
- written start and enddates,
- recognized start and enddates which are derived from the written dates
using RegEx,
- a review boolean field to mark an object as reviewed.
"""

review = models.BooleanField(
default=False,
help_text="Should be set to True, if the "
"data record holds up quality "
"standards.",
)
start_date = models.DateField(blank=True, null=True)
start_start_date = models.DateField(blank=True, null=True)
start_end_date = models.DateField(blank=True, null=True)
end_date = models.DateField(blank=True, null=True)
end_start_date = models.DateField(blank=True, null=True)
end_end_date = models.DateField(blank=True, null=True)
start_date_written = models.CharField(
max_length=255,
blank=True,
null=True,
verbose_name="Start",
)
end_date_written = models.CharField(
max_length=255,
blank=True,
null=True,
verbose_name="End",
)
# TODO RDF: Make Text also a Subclass of RootObject
collection = models.ManyToManyField("apis_metainfo.Collection")
status = models.CharField(max_length=100)
references = models.TextField(blank=True, null=True)
notes = models.TextField(blank=True, null=True)
published = models.BooleanField(default=False)
objects = models.Manager()
objects_inheritance = InheritanceManager()

def __str__(self):
if self.name != "" and hasattr(
self, "first_name" # TODO RDF: remove first_name here
): # relation usually don´t have names
return "{}, {} (ID: {})".format(self.name, self.first_name, self.id)
elif self.name != "":
return "{} (ID: {})".format(self.name, self.id)
else:
return "(ID: {})".format(self.id)

def save(self, parse_dates=True, *args, **kwargs):
"""
Adaption of the save() method of the class to automatically parse
string-dates into date objects.
"""

if parse_dates:
# overwrite every field with None as default
start_date = None
start_start_date = None
start_end_date = None
end_date = None
end_start_date = None
end_end_date = None

# if some textual user input of a start date is there, parse it
if self.start_date_written:
start_date, start_start_date, start_end_date = DateParser.parse_date(
self.start_date_written
)

# if some textual user input of an end date is there, parse it
if self.end_date_written:
end_date, end_start_date, end_end_date = DateParser.parse_date(
self.end_date_written
)

self.start_date = start_date
self.start_start_date = start_start_date
self.start_end_date = start_end_date
self.end_date = end_date
self.end_start_date = end_start_date
self.end_end_date = end_end_date

if self.name:
self.name = unicodedata.normalize("NFC", self.name)

super(TempEntityClass, self).save(*args, **kwargs)

return self


def prepare_fields_dict(fields_list, vocabs, vocabs_m2m):
res = dict()
for f in fields_list:
Expand Down
13 changes: 0 additions & 13 deletions apis_core/apis_metainfo/dal_urls.py

This file was deleted.

13 changes: 0 additions & 13 deletions apis_core/apis_metainfo/dal_views.py

This file was deleted.

20 changes: 0 additions & 20 deletions apis_core/apis_metainfo/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from rest_framework import serializers

from .models import Collection, Text, Uri
from apis_core.apis_entities.models import TempEntityClass


class CollectionSerializer(serializers.HyperlinkedModelSerializer):
Expand Down Expand Up @@ -56,25 +55,6 @@ class Meta:
model = Uri


class TempEntityClassSerializer(serializers.HyperlinkedModelSerializer):
url = serializers.HyperlinkedIdentityField(
view_name="apis:apis_api:tempentityclass-detail", lookup_field="pk"
)
collection = serializers.HyperlinkedRelatedField(
view_name="apis:apis_api:collection-detail", lookup_field="pk", read_only=True
)
text = serializers.HyperlinkedRelatedField(
view_name="apis:apis_api:text-detail", lookup_field="pk", read_only=True
)
source = serializers.HyperlinkedRelatedField(
view_name="apis:apis_api:source-detail", lookup_field="pk", read_only=True
)

class Meta:
fields = "__all__"
model = TempEntityClass


class ContentTypeSerializer(serializers.HyperlinkedModelSerializer):
url = serializers.HyperlinkedIdentityField(
view_name="apis:apis_api:contenttype-detail", lookup_field="pk"
Expand Down
4 changes: 1 addition & 3 deletions apis_core/utils/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def _init_all_ontology_classes():

# the imports are done here as this module here might be called before full Django
# initialization. Otherwise, it would break.
from apis_core.apis_entities.models import AbstractEntity, TempEntityClass
from apis_core.apis_entities.models import AbstractEntity

# from apis_core.apis_relations.models import AbstractReification
# from apis_core.apis_vocabularies.models import AbstractVocabulary
Expand Down Expand Up @@ -75,8 +75,6 @@ def _init_all_ontology_classes():
if (
issubclass(ontology_class, AbstractEntity)
and not ontology_class._meta.abstract
and ontology_class
is not TempEntityClass # TODO RDF: remove this once TempEntityClass is removed
):
_entity_classes.append(ontology_class)
_entity_class_names.append(ontology_class_name.lower())
Expand Down
Loading

0 comments on commit a16dab5

Please sign in to comment.