Browse Source

Refactor common operations

Baptiste Jonglez 11 years ago
parent
commit
6916a2b93a
3 changed files with 23 additions and 20 deletions
  1. 4 0
      coin/utils.py
  2. 12 0
      coin/validation.py
  3. 7 20
      coin/vpn/models.py

+ 4 - 0
coin/utils.py

@@ -5,6 +5,10 @@ import binascii
 import base64
 
 
+def str_or_none(obj):
+    return str(obj) if obj else None
+
+
 def ldap_hash(password):
     """Hash a password for use with LDAP.  If the password is already hashed,
     do nothing."""

+ 12 - 0
coin/validation.py

@@ -0,0 +1,12 @@
+# -*- coding: utf-8 -*-
+from django.core.exceptions import ValidationError
+
+
+def validate_v4(address):
+    if address.version != 4:
+        raise ValidationError('{} is not an IPv4 address'.format(address))
+
+
+def validate_v6(address):
+    if address.version != 6:
+        raise ValidationError('{} is not an IPv6 address'.format(address))

+ 7 - 20
coin/vpn/models.py

@@ -8,20 +8,7 @@ from ldapdb.models.fields import CharField, ListField
 from coin.models import CoinLdapSyncModel
 from coin.offers.models import OfferSubscription
 from coin import utils
-
-
-def validate_v4(address):
-    if address.version != 4:
-        raise ValidationError('{} is not an IPv4 address'.format(address))
-
-
-def validate_v6(address):
-    if address.version != 6:
-        raise ValidationError('{} is not an IPv6 address'.format(address))
-
-
-def str_or_none(obj):
-    return str(obj) if obj else None
+from coin import validation
 
 
 def validate_backend_type(subscription):
@@ -58,10 +45,10 @@ class VPNSubscription(CoinLdapSyncModel):
     activated = models.BooleanField(default=False)
     login = models.CharField(max_length=50, unique=True)
     password = models.CharField(max_length=256)
-    ipv4_endpoint = InetAddressField(validators=[validate_v4], blank=True,
-                                     null=True)
-    ipv6_endpoint = InetAddressField(validators=[validate_v6], blank=True,
-                                     null=True)
+    ipv4_endpoint = InetAddressField(validators=[validation.validate_v4],
+                                     blank=True, null=True)
+    ipv6_endpoint = InetAddressField(validators=[validation.validate_v6],
+                                     blank=True, null=True)
     comment = models.CharField(blank=True, max_length=512)
 
     objects = NetManager()
@@ -78,8 +65,8 @@ class VPNSubscription(CoinLdapSyncModel):
         config.login = config.sn = self.login
         config.password = self.password
         config.active = 'yes' if self.activated else 'no'
-        config.ipv4_endpoint = str_or_none(self.ipv4_endpoint)
-        config.ipv6_endpoint = str_or_none(self.ipv6_endpoint)
+        config.ipv4_endpoint = utils.str_or_none(self.ipv4_endpoint)
+        config.ipv6_endpoint = utils.str_or_none(self.ipv6_endpoint)
         config.ranges_v4 = [str(s) for s in self.get_subnets(4)]
         config.ranges_v6 = [str(s) for s in self.get_subnets(6)]
         config.save()