1
1
import re
2
- import os .path
3
2
4
3
from django import forms
5
4
from django .core .exceptions import ValidationError
6
5
from django .core .validators import URLValidator
7
6
from django .db import models
8
- from django .db import IntegrityError
9
- from django .template .defaultfilters import filesizeformat
10
7
from django .utils .translation import ugettext_lazy as _
11
8
from django .utils .encoding import smart_unicode
12
- from django .utils import importlib
9
+ try :
10
+ from importlib import import_module
11
+ except ImportError :
12
+ # Django < 1.9 and Python < 2.7
13
+ from django .utils .importlib import import_module
13
14
14
15
from select_url_field .choice_with_other import ChoiceWithOtherField
15
16
@@ -18,7 +19,7 @@ class SelectURLField(models.CharField):
18
19
19
20
def __init__ (self , verbose_name = None , name = None , ** kwargs ):
20
21
kwargs ['max_length' ] = kwargs .get ('max_length' , 200 )
21
- # handle choices option:
22
+ # Handle choices option:
22
23
# from custom_site.url_choices import get_url_choices
23
24
# link = SelectURLField(blank=True, choices=get_url_choices)
24
25
self ._has_choices = False
@@ -35,8 +36,8 @@ def formfield(self, **kwargs):
35
36
'form_class' : SelectURLFormField ,
36
37
}
37
38
defaults .update (kwargs )
38
- ## when choices given, use them.
39
- ## when not, use global settings.
39
+ # When choices given, use them.
40
+ # When not, use global settings
40
41
if self ._has_choices :
41
42
if callable (self ._url_choices ):
42
43
choices = self ._url_choices ()
@@ -46,7 +47,7 @@ def formfield(self, **kwargs):
46
47
from django .conf import settings
47
48
mod_path , func_name = settings .URL_CHOICES_FUNC .rsplit ('.' , 1 )
48
49
mod = importlib .import_module (mod_path )
49
- choices_func = getattr (mod , func_name )
50
+ choices_func = getattr (mod , func_name )
50
51
choices = choices_func ()
51
52
required = not self .blank
52
53
return ChoiceWithOtherField (choices = choices , required = required )
@@ -59,11 +60,10 @@ def to_python(self, value):
59
60
if domain :
60
61
domain_pattern = r'^(?:http|ftp)s?://' + domain
61
62
domain_regex = re .compile (domain_pattern , re .IGNORECASE )
62
- #match = domain_regex.search(value)
63
63
value = domain_regex .sub ('' , value )
64
64
return super (SelectURLField , self ).to_python (value )
65
65
66
- #We need IxxyURLField so this is backwards compatible
66
+ # We need IxxyURLField so this is backwards compatible
67
67
IxxyURLField = SelectURLField
68
68
69
69
class SelectURLValidator (object ):
@@ -75,10 +75,10 @@ def __init__(self):
75
75
76
76
def __call__ (self , value ):
77
77
try :
78
- #OK if it's a valid url
78
+ # OK if it's a valid url
79
79
self .url_validator (value )
80
80
except ValidationError , e :
81
- #Not a valid url, see it's a path
81
+ # Not a valid url, see it's a path
82
82
if not self .regex .search (smart_unicode (value )):
83
83
raise e
84
84
@@ -91,7 +91,9 @@ def __init__(self, max_length=None, min_length=None, *args, **kwargs):
91
91
super (SelectURLFormField , self ).__init__ (max_length , min_length , * args , ** kwargs )
92
92
self .validators .append (SelectURLValidator ())
93
93
94
-
95
- from south .modelsinspector import add_introspection_rules
96
- add_introspection_rules ([], ["^select_url_field\.fields\.IxxyURLField" ])
97
- add_introspection_rules ([], ["^select_url_field\.fields\.SelectURLField" ])
94
+ try :
95
+ from south .modelsinspector import add_introspection_rules
96
+ add_introspection_rules ([], ["^select_url_field\.fields\.IxxyURLField" ])
97
+ add_introspection_rules ([], ["^select_url_field\.fields\.SelectURLField" ])
98
+ except ImportError :
99
+ pass
0 commit comments