Continuing to clean up and fix bugs around Attribute/Tag

This commit is contained in:
Griatch 2014-02-16 22:09:35 +01:00
parent bad24513e0
commit b1e08c7da6
5 changed files with 53 additions and 44 deletions

View file

@ -15,6 +15,39 @@ __all__ = ("AttributeManager", "TypedObjectManager")
_GA = object.__getattribute__
_ObjectDB = None
#
# helper functions for the TypedObjectManager.
#
def returns_typeclass_list(method):
"""
Decorator: Changes return of the decorated method (which are
TypeClassed objects) into object_classes(s) instead. Will always
return a list (may be empty).
"""
def func(self, *args, **kwargs):
"decorator. Returns a list."
self.__doc__ = method.__doc__
matches = make_iter(method(self, *args, **kwargs))
return [(hasattr(dbobj, "typeclass") and dbobj.typeclass) or dbobj
for dbobj in make_iter(matches)]
return update_wrapper(func, method)
def returns_typeclass(method):
"""
Decorator: Will always return a single typeclassed result or None.
"""
def func(self, *args, **kwargs):
"decorator. Returns result or None."
self.__doc__ = method.__doc__
matches = method(self, *args, **kwargs)
dbobj = matches and make_iter(matches)[0] or None
if dbobj:
return (hasattr(dbobj, "typeclass") and dbobj.typeclass) or dbobj
return None
return update_wrapper(func, method)
# Managers
def _attr_pickled(method):
@ -134,6 +167,7 @@ class TagManager(models.Manager):
else:
return list(tags)
@returns_typeclass_list
def get_objs_with_tag(self, key=None, category=None, model="objects.objectdb", tagtype=None):
"""
Search and return all objects of objclass that has tags matching
@ -173,38 +207,6 @@ class TagManager(models.Manager):
return make_iter(tag)[0]
#
# helper functions for the TypedObjectManager.
#
def returns_typeclass_list(method):
"""
Decorator: Changes return of the decorated method (which are
TypeClassed objects) into object_classes(s) instead. Will always
return a list (may be empty).
"""
def func(self, *args, **kwargs):
"decorator. Returns a list."
self.__doc__ = method.__doc__
matches = make_iter(method(self, *args, **kwargs))
return [(hasattr(dbobj, "typeclass") and dbobj.typeclass) or dbobj
for dbobj in make_iter(matches)]
return update_wrapper(func, method)
def returns_typeclass(method):
"""
Decorator: Will always return a single typeclassed result or None.
"""
def func(self, *args, **kwargs):
"decorator. Returns result or None."
self.__doc__ = method.__doc__
matches = method(self, *args, **kwargs)
dbobj = matches and make_iter(matches)[0] or None
if dbobj:
return (hasattr(dbobj, "typeclass") and dbobj.typeclass) or dbobj
return None
return update_wrapper(func, method)
#class TypedObjectManager(idmap.CachingManager):

View file

@ -533,8 +533,10 @@ class TagHandler(object):
def add(self, tag, category=None, data=None):
"Add a new tag to the handler. Tag is a string or a list of strings."
for tagstr in make_iter(tag):
tagstr = tagstr.strip().lower() if tagstr is not None else None
category = category().lower() if category is not None else None
if not tagstr:
continue
tagstr = tagstr.strip().lower()
category = category.strip().lower() if category is not None else None
data = str(data) if data is not None else None
# this will only create tag if no matches existed beforehand (it
# will overload data on an existing tag since that is not