Skip to content

Commit

Permalink
fix(apis_entities): fix reversion for tempentity classes
Browse files Browse the repository at this point in the history
The TempEntityClass reversion should follow the rootobject_ptr. In
addition, the method to create a default Uri for a model has to check
for the raw argument, because reversion restores the model in a
temporary transaction.
  • Loading branch information
b1rger committed Sep 21, 2023
1 parent b7217b9 commit cfb1771
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions apis_core/apis_entities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get_entity_list_filter(cls):
return None


@reversion.register()
@reversion.register(follow=["rootobject_ptr"])
class TempEntityClass(AbstractEntity):
"""
Base class to bind common attributes to many classes.
Expand Down Expand Up @@ -131,7 +131,6 @@ def save(self, parse_dates=True, *args, **kwargs):
"""

if parse_dates:

# overwrite every field with None as default
start_date = None
start_start_date = None
Expand Down Expand Up @@ -406,20 +405,25 @@ def prepare_fields_dict(fields_list, vocabs, vocabs_m2m):


@receiver(post_save, dispatch_uid="create_default_uri")
def create_default_uri(sender, instance, **kwargs):
from apis_core.apis_metainfo.models import Uri
def create_default_uri(sender, instance, raw, **kwargs):
# with django reversion, browsing deleted entries in the admin interface
# leads to firing the `post_save` signal
# (https://github.com/etianen/django-reversion/issues/936) - a workaround
# is to check for the raw argument
if not raw:
from apis_core.apis_metainfo.models import Uri

if kwargs["created"] and sender in caching.get_all_ontology_classes():
if BASE_URI.endswith("/"):
base1 = BASE_URI[:-1]
else:
base1 = BASE_URI
uri_c = "{}{}".format(
base1,
reverse("GetEntityGenericRoot", kwargs={"pk": instance.pk}),
)
uri2 = Uri(uri=uri_c, domain="apis default", root_object=instance)
uri2.save()
if kwargs["created"] and sender in caching.get_all_ontology_classes():
if BASE_URI.endswith("/"):
base1 = BASE_URI[:-1]
else:
base1 = BASE_URI
uri_c = "{}{}".format(
base1,
reverse("GetEntityGenericRoot", kwargs={"pk": instance.pk}),
)
uri2 = Uri(uri=uri_c, domain="apis default", root_object=instance)
uri2.save()


if "registration" in getattr(settings, "INSTALLED_APPS", []):
Expand Down

0 comments on commit cfb1771

Please sign in to comment.