Fixed some really stupid bugs.

This commit is contained in:
Andrew Bastien 2020-04-12 15:43:29 -07:00
parent 467fd93f12
commit ff04849031
2 changed files with 18 additions and 10 deletions

View file

@ -48,31 +48,40 @@ class ContentsHandler(object):
self._typecache = defaultdict(set) self._typecache = defaultdict(set)
self.init() self.init()
def load(self):
"""
Retrieves all objects from database. Used for initializing.
Returns:
Objects (list of ObjectDB)
"""
return list(self.obj.locations_set.all())
def init(self): def init(self):
""" """
Re-initialize the content cache Re-initialize the content cache
""" """
objects = ObjectDB.objects.filter(db_location=self.obj) objects = self.load()
self._pkcache = {obj.pk for obj in objects} self._pkcache = {obj.pk for obj in objects}
for obj in objects: for obj in objects:
for ctype in obj._content_types: for ctype in obj._content_types:
self._typecache[ctype].add(obj.pk) self._typecache[ctype].add(obj.pk)
def get(self, exclude=None, category=None): def get(self, exclude=None, content_type=None):
""" """
Return the contents of the cache. Return the contents of the cache.
Args: Args:
exclude (Object or list of Object): object(s) to ignore exclude (Object or list of Object): object(s) to ignore
category (str or None): Filter list by a content-type. If None, don't filter. content_type (str or None): Filter list by a content-type. If None, don't filter.
Returns: Returns:
objects (list): the Objects inside this location objects (list): the Objects inside this location
""" """
if category is not None: if content_type is not None:
pks = self._typecache[category] pks = self._typecache[content_type]
else: else:
pks = self._pkcache pks = self._pkcache
if exclude: if exclude:
@ -88,7 +97,7 @@ class ContentsHandler(object):
except KeyError: except KeyError:
# this means an actual failure of caching. Return real database match. # this means an actual failure of caching. Return real database match.
logger.log_err("contents cache failed for %s." % self.obj.key) logger.log_err("contents cache failed for %s." % self.obj.key)
return list(ObjectDB.objects.filter(db_location=self.obj)) return self.load()
def add(self, obj): def add(self, obj):
""" """
@ -112,7 +121,8 @@ class ContentsHandler(object):
""" """
self._pkcache.remove(obj.pk) self._pkcache.remove(obj.pk)
for ctype in obj._content_types: for ctype in obj._content_types:
self._typecache[ctype].discard(obj.pk) if obj.pk in self._typecache[ctype]:
self._typecache[ctype].remove(obj.pk)
def clear(self): def clear(self):
""" """

View file

@ -279,9 +279,7 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
and filtering. and filtering.
""" """
con = self.contents_cache.get(exclude=exclude) return self.contents_cache.get(exclude=exclude, content_type=content_type)
# print "contents_get:", self, con, id(self), calledby() # DEBUG
return con
def contents_set(self, *args): def contents_set(self, *args):
"You cannot replace this property" "You cannot replace this property"