FSIPAddressField   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
dl 0
loc 18
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 3 1
1
# Copyright 2013 Mathias WOLFF
2
# This file is part of pyfreebilling.
3
# 
4
# pyfreebilling is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
# 
9
# pyfreebilling is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
# 
14
# You should have received a copy of the GNU General Public License
15
# along with pyfreebilling.  If not, see <http://www.gnu.org/licenses/>
16
17
from django.db import models
18
from django.core.validators import EMPTY_VALUES
19
from django.utils.encoding import smart_unicode
20
from django.utils.translation import ugettext_lazy as _
21
from .validators import *
22
23
24 View Code Duplication
class FSIntegerField(models.CharField):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
25
    """
26
    A field that will accept FreeSWITCH config file variables, or integers.
27
28
    Stores as string.
29
    """
30
    default_validators = [validators.validate_freeswitch_integer]
31
    default_error_messages = {
32
        'invalid': _('This value must be an integer or a FreeSWITCH variable.'),
33
    }
34
    description = _("Integer or FreeSWITCH variable")
35
36
    def __init__(self, *args, **kwargs):
37
        kwargs['max_length'] = kwargs.get('max_length', 64)
38
        super(FSIntegerField, self).__init__(*args, **kwargs)
39
40
    def to_python(self, value):
41
        try:
42
            value = smart_unicode(int(value))
43
        except (TypeError, ValueError):
44
            pass
45
        validators.validate_freeswitch_integer(value)
46
        return value
47
48
49
class FSIPAddressField(models.CharField):
50
    """
51
    Crude field that will accept CIDR network address values or
52
    FreeSWITCH config file variables.
53
54
    Stores as string, so that null can be used.
55
    """
56
    default_validators = [validators.validate_freeswitch_ipaddress]
57
    default_error_messages = {
58
        'invalid': _('Enter a valid IPv4 address (dot-decimal or CIDR '
59
            'notation) or a FreeSWITCH variable.'),
60
    }
61
    description = _("IPv4 address, in dot-decimal or CIDR notation, or "
62
        "FreeSWITCH variable")
63
64
    def __init__(self, *args, **kwargs):
65
        kwargs['max_length'] = kwargs.get('max_length', 64)
66
        super(FSIPAddressField, self).__init__(*args, **kwargs)
67