/
ipaddressfield.py
51 lines (36 loc) · 1.36 KB
/
ipaddressfield.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from django.db import models
from django.utils.translation import ugettext as _
import iptools
class IPAddressField(models.Field):
__metaclass__ = models.SubfieldBase
description = _("Real IP Address field")
empty_strings_allowed = False
default_error_messages = {
'invalid': _('Please enter a valid IP Address (x.x.x.x)'),
}
def get_internal_type(self):
return "IntegerField"
def to_python(self, value):
if value is None:
return value
elif isinstance(value, str) or isinstance(value, unicode):
return value
elif isinstance(value, int) or isinstance(value, long):
return iptools.long2ip(value)
assert False, "This should never happen"
def get_db_prep_value(self, value, connection, prepared=False):
if value is None:
return value
elif isinstance(value, str):
return iptools.ip2long(value)
elif isinstance(value, int) or isinstance(value, long):
return value
def get_prep_lookup(self, lookup_type, value):
if value is None:
return value
elif isinstance(value, str) or isinstance(value, unicode):
return iptools.ip2long(value)
elif isinstance(value, int):
return int
else:
assert False, "This should never happen"