From 3aeec1298a78a25112119bfd18119967f379e781 Mon Sep 17 00:00:00 2001 From: Griatch Date: Mon, 2 Dec 2013 16:39:21 +0100 Subject: [PATCH] Added category support to @tag command; also fixed some bugs. --- src/commands/default/building.py | 36 +++++++++++++++++++++----------- src/typeclasses/models.py | 7 ++++++- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/commands/default/building.py b/src/commands/default/building.py index f10d6946f..18652d631 100644 --- a/src/commands/default/building.py +++ b/src/commands/default/building.py @@ -2132,15 +2132,22 @@ class CmdTag(MuxCommand): return if "search" in self.switches: # search by tag - objs = search.search_tag(self.args) + tag = self.args + category = None + if ":" in tag: + tag, category = [part.strip() for part in tag.split(":", 1)] + category = "object_%s" % category + objs = search.search_tag(tag, category=category) nobjs = len(objs) if nobjs > 0: - string = "Found %i object%s with tag '%s':\n %s" % (nobjs, - "s" if nobjs > 1 else "", - self.args, - ", ".join(o.key for o in objs)) + string = "Found %i object%s with tag '%s'%s:\n %s" % (nobjs, + "s" if nobjs > 1 else "", + tag, + " (category: %s)" % category if category else "", + ", ".join("%s(#%i)" % (o.key, o.dbid) for o in objs)) else: - string = "No objects found with tag %s." % self.args + string = "No objects found with tag '%s%s'." % (tag, + " (category: %s)" % category if category else "") self.caller.msg(string) return if "del" in self.switches: @@ -2155,7 +2162,9 @@ class CmdTag(MuxCommand): if ":" in tag: tag, category = [part.strip() for part in tag.split(":", 1)] obj.tags.remove(tag, category=category) - string = "Removed tag '%s' from %s (if it existed)" % (tag, obj) + string = "Removed tag '%s'%s from %s (if it existed)" % (tag, + " (category: %s)" % category if category else "", + obj) else: # no tag specified, clear all tags obj.tags.clear() @@ -2174,19 +2183,22 @@ class CmdTag(MuxCommand): tag, category = [part.strip() for part in tag.split(":", 1)] # create the tag obj.tags.add(tag, category=category) - string = "Added tag '%s' to %s." % (tag, obj) + string = "Added tag '%s'%s to %s." % (tag, + " (category: %s)" % category if category else "", + obj) self.caller.msg(string) else: # no = found - list tags on object obj = self.caller.search(self.args, global_search=True) if not obj: return - tags = obj.tags.all() + tags = obj.db_tags.all() ntags = len(tags) + categories = [tag.db_category.split("_", 1)[1] for tag in tags] + categories = [" (category: %s)" % cater if cater else "" for cater in categories] if ntags: - string = "Tag%s on %s: %s" % ("s" if ntags > 1 else "", - obj, - ", ".join("'%s'" % tag for tag in obj.tags.all())) + string = "Tag%s on %s: %s" % ("s" if ntags > 1 else "", obj, + ", ".join("'%s'%s" % (tags[i], categories[i]) for i in range(ntags))) else: string = "No tags attached to %s." % obj self.caller.msg(string) diff --git a/src/typeclasses/models.py b/src/typeclasses/models.py index 551b7b28a..537640f74 100644 --- a/src/typeclasses/models.py +++ b/src/typeclasses/models.py @@ -555,10 +555,15 @@ class TagHandler(object): _GA(self.obj, self._m2m_fieldname).remove(tag) self._recache() - def all(self): + def all(self, category=None): "Get all tags in this handler" if self._cache is None or not _TYPECLASS_AGGRESSIVE_CACHE: self._recache() + if category: + category = "%s%s" % (self.prefix, category.strip().lower() + if category is not None else "") + return [to_str(p[0]) for p in _GA(self.obj, self._m2m_fieldname).filter( + db_category=category).values_list("db_key") if p[0]] return self._cache.keys() #return [to_str(p[0]) for p in _GA(self.obj, self._m2m_fieldname).filter(db_category__startswith=self.prefix).values_list("db_key") if p[0]]