Skip to content

Commit 942b033

Browse files
committed
rename field to SelectURLField, add choices kwarg
1 parent 24827e3 commit 942b033

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

ixxy_url_field/fields.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,19 @@
1313

1414
from ixxy_url_field.choice_with_other import ChoiceWithOtherField
1515

16-
class IxxyURLField(models.CharField):
16+
class SelectURLField(models.CharField):
1717
description = _("URL")
1818

1919
def __init__(self, verbose_name=None, name=None, **kwargs):
2020
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+
2129
models.CharField.__init__(self, verbose_name, name, **kwargs)
2230
self.validators.append(IxxyURLValidator())
2331

@@ -27,11 +35,19 @@ def formfield(self, **kwargs):
2735
'form_class': IxxyURLFormField,
2836
}
2937
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()
3551
required = not self.blank
3652
return ChoiceWithOtherField(choices=choices, required=required)
3753

@@ -45,9 +61,10 @@ def to_python(self, value):
4561
domain_regex = re.compile(domain_pattern, re.IGNORECASE)
4662
#match = domain_regex.search(value)
4763
value = domain_regex.sub('', value)
48-
return super(IxxyURLField, self).to_python(value)
49-
64+
return super(SelectURLField, self).to_python(value)
5065

66+
#We need IxxyURLField so this is backwards compatible
67+
IxxyURLField = SelectURLField
5168

5269
class IxxyURLValidator(object):
5370
code = 'invalid'
@@ -74,5 +91,7 @@ def __init__(self, max_length=None, min_length=None, *args, **kwargs):
7491
super(IxxyURLFormField, self).__init__(max_length, min_length, *args, **kwargs)
7592
self.validators.append(IxxyURLValidator())
7693

94+
7795
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

Comments
 (0)