fields.py 3.39 KB
Newer Older
Őry Máté committed
1 2 3 4
from django.core.exceptions import ValidationError
from django.forms import fields
from django.db import models
from django.utils.translation import ugettext_lazy as _
5
from django.utils.ipv6 import is_valid_ipv6_address
Őry Máté committed
6 7 8
from south.modelsinspector import add_introspection_rules
import re

9
mac_re = re.compile(r'^([0-9a-fA-F]{2}(:|$)){6}$')
Őry Máté committed
10 11 12
alfanum_re = re.compile(r'^[A-Za-z0-9_-]+$')
domain_re = re.compile(r'^([A-Za-z0-9_-]\.?)+$')
ipv4_re = re.compile('^[0-9]+\.([0-9]+)\.([0-9]+)\.([0-9]+)$')
13
reverse_domain_re = re.compile(r'^(%\([abcd]\)d|[a-z0-9.-])+$')
Őry Máté committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

class MACAddressFormField(fields.RegexField):
    default_error_messages = {
        'invalid': _(u'Enter a valid MAC address.'),
    }

    def __init__(self, *args, **kwargs):
        super(MACAddressFormField, self).__init__(mac_re, *args, **kwargs)

class MACAddressField(models.Field):
    empty_strings_allowed = False
    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 17
        super(MACAddressField, self).__init__(*args, **kwargs)

    def get_internal_type(self):
30
        return 'CharField'
Őry Máté committed
31 32 33 34 35 36 37 38

    def formfield(self, **kwargs):
        defaults = {'form_class': MACAddressFormField}
        defaults.update(kwargs)
        return super(MACAddressField, self).formfield(**defaults)
add_introspection_rules([], ["firewall\.fields\.MACAddressField"])

def val_alfanum(value):
39
    """Validate whether the parameter is a valid alphanumeric value."""
40
    if not alfanum_re.match(value):
41 42 43 44 45 46
        raise ValidationError(_(u'%s - only letters, numbers, underscores '
            'and hyphens are allowed!') % value)

def is_valid_domain(value):
    """Check whether the parameter is a valid domain name."""
    return domain_re.match(value) is not None
Őry Máté committed
47 48

def val_domain(value):
49 50 51 52 53 54 55
    """Validate whether the parameter is a valid domin name."""
    if not is_valid_domain(value):
        raise ValidationError(_(u'%s - invalid domain name') % value)

def is_valid_reverse_domain(value):
    """Check whether the parameter is a valid reverse domain name."""
    return reverse_domain_re.match(value) is not None
Őry Máté committed
56

57
def val_reverse_domain(value):
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    """Validate whether the parameter is a valid reverse domain name."""
    if not is_valid_reverse_domain(value):
        raise ValidationError(u'%s - invalid reverse domain name' % value)

def is_valid_ipv4_address(value):
    """Check whether the parameter is a valid IPv4 address."""
    return ipv4_re.match(value) is not None

def val_ipv4(value):
    """Validate whether the parameter is a valid IPv4 address."""
    if not is_valid_ipv4_address(value):
        raise ValidationError(_(u'%s - not an IPv4 address') % value)

def val_ipv6(value):
    """Validate whether the parameter is a valid IPv6 address."""
    if not is_valid_ipv6_address(value):
        raise ValidationError(_(u'%s - not an IPv6 address') % value)
75

76 77 78 79 80 81 82 83 84 85 86
def val_mx(value):
    """Validate whether the parameter is a valid MX address definition.

    Expected form is <priority>:<hostname>.
    """
    mx = self.address.split(':', 1)
    if not (len(mx) == 2 and mx[0].isdigit() and
            domain_re.match(mx[1])):
        raise ValidationError(_("Bad MX address format. "
                                "Should be: <priority>:<hostname>"))

Őry Máté committed
87
def ipv4_2_ipv6(ipv4):
Dudás Ádám committed
88
    """Convert IPv4 address string to IPv6 address string."""
89
    val_ipv4(ipv4)
x committed
90
    m = ipv4_re.match(ipv4)
91 92
    return ("2001:738:2001:4031:%s:%s:%s:0" %
        (m.group(1), m.group(2), m.group(3)))