12
12
# Django < 1.9 and Python < 2.7
13
13
from django .utils .importlib import import_module
14
14
15
+
15
16
from select_url_field .choice_with_other import ChoiceWithOtherField
16
17
18
+
17
19
class SelectURLField (models .CharField ):
18
20
description = _ ("URL" )
19
21
@@ -52,7 +54,6 @@ def formfield(self, **kwargs):
52
54
required = not self .blank
53
55
return ChoiceWithOtherField (choices = choices , required = required )
54
56
55
-
56
57
def to_python (self , value ):
57
58
from django .conf import settings
58
59
if value :
@@ -63,10 +64,13 @@ def to_python(self, value):
63
64
value = domain_regex .sub ('' , value )
64
65
return super (SelectURLField , self ).to_python (value )
65
66
67
+
66
68
# We need IxxyURLField so this is backwards compatible
67
69
IxxyURLField = SelectURLField
68
70
71
+
69
72
class SelectURLValidator (object ):
73
+
70
74
code = 'invalid'
71
75
regex = re .compile (r'(?:[/?]\S+)$' , re .IGNORECASE )
72
76
@@ -75,13 +79,14 @@ def __init__(self):
75
79
76
80
def __call__ (self , value ):
77
81
try :
78
- # OK if it's a valid url
82
+ # OK if it's a valid url
79
83
self .url_validator (value )
80
84
except ValidationError , e :
81
- # Not a valid url, see it's a path
85
+ # Not a valid url, see it's a path
82
86
if not self .regex .search (smart_unicode (value )):
83
87
raise e
84
88
89
+
85
90
class SelectURLFormField (forms .CharField ):
86
91
default_error_messages = {
87
92
'invalid' : _ (u'Enter a valid URL.' ),
@@ -91,6 +96,7 @@ def __init__(self, max_length=None, min_length=None, *args, **kwargs):
91
96
super (SelectURLFormField , self ).__init__ (max_length , min_length , * args , ** kwargs )
92
97
self .validators .append (SelectURLValidator ())
93
98
99
+
94
100
try :
95
101
from south .modelsinspector import add_introspection_rules
96
102
add_introspection_rules ([], ["^select_url_field\.fields\.IxxyURLField" ])
0 commit comments