1
1
from __future__ import unicode_literals
2
2
3
3
import json
4
-
5
4
from django import forms
6
5
from django .conf import settings
7
6
from django .contrib .contenttypes .models import ContentType
10
9
from django .template .defaultfilters import force_escape
11
10
from django .template .loader import render_to_string
12
11
from django .utils .encoding import force_text
12
+ from django .utils .module_loading import import_string
13
13
from django .utils .safestring import mark_safe
14
14
from django .utils .six import text_type
15
15
from django .utils .translation import ugettext as _
16
- from django .utils .module_loading import import_string
17
16
18
17
from ajax_select .registry import registry
19
18
23
22
# < django 1.10
24
23
from django .core .urlresolvers import reverse
25
24
26
-
27
25
as_default_help = 'Enter text to search.'
28
26
29
27
30
28
def _media (self ):
31
- # unless AJAX_SELECT_BOOTSTRAP == False
32
- # then load jquery and jquery ui + default css
33
- # where needed
34
- js = ( 'ajax_select/js/ bootstrap.js' , 'ajax_select/js/ajax_select.js' )
35
- try :
36
- if not settings . AJAX_SELECT_BOOTSTRAP :
37
- js = ( 'ajax_select/js/ajax_select.js' ,)
38
- except AttributeError :
39
- pass
29
+ js = [ 'admin/js/jquery.init.js' ]
30
+
31
+ # Unless AJAX_SELECT_BOOTSTRAP == False
32
+ # then load include bootstrap which will load jquery and jquery ui + default css as needed
33
+ if getattr ( settings , "AJAX_SELECT_BOOTSTRAP" , True ) :
34
+ js . append ( 'ajax_select/js/bootstrap.js' )
35
+
36
+ js . append ( 'ajax_select/js/ajax_select.js' )
37
+
40
38
return forms .Media (css = {'all' : ('ajax_select/css/ajax_select.css' ,)}, js = js )
41
39
42
40
@@ -48,7 +46,6 @@ def _media(self):
48
46
49
47
50
48
class AutoCompleteSelectWidget (forms .widgets .TextInput ):
51
-
52
49
"""
53
50
Widget to search for a model and return it as text for use in a CharField.
54
51
"""
@@ -106,7 +103,7 @@ def render(self, name, value, attrs=None, renderer=None, **_kwargs):
106
103
'add_link' : self .add_link ,
107
104
}
108
105
context .update (
109
- make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
106
+ make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
110
107
templates = (
111
108
'ajax_select/autocompleteselect_%s.html' % self .channel ,
112
109
'ajax_select/autocompleteselect.html' )
@@ -121,7 +118,6 @@ def id_for_label(self, id_):
121
118
122
119
123
120
class AutoCompleteSelectField (forms .fields .CharField ):
124
-
125
121
"""Form field to select a Model for a ForeignKey db field."""
126
122
127
123
channel = None
@@ -130,15 +126,15 @@ def __init__(self, channel, *args, **kwargs):
130
126
self .channel = channel
131
127
132
128
widget_kwargs = dict (
133
- channel = channel ,
134
- help_text = kwargs .get ('help_text' , _ (as_default_help )),
135
- show_help_text = kwargs .pop ('show_help_text' , True ),
136
- plugin_options = kwargs .pop ('plugin_options' , {})
129
+ channel = channel ,
130
+ help_text = kwargs .get ('help_text' , _ (as_default_help )),
131
+ show_help_text = kwargs .pop ('show_help_text' , True ),
132
+ plugin_options = kwargs .pop ('plugin_options' , {})
137
133
)
138
134
widget_kwargs .update (kwargs .pop ('widget_options' , {}))
139
135
kwargs ["widget" ] = AutoCompleteSelectWidget (** widget_kwargs )
140
136
super (AutoCompleteSelectField , self ).__init__ (
141
- max_length = 255 , * args , ** kwargs )
137
+ max_length = 255 , * args , ** kwargs )
142
138
143
139
def clean (self , value ):
144
140
if value :
@@ -150,7 +146,7 @@ def clean(self, value):
150
146
# out of the scope of this field to do anything more than
151
147
# tell you it doesn't exist
152
148
raise forms .ValidationError (
153
- "%s cannot find object: %s" % (lookup , value ))
149
+ "%s cannot find object: %s" % (lookup , value ))
154
150
return objs [0 ]
155
151
else :
156
152
if self .required :
@@ -171,7 +167,6 @@ def has_changed(self, initial, data):
171
167
172
168
173
169
class AutoCompleteSelectMultipleWidget (forms .widgets .SelectMultiple ):
174
-
175
170
"""
176
171
Widget to select multiple models for a ManyToMany db field.
177
172
"""
@@ -231,15 +226,15 @@ def render(self, name, value, attrs=None, renderer=None, **_kwargs):
231
226
'current' : value ,
232
227
'current_ids' : current_ids ,
233
228
'current_reprs' : mark_safe (
234
- json .dumps (initial , cls = json_encoder )
229
+ json .dumps (initial , cls = json_encoder )
235
230
),
236
231
'help_text' : help_text ,
237
232
'extra_attrs' : mark_safe (flatatt (final_attrs )),
238
233
'func_slug' : self .html_id .replace ("-" , "" ),
239
234
'add_link' : self .add_link ,
240
235
}
241
236
context .update (
242
- make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
237
+ make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
243
238
templates = ('ajax_select/autocompleteselectmultiple_%s.html' % self .channel ,
244
239
'ajax_select/autocompleteselectmultiple.html' )
245
240
out = render_to_string (templates , context )
@@ -254,7 +249,6 @@ def id_for_label(self, id_):
254
249
255
250
256
251
class AutoCompleteSelectMultipleField (forms .fields .CharField ):
257
-
258
252
"""
259
253
Form field to select multiple models for a ManyToMany db field.
260
254
"""
@@ -286,7 +280,7 @@ def __init__(self, channel, *args, **kwargs):
286
280
django_default_help = _ (dh ).translate (settings .LANGUAGE_CODE )
287
281
if django_default_help in translated :
288
282
cleaned_help = translated .replace (
289
- django_default_help , '' ).strip ()
283
+ django_default_help , '' ).strip ()
290
284
# probably will not show up in translations
291
285
if cleaned_help :
292
286
help_text = cleaned_help
@@ -327,11 +321,11 @@ def has_changed(self, initial_value, data_value):
327
321
dvs = [text_type (v ) for v in (data_value or [])]
328
322
return ivs != dvs
329
323
324
+
330
325
###############################################################################
331
326
332
327
333
328
class AutoCompleteWidget (forms .TextInput ):
334
-
335
329
"""
336
330
Widget to select a search result and enter the result as raw text in the
337
331
text input field. The user may also simply enter text and ignore any
@@ -376,14 +370,13 @@ def render(self, name, value, attrs=None, renderer=None, **_kwargs):
376
370
'func_slug' : self .html_id .replace ("-" , "" ),
377
371
}
378
372
context .update (
379
- make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
373
+ make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
380
374
templates = ('ajax_select/autocomplete_%s.html' % self .channel ,
381
375
'ajax_select/autocomplete.html' )
382
376
return mark_safe (render_to_string (templates , context ))
383
377
384
378
385
379
class AutoCompleteField (forms .CharField ):
386
-
387
380
"""
388
381
A CharField that uses an AutoCompleteWidget to lookup matching
389
382
and stores the result as plain text.
@@ -394,9 +387,9 @@ def __init__(self, channel, *args, **kwargs):
394
387
self .channel = channel
395
388
396
389
widget_kwargs = dict (
397
- help_text = kwargs .get ('help_text' , _ (as_default_help )),
398
- show_help_text = kwargs .pop ('show_help_text' , True ),
399
- plugin_options = kwargs .pop ('plugin_options' , {})
390
+ help_text = kwargs .get ('help_text' , _ (as_default_help )),
391
+ show_help_text = kwargs .pop ('show_help_text' , True ),
392
+ plugin_options = kwargs .pop ('plugin_options' , {})
400
393
)
401
394
widget_kwargs .update (kwargs .pop ('widget_options' , {}))
402
395
if 'attrs' in kwargs :
@@ -431,7 +424,7 @@ def _check_can_add(self, user, related_model):
431
424
app_label = related_model ._meta .app_label
432
425
model = related_model ._meta .object_name .lower ()
433
426
self .widget .add_link = reverse (
434
- 'admin:%s_%s_add' % (app_label , model )) + '?_popup=1'
427
+ 'admin:%s_%s_add' % (app_label , model )) + '?_popup=1'
435
428
436
429
437
430
def autoselect_fields_check_can_add (form , model , user ):
@@ -466,7 +459,7 @@ def make_plugin_options(lookup, channel_name, widget_plugin_options, initial):
466
459
return {
467
460
'plugin_options' : mark_safe (json .dumps (po , cls = json_encoder )),
468
461
'data_plugin_options' : force_escape (
469
- json .dumps (po , cls = json_encoder )
462
+ json .dumps (po , cls = json_encoder )
470
463
)
471
464
}
472
465
0 commit comments