From cfb17712fc03d36ff2f77d402797d0957da046d3 Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Tue, 23 May 2023 12:30:35 +0200 Subject: [PATCH] fix(apis_entities): fix reversion for tempentity classes 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. --- apis_core/apis_entities/models.py | 34 +++++++++++++++++-------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/apis_core/apis_entities/models.py b/apis_core/apis_entities/models.py index 4b3497bc3..6ba282177 100644 --- a/apis_core/apis_entities/models.py +++ b/apis_core/apis_entities/models.py @@ -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. @@ -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 @@ -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", []):