Optimized typeclass conversion in typeclass manager.
This commit is contained in:
parent
91f2a5930c
commit
018a98b92c
1 changed files with 13 additions and 35 deletions
|
|
@ -5,6 +5,7 @@ all Attributes and TypedObjects).
|
||||||
"""
|
"""
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from src.utils import idmapper
|
from src.utils import idmapper
|
||||||
|
from src.utils.utils import make_iter
|
||||||
#from src.typeclasses import idmap
|
#from src.typeclasses import idmap
|
||||||
|
|
||||||
# Managers
|
# Managers
|
||||||
|
|
@ -26,55 +27,32 @@ class AttributeManager(models.Manager):
|
||||||
return self.filter(db_obj=obj).filter(
|
return self.filter(db_obj=obj).filter(
|
||||||
db_key__icontains=searchstr)
|
db_key__icontains=searchstr)
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# helper functions for the TypedObjectManager.
|
# helper functions for the TypedObjectManager.
|
||||||
#
|
#
|
||||||
|
|
||||||
def returns_typeclass_list(method):
|
def returns_typeclass_list(method):
|
||||||
"""
|
"""
|
||||||
Decorator function that turns the return of the
|
Decorator: Chantes return of the decorated method (which are
|
||||||
decorated method (which are ObjectDB objects)
|
TypeClassed objects) into object_classes(s) instead. Will always
|
||||||
into object_classes(s) instead.
|
return a list (may be empty).
|
||||||
Will always return a list or None.
|
|
||||||
"""
|
"""
|
||||||
def func(self, *args, **kwargs):
|
def func(self, *args, **kwargs):
|
||||||
"""
|
"decorator. Returns a list."
|
||||||
This overloads the relevant method.
|
matches = method(self, *args, **kwargs)
|
||||||
The return is *always* either None
|
return [dbobj.typeclass or dbobj for dbobj in make_iter(matches)]
|
||||||
or a list.
|
|
||||||
"""
|
|
||||||
match = method(self, *args, **kwargs)
|
|
||||||
#print "deco: %s" % match,
|
|
||||||
if not match:
|
|
||||||
return []
|
|
||||||
try:
|
|
||||||
match = list(match)
|
|
||||||
except TypeError:
|
|
||||||
match = [match]
|
|
||||||
obj_classes = []
|
|
||||||
for dbobj in match:
|
|
||||||
try:
|
|
||||||
obj_classes.append(dbobj.typeclass)
|
|
||||||
except Exception:
|
|
||||||
obj_classes.append(dbobj)
|
|
||||||
#logger.log_trace()
|
|
||||||
#print "-> %s" % obj_classes
|
|
||||||
#if not obj_classes:
|
|
||||||
# return None
|
|
||||||
return obj_classes
|
|
||||||
return func
|
return func
|
||||||
|
|
||||||
def returns_typeclass(method):
|
def returns_typeclass(method):
|
||||||
"""
|
"""
|
||||||
Decorator: Will always return a single result or None.
|
Decorator: Will always return a single typeclassed result or None.
|
||||||
"""
|
"""
|
||||||
def func(self, *args, **kwargs):
|
def func(self, *args, **kwargs):
|
||||||
"decorator"
|
"decorator. Returns result or None."
|
||||||
rfunc = returns_typeclass_list(method)
|
rfunc = returns_typeclass_list(method)
|
||||||
match = rfunc(self, *args, **kwargs)
|
try:
|
||||||
if match:
|
return rfunc(self, *args, **kwargs)[0]
|
||||||
return match[0]
|
except IndexError:
|
||||||
return None
|
return None
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue