delay some init code to workaround test
This commit is contained in:
parent
20bb990049
commit
3b110d89d7
4 changed files with 43 additions and 36 deletions
|
@ -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
|
||||
|
|
|
@ -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()}")
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ app_name = "journal"
|
|||
|
||||
|
||||
def _get_all_categories():
|
||||
res = "|".join(all_categories().keys())
|
||||
res = "|".join(item_categories().keys())
|
||||
return res
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue