Added @tag command for easily manipulating tags. Fixed some bugs at the same time.

This commit is contained in:
Griatch 2013-12-02 15:40:02 +01:00
parent 6d06ab5338
commit 88d103b55f
4 changed files with 105 additions and 6 deletions

View file

@ -12,7 +12,7 @@ from src.utils.dbserialize import to_pickle
__all__ = ("AttributeManager", "TypedObjectManager")
_GA = object.__getattribute__
_ObjectDB = None
# Managers
@ -131,14 +131,19 @@ class TagManager(models.Manager):
else:
return list(tags)
def get_objs_with_tag(self, objclass, key=None, category=None):
def get_objs_with_tag(self, key=None, category=None, objclass=None):
"""
Search and return all objects of objclass that has tags matching
the given search criteria.
objclass (dbmodel) - the object class to search
key (string) - the tag identifier
category (string) - the tag category
objclass (dbmodel) - the object class to search. If not given, use ObjectDB.
"""
global _ObjectDB
if not objclass:
if not _ObjectDB:
from src.objects.models import ObjectDB as _ObjectDB
objclass = _ObjectDB
key_cands = Q(db_tags__db_key__iexact=key.lower().strip()) if key is not None else Q()
cat_cands = Q(db_tags__db_category__iexact=category.lower().strip()) if category is not None else Q()
return objclass.objects.filter(key_cands & cat_cands)

View file

@ -551,7 +551,8 @@ class TagHandler(object):
def clear(self):
"Remove all tags from the handler"
_GA(self.obj, self._m2m_fieldname).filter(db_category__startswith=self.prefix).clear()
for tag in _GA(self.obj, self._m2m_fieldname).filter(db_category__startswith=self.prefix):
_GA(self.obj, self._m2m_fieldname).remove(tag)
self._recache()
def all(self):