|
@@ -2,6 +2,10 @@
|
|
|
from django.db import models
|
|
|
from polymorphic import PolymorphicModel
|
|
|
from coin.offers.models import OfferSubscription
|
|
|
+from django.db.models.signals import post_save, post_delete
|
|
|
+from django.dispatch import receiver
|
|
|
+
|
|
|
+from coin.resources.models import IPSubnet
|
|
|
|
|
|
"""
|
|
|
Implementation note : Configuration is a PolymorphicModel.
|
|
@@ -57,3 +61,36 @@ class Configuration(PolymorphicModel):
|
|
|
return self.url_namespace
|
|
|
else:
|
|
|
return self.model_name().lower()
|
|
|
+
|
|
|
+
|
|
|
+@receiver(post_save, sender=IPSubnet)
|
|
|
+def subnet_save_event(sender, **kwargs):
|
|
|
+ """Fires when a subnet is saved (created/modified). We tell the
|
|
|
+ configuration backend to do whatever it needs to do with it.
|
|
|
+
|
|
|
+ We should use a pre_save signal, so that if anything goes wrong in the
|
|
|
+ backend (exception raised), nothing is actually saved in the database.
|
|
|
+ But it has a big problem: the configuration backend will not see the
|
|
|
+ change, since it has not been saved into the database yet.
|
|
|
+
|
|
|
+ That's why we use a post_save signal instead. But surprisingly, all
|
|
|
+ is well: if we raise an exception here, the IPSubnet object will not
|
|
|
+ be saved in the database. But the backend *does* see the new state of
|
|
|
+ the database. It looks like the database rollbacks if an exception is
|
|
|
+ raised. Whatever the reason, this is not a documented feature of
|
|
|
+ Django signals.
|
|
|
+ """
|
|
|
+ subnet = kwargs['instance']
|
|
|
+ config = subnet.configuration
|
|
|
+ if config:
|
|
|
+ config.save_subnet(subnet, kwargs['created'])
|
|
|
+
|
|
|
+@receiver(post_delete, sender=IPSubnet)
|
|
|
+def subnet_delete_event(sender, **kwargs):
|
|
|
+ """Fires when a subnet is deleted. We tell the configuration backend to
|
|
|
+ do whatever it needs to do with it.
|
|
|
+ """
|
|
|
+ subnet = kwargs['instance']
|
|
|
+ config = subnet.configuration
|
|
|
+ if config:
|
|
|
+ config.delete_subnet(subnet)
|