13
13
14
14
from ixxy_url_field .choice_with_other import ChoiceWithOtherField
15
15
16
- class IxxyURLField (models .CharField ):
16
+ class SelectURLField (models .CharField ):
17
17
description = _ ("URL" )
18
18
19
19
def __init__ (self , verbose_name = None , name = None , ** kwargs ):
20
20
kwargs ['max_length' ] = kwargs .get ('max_length' , 200 )
21
+ # handle choices option:
22
+ # from custom_site.url_choices import get_url_choices
23
+ # link = SelectURLField(blank=True, choices=get_url_choices)
24
+ self ._has_choices = False
25
+ if 'choices' in kwargs :
26
+ self ._has_choices = True
27
+ self ._url_choices = kwargs .pop ('choices' )
28
+
21
29
models .CharField .__init__ (self , verbose_name , name , ** kwargs )
22
30
self .validators .append (IxxyURLValidator ())
23
31
@@ -27,11 +35,19 @@ def formfield(self, **kwargs):
27
35
'form_class' : IxxyURLFormField ,
28
36
}
29
37
defaults .update (kwargs )
30
- from django .conf import settings
31
- mod_path , func_name = settings .URL_CHOICES_FUNC .rsplit ('.' , 1 )
32
- mod = importlib .import_module (mod_path )
33
- choices_func = getattr (mod , func_name )
34
- choices = choices_func ()
38
+ ## when choices given, use them.
39
+ ## when not, use global settings.
40
+ if self ._has_choices :
41
+ if callable (self ._url_choices ):
42
+ choices = self ._url_choices ()
43
+ else :
44
+ choices = self ._url_choices
45
+ else :
46
+ from django .conf import settings
47
+ mod_path , func_name = settings .URL_CHOICES_FUNC .rsplit ('.' , 1 )
48
+ mod = importlib .import_module (mod_path )
49
+ choices_func = getattr (mod , func_name )
50
+ choices = choices_func ()
35
51
required = not self .blank
36
52
return ChoiceWithOtherField (choices = choices , required = required )
37
53
@@ -45,9 +61,10 @@ def to_python(self, value):
45
61
domain_regex = re .compile (domain_pattern , re .IGNORECASE )
46
62
#match = domain_regex.search(value)
47
63
value = domain_regex .sub ('' , value )
48
- return super (IxxyURLField , self ).to_python (value )
49
-
64
+ return super (SelectURLField , self ).to_python (value )
50
65
66
+ #We need IxxyURLField so this is backwards compatible
67
+ IxxyURLField = SelectURLField
51
68
52
69
class IxxyURLValidator (object ):
53
70
code = 'invalid'
@@ -74,5 +91,7 @@ def __init__(self, max_length=None, min_length=None, *args, **kwargs):
74
91
super (IxxyURLFormField , self ).__init__ (max_length , min_length , * args , ** kwargs )
75
92
self .validators .append (IxxyURLValidator ())
76
93
94
+
77
95
from south .modelsinspector import add_introspection_rules
78
- add_introspection_rules ([], ["^ixxy_url_field\.fields\.IxxyURLField" ])
96
+ add_introspection_rules ([], ["^ixxy_url_field\.fields\.IxxyURLField" ])
97
+ add_introspection_rules ([], ["^ixxy_url_field\.fields\.SelectURLField" ])
0 commit comments