Made several admin commands handle multi-word global searching; defined a new search function on objects.model for global name searching.

/Griatch
This commit is contained in:
Griatch 2009-10-21 18:42:52 +00:00
parent d8787bfc70
commit 9b6bd7125f
3 changed files with 40 additions and 31 deletions

View file

@ -16,7 +16,7 @@ def cmd_teleport(command):
teleport teleport
Usage: Usage:
teleport/switch [<object> = <location>] teleport/switch [<object> =] <location>
Switches: Switches:
quiet - don't inform the source and target quiet - don't inform the source and target
@ -48,16 +48,14 @@ def cmd_teleport(command):
# Use search_for_object to handle duplicate/nonexistant results. # Use search_for_object to handle duplicate/nonexistant results.
if not victim: if not victim:
return return
destination = source_object.search_for_object(eq_args[1])
# Use search_for_object to handle duplicate/nonexistant results.
if not destination:
return
if victim.is_room(): if victim.is_room():
source_object.emit_to("You can't teleport a room.") source_object.emit_to("You can't teleport a room.")
return return
destination = source_object.search_for_object_global(eq_args[1],exact_match=True,
limit_types=[defines_global.OTYPE_THING,
defines_global.OTYPE_ROOM])
if not destination:
return
if victim == destination: if victim == destination:
source_object.emit_to("You can't teleport an object inside of itself!") source_object.emit_to("You can't teleport an object inside of itself!")
return return
@ -65,7 +63,9 @@ def cmd_teleport(command):
victim.move_to(destination, quiet=tel_quietly) victim.move_to(destination, quiet=tel_quietly)
else: else:
# Direct teleport (no equal sign) # Direct teleport (no equal sign)
target_obj = source_object.search_for_object(command.command_argument) target_obj = source_object.search_for_object_global(eq_args[0],exact_match=True,
limit_types=[defines_global.OTYPE_THING,
defines_global.OTYPE_ROOM])
# Use search_for_object to handle duplicate/nonexistant results. # Use search_for_object to handle duplicate/nonexistant results.
if not target_obj: if not target_obj:
return return
@ -687,17 +687,11 @@ def cmd_open(command):
else: else:
# We have the name of a destination. Try to find it. # We have the name of a destination. Try to find it.
destination = Object.objects.global_object_name_search(dest_name) destination = source_object.search_for_object_global(dest_name, exact_match=True,
limit_types=[defines_global.OTYPE_THING,
defines_global.OTYPE_ROOM])
if not destination: if not destination:
source_object.emit_to("No matches found for '%s'." % dest_name)
return return
if len(destination) > 1:
s = "There are multiple matches. Please use #dbref to be more specific."
for d in destination:
s += "\n %s" % destination.get_name()
source_object.emit_to(s)
return
destination = destination[0]
if destination.is_exit(): if destination.is_exit():
source_object.emit_to("You can't open an exit to an exit!") source_object.emit_to("You can't open an exit to an exit!")
@ -914,18 +908,11 @@ def cmd_link(command):
if not source_object.controls_other(obj): if not source_object.controls_other(obj):
source_object.emit_to(defines_global.NOCONTROL_MSG) source_object.emit_to(defines_global.NOCONTROL_MSG)
return return
destination = Object.objects.global_object_name_search(dest_name) destination = source_object.search_for_object_global(dest_name, exact_match=True,
limit_types=[defines_global.OTYPE_THING,
defines_global.OTYPE_ROOM])
if not destination: if not destination:
source_object.emit_to("No matches found for '%s'." % dest_name)
return return
if len(destination) > 1:
s = "There are multiple matches. Please use #dbref to be more specific."
for d in destination:
s += "\n %s" % destination.get_name(show_dbref=True)
source_object.emit_to(s)
return
destination = destination[0]
# do the link. # do the link.
oldhome = obj.get_home() oldhome = obj.get_home()
ohome_text = "" ohome_text = ""

View file

@ -145,8 +145,9 @@ class ObjectManager(models.Manager):
o_query = self.filter(name__iexact=ostring) o_query = self.filter(name__iexact=ostring)
else: else:
o_query = self.filter(name__icontains=ostring) o_query = self.filter(name__icontains=ostring)
if limit_types: if limit_types is not False:
o_query = o_query.include(type__in=limit_types) for limiter in limit_types:
o_query.filter(type=limiter)
return o_query.exclude(type__in=[defines_global.OTYPE_GARBAGE, return o_query.exclude(type__in=[defines_global.OTYPE_GARBAGE,
defines_global.OTYPE_GOING]) defines_global.OTYPE_GOING])

View file

@ -221,6 +221,27 @@ class Object(models.Model):
else: else:
return results[0] return results[0]
def search_for_object_global(self, ostring, exact_match=True, limit_types=[]):
"""
Search for ostring in all objects, globally. Handle multiple-matches
and no matches gracefully. This is mainly intended to be used by
admin and build-type commands. It also accepts #dbref
search queries.
"""
results = Object.objects.global_object_name_search(ostring, exact_match=exact_match,
limit_types=limit_types)
if not results:
self.emit_to("No matches found for '%s'." % ostring)
return
if len(results) > 1:
string = "More than one match for '%s' (please narrow target):" % ostring
for res in results:
string += "\n %s" % res.get_name()
self.emit_to(string)
return
return results[0]
def get_sessions(self): def get_sessions(self):
""" """
Returns a list of sessions matching this object. Returns a list of sessions matching this object.