Fold long lines, PEP 8, comparision to None
+Indent, comment and typo edits
This commit is contained in:
parent
684f163fd7
commit
7010f998f1
1 changed files with 40 additions and 25 deletions
|
|
@ -21,6 +21,7 @@ _MULTIMATCH_REGEX = re.compile(settings.SEARCH_MULTIMATCH_REGEX, re.I + re.U)
|
||||||
|
|
||||||
# Try to use a custom way to parse id-tagged multimatches.
|
# Try to use a custom way to parse id-tagged multimatches.
|
||||||
|
|
||||||
|
|
||||||
class ObjectDBManager(TypedObjectManager):
|
class ObjectDBManager(TypedObjectManager):
|
||||||
"""
|
"""
|
||||||
This ObjectManager implements methods for searching
|
This ObjectManager implements methods for searching
|
||||||
|
|
@ -79,11 +80,13 @@ class ObjectDBManager(TypedObjectManager):
|
||||||
if dbref:
|
if dbref:
|
||||||
return dbref
|
return dbref
|
||||||
# not a dbref. Search by name.
|
# not a dbref. Search by name.
|
||||||
cand_restriction = candidates != None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
|
cand_restriction = candidates is not 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__username__iexact=ostring))
|
return self.filter(cand_restriction & Q(db_player__username__iexact=ostring))
|
||||||
else: # fuzzy matching
|
else: # fuzzy matching
|
||||||
ply_cands = self.filter(cand_restriction & Q(playerdb__username__istartswith=ostring)).values_list("db_key", flat=True)
|
ply_cands = self.filter(cand_restriction & Q(playerdb__username__istartswith=ostring)
|
||||||
|
).values_list("db_key", flat=True)
|
||||||
if candidates:
|
if candidates:
|
||||||
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]
|
||||||
|
|
@ -103,7 +106,8 @@ class ObjectDBManager(TypedObjectManager):
|
||||||
Returns:
|
Returns:
|
||||||
matches (list): The matching objects.
|
matches (list): The matching objects.
|
||||||
"""
|
"""
|
||||||
cand_restriction = candidates != None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
|
cand_restriction = candidates is not 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
|
||||||
|
|
@ -121,7 +125,9 @@ class ObjectDBManager(TypedObjectManager):
|
||||||
matches (list): All objects having the given attribute_name defined at all.
|
matches (list): All objects having the given attribute_name defined at all.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
cand_restriction = candidates != None and Q(db_attributes__db_obj__pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
|
cand_restriction = candidates is not None and Q(db_attributes__db_obj__pk__in=[_GA(obj, "id") for obj
|
||||||
|
in make_iter(candidates)
|
||||||
|
if obj]) or Q()
|
||||||
return list(self.filter(cand_restriction & Q(db_attributes__db_key=attribute_name)))
|
return list(self.filter(cand_restriction & Q(db_attributes__db_key=attribute_name)))
|
||||||
|
|
||||||
@returns_typeclass_list
|
@returns_typeclass_list
|
||||||
|
|
@ -144,20 +150,23 @@ class ObjectDBManager(TypedObjectManager):
|
||||||
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 != None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
|
cand_restriction = candidates is not 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
|
||||||
|
|
||||||
if isinstance(attribute_value, (basestring, int, float, bool)):
|
if isinstance(attribute_value, (basestring, int, float, bool)):
|
||||||
return self.filter(cand_restriction & type_restriction & Q(db_attributes__db_key=attribute_name, db_attributes__db_value=attribute_value))
|
return self.filter(cand_restriction & type_restriction & Q(db_attributes__db_key=attribute_name,
|
||||||
|
db_attributes__db_value=attribute_value))
|
||||||
else:
|
else:
|
||||||
# We have to loop for safety since the referenced lookup gives deepcopy error if attribute value is an object.
|
# We must loop for safety since the referenced lookup gives deepcopy error if attribute value is an object.
|
||||||
global _ATTR
|
global _ATTR
|
||||||
if not _ATTR:
|
if not _ATTR:
|
||||||
from evennia.typeclasses.models import Attribute as _ATTR
|
from evennia.typeclasses.models import Attribute as _ATTR
|
||||||
cands = list(self.filter(cand_restriction & type_restriction & Q(db_attributes__db_key=attribute_name)))
|
cands = list(self.filter(cand_restriction & type_restriction & Q(db_attributes__db_key=attribute_name)))
|
||||||
results = [attr.objectdb_set.all() for attr in _ATTR.objects.filter(objectdb__in=cands, db_value=attribute_value)]
|
results = [attr.objectdb_set.all() for attr in _ATTR.objects.filter(objectdb__in=cands,
|
||||||
|
db_value=attribute_value)]
|
||||||
return chain(*results)
|
return chain(*results)
|
||||||
|
|
||||||
@returns_typeclass_list
|
@returns_typeclass_list
|
||||||
|
|
@ -174,8 +183,9 @@ class ObjectDBManager(TypedObjectManager):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
property_name = "db_%s" % property_name.lstrip('db_')
|
property_name = "db_%s" % property_name.lstrip('db_')
|
||||||
cand_restriction = candidates != None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
|
cand_restriction = candidates is not None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates)
|
||||||
querykwargs = {property_name:None}
|
if obj]) or Q()
|
||||||
|
querykwargs = {property_name: None}
|
||||||
try:
|
try:
|
||||||
return list(self.filter(cand_restriction).exclude(Q(**querykwargs)))
|
return list(self.filter(cand_restriction).exclude(Q(**querykwargs)))
|
||||||
except exceptions.FieldError:
|
except exceptions.FieldError:
|
||||||
|
|
@ -198,8 +208,9 @@ class ObjectDBManager(TypedObjectManager):
|
||||||
if isinstance(property_name, basestring):
|
if isinstance(property_name, basestring):
|
||||||
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 != None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
|
cand_restriction = candidates is not 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)))
|
||||||
|
|
@ -207,7 +218,8 @@ class ObjectDBManager(TypedObjectManager):
|
||||||
return []
|
return []
|
||||||
except ValueError:
|
except ValueError:
|
||||||
from evennia.utils import logger
|
from evennia.utils import logger
|
||||||
logger.log_err("The property '%s' does not support search criteria of the type %s." % (property_name, type(property_value)))
|
logger.log_err("The property '%s' does not support search criteria of the type %s." %
|
||||||
|
(property_name, type(property_value)))
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@returns_typeclass_list
|
@returns_typeclass_list
|
||||||
|
|
@ -253,7 +265,7 @@ class ObjectDBManager(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 != None and Q(pk__in=candidates_id) or Q()
|
cand_restriction = candidates is not None and Q(pk__in=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
|
||||||
|
|
@ -264,7 +276,8 @@ class ObjectDBManager(TypedObjectManager):
|
||||||
search_candidates = self.filter(cand_restriction & type_restriction)
|
search_candidates = self.filter(cand_restriction & type_restriction)
|
||||||
else:
|
else:
|
||||||
# fuzzy without supplied candidates - we select our own candidates
|
# fuzzy without supplied candidates - we select our own candidates
|
||||||
search_candidates = self.filter(type_restriction & (Q(db_key__istartswith=ostring) | Q(db_tags__db_key__istartswith=ostring))).distinct()
|
search_candidates = self.filter(type_restriction & (Q(db_key__istartswith=ostring) |
|
||||||
|
Q(db_tags__db_key__istartswith=ostring))).distinct()
|
||||||
# fuzzy matching
|
# fuzzy matching
|
||||||
key_strings = search_candidates.values_list("db_key", flat=True).order_by("id")
|
key_strings = search_candidates.values_list("db_key", flat=True).order_by("id")
|
||||||
|
|
||||||
|
|
@ -278,7 +291,7 @@ class ObjectDBManager(TypedObjectManager):
|
||||||
db_tags__db_key__icontains=ostring)
|
db_tags__db_key__icontains=ostring)
|
||||||
alias_strings = []
|
alias_strings = []
|
||||||
alias_candidates = []
|
alias_candidates = []
|
||||||
#TODO create the alias_strings and alias_candidates lists more effiently?
|
# TODO create the alias_strings and alias_candidates lists more efficiently?
|
||||||
for candidate in search_candidates:
|
for candidate in search_candidates:
|
||||||
for alias in candidate.aliases.all():
|
for alias in candidate.aliases.all():
|
||||||
alias_strings.append(alias)
|
alias_strings.append(alias)
|
||||||
|
|
@ -343,13 +356,16 @@ class ObjectDBManager(TypedObjectManager):
|
||||||
"""
|
"""
|
||||||
if attribute_name:
|
if attribute_name:
|
||||||
# attribute/property search (always exact).
|
# attribute/property search (always exact).
|
||||||
matches = self.get_objs_with_db_property_value(attribute_name, searchdata, candidates=candidates, typeclasses=typeclass)
|
matches = self.get_objs_with_db_property_value(attribute_name, searchdata,
|
||||||
|
candidates=candidates, typeclasses=typeclass)
|
||||||
if matches:
|
if matches:
|
||||||
return matches
|
return matches
|
||||||
return self.get_objs_with_attr_value(attribute_name, searchdata, candidates=candidates, typeclasses=typeclass)
|
return self.get_objs_with_attr_value(attribute_name, searchdata,
|
||||||
|
candidates=candidates, typeclasses=typeclass)
|
||||||
else:
|
else:
|
||||||
# normal key/alias search
|
# normal key/alias search
|
||||||
return self.get_objs_with_key_or_alias(searchdata, exact=exact, candidates=candidates, typeclasses=typeclass)
|
return self.get_objs_with_key_or_alias(searchdata, exact=exact,
|
||||||
|
candidates=candidates, typeclasses=typeclass)
|
||||||
|
|
||||||
if not searchdata and searchdata != 0:
|
if not searchdata and searchdata != 0:
|
||||||
return []
|
return []
|
||||||
|
|
@ -418,7 +434,6 @@ class ObjectDBManager(TypedObjectManager):
|
||||||
|
|
||||||
#
|
#
|
||||||
# ObjectManager Copy method
|
# ObjectManager Copy method
|
||||||
#
|
|
||||||
|
|
||||||
def copy_object(self, original_object, new_key=None,
|
def copy_object(self, original_object, new_key=None,
|
||||||
new_location=None, new_home=None,
|
new_location=None, new_home=None,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue