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:
parent
b67faf45d9
commit
da008a69d9
4 changed files with 17 additions and 43 deletions
|
|
@ -9,4 +9,3 @@ Attribute and Tag models are defined along with their handlers.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
default_app_config = "evennia.typeclasses.apps.TypeclassesConfig"
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -46,6 +46,7 @@ from evennia.utils.utils import (
|
||||||
class_from_module)
|
class_from_module)
|
||||||
from evennia.utils.logger import log_trace
|
from evennia.utils.logger import log_trace
|
||||||
from evennia.typeclasses.django_new_patch import patched_new
|
from evennia.typeclasses.django_new_patch import patched_new
|
||||||
|
from .signals import remove_attributes_on_delete, post_save
|
||||||
|
|
||||||
__all__ = ("TypedObject", )
|
__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):
|
class TypeclassBase(SharedMemoryModelBase):
|
||||||
"""
|
"""
|
||||||
Metaclass which should be set for the root of model proxies
|
Metaclass which should be set for the root of model proxies
|
||||||
|
|
@ -107,9 +100,9 @@ class TypeclassBase(SharedMemoryModelBase):
|
||||||
# https://code.djangoproject.com/ticket/11560
|
# https://code.djangoproject.com/ticket/11560
|
||||||
new_class = patched_new(cls, name, bases, attrs)
|
new_class = patched_new(cls, name, bases, attrs)
|
||||||
|
|
||||||
# attach signal
|
# attach signals
|
||||||
signals.post_save.connect(post_save, sender=new_class)
|
signals.post_save.connect(post_save, sender=new_class)
|
||||||
|
signals.pre_delete.connect(remove_attributes_on_delete, sender=new_class)
|
||||||
return new_class
|
return new_class
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -606,7 +599,11 @@ class TypedObject(SharedMemoryModel):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
global TICKER_HANDLER
|
global TICKER_HANDLER
|
||||||
|
self.permissions.clear()
|
||||||
|
self.attributes.clear()
|
||||||
|
self.aliases.clear()
|
||||||
|
if hasattr(self, "nicks"):
|
||||||
|
self.nicks.clear()
|
||||||
# scrambling properties
|
# scrambling properties
|
||||||
self.delete = self._deleted
|
self.delete = self._deleted
|
||||||
super(TypedObject, self).delete()
|
super(TypedObject, self).delete()
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,16 @@
|
||||||
from .models import TypedObject
|
|
||||||
from django.db.models.signals import pre_delete
|
|
||||||
|
|
||||||
|
|
||||||
def get_subclasses(cls):
|
# signal receivers. Assigned in __new__
|
||||||
result = []
|
def post_save(sender, instance, created, **kwargs):
|
||||||
classes_to_inspect = [cls]
|
"""
|
||||||
while classes_to_inspect:
|
Receives a signal just after the object is saved.
|
||||||
class_to_inspect = classes_to_inspect.pop()
|
"""
|
||||||
for subclass in class_to_inspect.__subclasses__():
|
if created:
|
||||||
if subclass not in result:
|
instance.at_first_save()
|
||||||
result.append(subclass)
|
|
||||||
classes_to_inspect.append(subclass)
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def remove_attributes_on_delete(sender, instance, **kwargs):
|
def remove_attributes_on_delete(sender, instance, **kwargs):
|
||||||
print "remove_attribtes_on_delete called in instance %s" % instance
|
instance.db_attributes.all().delete()
|
||||||
instance.permissions.clear()
|
|
||||||
instance.attributes.clear()
|
|
||||||
instance.aliases.clear()
|
|
||||||
if hasattr(instance, "nicks"):
|
|
||||||
instance.nicks.clear()
|
|
||||||
|
|
||||||
|
|
||||||
for subclass in get_subclasses(TypedObject):
|
|
||||||
pre_delete.connect(remove_attributes_on_delete, subclass)
|
|
||||||
print "connected to subclass %s" % subclass
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue