Parcourir la source

Working logs for IP allocations

Alexandre Aubin il y a 7 ans
Parent
commit
04b196f930
1 fichiers modifiés avec 29 ajouts et 7 suppressions
  1. 29 7
      coin/configuration/models.py

+ 29 - 7
coin/configuration/models.py

@@ -1,6 +1,8 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
 from __future__ import unicode_literals
 from __future__ import unicode_literals
 
 
+import logging
+
 from django.db import models
 from django.db import models
 from polymorphic import PolymorphicModel
 from polymorphic import PolymorphicModel
 from coin.offers.models import OfferSubscription
 from coin.offers.models import OfferSubscription
@@ -94,10 +96,19 @@ def offer_subscription_event(sender, **kwargs):
                                 ip_pool=offer_ip_pool.ip_pool)
                                 ip_pool=offer_ip_pool.ip_pool)
             config.save()
             config.save()
 
 
-subnet_log = logging.getLogger("coin.subnets")
 
 
 @receiver(post_save, sender=IPSubnet)
 @receiver(post_save, sender=IPSubnet)
+def subnet_event_save(sender, **kwargs):
+    kwargs["signal_type"] = "save"
+    subnet_event(sender, **kwargs)
+
+
 @receiver(post_delete, sender=IPSubnet)
 @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):
 def subnet_event(sender, **kwargs):
     """Fires when a subnet is created, modified or deleted.  We tell the
     """Fires when a subnet is created, modified or deleted.  We tell the
     configuration backend to do whatever it needs to do with it.
     configuration backend to do whatever it needs to do with it.
@@ -131,11 +142,22 @@ def subnet_event(sender, **kwargs):
         config = subnet.configuration
         config = subnet.configuration
         if hasattr(config, 'subnet_event'):
         if hasattr(config, 'subnet_event'):
             config.subnet_event()
             config.subnet_event()
-        subnet_log.info("zob")
-        subnet_log.info(sender)
-        subnet_log.info(conf)
-        subnet_log.info(conf.offersubscription)
-        subnet_log.info(conf.offersubscription.member)
-        subnet_log.info(subnet.inet)
+
+        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:
     except ObjectDoesNotExist:
         pass
         pass