Move reset of cached_tag_flat_dict out of GroupableMixin into Tag.

That should not have been there!
Needed to add the BAIL sentinel so that Tag methods would only
reset the cache if the internal method actually did made any changes.
This commit is contained in:
voussoir 2020-09-12 01:19:03 -07:00
parent 74445d0a1f
commit d41bad63e7

View file

@ -10,6 +10,7 @@ import traceback
from voussoirkit import bytestring from voussoirkit import bytestring
from voussoirkit import pathclass from voussoirkit import pathclass
from voussoirkit import sentinel
from voussoirkit import spinal from voussoirkit import spinal
from voussoirkit import sqlhelpers from voussoirkit import sqlhelpers
@ -19,6 +20,8 @@ from . import exceptions
from . import helpers from . import helpers
BAIL = sentinel.Sentinel('BAIL')
def normalize_db_row(db_row, table): def normalize_db_row(db_row, table):
if isinstance(db_row, (list, tuple)): if isinstance(db_row, (list, tuple)):
db_row = dict(zip(constants.SQL_COLUMNS[table], db_row)) db_row = dict(zip(constants.SQL_COLUMNS[table], db_row))
@ -98,7 +101,7 @@ class GroupableMixin:
raise exceptions.CantGroupSelf(self) raise exceptions.CantGroupSelf(self)
if self.has_child(member): if self.has_child(member):
return return BAIL
self.photodb.log.debug('Adding child %s to %s.', member, self) self.photodb.log.debug('Adding child %s to %s.', member, self)
@ -112,14 +115,12 @@ class GroupableMixin:
} }
self.photodb.sql_insert(table=self.group_table, data=data) self.photodb.sql_insert(table=self.group_table, data=data)
self.photodb._cached_tag_flat_dict = None
def add_child(self, member): def add_child(self, member):
return self._add_child(member) return self._add_child(member)
def add_children(self, members): def add_children(self, members):
for member in members: if all(self._add_child(member) is BAIL for member in members):
self._add_child(member) return BAIL
def assert_same_type(self, other): def assert_same_type(self, other):
if not isinstance(other, type(self)): if not isinstance(other, type(self)):
@ -140,7 +141,6 @@ class GroupableMixin:
If True, all children will be deleted. If True, all children will be deleted.
Otherwise they'll just be raised up one level. Otherwise they'll just be raised up one level.
''' '''
self.photodb._cached_tag_flat_dict = None
if delete_children: if delete_children:
for child in self.get_children(): for child in self.get_children():
child.delete(delete_children=True) child.delete(delete_children=True)
@ -191,7 +191,7 @@ class GroupableMixin:
def remove_child(self, member): def remove_child(self, member):
if not self.has_child(member): if not self.has_child(member):
return return BAIL
self.photodb.log.debug('Removing child %s from %s.', member, self) self.photodb.log.debug('Removing child %s from %s.', member, self)
@ -200,7 +200,6 @@ class GroupableMixin:
'memberid': member.id, 'memberid': member.id,
} }
self.photodb.sql_delete(table=self.group_table, pairs=pairs) self.photodb.sql_delete(table=self.group_table, pairs=pairs)
self.photodb._cached_tag_flat_dict = None
def walk_children(self): def walk_children(self):
''' '''
@ -1203,12 +1202,20 @@ class Tag(ObjectBase, GroupableMixin):
@decorators.required_feature('tag.edit') @decorators.required_feature('tag.edit')
@decorators.transaction @decorators.transaction
def add_child(self, *args, **kwargs): def add_child(self, *args, **kwargs):
return super().add_child(*args, **kwargs) ret = super().add_child(*args, **kwargs)
if ret is BAIL:
return
self.photodb._cached_tag_flat_dict = None
return ret
@decorators.required_feature('tag.edit') @decorators.required_feature('tag.edit')
@decorators.transaction @decorators.transaction
def add_children(self, *args, **kwargs): def add_children(self, *args, **kwargs):
return super().add_children(*args, **kwargs) ret = super().add_children(*args, **kwargs)
if ret is BAIL:
return
self.photodb._cached_tag_flat_dict = None
return ret
@decorators.required_feature('tag.edit') @decorators.required_feature('tag.edit')
@decorators.transaction @decorators.transaction
@ -1299,10 +1306,11 @@ class Tag(ObjectBase, GroupableMixin):
def delete(self, *, delete_children=False): def delete(self, *, delete_children=False):
self.photodb.log.debug('Deleting %s', self) self.photodb.log.debug('Deleting %s', self)
self.photodb._cached_tag_flat_dict = None self.photodb._cached_tag_flat_dict = None
GroupableMixin.delete(self, delete_children=delete_children) super().delete(delete_children=delete_children)
self.photodb.sql_delete(table='photo_tag_rel', pairs={'tagid': self.id}) self.photodb.sql_delete(table='photo_tag_rel', pairs={'tagid': self.id})
self.photodb.sql_delete(table='tag_synonyms', pairs={'mastername': self.name}) self.photodb.sql_delete(table='tag_synonyms', pairs={'mastername': self.name})
self.photodb.sql_delete(table='tags', pairs={'id': self.id}) self.photodb.sql_delete(table='tags', pairs={'id': self.id})
self.photodb._cached_tag_flat_dict = None
self._uncache() self._uncache()
@decorators.required_feature('tag.edit') @decorators.required_feature('tag.edit')
@ -1335,7 +1343,11 @@ class Tag(ObjectBase, GroupableMixin):
@decorators.required_feature('tag.edit') @decorators.required_feature('tag.edit')
@decorators.transaction @decorators.transaction
def remove_child(self, *args, **kwargs): def remove_child(self, *args, **kwargs):
return super().remove_child(*args, **kwargs) ret = super().remove_child(*args, **kwargs)
if ret is BAIL:
return
self.photodb._cached_tag_flat_dict = None
return ret
@decorators.required_feature('tag.edit') @decorators.required_feature('tag.edit')
@decorators.transaction @decorators.transaction