From b1e08c7da6d90e62e144ace71ae5894a4981ea40 Mon Sep 17 00:00:00 2001 From: Griatch Date: Sun, 16 Feb 2014 22:09:35 +0100 Subject: [PATCH] Continuing to clean up and fix bugs around Attribute/Tag --- src/commands/default/building.py | 10 +++-- src/objects/models.py | 4 +- src/typeclasses/managers.py | 66 ++++++++++++++++---------------- src/typeclasses/models.py | 6 ++- src/utils/search.py | 11 +++--- 5 files changed, 53 insertions(+), 44 deletions(-) diff --git a/src/commands/default/building.py b/src/commands/default/building.py index a9f137820d..fc1f3ad289 100644 --- a/src/commands/default/building.py +++ b/src/commands/default/building.py @@ -1676,15 +1676,19 @@ class CmdExamine(ObjManipCommand): perms_string += " [Superuser]" string += "\n{wPermissions{n: %s" % perms_string + + tags_string = utils.fill(", ".join(tag for tag in obj.tags.all()), indent=5) + if tags_string: + string += "\n{wTags{n: %s" % tags_string + locks = str(obj.locks) if locks: locks_string = utils.fill("; ".join([lock for lock in locks.split(';')]), indent=6) else: locks_string = " Default" - - string += "\n{wLocks{n:%s" % locks_string + if not (len(obj.cmdset.all()) == 1 and obj.cmdset.current.key == "_EMPTY_CMDSET"): stored_cmdsets = obj.cmdset.all() stored_cmdsets.sort(key=lambda x: x.priority, reverse=True) @@ -2148,7 +2152,7 @@ class CmdTag(MuxCommand): "Implement the @tag functionality" if not self.args: - self.caller.msg("Usage: @tag[/switches] [|[=[]]") + self.caller.msg("Usage: @tag[/switches] [= [:]]") return if "search" in self.switches: # search by tag diff --git a/src/objects/models.py b/src/objects/models.py index 1dcd27ef68..5b8b0595a8 100644 --- a/src/objects/models.py +++ b/src/objects/models.py @@ -375,9 +375,9 @@ class ObjectDB(TypedObject): if use_nicks: # get all valid nicks to search - nicks = self.nicks.all(category="object") + nicks = self.nicks.get(category="object") if self.has_player: - pnicks = self.nicks.all(category="player") + pnicks = self.nicks.get(category="player") nicks = nicks + pnicks for nick in nicks: if searchdata == nick.db_key: diff --git a/src/typeclasses/managers.py b/src/typeclasses/managers.py index f928494d27..e64fee583a 100644 --- a/src/typeclasses/managers.py +++ b/src/typeclasses/managers.py @@ -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): diff --git a/src/typeclasses/models.py b/src/typeclasses/models.py index a9a7b35af6..276d4659bf 100644 --- a/src/typeclasses/models.py +++ b/src/typeclasses/models.py @@ -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 diff --git a/src/utils/search.py b/src/utils/search.py index 8b951a7b77..51ad3dbb95 100644 --- a/src/utils/search.py +++ b/src/utils/search.py @@ -32,7 +32,8 @@ from django.contrib.contenttypes.models import ContentType # limit symbol import from API __all__ = ("search_object", "search_player", "search_script", "search_message", "search_channel", "search_help_entry", - "search_tag") + "search_object_tag", "search_script_tag", "search_player_tag", + "search_channel_tag") # import objects this way to avoid circular import problems @@ -189,10 +190,10 @@ help_entries = search_help_entries # Note that this returns the object attached to the tag, not the tag itself # (this is usually what you want) search_tag = Tag.objects.get_objs_with_tag -search_player_tag = lambda key, category: Tag.objects.get_objs_with_tag(key, category, model="objects.objectdb") -search_player_tag = lambda key, category: Tag.objects.get_objs_with_tag(key, category, model="players.playerdb") -search_script_tag = lambda key, category: Tag.objects.get_objs_with_tag(key, category, model="scripts.scriptdb") -search_channel_tag = lambda key, category: Tag.objects.get_objs_with_tag(key, category, model="comms.channeldb") +def search_object_tag(key, category=None): return Tag.objects.get_objs_with_tag(key, category, model="objects.objectdb") +def search_player_tag(key, category=None): return Tag.objects.get_objs_with_tag(key, category, model="players.playerdb") +def search_script_tag(key, category=None): return Tag.objects.get_objs_with_tag(key, category, model="scripts.scriptdb") +def search_channel_tag(key, category=None): return Tag.objects.get_objs_with_tag(key, category, model="comms.channeldb") # """ # Search and return all tags matching any combination of