From 0454e8830c8dda9d5151bd9ffcf0c7970da6f9ab Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 28 Dec 2022 10:24:07 -0500 Subject: [PATCH] fix test with late init --- catalog/models.py | 31 ++++++++++++++++++++----------- journal/models.py | 5 +++-- journal/urls.py | 2 +- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/catalog/models.py b/catalog/models.py index 9adb3054..306f57c1 100644 --- a/catalog/models.py +++ b/catalog/models.py @@ -28,18 +28,27 @@ from django.contrib.contenttypes.models import ContentType # proxy = True -CATEGORY_LIST = {} -CONTENT_TYPE_LIST = {} +_CATEGORY_LIST = None +_CONTENT_TYPE_LIST = None -def _init_item_subclasses(): - 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) - CONTENT_TYPE_LIST[cls] = ContentType.objects.get(app_label='catalog', model=cls.__name__.lower()).id +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 -_init_item_subclasses() +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 diff --git a/journal/models.py b/journal/models.py index 1206f450..554a0db0 100644 --- a/journal/models.py +++ b/journal/models.py @@ -52,12 +52,13 @@ def query_following(user): def query_item_category(item_category): - classes = CATEGORY_LIST[item_category] + classes = all_categories()[item_category] # q = Q(item__instance_of=classes[0]) # for cls in classes[1:]: # q = q | Q(instance_of=cls) # return q - contenttype_ids = [CONTENT_TYPE_LIST[cls] for cls in classes] + ct = all_content_types() + contenttype_ids = [ct[cls] for cls in classes] return Q(item__polymorphic_ctype__in=sorted(contenttype_ids)) diff --git a/journal/urls.py b/journal/urls.py index 10f01640..ff6cf450 100644 --- a/journal/urls.py +++ b/journal/urls.py @@ -7,7 +7,7 @@ app_name = 'journal' def _get_all_categories(): - res = "|".join(CATEGORY_LIST.keys()) + res = "|".join(all_categories().keys()) return res