Allow TagHandler.remove to remove-by category only

This commit is contained in:
Griatch 2019-04-18 22:18:16 +02:00
parent 9da63f4baf
commit dfef343a27

View file

@ -300,17 +300,27 @@ class TagHandler(object):
return ret if ret else [default] if default is not None else [] return ret if ret else [default] if default is not None else []
return ret[0] if len(ret) == 1 else (ret if ret else default) return ret[0] if len(ret) == 1 else (ret if ret else default)
def remove(self, key, category=None): def remove(self, key=None, category=None):
""" """
Remove a tag from the handler based ond key and category. Remove a tag from the handler based ond key and/or category.
Args: Args:
key (str or list): The tag or tags to retrieve. key (str or list, optional): The tag or tags to retrieve.
category (str, optional): The Tag category to limit the category (str, optional): The Tag category to limit the
request to. Note that `None` is the valid, default request to. Note that `None` is the valid, default
category. category
Raises:
RuntimeError: If neither key nor category is specifed.
""" """
if key is None and category is None:
raise RuntimeError("TagHandler.remove requires either key or category. "
"Use TagHandler.clear to remove all tags.")
if not key:
# only category
self.clear(category=category)
return
for key in make_iter(key): for key in make_iter(key):
if not (key or key.strip()): # we don't allow empty tags if not (key or key.strip()): # we don't allow empty tags
continue continue
@ -320,8 +330,10 @@ class TagHandler(object):
# This does not delete the tag object itself. Maybe it should do # This does not delete the tag object itself. Maybe it should do
# that when no objects reference the tag anymore (but how to check)? # that when no objects reference the tag anymore (but how to check)?
# For now, tags are never deleted, only their connection to objects. # For now, tags are never deleted, only their connection to objects.
tagobj = getattr(self.obj, self._m2m_fieldname).filter(db_key=tagstr, db_category=category, tagobj = getattr(self.obj, self._m2m_fieldname).filter(
db_model=self._model, db_tagtype=self._tagtype) db_key=tagstr, db_category=category,
db_model=self._model,
db_tagtype=self._tagtype)
if tagobj: if tagobj:
getattr(self.obj, self._m2m_fieldname).remove(tagobj[0]) getattr(self.obj, self._m2m_fieldname).remove(tagobj[0])
self._delcache(key, category) self._delcache(key, category)