Browse Source

Associate IP subnet to configuration instead of subscriptions

Conflicts:
	coin/vpn/templates/vpn/vpn.html
Baptiste Jonglez 10 years ago
parent
commit
71b373cd5b

+ 37 - 0
coin/configuration/models.py

@@ -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)

+ 0 - 37
coin/offers/models.py

@@ -2,10 +2,6 @@
 import datetime
 
 from django.db import models
-from django.db.models.signals import post_save, post_delete
-from django.dispatch import receiver
-
-from coin.resources.models import IPSubnet
 
 
 class Offer(models.Model):
@@ -100,36 +96,3 @@ class OfferSubscription(models.Model):
     class Meta:
         verbose_name = 'abonnement'
 
-
-@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.offer_subscription.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.offer_subscription.configuration
-    if config:
-        config.delete_subnet(subnet)

+ 25 - 0
coin/resources/migrations/0003_auto_20140920_1615.py

@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('configuration', '0002_auto_20140919_2158'),
+        ('resources', '0002_auto_20140919_2158'),
+    ]
+
+    operations = [
+        migrations.RemoveField(
+            model_name='ipsubnet',
+            name='offer_subscription',
+        ),
+        migrations.AddField(
+            model_name='ipsubnet',
+            name='configuration',
+            field=models.ForeignKey(related_name=b'ip_subnet', default=None, to='configuration.Configuration'),
+            preserve_default=False,
+        ),
+    ]

+ 2 - 3
coin/resources/models.py

@@ -57,9 +57,8 @@ class IPSubnet(models.Model):
                             help_text="Leave empty for automatic allocation")
     objects = NetManager()
     ip_pool = models.ForeignKey(IPPool, verbose_name="IP pool")
-    offer_subscription = models.ForeignKey('offers.OfferSubscription',
-                                           related_name='ip_subnet',
-                                           verbose_name="subscription")
+    configuration = models.ForeignKey('configuration.Configuration',
+                                      related_name='ip_subnet')
     delegate_reverse_dns = models.BooleanField(default=False)
     name_server = models.ManyToManyField('reverse_dns.NameServer',
                                          blank=True,

+ 3 - 3
coin/vpn/models.py

@@ -54,7 +54,7 @@ class VPNConfiguration(CoinLdapSyncMixin, Configuration):
         self.save_subnet(subnet, False)
 
     def get_subnets(self, version):
-        subnets = self.offersubscription.ip_subnet.all()
+        subnets = self.ip_subnet.all()
         return [subnet for subnet in subnets if subnet.inet.version == version]
 
     def sync_to_ldap(self, creation, *args, **kwargs):
@@ -85,7 +85,7 @@ class VPNConfiguration(CoinLdapSyncMixin, Configuration):
         TODO: this should be factored for other technologies (DSL, etc)
 
         """
-        subnets = self.offersubscription.ip_subnet.all()
+        subnets = self.ip_subnet.all()
         updated = False
         if v4 and self.ipv4_endpoint is None:
             subnets_v4 = [s for s in subnets if s.inet.version == 4]
@@ -109,7 +109,7 @@ class VPNConfiguration(CoinLdapSyncMixin, Configuration):
         If [delete] is True, then simply delete the faulty endpoints
         instead of raising an exception.
         """
-        subnets = self.offersubscription.ip_subnet.all()
+        subnets = self.ip_subnet.all()
         is_faulty = lambda endpoint : endpoint and not any([endpoint in subnet.inet for subnet in subnets])
         if is_faulty(self.ipv4_endpoint):
             if delete:

+ 1 - 1
coin/vpn/templates/vpn/vpn.html

@@ -50,7 +50,7 @@
                 <tr>
                     <td class="center"><span class="label">Sous-réseaux</span></td>
                     <td>
-                        {% for subnet in object.administrative_subscription.ip_subnet.all %}{{ subnet }}<br/>{% endfor %}
+                        {% for subnet in object.ip_subnet.all %}{{ subnet }}<br/>{% endfor %}
                     </td>
                 </tr>
               <tr><td class="center" colspan="2"><input type="submit" value="Valider" /></td></tr>

+ 2 - 4
coin/vpn/tests.py

@@ -60,8 +60,7 @@ class VPNTestCase(TestCase):
         """If there is not endpoint, we consider it to be correct."""
         vpn = VPNConfiguration.objects.all()[0]
         if vpn.ipv4_endpoint is not None:
-            abo = vpn.offersubscription
-            subnet = abo.ip_subnet.get(ip_pool=self.v4_pool)
+            subnet = vpn.ip_subnet.get(ip_pool=self.v4_pool)
             self.assertIn(vpn.ipv4_endpoint, subnet.inet)
 
     def test_has_ipv6_endpoint(self):
@@ -72,8 +71,7 @@ class VPNTestCase(TestCase):
         """If there is not endpoint, we consider it to be correct."""
         vpn = VPNConfiguration.objects.all()[0]
         if vpn.ipv6_endpoint is not None:
-            abo = vpn.offersubscription
-            subnet = abo.ip_subnet.get(ip_pool=self.v6_pool)
+            subnet = vpn.ip_subnet.get(ip_pool=self.v6_pool)
             self.assertIn(vpn.ipv6_endpoint, subnet.inet)
 
     def test_change_v4subnet_is_vpn_endpoint_correct(self):