summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2005-09-23 02:02:15 +0000
committerAdrian Holovaty <adrian@holovaty.com>2005-09-23 02:02:15 +0000
commit3d426607a8e04f79fb4ee4393e92c4838539bd23 (patch)
tree097f4971ae00703e1aca9656343eb3764d925ed0 /django
parent36fc73a45bc857f0ed1c1a5c6f8e61017709e73e (diff)
Fixed #264 -- Added django.core.validators.isValidIPAddress4. Thanks, Hugo and Simon
git-svn-id: http://code.djangoproject.com/svn/django/trunk@671 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/core/formfields.py11
-rw-r--r--django/core/validators.py8
2 files changed, 19 insertions, 0 deletions
diff --git a/django/core/formfields.py b/django/core/formfields.py
index 3bc315161f..5056dd7bad 100644
--- a/django/core/formfields.py
+++ b/django/core/formfields.py
@@ -687,6 +687,17 @@ class URLField(TextField):
raise validators.CriticalValidationError, e.messages
class IPAddressField(TextField):
+ def __init__(self, field_name, length=15, maxlength=15, is_required=False, validator_list=[]):
+ validator_list = [self.isValidIPAddress] + validator_list
+ TextField.__init__(self, field_name, length=length, maxlength=maxlength,
+ is_required=is_required, validator_list=validator_list)
+
+ def isValidIPAddress(self, field_data, all_data):
+ try:
+ validators.isValidIPAddress4(field_data, all_data)
+ except validators.ValidationError, e:
+ raise validators.CriticalValidationError, e.messages
+
def html2python(data):
return data or None
html2python = staticmethod(html2python)
diff --git a/django/core/validators.py b/django/core/validators.py
index ee440e1140..6714bfda3e 100644
--- a/django/core/validators.py
+++ b/django/core/validators.py
@@ -19,6 +19,7 @@ ansi_time_re = re.compile('^%s$' % _timere)
ansi_datetime_re = re.compile('^%s %s$' % (_datere, _timere))
email_re = re.compile(r'^[-\w.+]+@\w[\w.-]+$')
integer_re = re.compile(r'^-?\d+$')
+ip4_re = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')
phone_re = re.compile(r'^[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}$', re.IGNORECASE)
url_re = re.compile(r'^http://\S+$')
@@ -85,6 +86,13 @@ def isCommaSeparatedEmailList(field_data, all_data):
except ValidationError:
raise ValidationError, "Enter valid e-mail addresses separated by commas."
+def isValidIPAddress4(field_data, all_data):
+ if ip4_re.search(field_data):
+ valid_parts = [el for el in field_data.split('.') if 0 <= int(el) <= 255]
+ if len(valid_parts) == 4:
+ return
+ raise validators.ValidationError, "Please enter a valid IP address."
+
def isNotEmpty(field_data, all_data):
if field_data.strip() == '':
raise ValidationError, "Empty values are not allowed here."