Object search routines now distinguish between candidates=None, and

candidates=[]
This commit is contained in:
Daniel Benoy 2013-07-17 03:30:48 -04:00
parent b93a9b4487
commit 1e5b52f685

View file

@ -90,12 +90,12 @@ class ObjectManager(TypedObjectManager):
if dbref: if dbref:
return dbref return dbref
# not a dbref. Search by name. # not a dbref. Search by name.
cand_restriction = candidates and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q() cand_restriction = candidates != None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
if exact: if exact:
return self.filter(cand_restriction & Q(db_player__user__username__iexact=ostring)) return self.filter(cand_restriction & Q(db_player__user__username__iexact=ostring))
else: # fuzzy matching else: # fuzzy matching
ply_cands = self.filter(cand_restriction & Q(playerdb__user__username__istartswith=ostring)).values_list("db_key", flat=True) ply_cands = self.filter(cand_restriction & Q(playerdb__user__username__istartswith=ostring)).values_list("db_key", flat=True)
if candidates: if candidates != None:
index_matches = string_partial_matching(ply_cands, ostring, ret_index=True) index_matches = string_partial_matching(ply_cands, ostring, ret_index=True)
return [obj for ind, obj in enumerate(make_iter(candidates)) if ind in index_matches] return [obj for ind, obj in enumerate(make_iter(candidates)) if ind in index_matches]
else: else:
@ -106,7 +106,7 @@ class ObjectManager(TypedObjectManager):
""" """
Returns objects based on simultaneous key and typeclass match. Returns objects based on simultaneous key and typeclass match.
""" """
cand_restriction = candidates and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q() cand_restriction = candidates != None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
return self.filter(cand_restriction & Q(db_key__iexact=oname, db_typeclass_path__exact=otypeclass_path)) return self.filter(cand_restriction & Q(db_key__iexact=oname, db_typeclass_path__exact=otypeclass_path))
# attr/property related # attr/property related
@ -117,7 +117,7 @@ class ObjectManager(TypedObjectManager):
Returns all objects having the given attribute_name defined at all. Location Returns all objects having the given attribute_name defined at all. Location
should be a valid location object. should be a valid location object.
""" """
cand_restriction = candidates and Q(objattribute__db_obj__pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q() cand_restriction = candidates != None and Q(objattribute__db_obj__pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
return list(self.filter(cand_restriction & Q(objattribute__db_key=attribute_name))) return list(self.filter(cand_restriction & Q(objattribute__db_key=attribute_name)))
@returns_typeclass_list @returns_typeclass_list
@ -132,7 +132,7 @@ class ObjectManager(TypedObjectManager):
the internal representation. This is reasonably effective but since Attribute values the internal representation. This is reasonably effective but since Attribute values
cannot be indexed, searching by Attribute key is to be preferred whenever possible. cannot be indexed, searching by Attribute key is to be preferred whenever possible.
""" """
cand_restriction = candidates and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q() cand_restriction = candidates != None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
type_restriction = typeclasses and Q(db_typeclass_path__in=make_iter(typeclasses)) or Q() type_restriction = typeclasses and Q(db_typeclass_path__in=make_iter(typeclasses)) or Q()
## This doesn't work if attribute_value is an object. Workaround below ## This doesn't work if attribute_value is an object. Workaround below
@ -157,7 +157,7 @@ class ObjectManager(TypedObjectManager):
candidates - list of candidate objects to search candidates - list of candidate objects to search
""" """
property_name = "db_%s" % property_name.lstrip('db_') property_name = "db_%s" % property_name.lstrip('db_')
cand_restriction = candidates and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q() cand_restriction = candidates != None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
querykwargs = {property_name:None} querykwargs = {property_name:None}
try: try:
return list(self.filter(cand_restriction).exclude(Q(**querykwargs))) return list(self.filter(cand_restriction).exclude(Q(**querykwargs)))
@ -177,7 +177,7 @@ class ObjectManager(TypedObjectManager):
if not property_name.startswith('db_'): if not property_name.startswith('db_'):
property_name = "db_%s" % property_name property_name = "db_%s" % property_name
querykwargs = {property_name:property_value} querykwargs = {property_name:property_value}
cand_restriction = candidates and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q() cand_restriction = candidates != None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
type_restriction = typeclasses and Q(db_typeclass_path__in=make_iter(typeclasses)) or Q() type_restriction = typeclasses and Q(db_typeclass_path__in=make_iter(typeclasses)) or Q()
try: try:
return list(self.filter(cand_restriction & type_restriction & Q(**querykwargs))) return list(self.filter(cand_restriction & type_restriction & Q(**querykwargs)))
@ -215,12 +215,12 @@ class ObjectManager(TypedObjectManager):
# build query objects # build query objects
candidates_id = [_GA(obj, "id") for obj in make_iter(candidates) if obj] candidates_id = [_GA(obj, "id") for obj in make_iter(candidates) if obj]
cand_restriction = candidates and Q(pk__in=make_iter(candidates_id)) or Q() cand_restriction = candidates != None and Q(pk__in=make_iter(candidates_id)) or Q()
type_restriction = typeclasses and Q(db_typeclass_path__in=make_iter(typeclasses)) or Q() type_restriction = typeclasses and Q(db_typeclass_path__in=make_iter(typeclasses)) or Q()
if exact: if exact:
# exact match - do direct search # exact match - do direct search
return self.filter(cand_restriction & type_restriction & (Q(db_key__iexact=ostring) | Q(alias__db_key__iexact=ostring))).distinct() return self.filter(cand_restriction & type_restriction & (Q(db_key__iexact=ostring) | Q(alias__db_key__iexact=ostring))).distinct()
elif candidates: elif candidates != None:
# fuzzy with candidates # fuzzy with candidates
key_candidates = self.filter(cand_restriction & type_restriction) key_candidates = self.filter(cand_restriction & type_restriction)
else: else: