diff --git a/src/comms/comms.py b/src/comms/comms.py index 5e62202c18..5f5d052d24 100644 --- a/src/comms/comms.py +++ b/src/comms/comms.py @@ -18,6 +18,34 @@ class Channel(ChannelDB): __metaclass__ = TypeclassBase objects = ChannelManager() + def at_first_save(self): + """ + Called by the typeclass system the very first time the channel + is saved to the database. Generally, don't overload this but + the hooks called by this method. + """ + self.at_channel_creation() + + if hasattr(self, "_createdict"): + # this is only set if the channel was created + # with the utils.create.create_channel function. + cdict = self._createdict + if not cdict["key"]: + self.db_key = "#i" % self.dbid + elif cdict["key"] and self.key != cdict["key"]: + self.key = cdict["key"] + if cdict["keep_log"]: + self.db_keep_log = cdict["keep_log"] + if cdict["aliases"]: + self.aliases.add(cdict["aliases"]) + if cdict["locks"]: + self.locks.add(cdict["locks"]) + if cdict["keep_log"]: + self.attributes.add("keep_log", cdict["keep_log"]) + if cdict["desc"]: + self.attributes.add("desc", cdict["desc"]) + + # helper methods, for easy overloading def channel_prefix(self, msg=None, emit=False): diff --git a/src/comms/models.py b/src/comms/models.py index fa822d81a9..69704ed31e 100644 --- a/src/comms/models.py +++ b/src/comms/models.py @@ -346,7 +346,6 @@ class ChannelDB(TypedObject): key - main name for channel desc - optional description of channel aliases - alternative names for the channel - keep_log - bool if the channel should remember messages permissions - perm strings """ diff --git a/src/utils/create.py b/src/utils/create.py index 92a428b116..53fd3a2bc0 100644 --- a/src/utils/create.py +++ b/src/utils/create.py @@ -271,33 +271,23 @@ def create_channel(key, aliases=None, desc=None, aliases - list of alternative (likely shorter) keynames. locks - lock string definitions """ - global _ChannelDB, _channelhandler - if not _ChannelDB: - from src.comms.models import ChannelDB as _ChannelDB - if not _channelhandler: - from src.comms import channelhandler as _channelhandler - if not typeclass: - typeclass = settings.BASE_CHANNEL_TYPECLASS - try: - new_channel = _ChannelDB(typeclass=typeclass, db_key=key) - new_channel.save() - new_channel = new_channel.typeclass - if aliases: - if not utils.is_iter(aliases): - aliases = [aliases] - new_channel.aliases.add(aliases) - new_channel.save() - new_channel.db.desc = desc - new_channel.db.keep_log = keep_log - except IntegrityError: - string = "Could not add channel: key '%s' already exists." % key - logger.log_errmsg(string) - return None - if locks: - new_channel.locks.add(locks) + typeclass = typeclass if typeclass else settings.BASE_CHANNEL_TYPECLASS + + if isinstance(typeclass, basestring): + # a path is given. Load the actual typeclass + typeclass = class_from_module(typeclass, settings.CHANNEL_TYPECLASS_PATHS) + + # create new instance + new_channel = typeclass(db_key=key) + + # store call signature for the signal + new_channel._createdict = {"key":key, "aliases":aliases, + "desc":desc, "locks":locks, "keep_log":keep_log} + + # this will trigger the save signal which in turn calls the + # at_first_save hook on the typeclass, where the _createdict can be + # used. new_channel.save() - _channelhandler.CHANNELHANDLER.add_channel(new_channel) - new_channel.at_channel_create() return new_channel channel = create_channel