fix test with late init

This commit is contained in:
Your Name 2022-12-28 10:24:07 -05:00
parent 552b88cbee
commit 0454e8830c
3 changed files with 24 additions and 14 deletions

View file

@ -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

View file

@ -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))

View file

@ -7,7 +7,7 @@ app_name = 'journal'
def _get_all_categories():
res = "|".join(CATEGORY_LIST.keys())
res = "|".join(all_categories().keys())
return res