from django.db.models import CharField, NOT_PROVIDED
from django.core.exceptions import ValidationError
from south.modelsinspector import add_introspection_rules

from cyder.cydhcp.validation import validate_mac


class MacAddrField(CharField):
    """A general purpose MAC address field
    This field holds a MAC address. clean() removes colons and hyphens from the
    field value, raising an exception if the value is invalid or empty.

    Arguments:

    dhcp_enabled (string): The name of another attribute (possibly a field) in
                           the model that holds a boolean specifying whether to
                           validate this MacAddrField; if not specified, always
                           validate.
    """

    def __init__(self, *args, **kwargs):
        if 'dhcp_enabled' in kwargs:
            self.dhcp_enabled = kwargs.pop('dhcp_enabled')
        else:
            self.dhcp_enabled = None # always validate

        kwargs['max_length'] = 17
        kwargs['blank'] = True

        super(MacAddrField, self).__init__(*args, **kwargs)

    def clean(self, value, model_instance):
        # [   always validate   ]  [             DHCP is enabled              ]
        if not self.dhcp_enabled or getattr(model_instance, self.dhcp_enabled):
            if value == '':
                raise ValidationError(
                    "This field is required when DHCP is enabled")
            value = value.lower().replace(':', '').replace('-', '')
            validate_mac(value)

        value = super(CharField, self).clean(value, model_instance)
        return value


add_introspection_rules([
    (
        [MacAddrField], # model
        [], # args
        {'dhcp_enabled': ('dhcp_enabled', {})}, # kwargs
    )
], [r'^cyder\.core\.fields\.MacAddrField'])

@@ -45,16 +45,30 @@
 PROVIDED
+, SubfieldBase
 %0Afrom dj
@@ -749,16 +749,50 @@
    %22%22%22%0A%0A
+    __metaclass__ = SubfieldBase%0A%0A
     def 
@@ -1129,42 +1129,317 @@
 def 
-clean(self, value, model_instance)
+get_prep_value(self, value):%0A        value = super(MacAddrField, self).get_prep_value(value)%0A        if value:%0A            value = value.lower().replace(':', '').replace('-', '')%0A        return value%0A%0A    def to_python(self, value):%0A        value = super(MacAddrField, self).to_python(value)%0A%0A        if value
 :%0A  
@@ -1448,79 +1448,286 @@
     
-# %5B
+ 
    
+v
 al
-ways validate
+ue = value.lower().replace(':', '').replace('-', '')%0A            validate_mac(value)%0A
    
-%5D
   
-%5B
        
-      DHCP is enabled              %5D
+value = reduce(lambda x,y: x + ':' + y,%0A                           (value%5Bi:i+2%5D for i in xrange(0, 12, 2)))%0A        return value%0A%0A    def clean(self, value, model_instance):
 %0A   
@@ -1730,24 +1730,26 @@
 %0A        if 
+((
 not self.dhc
@@ -1757,16 +1757,32 @@
 _enabled
+%0A               
  or geta
@@ -1819,17 +1819,17 @@
 enabled)
-:
+)
 %0A       
@@ -1829,26 +1829,31 @@
             
-if
+    and
  value == ''
@@ -1852,22 +1852,19 @@
 ue == ''
+)
 :%0A
-    
         
@@ -1890,20 +1890,16 @@
 nError(%0A
-    
         
@@ -1953,28 +1953,25 @@
 abled%22)%0A
-    
+%0A
         
 value = 
@@ -1966,122 +1966,27 @@
     
-value = value.lower().replace(':', '').replace('-', '')%0A            validate_mac(value)%0A%0A        value =
+return
  super(
-Cha
+MacAdd
 rFie
@@ -2024,37 +2024,16 @@
 stance)%0A
-        return value%0A
 %0A%0Aadd_in

Improve MacAddrField