Have tags/attrs.all() switch to direct query on AGGRESSIVE_TYPECLASS_CACHE=False. Resolves #2007

This commit is contained in:
Griatch 2020-01-20 19:14:50 +01:00
parent d806909f33
commit c428eb72a5
2 changed files with 32 additions and 16 deletions

View file

@ -235,19 +235,23 @@ class AttributeHandler(object):
# full cache was run on all attributes
self._cache_complete = False
def _fullcache(self):
"""Cache all attributes of this object"""
if not _TYPECLASS_AGGRESSIVE_CACHE:
return
def _query_all(self):
"Fetch all Attributes on this object"
query = {
"%s__id" % self._model: self._objid,
"attribute__db_model__iexact": self._model,
"attribute__db_attrtype": self._attrtype,
}
attrs = [
return [
conn.attribute
for conn in getattr(self.obj, self._m2m_fieldname).through.objects.filter(**query)
]
def _fullcache(self):
"""Cache all attributes of this object"""
if not _TYPECLASS_AGGRESSIVE_CACHE:
return
attrs = self._query_all()
self._cache = dict(
(
"%s-%s"
@ -776,9 +780,13 @@ class AttributeHandler(object):
their values!) in the handler.
"""
if not self._cache_complete:
self._fullcache()
attrs = sorted([attr for attr in self._cache.values() if attr], key=lambda o: o.id)
if _TYPECLASS_AGGRESSIVE_CACHE:
if not self._cache_complete:
self._fullcache()
attrs = sorted([attr for attr in self._cache.values() if attr], key=lambda o: o.id)
else:
attrs = sorted([attr for attr in self._query_all() if attr], key=lambda o: o.id)
if accessing_obj:
return [
attr

View file

@ -124,19 +124,23 @@ class TagHandler(object):
# full cache was run on all tags
self._cache_complete = False
def _fullcache(self):
"Cache all tags of this object"
if not _TYPECLASS_AGGRESSIVE_CACHE:
return
def _query_all(self):
"Get all tags for this objects"
query = {
"%s__id" % self._model: self._objid,
"tag__db_model": self._model,
"tag__db_tagtype": self._tagtype,
}
tags = [
return [
conn.tag
for conn in getattr(self.obj, self._m2m_fieldname).through.objects.filter(**query)
]
def _fullcache(self):
"Cache all tags of this object"
if not _TYPECLASS_AGGRESSIVE_CACHE:
return
tags = self._query_all()
self._cache = dict(
(
"%s-%s"
@ -425,9 +429,13 @@ class TagHandler(object):
`return_key_and_category` is set.
"""
if not self._cache_complete:
self._fullcache()
tags = sorted(self._cache.values())
if _TYPECLASS_AGGRESSIVE_CACHE:
if not self._cache_complete:
self._fullcache()
tags = sorted(self._cache.values())
else:
tags = sorted(self._query_all())
if return_key_and_category:
# return tuple (key, category)
return [(to_str(tag.db_key), tag.db_category) for tag in tags]