From 3b110d89d7b9177069da40b6bc372496773eeb07 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 19 Jun 2023 12:57:45 -0400 Subject: [PATCH] delay some init code to workaround test --- catalog/common/models.py | 35 ++++++++++++++++++++++++++++++++--- catalog/models.py | 38 ++++++++------------------------------ journal/models.py | 4 ++-- journal/urls.py | 2 +- 4 files changed, 43 insertions(+), 36 deletions(-) diff --git a/catalog/common/models.py b/catalog/common/models.py index ef51c815..6468953f 100644 --- a/catalog/common/models.py +++ b/catalog/common/models.py @@ -280,13 +280,11 @@ class Item(SoftDeleteMixin, PolymorphicModel): ] ] - _content_type_ids = [] - @cached_property def history(self): # can't use AuditlogHistoryField bc it will only return history with current content type return LogEntry.objects.filter( - object_id=self.id, content_type_id__in=self._content_type_ids + object_id=self.id, content_type_id__in=list(item_content_types().values()) ) def clear(self): @@ -588,3 +586,34 @@ class ExternalResource(models.Model): else: raise ValueError(f"preferred model {model} does not exist") return None + + +_CONTENT_TYPE_LIST = None + + +def item_content_types(): + global _CONTENT_TYPE_LIST + if _CONTENT_TYPE_LIST is None: + _CONTENT_TYPE_LIST = {} + for cls in Item.__subclasses__(): + _CONTENT_TYPE_LIST[cls] = ContentType.objects.get( + app_label="catalog", model=cls.__name__.lower() + ).id + return _CONTENT_TYPE_LIST + + +_CATEGORY_LIST = None + + +def item_categories(): + global _CATEGORY_LIST + if _CATEGORY_LIST is None: + _CATEGORY_LIST = {} + for cls in Item.__subclasses__(): + c = getattr(cls, "category", None) + if c not in _CATEGORY_LIST: + _CATEGORY_LIST[c] = [cls] + else: + _CATEGORY_LIST[c].append(cls) + print(_CATEGORY_LIST) + return _CATEGORY_LIST diff --git a/catalog/models.py b/catalog/models.py index a16feae7..4c074e51 100644 --- a/catalog/models.py +++ b/catalog/models.py @@ -1,4 +1,10 @@ -from .common.models import ExternalResource, Item, ItemSchema +from .common.models import ( + ExternalResource, + Item, + ItemSchema, + item_content_types, + item_categories, +) from .book.models import Edition, Work, Series, EditionSchema, EditionInSchema from .movie.models import Movie, MovieSchema, MovieInSchema from .tv.models import ( @@ -41,34 +47,6 @@ _logger = logging.getLogger(__name__) # proxy = True -_CATEGORY_LIST = None -_CONTENT_TYPE_LIST = None - - -def all_content_types(): - global _CONTENT_TYPE_LIST - if _CONTENT_TYPE_LIST is None: - _CONTENT_TYPE_LIST = {} - for cls in Item.__subclasses__(): - _CONTENT_TYPE_LIST[cls] = ContentType.objects.get( - app_label="catalog", model=cls.__name__.lower() - ).id - return _CONTENT_TYPE_LIST - - -def all_categories(): - global _CATEGORY_LIST - if _CATEGORY_LIST is None: - _CATEGORY_LIST = {} - for cls in Item.__subclasses__(): - c = getattr(cls, "category", None) - if c not in _CATEGORY_LIST: - _CATEGORY_LIST[c] = [cls] - else: - _CATEGORY_LIST[c].append(cls) - return _CATEGORY_LIST - - def init_catalog_search_models(): if settings.DISABLE_MODEL_SIGNAL: _logger.warn( @@ -100,4 +78,4 @@ def init_catalog_audit_log(): ExternalResource, include_fields=["item", "id_type", "id_value", "url"] ) - Item._content_type_ids = list(all_content_types().values()) + # _logger.debug(f"Catalog audit log initialized for {item_content_types().values()}") diff --git a/journal/models.py b/journal/models.py index 7c3e10f8..e2478a21 100644 --- a/journal/models.py +++ b/journal/models.py @@ -70,12 +70,12 @@ def query_following(user): def query_item_category(item_category): - classes = all_categories()[item_category] + classes = item_categories()[item_category] # q = Q(item__instance_of=classes[0]) # for cls in classes[1:]: # q = q | Q(instance_of=cls) # return q - ct = all_content_types() + ct = item_content_types() contenttype_ids = [ct[cls] for cls in classes] return Q(item__polymorphic_ctype__in=contenttype_ids) diff --git a/journal/urls.py b/journal/urls.py index a2d6a7cd..de66d1d7 100644 --- a/journal/urls.py +++ b/journal/urls.py @@ -8,7 +8,7 @@ app_name = "journal" def _get_all_categories(): - res = "|".join(all_categories().keys()) + res = "|".join(item_categories().keys()) return res