Parcourir la source

Working logs for IP allocations

Alexandre Aubin il y a 7 ans
Parent
commit
5d3eeabea1
1 fichiers modifiés avec 53 ajouts et 4 suppressions
  1. 53 4
      coin/configuration/models.py

+ 53 - 4
coin/configuration/models.py

@@ -1,6 +1,8 @@
 # -*- coding: utf-8 -*-
 from __future__ import unicode_literals
 
+import logging
+
 from django.db import models
 from polymorphic import PolymorphicModel
 from coin.offers.models import OfferSubscription
@@ -17,7 +19,7 @@ technical informations of a subscription.
 
 To add a new configuration backend, you have to create a new app with a model
 which inherit from Configuration.
-Your model can implement Meta verbose_name to have human readable name and a 
+Your model can implement Meta verbose_name to have human readable name and a
 url_namespace variable to specify the url namespace used by this model.
 """
 
@@ -35,9 +37,9 @@ class Configuration(PolymorphicModel):
         Génère automatiquement la liste de choix possibles de configurations
         en fonction des classes enfants de Configuration
         """
-        return tuple((x().__class__.__name__,x()._meta.verbose_name) 
+        return tuple((x().__class__.__name__,x()._meta.verbose_name)
             for x in Configuration.__subclasses__())
-    
+
     def model_name(self):
         return self.__class__.__name__
     model_name.short_description = 'Nom du modèle'
@@ -52,7 +54,7 @@ class Configuration(PolymorphicModel):
         Une url doit être nommée "details"
         """
         from django.core.urlresolvers import reverse
-        return reverse('%s:details' % self.get_url_namespace(), 
+        return reverse('%s:details' % self.get_url_namespace(),
                        args=[str(self.id)])
 
     def get_url_namespace(self):
@@ -70,8 +72,38 @@ class Configuration(PolymorphicModel):
         verbose_name = 'configuration'
 
 
+@receiver(post_save, sender=OfferSubscription)
+def offer_subscription_event(sender, **kwargs):
+    os = kwargs['instance']
+
+    if not hasattr(os, 'configuration'):
+        config_cls = None
+        for subconfig_cls in Configuration.__subclasses__():
+            if subconfig_cls().__class__.__name__ == os.offer.configuration_type:
+                config_cls = subconfig_cls
+                break
+
+        if config_cls is not None:
+            config = config_cls.objects.create(offersubscription=os)
+            for offer_ip_pool in os.offer.offerippool_set.order_by('-to_assign'):
+                IPSubnet.objects.create(
+                                configuration=config,
+                                ip_pool=offer_ip_pool.ip_pool)
+            config.save()
+
+
 @receiver(post_save, sender=IPSubnet)
+def subnet_event_save(sender, **kwargs):
+    kwargs["signal_type"] = "save"
+    subnet_event(sender, **kwargs)
+
+
 @receiver(post_delete, sender=IPSubnet)
+def subnet_event_delete(sender, **kwargs):
+    kwargs["signal_type"] = "delete"
+    subnet_event(sender, **kwargs)
+
+subnet_log = logging.getLogger("coin.subnets")
 def subnet_event(sender, **kwargs):
     """Fires when a subnet is created, modified or deleted.  We tell the
     configuration backend to do whatever it needs to do with it.
@@ -105,5 +137,22 @@ def subnet_event(sender, **kwargs):
         config = subnet.configuration
         if hasattr(config, 'subnet_event'):
             config.subnet_event()
+
+        offer = config.offersubscription.offer
+        member = config.offersubscription.member
+        ip = subnet.inet
+
+        if kwargs['signal_type'] == "save":
+            msg = "Allocating IP %s to member %s (%s - %s %s) for offer %s"
+        elif kwargs['signal_type'] == "delete":
+            msg = "Deallocating IP %s from member %s (%s - %s %s) (was offer %s)"
+        else:
+            # Does not happens
+            msg = ""
+
+        subnet_log.info(msg % (ip, str(member.pk),
+                        member.username, member.first_name, member.last_name,
+                        offer.name))
+
     except ObjectDoesNotExist:
         pass