import re
from django import forms


mac_pattern = re.compile("^[0-9a-f]{12}$")


class MacAddrFormField(forms.CharField):
    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 17
        super(MacAddrFormField, self).__init__(*args, **kwargs)

    def clean(self, value):
        value = super(MacAddrFormField, self).clean(value)

        value = filter(lambda x: x in "0123456789abcdef", value)

        if mac_pattern.match(value) is None:
            raise forms.ValidationError('Invalid MAC address')
        value = reduce(lambda x,y: x + ':' + y,
                       (value[i:i+2] for i in xrange(0, 12, 2)))

        return value

import re
from django import forms


mac_pattern = re.compile("^[0-9a-f]{12}$")


class MacAddrFormField(forms.CharField):
    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 17
        super(MacAddrFormField, self).__init__(*args, **kwargs)

    def clean(self, value):
        value = super(MacAddrFormField, self).clean(value)

        value = value.lower().replace(':', '').replace('-', '')
        if mac_pattern.match(value) is None:
            raise forms.ValidationError('Invalid MAC address')
        value = reduce(lambda x,y: x + ':' + y,
                       (value[i:i+2] for i in xrange(0, 12, 2)))

        return value

Revert "Properly handle non-hex characters in MAC"
Revert "Properly handle non-hex characters in MAC"

This reverts commit 2734a3f0212c722fb9fe3698dfea0dbd8a14faa7.

  import re
  from django import forms
  
  
  mac_pattern = re.compile("^[0-9a-f]{12}$")
  
  
  class MacAddrFormField(forms.CharField):
      def __init__(self, *args, **kwargs):
          kwargs['max_length'] = 17
          super(MacAddrFormField, self).__init__(*args, **kwargs)
  
      def clean(self, value):
          value = super(MacAddrFormField, self).clean(value)
  
+         value = value.lower().replace(':', '').replace('-', '')
-         value = filter(lambda x: x in "0123456789abcdef", value)
- 
          if mac_pattern.match(value) is None:
              raise forms.ValidationError('Invalid MAC address')
          value = reduce(lambda x,y: x + ':' + y,
                         (value[i:i+2] for i in xrange(0, 12, 2)))
  
          return value
  
Revert "Properly handle non-hex characters in MAC"
## Code Before:
import re
from django import forms


mac_pattern = re.compile("^[0-9a-f]{12}$")


class MacAddrFormField(forms.CharField):
    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 17
        super(MacAddrFormField, self).__init__(*args, **kwargs)

    def clean(self, value):
        value = super(MacAddrFormField, self).clean(value)

        value = filter(lambda x: x in "0123456789abcdef", value)

        if mac_pattern.match(value) is None:
            raise forms.ValidationError('Invalid MAC address')
        value = reduce(lambda x,y: x + ':' + y,
                       (value[i:i+2] for i in xrange(0, 12, 2)))

        return value

## Instruction:
Revert "Properly handle non-hex characters in MAC"
## Code After:
import re
from django import forms


mac_pattern = re.compile("^[0-9a-f]{12}$")


class MacAddrFormField(forms.CharField):
    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 17
        super(MacAddrFormField, self).__init__(*args, **kwargs)

    def clean(self, value):
        value = super(MacAddrFormField, self).clean(value)

        value = value.lower().replace(':', '').replace('-', '')
        if mac_pattern.match(value) is None:
            raise forms.ValidationError('Invalid MAC address')
        value = reduce(lambda x,y: x + ':' + y,
                       (value[i:i+2] for i in xrange(0, 12, 2)))

        return value
