Made aliases work with the new handler. The location.contents updated is not working yet - this causes locational information to not be available until objects and manually initialized (e.g. by calling examine #dbref)

This commit is contained in:
Griatch 2013-07-12 20:21:52 +02:00
parent 45706f598a
commit 6bc16e46cc
13 changed files with 236 additions and 76 deletions

View file

@ -153,7 +153,7 @@ class CmdSetObjAlias(MuxCommand):
# save back to object. # save back to object.
obj.aliases.add(aliases) obj.aliases.add(aliases)
# we treat this as a re-caching (relevant for exits to re-build their exit commands with the correct aliases) # we treat this as a re-caching (relevant for exits to re-build their exit commands with the correct aliases)
caller.msg("Aliases for '%s' are now set to %s." % (obj.key, ", ".join(obj.aliases))) caller.msg("Aliases for '%s' are now %s." % (obj.key, str(obj.aliases)))
class CmdCopy(ObjManipCommand): class CmdCopy(ObjManipCommand):
""" """
@ -1569,7 +1569,7 @@ class CmdExamine(ObjManipCommand):
string = "\n{wName/key{n: {c%s{n (%s)" % (obj.name, obj.dbref) string = "\n{wName/key{n: {c%s{n (%s)" % (obj.name, obj.dbref)
if hasattr(obj, "aliases") and obj.aliases.all(): if hasattr(obj, "aliases") and obj.aliases.all():
string += "\n{wAliases{n: %s" % (", ".join(utils.make_iter(obj.aliases.all()))) string += "\n{wAliases{n: %s" % (", ".join(utils.make_iter(str(obj.aliases))))
if hasattr(obj, "sessid") and obj.sessid: if hasattr(obj, "sessid") and obj.sessid:
string += "\n{wsession{n: %s" % obj.sessid string += "\n{wsession{n: %s" % obj.sessid
elif hasattr(obj, "sessions") and obj.sessions: elif hasattr(obj, "sessions") and obj.sessions:

View file

@ -103,7 +103,7 @@ class CmdAddCom(MuxPlayerCommand):
if alias: if alias:
# create a nick and add it to the caller. # create a nick and add it to the caller.
caller.nicks.add(alias, channel.key, nick_type="channel") caller.nicks.add(alias, channel.key, category="channel")
string += " You can now refer to the channel %s with the alias '%s'." string += " You can now refer to the channel %s with the alias '%s'."
self.msg(string % (channel.key, alias)) self.msg(string % (channel.key, alias))
else: else:
@ -147,21 +147,21 @@ class CmdDelCom(MuxPlayerCommand):
return return
chkey = channel.key.lower() chkey = channel.key.lower()
# find all nicks linked to this channel and delete them # find all nicks linked to this channel and delete them
for nick in [nick for nick in caller.nicks.get(nick_type="channel") for nick in [nick for nick in caller.nicks.get(category="channel")
if nick.db_real.lower() == chkey]: if nick.db_data.lower() == chkey]:
nick.delete() nick.delete()
channel.disconnect_from(player) channel.disconnect_from(player)
self.msg("You stop listening to channel '%s'. Eventual aliases were removed." % channel.key) self.msg("You stop listening to channel '%s'. Eventual aliases were removed." % channel.key)
return return
else: else:
# we are removing a channel nick # we are removing a channel nick
channame = caller.nicks.get(ostring, nick_type="channel") channame = caller.nicks.get_replace(key=ostring, category="channel")
channel = find_channel(caller, channame, silent=True) channel = find_channel(caller, channame, silent=True)
if not channel: if not channel:
self.msg("No channel with alias '%s' was found." % ostring) self.msg("No channel with alias '%s' was found." % ostring)
else: else:
if caller.nicks.get(ostring, nick_type="channel", default=False): if caller.nicks.get(ostring, category="channel"):
caller.nicks.delete(ostring, nick_type="channel") caller.nicks.remove(ostring, category="channel")
self.msg("Your alias '%s' for channel %s was cleared." % (ostring, channel.key)) self.msg("Your alias '%s' for channel %s was cleared." % (ostring, channel.key))
else: else:
self.msg("You had no such alias defined for this channel.") self.msg("You had no such alias defined for this channel.")
@ -263,7 +263,7 @@ class CmdChannels(MuxPlayerCommand):
comtable = prettytable.PrettyTable(["{wchannel","{wmy aliases", "{wdescription"]) comtable = prettytable.PrettyTable(["{wchannel","{wmy aliases", "{wdescription"])
for chan in subs: for chan in subs:
clower = chan.key.lower() clower = chan.key.lower()
nicks = [nick for nick in caller.nicks.get(nick_type="channel")] nicks = [nick for nick in caller.nicks.get(category="channel")]
comtable.add_row(["%s%s" % (chan.key, chan.aliases and "(%s)" % ",".join(chan.aliases) or ""), comtable.add_row(["%s%s" % (chan.key, chan.aliases and "(%s)" % ",".join(chan.aliases) or ""),
"%s".join(nick.db_nick for nick in nicks if nick.db_real.lower()==clower()), "%s".join(nick.db_nick for nick in nicks if nick.db_real.lower()==clower()),
chan.desc]) chan.desc])
@ -272,7 +272,7 @@ class CmdChannels(MuxPlayerCommand):
# full listing (of channels caller is able to listen to) # full listing (of channels caller is able to listen to)
comtable = prettytable.PrettyTable(["{wsub","{wchannel","{wmy aliases","{wlocks","{wdescription"]) comtable = prettytable.PrettyTable(["{wsub","{wchannel","{wmy aliases","{wlocks","{wdescription"])
for chan in channels: for chan in channels:
nicks = [nick for nick in caller.nicks.get(nick_type="channel")] nicks = [nick for nick in caller.nicks.get(category="channel")]
comtable.add_row([chan in subs and "{gYes{n" or "{rNo{n", comtable.add_row([chan in subs and "{gYes{n" or "{rNo{n",
"%s%s" % (chan.key, chan.aliases and "(%s)" % ",".join(chan.aliases) or ""), "%s%s" % (chan.key, chan.aliases and "(%s)" % ",".join(chan.aliases) or ""),
"%s".join(nick.db_nick for nick in nicks if nick.db_real.lower()==clower()), "%s".join(nick.db_nick for nick in nicks if nick.db_real.lower()==clower()),
@ -368,7 +368,7 @@ class CmdCBoot(MuxPlayerCommand):
string = "%s boots %s from channel.%s" % (self.caller, player.key, reason) string = "%s boots %s from channel.%s" % (self.caller, player.key, reason)
channel.msg(string) channel.msg(string)
# find all player's nicks linked to this channel and delete them # find all player's nicks linked to this channel and delete them
for nick in [nick for nick in player.character.nicks.get(nick_type="channel") for nick in [nick for nick in player.character.nicks.get(category="channel")
if nick.db_real.lower() == channel.key]: if nick.db_real.lower() == channel.key]:
nick.delete() nick.delete()
# disconnect player # disconnect player

View file

@ -151,13 +151,14 @@ class CmdNick(MuxCommand):
switches = ["inputline"] switches = ["inputline"]
string = "" string = ""
for switch in switches: for switch in switches:
oldnick = Nick.objects.filter(db_obj=caller.dbobj, db_nick__iexact=nick, db_type__iexact=switch) oldnick = caller.nicks.get(key=nick, category=switch)
#oldnick = Nick.objects.filter(db_obj=caller.dbobj, db_nick__iexact=nick, db_type__iexact=switch)
if not real: if not real:
# removal of nick # removal of nick
if oldnick: if oldnick:
# clear the alias # clear the alias
string += "\nNick '%s' (= '%s') was cleared." % (nick, oldnick[0].db_real) string += "\nNick '%s' (= '%s') was cleared." % (nick, oldnick[0].db_real)
caller.nicks.delete(nick, nick_type=switch) caller.nicks.delete(nick, category=switch)
else: else:
string += "\nNo nick '%s' found, so it could not be removed." % nick string += "\nNo nick '%s' found, so it could not be removed." % nick
else: else:
@ -166,7 +167,7 @@ class CmdNick(MuxCommand):
string += "\nNick %s changed from '%s' to '%s'." % (nick, oldnick[0].db_real, real) string += "\nNick %s changed from '%s' to '%s'." % (nick, oldnick[0].db_real, real)
else: else:
string += "\nNick set: '%s' = '%s'." % (nick, real) string += "\nNick set: '%s' = '%s'." % (nick, real)
caller.nicks.add(nick, real, nick_type=switch) caller.nicks.add(nick, real, category=switch)
caller.msg(string) caller.msg(string)
class CmdInventory(MuxCommand): class CmdInventory(MuxCommand):

View file

@ -465,18 +465,13 @@ class CmdPassword(MuxPlayerCommand):
return return
oldpass = self.lhslist[0] # this is already stripped by parse() oldpass = self.lhslist[0] # this is already stripped by parse()
newpass = self.rhslist[0] # '' newpass = self.rhslist[0] # ''
try: if not player.check_password(oldpass):
uaccount = player.user
except AttributeError:
self.msg("This is only applicable for players.")
return
if not uaccount.check_password(oldpass):
self.msg("The specified old password isn't correct.") self.msg("The specified old password isn't correct.")
elif len(newpass) < 3: elif len(newpass) < 3:
self.msg("Passwords must be at least three characters long.") self.msg("Passwords must be at least three characters long.")
else: else:
uaccount.set_password(newpass) player.set_password(newpass)
uaccount.save() player.save()
self.msg("Password changed.") self.msg("Password changed.")
class CmdQuit(MuxPlayerCommand): class CmdQuit(MuxPlayerCommand):

View file

@ -110,9 +110,9 @@ class TestGeneral(CommandTest):
self.call(general.CmdNick(), "testalias = testaliasedstring1", "Nick set:") self.call(general.CmdNick(), "testalias = testaliasedstring1", "Nick set:")
self.call(general.CmdNick(), "/player testalias = testaliasedstring2", "Nick set:") self.call(general.CmdNick(), "/player testalias = testaliasedstring2", "Nick set:")
self.call(general.CmdNick(), "/object testalias = testaliasedstring3", "Nick set:") self.call(general.CmdNick(), "/object testalias = testaliasedstring3", "Nick set:")
self.assertEqual(u"testaliasedstring1", self.char1.nicks.get("testalias")) self.assertEqual(u"testaliasedstring1", self.char1.nicks.get_replace("testalias"))
self.assertEqual(u"testaliasedstring2", self.char1.nicks.get("testalias", nick_type="player")) self.assertEqual(u"testaliasedstring2", self.char1.nicks.get_replace("testalias", category="player"))
self.assertEqual(u"testaliasedstring3", self.char1.nicks.get("testalias", nick_type="object")) self.assertEqual(u"testaliasedstring3", self.char1.nicks.get_replace("testalias", category="object"))
self.call(general.CmdGet(), "Obj1", "You pick up Obj1.") self.call(general.CmdGet(), "Obj1", "You pick up Obj1.")
self.call(general.CmdDrop(), "Obj1", "You drop Obj1.") self.call(general.CmdDrop(), "Obj1", "You drop Obj1.")
self.call(general.CmdSay(), "Testing", "You say, \"Testing\"") self.call(general.CmdSay(), "Testing", "You say, \"Testing\"")

View file

@ -346,7 +346,7 @@ class ChannelManager(models.Manager):
channels = self.filter(db_key__iexact=ostring) channels = self.filter(db_key__iexact=ostring)
if not channels: if not channels:
# still no match. Search by alias. # still no match. Search by alias.
channels = [channel for channel in self.all() if ostring.lower in [a.lower for a in channel.aliases]] channels = [channel for channel in self.all() if ostring.lower() in [a.lower for a in channel.aliases]]
return channels return channels
# #

View file

@ -198,7 +198,8 @@ class ObjectManager(TypedObjectManager):
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(db_tags__db_key__iexact=ostring) & Q(db_tags__db_category__iexact="object_alias"))).distinct()
elif candidates: elif candidates:
# fuzzy with candidates # fuzzy with candidates
key_candidates = self.filter(cand_restriction & type_restriction) key_candidates = self.filter(cand_restriction & type_restriction)
@ -212,7 +213,7 @@ class ObjectManager(TypedObjectManager):
if index_matches: if index_matches:
return [obj for ind, obj in enumerate(key_candidates) if ind in index_matches] return [obj for ind, obj in enumerate(key_candidates) if ind in index_matches]
else: else:
alias_candidates = self.model.alias_set.related.model.objects.filter(db_obj__pk__in=candidates_id) alias_candidates = self.filter(id__in=candidates_id, db_tags__db_category__iexact="object_alias")
alias_strings = alias_candidates.values_list("db_key", flat=True) alias_strings = alias_candidates.values_list("db_key", flat=True)
index_matches = string_partial_matching(alias_strings, ostring, ret_index=True) index_matches = string_partial_matching(alias_strings, ostring, ret_index=True)
if index_matches: if index_matches:

View file

@ -140,9 +140,9 @@ class ObjectDB(TypedObject):
_SA(self, "cmdset", CmdSetHandler(self)) _SA(self, "cmdset", CmdSetHandler(self))
_GA(self, "cmdset").update(init_mode=True) _GA(self, "cmdset").update(init_mode=True)
_SA(self, "scripts", ScriptHandler(self)) _SA(self, "scripts", ScriptHandler(self))
_SA(self, "tags", TagHandler(self, "object")) _SA(self, "tags", TagHandler(self, category_prefix="object_"))
_SA(self, "aliases", AliasHandler(self, "object")) _SA(self, "aliases", AliasHandler(self, category_prefix="object_"))
_SA(self, "nicks", NickHandler(self, "object")) _SA(self, "nicks", NickHandler(self, category_prefix="object_"))
# Wrapper properties to easily set database fields. These are # Wrapper properties to easily set database fields. These are
# @property decorators that allows to access these fields using # @property decorators that allows to access these fields using
@ -569,8 +569,8 @@ class ObjectDB(TypedObject):
pnicks = self.nicks.get(category="player_nick_%s" % nicktype) pnicks = self.nicks.get(category="player_nick_%s" % nicktype)
nicks = nicks + pnicks nicks = nicks + pnicks
for nick in nicks: for nick in nicks:
if searchdata == nick.db_nick: if searchdata == nick.db_key:
searchdata = nick.db_real searchdata = nick.db_data
break break
candidates=None candidates=None

View file

@ -120,9 +120,9 @@ class PlayerDB(TypedObject, AbstractUser):
# handlers # handlers
_SA(self, "cmdset", CmdSetHandler(self)) _SA(self, "cmdset", CmdSetHandler(self))
_GA(self, "cmdset").update(init_mode=True) _GA(self, "cmdset").update(init_mode=True)
_SA(self, "tags", TagHandler(self, "player")) _SA(self, "tags", TagHandler(self, category_prefix="player_"))
_SA(self, "aliases", AliasHandler(self, "player")) _SA(self, "aliases", AliasHandler(self, category_prefix="player_"))
_SA(self, "nicks", NickHandler(self, "player")) _SA(self, "nicks", NickHandler(self, category_prefix="player_"))
# Wrapper properties to easily set database fields. These are # Wrapper properties to easily set database fields. These are
# @property decorators that allows to access these fields using # @property decorators that allows to access these fields using

View file

@ -0,0 +1,146 @@
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding M2M table for field db_liteattributes on 'ScriptDB'
m2m_table_name = db.shorten_name(u'scripts_scriptdb_db_liteattributes')
db.create_table(m2m_table_name, (
('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
('scriptdb', models.ForeignKey(orm[u'scripts.scriptdb'], null=False)),
('liteattribute', models.ForeignKey(orm[u'typeclasses.liteattribute'], null=False))
))
db.create_unique(m2m_table_name, ['scriptdb_id', 'liteattribute_id'])
# Adding M2M table for field db_tags on 'ScriptDB'
m2m_table_name = db.shorten_name(u'scripts_scriptdb_db_tags')
db.create_table(m2m_table_name, (
('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
('scriptdb', models.ForeignKey(orm[u'scripts.scriptdb'], null=False)),
('tag', models.ForeignKey(orm[u'typeclasses.tag'], null=False))
))
db.create_unique(m2m_table_name, ['scriptdb_id', 'tag_id'])
def backwards(self, orm):
# Removing M2M table for field db_liteattributes on 'ScriptDB'
db.delete_table(db.shorten_name(u'scripts_scriptdb_db_liteattributes'))
# Removing M2M table for field db_tags on 'ScriptDB'
db.delete_table(db.shorten_name(u'scripts_scriptdb_db_tags'))
models = {
u'auth.group': {
'Meta': {'object_name': 'Group'},
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
},
u'auth.permission': {
'Meta': {'ordering': "(u'content_type__app_label', u'content_type__model', u'codename')", 'unique_together': "((u'content_type', u'codename'),)", 'object_name': 'Permission'},
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
u'contenttypes.contenttype': {
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
},
u'objects.objectdb': {
'Meta': {'object_name': 'ObjectDB'},
'db_attributes': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['typeclasses.Attribute']", 'null': 'True', 'symmetrical': 'False'}),
'db_cmdset_storage': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
'db_date_created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'db_destination': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'destinations_set'", 'null': 'True', 'to': u"orm['objects.ObjectDB']"}),
'db_home': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'homes_set'", 'null': 'True', 'to': u"orm['objects.ObjectDB']"}),
'db_key': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': 'True'}),
'db_liteattributes': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['typeclasses.LiteAttribute']", 'null': 'True', 'symmetrical': 'False'}),
'db_location': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'locations_set'", 'null': 'True', 'to': u"orm['objects.ObjectDB']"}),
'db_lock_storage': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'db_permissions': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
'db_player': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['players.PlayerDB']", 'null': 'True', 'blank': 'True'}),
'db_sessid': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
'db_tags': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['typeclasses.Tag']", 'null': 'True', 'symmetrical': 'False'}),
'db_typeclass_path': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
},
u'players.playerdb': {
'Meta': {'object_name': 'PlayerDB'},
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'db_attributes': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['typeclasses.Attribute']", 'null': 'True', 'symmetrical': 'False'}),
'db_cmdset_storage': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True'}),
'db_date_created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'db_is_connected': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'db_key': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': 'True'}),
'db_liteattributes': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['typeclasses.LiteAttribute']", 'null': 'True', 'symmetrical': 'False'}),
'db_lock_storage': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'db_permissions': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
'db_tags': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['typeclasses.Tag']", 'null': 'True', 'symmetrical': 'False'}),
'db_typeclass_path': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True'}),
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
},
u'scripts.scriptdb': {
'Meta': {'object_name': 'ScriptDB'},
'db_attributes': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['typeclasses.Attribute']", 'null': 'True', 'symmetrical': 'False'}),
'db_date_created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'db_desc': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
'db_interval': ('django.db.models.fields.IntegerField', [], {'default': '-1'}),
'db_is_active': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'db_key': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': 'True'}),
'db_liteattributes': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['typeclasses.LiteAttribute']", 'null': 'True', 'symmetrical': 'False'}),
'db_lock_storage': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'db_obj': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['objects.ObjectDB']", 'null': 'True', 'blank': 'True'}),
'db_permissions': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
'db_persistent': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'db_repeats': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
'db_start_delay': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'db_tags': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['typeclasses.Tag']", 'null': 'True', 'symmetrical': 'False'}),
'db_typeclass_path': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
},
u'typeclasses.attribute': {
'Meta': {'object_name': 'Attribute'},
'db_date_created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'db_key': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': 'True'}),
'db_lock_storage': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'db_value': ('src.utils.picklefield.PickledObjectField', [], {'null': 'True'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
},
u'typeclasses.liteattribute': {
'Meta': {'object_name': 'LiteAttribute', 'index_together': "(('db_key', 'db_category'),)"},
'db_category': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True', 'blank': 'True'}),
'db_data': ('django.db.models.fields.TextField', [], {}),
'db_key': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
},
u'typeclasses.tag': {
'Meta': {'unique_together': "(('db_key', 'db_category'),)", 'object_name': 'Tag', 'index_together': "(('db_key', 'db_category'),)"},
'db_category': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True'}),
'db_data': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
'db_key': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
}
}
complete_apps = ['scripts']

View file

@ -33,6 +33,7 @@ from django.contrib.contenttypes.models import ContentType
from src.scripts.manager import ScriptManager from src.scripts.manager import ScriptManager
__all__ = ("ScriptDB",) __all__ = ("ScriptDB",)
_SA = object.__setattr__
#------------------------------------------------------------ #------------------------------------------------------------
@ -106,8 +107,8 @@ class ScriptDB(TypedObject):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(ScriptDB, self).__init__(*args, **kwargs) super(ScriptDB, self).__init__(*args, **kwargs)
_SA(self, "tags", TagHandler(self, "script")) _SA(self, "tags", TagHandler(self, category_prefix="script_"))
_SA(self, "aliases", AliasHandler(self, "script")) _SA(self, "aliases", AliasHandler(self, category_prefix="script_"))
# Wrapper properties to easily set database fields. These are # Wrapper properties to easily set database fields. These are

View file

@ -150,18 +150,18 @@ class TagManager(models.Manager):
""" """
Extra manager methods for Tags Extra manager methods for Tags
""" """
def get_tags_on_obj(self, obj, search_key=None, category=None): def get_tags_on_obj(self, obj, key=None, category=None):
""" """
Get all tags on obj, optionally limited by key and/or category Get all tags on obj, optionally limited by key and/or category
""" """
if search_key or category: if key or category:
key_cands = Q(db_key__iexact=search_key.lower().strip()) if search_key!=None else Q() key_cands = Q(db_key__iexact=key.lower().strip()) if key!=None else Q()
cat_cands = Q(db_category__iexact=category.lower.strip()) if search_key!=None else Q() cat_cands = Q(db_category__iexact=category.lower.strip()) if key!=None else Q()
return _GA(obj, "db_tags").filter(cat_cands & key_cands) return _GA(obj, "db_tags").filter(cat_cands & key_cands)
else: else:
return list(_GA(obj, "db_tags").all()) return list(_GA(obj, "db_tags").all())
def get_tag(self, search_key=None, category=None): def get_tag(self, key=None, category=None):
""" """
Search and return all tags matching any combination of Search and return all tags matching any combination of
the search criteria. the search criteria.
@ -171,23 +171,23 @@ class TagManager(models.Manager):
Returns a single Tag (or None) if both key and category is given, otherwise Returns a single Tag (or None) if both key and category is given, otherwise
it will return a list. it will return a list.
""" """
key_cands = Q(db_key__iexact=search_key.lower().strip()) if search_key!=None else Q() key_cands = Q(db_key__iexact=key.lower().strip()) if key!=None else Q()
cat_cands = Q(db_category__iexact=category.lower.strip()) if search_key!=None else Q() cat_cands = Q(db_category__iexact=category.lower().strip()) if category!=None else Q()
tags = self.filter(key_cands & cat_cands) tags = self.filter(key_cands & cat_cands)
if search_key and category: if key and category:
return tags[0] if tags else None return tags[0] if tags else None
else: else:
return list(tags) return list(tags)
def get_objs_with_tag(self, objclass, search_key=None, category=None): def get_objs_with_tag(self, objclass, key=None, category=None):
""" """
Search and return all objects of objclass that has tags matching Search and return all objects of objclass that has tags matching
the given search criteria. the given search criteria.
objclass (dbmodel) - the object class to search objclass (dbmodel) - the object class to search
search_key (string) - the tag identifier key (string) - the tag identifier
category (string) - the tag category category (string) - the tag category
""" """
key_cands = Q(db_tags__db_key__iexact=search_key.lower().strip()) if search_key!=None else Q() key_cands = Q(db_tags__db_key__iexact=key.lower().strip()) if search_key!=None else Q()
cat_cands = Q(db_tags__db_category__iexact=category.lower().strip()) if category!=None else Q() cat_cands = Q(db_tags__db_category__iexact=category.lower().strip()) if category!=None else Q()
return objclass.objects.filter(key_cands & cat_cands) return objclass.objects.filter(key_cands & cat_cands)
@ -201,14 +201,14 @@ class TagManager(models.Manager):
""" """
data = str(data) if data!=None else None data = str(data) if data!=None else None
tag = self.get_tag(search_key=key, search_category=category) tag = self.get_tag(key=key, category=category)
if tag and data != None: if tag and data != None:
tag.db_data = data tag.db_data = data
tag.save() tag.save()
elif not tag: elif not tag:
tag = self.objects.create(db_key=key.lower().strip() if key!=None else None, tag = self.create(db_key=key.lower().strip() if key!=None else None,
db_category=category.lower().strip() if key!=None else None, db_category=category.lower().strip() if key!=None else None,
db_data=str(data) if data!=None else None) db_data=str(data) if data!=None else None)
tag.save() tag.save()
return tag return tag

View file

@ -390,7 +390,7 @@ class TagHandler(object):
def all(self): def all(self):
"Get all tags in this handler" "Get all tags in this handler"
return self.obj.db_tags.all().values_list("db_key") return [p[0] for p in self.obj.db_tags.all().values_list("db_key")]
def __str__(self): def __str__(self):
return ",".join(self.all()) return ",".join(self.all())
@ -415,9 +415,9 @@ class AliasHandler(object):
def add(self, alias): def add(self, alias):
"Add a new nick to the handler" "Add a new nick to the handler"
if not alias or not alias.strip():
return
for al in make_iter(alias): for al in make_iter(alias):
if not al or not al.strip():
continue
al = al.strip() al = al.strip()
# create a unique tag only if it didn't already exist # create a unique tag only if it didn't already exist
aliasobj = Tag.objects.create_tag(key=al, category=self.category) aliasobj = Tag.objects.create_tag(key=al, category=self.category)
@ -436,7 +436,7 @@ class AliasHandler(object):
def all(self): def all(self):
"Get all aliases in this handler" "Get all aliases in this handler"
return list(self.obj.db_tags.filter(db_category=self.category).values_list("db_key")) return [p[0] for p in self.obj.db_tags.filter(db_category=self.category).values_list("db_key")]
def __str__(self): def __str__(self):
return ",".join(self.all()) return ",".join(self.all())
@ -468,10 +468,10 @@ class NickHandler(object):
self.obj = obj self.obj = obj
self.prefix = "%snick_" % category_prefix.strip().lower() if category_prefix else "" self.prefix = "%snick_" % category_prefix.strip().lower() if category_prefix else ""
def add(self, nick, realname, nick_type="inputline"): def add(self, nick, realname, category="inputline"):
""" """
Assign a new nick for realname. Assign a new nick for realname.
nick_types used by Evennia are category used by Evennia are
'inputline', 'player', 'obj' and 'channel' 'inputline', 'player', 'obj' and 'channel'
""" """
if not nick or not nick.strip(): if not nick or not nick.strip():
@ -479,7 +479,7 @@ class NickHandler(object):
for nick in make_iter(nick): for nick in make_iter(nick):
nick = nick.strip() nick = nick.strip()
real = realname real = realname
nick_type = "%s%s" % (self.prefix, nick_type.strip().lower()) nick_type = "%s%s" % (self.prefix, category.strip().lower())
query = self.obj.db_liteattributes.filter(db_key__iexact=nick, db_category__iexact=nick_type) query = self.obj.db_liteattributes.filter(db_key__iexact=nick, db_category__iexact=nick_type)
if query.count(): if query.count():
old_nick = query[0] old_nick = query[0]
@ -490,21 +490,38 @@ class NickHandler(object):
new_nick.save() new_nick.save()
self.obj.db_liteattributes.add(new_nick) self.obj.db_liteattributes.add(new_nick)
def remove(self, nick, nick_type="inputline"): def remove(self, key, category="inputline"):
"Removes a previously stored nick" "Removes a previously stored nick"
for nick in make_iter(nick): for nick in make_iter(key):
nick = nick.strip() nick = nick.strip()
nick_type = "%s%s" % (self.prefix, nick_type.strip().lower()) nick_type = "%s%s" % (self.prefix, category.strip().lower())
query = self.obj.liteattributes.filter(db_key__iexact=nick, db_category__iexact=nick_type) query = self.obj.db_liteattributes.filter(db_key__iexact=nick, db_category__iexact=nick_type)
if query.count(): if query.count():
# remove the found nick(s) # remove the found nick(s)
query.delete() self.obj.db_liteattributes.remove(query[0])
def delete(self, *args, **kwargs): def delete(self, *args, **kwargs):
"alias wrapper" "alias wrapper"
self.remove(self, *args, **kwargs) self.remove(*args, **kwargs)
def get(self, nick=None, nick_type="inputline", default=None): def get(self, key=None, category="inputline"):
"""
Retrieves a given nick object based on the input key and category.
If no key is given, returns a list of all matching nick
objects (LiteAttributes) on the object, or the empty list.
"""
returns = []
for nick in make_iter(key):
nick = nick.strip().lower() if nick!=None else None
nick_type = "%s%s" % (self.prefix, category.strip().lower())
if nick:
nicks = _GA(self.obj, "db_liteattributes").filter(db_key=nick, db_category=nick_type)
return nicks[0] if nicks else None
else:
returns.extend(list(self.obj.db_liteattributes.all()))
return returns
def get_replace(self, key, category="inputline", default=None):
""" """
Retrieves a given nick replacement based on the input nick. If Retrieves a given nick replacement based on the input nick. If
given but no matching conversion was found, returns given but no matching conversion was found, returns
@ -513,20 +530,19 @@ class NickHandler(object):
objects (LiteAttributes) on the object, or the empty list. objects (LiteAttributes) on the object, or the empty list.
""" """
returns = [] returns = []
for nick in make_iter(nick): for nick in make_iter(key):
nick = nick.strip().lower() if nick!=None else None nick = nick.strip().lower() if nick!=None else None
nick_type = "%s%s" % (self.prefix, nick_type.strip().lower()) nick_type = "%s%s" % (self.prefix, category.strip().lower())
if nick: nicks = _GA(self.obj, "db_liteattributes").filter(db_key=nick, db_category=nick_type)
nicks = _GA(self.obj, "db_liteattributes").objects.filter(db_key=nick, db_category=nick_type).prefetch_related("db_data") default = default if default!=None else nick
default = default if default!=None else nick returns.append(nicks[0].db_data) if nicks else returns.append(default)
return nicks[0].db_data if nicks else default if len(returns) == 1:
else: return returns[0]
returns.extend(list(self.obj.db_liteattributes.all()))
return returns return returns
def all(self): def all(self):
"Get all nicks in this handler" "Get all nicks in this handler"
return list(self.obj.db_nicks.filter(db_category=self.category).values_list("db_key")) return [p[0] for p in self.obj.db_nicks.filter(db_category=self.category).values_list("db_key")]