Completed
Push — master ( da0207...5a5796 )
by Mathias
01:37
created

FSIntegerField.__init__()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 3
Ratio 100 %
Metric Value
dl 3
loc 3
rs 10
cc 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
# Initial code from parseltone -- parseltone.org
17
# Modify by Mathias WOLFF
18
19
from django.db import models
20
from django.core.validators import EMPTY_VALUES
21
from django.utils.encoding import smart_unicode
22
from django.utils.translation import ugettext_lazy as _
23
from switch import validators
24
25 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...
26
    """
27
    A field that will accept FreeSWITCH config file variables, or integers.
28
    
29
    Stores as string.
30
    """
31
    default_validators = [validators.validate_freeswitch_integer]
32
    default_error_messages = {
33
        'invalid': _('This value must be an integer or a FreeSWITCH variable.'),
34
    }
35
    description = _("Integer or FreeSWITCH variable")
36
37
    def __init__(self, *args, **kwargs):
38
        kwargs['max_length'] = kwargs.get('max_length', 64)
39
        super(FSIntegerField, self).__init__(*args, **kwargs)
40
41
    def to_python(self, value):
42
        try:
43
            value = smart_unicode(int(value))
44
        except (TypeError, ValueError):
45
            pass
46
        validators.validate_freeswitch_integer(value)
47
        return value
48
49
50
class FSIPAddressField(models.CharField):
51
    """
52
    Crude field that will accept CIDR network address values or 
53
    FreeSWITCH config file variables.
54
    
55
    Stores as string, so that null can be used.
56
    """
57
    default_validators = [validators.validate_freeswitch_ipaddress]
58
    default_error_messages = {
59
        'invalid': _('Enter a valid IPv4 address (dot-decimal or CIDR '
60
            'notation) or a FreeSWITCH variable.'),
61
    }
62
    description = _("IPv4 address, in dot-decimal or CIDR notation, or "
63
        "FreeSWITCH variable")
64
65
    def __init__(self, *args, **kwargs):
66
        kwargs['max_length'] = kwargs.get('max_length', 64)
67
        super(FSIPAddressField, self).__init__(*args, **kwargs)