From ae0b4bad555ebcb2cb68e4591d04a037071db3d1 Mon Sep 17 00:00:00 2001 From: Tehom Date: Mon, 5 Dec 2016 07:53:06 -0500 Subject: [PATCH] Refactor of class_setting from init in typeclass so we can just call it from django admin, and avoid weirdness of calling init twice, which seemed to mess things up --- evennia/objects/admin.py | 2 +- evennia/typeclasses/models.py | 70 ++++++++++++++++++----------------- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/evennia/objects/admin.py b/evennia/objects/admin.py index 3e0d75f6cb..f5a52ad5fd 100644 --- a/evennia/objects/admin.py +++ b/evennia/objects/admin.py @@ -148,7 +148,7 @@ class ObjectDBAdmin(admin.ModelAdmin): if not change: # adding a new object # have to call init with typeclass passed to it - obj.__init__(typeclass=obj.db_typeclass_path) + obj.set_class_from_typeclass(typeclass_path=obj.db_typeclass_path) obj.basetype_setup() obj.basetype_posthook_setup() obj.at_object_creation() diff --git a/evennia/typeclasses/models.py b/evennia/typeclasses/models.py index 77dd246bb4..d33d75a193 100644 --- a/evennia/typeclasses/models.py +++ b/evennia/typeclasses/models.py @@ -195,39 +195,7 @@ class TypedObject(SharedMemoryModel): # typeclass mechanism - def __init__(self, *args, **kwargs): - """ - The `__init__` method of typeclasses is the core operational - code of the typeclass system, where it dynamically re-applies - a class based on the db_typeclass_path database field rather - than use the one in the model. - - Args: - Passed through to parent. - - Kwargs: - Passed through to parent. - - Notes: - The loading mechanism will attempt the following steps: - - 1. Attempt to load typeclass given on command line - 2. Attempt to load typeclass stored in db_typeclass_path - 3. Attempt to load `__settingsclasspath__`, which is by the - default classes defined to be the respective user-set - base typeclass settings, like `BASE_OBJECT_TYPECLASS`. - 4. Attempt to load `__defaultclasspath__`, which is the - base classes in the library, like DefaultObject etc. - 5. If everything else fails, use the database model. - - Normal operation is to load successfully at either step 1 - or 2 depending on how the class was called. Tracebacks - will be logged for every step the loader must take beyond - 2. - - """ - typeclass_path = kwargs.pop("typeclass", None) - super(TypedObject, self).__init__(*args, **kwargs) + def set_class_from_typeclass(self, typeclass_path=None): if typeclass_path: try: self.__class__ = class_from_module(typeclass_path, defaultpaths=settings.TYPECLASS_PATHS) @@ -266,6 +234,42 @@ class TypedObject(SharedMemoryModel): self.db_typeclass_path = "evennia.objects.objects.DefaultObject" log_trace("Critical: Class %s of %s is not a valid typeclass!\nTemporarily falling back to %s." % (err_class, self, self.__class__)) + def __init__(self, *args, **kwargs): + """ + The `__init__` method of typeclasses is the core operational + code of the typeclass system, where it dynamically re-applies + a class based on the db_typeclass_path database field rather + than use the one in the model. + + Args: + Passed through to parent. + + Kwargs: + Passed through to parent. + + Notes: + The loading mechanism will attempt the following steps: + + 1. Attempt to load typeclass given on command line + 2. Attempt to load typeclass stored in db_typeclass_path + 3. Attempt to load `__settingsclasspath__`, which is by the + default classes defined to be the respective user-set + base typeclass settings, like `BASE_OBJECT_TYPECLASS`. + 4. Attempt to load `__defaultclasspath__`, which is the + base classes in the library, like DefaultObject etc. + 5. If everything else fails, use the database model. + + Normal operation is to load successfully at either step 1 + or 2 depending on how the class was called. Tracebacks + will be logged for every step the loader must take beyond + 2. + + """ + typeclass_path = kwargs.pop("typeclass", None) + super(TypedObject, self).__init__(*args, **kwargs) + self.set_class_from_typeclass(typeclass_path=typeclass_path) + + # initialize all handlers in a lazy fashion @lazy_property def attributes(self):