Figured out that proxy models do not call signals attached to their classes they proxy for. I used the metaclass that already had a post_save signal attached to it, and it works fine now! I took out the unnecessary apps.py now that we're no longer attaching a signal to the base model classes and just doing it to the proxies.

This commit is contained in:
Tehom 2017-02-03 15:13:35 -05:00 committed by Griatch
parent b67faf45d9
commit da008a69d9
4 changed files with 17 additions and 43 deletions

View file

@ -9,4 +9,3 @@ Attribute and Tag models are defined along with their handlers.
"""
default_app_config = "evennia.typeclasses.apps.TypeclassesConfig"

View file

@ -1,10 +0,0 @@
from __future__ import unicode_literals
from django.apps import AppConfig
class TypeclassesConfig(AppConfig):
name = 'typeclasses'
def ready(self):
from . import signals

View file

@ -46,6 +46,7 @@ from evennia.utils.utils import (
class_from_module)
from evennia.utils.logger import log_trace
from evennia.typeclasses.django_new_patch import patched_new
from .signals import remove_attributes_on_delete, post_save
__all__ = ("TypedObject", )
@ -68,14 +69,6 @@ _SA = object.__setattr__
#
# signal receivers. Assigned in __new__
def post_save(sender, instance, created, **kwargs):
"""
Receives a signal just after the object is saved.
"""
if created:
instance.at_first_save()
class TypeclassBase(SharedMemoryModelBase):
"""
Metaclass which should be set for the root of model proxies
@ -107,9 +100,9 @@ class TypeclassBase(SharedMemoryModelBase):
# https://code.djangoproject.com/ticket/11560
new_class = patched_new(cls, name, bases, attrs)
# attach signal
# attach signals
signals.post_save.connect(post_save, sender=new_class)
signals.pre_delete.connect(remove_attributes_on_delete, sender=new_class)
return new_class
@ -606,7 +599,11 @@ class TypedObject(SharedMemoryModel):
"""
global TICKER_HANDLER
self.permissions.clear()
self.attributes.clear()
self.aliases.clear()
if hasattr(self, "nicks"):
self.nicks.clear()
# scrambling properties
self.delete = self._deleted
super(TypedObject, self).delete()

View file

@ -1,28 +1,16 @@
from .models import TypedObject
from django.db.models.signals import pre_delete
def get_subclasses(cls):
result = []
classes_to_inspect = [cls]
while classes_to_inspect:
class_to_inspect = classes_to_inspect.pop()
for subclass in class_to_inspect.__subclasses__():
if subclass not in result:
result.append(subclass)
classes_to_inspect.append(subclass)
return result
# signal receivers. Assigned in __new__
def post_save(sender, instance, created, **kwargs):
"""
Receives a signal just after the object is saved.
"""
if created:
instance.at_first_save()
def remove_attributes_on_delete(sender, instance, **kwargs):
print "remove_attribtes_on_delete called in instance %s" % instance
instance.permissions.clear()
instance.attributes.clear()
instance.aliases.clear()
if hasattr(instance, "nicks"):
instance.nicks.clear()
instance.db_attributes.all().delete()
for subclass in get_subclasses(TypedObject):
pre_delete.connect(remove_attributes_on_delete, subclass)
print "connected to subclass %s" % subclass