11from __future__ import unicode_literals
22
33import json
4-
54from django import forms
65from django .conf import settings
76from django .contrib .contenttypes .models import ContentType
109from django .template .defaultfilters import force_escape
1110from django .template .loader import render_to_string
1211from django .utils .encoding import force_text
12+ from django .utils .module_loading import import_string
1313from django .utils .safestring import mark_safe
1414from django .utils .six import text_type
1515from django .utils .translation import ugettext as _
16- from django .utils .module_loading import import_string
1716
1817from ajax_select .registry import registry
1918
2322 # < django 1.10
2423 from django .core .urlresolvers import reverse
2524
26-
2725as_default_help = 'Enter text to search.'
2826
2927
3028def _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+
4038 return forms .Media (css = {'all' : ('ajax_select/css/ajax_select.css' ,)}, js = js )
4139
4240
@@ -48,7 +46,6 @@ def _media(self):
4846
4947
5048class AutoCompleteSelectWidget (forms .widgets .TextInput ):
51-
5249 """
5350 Widget to search for a model and return it as text for use in a CharField.
5451 """
@@ -106,7 +103,7 @@ def render(self, name, value, attrs=None, renderer=None, **_kwargs):
106103 'add_link' : self .add_link ,
107104 }
108105 context .update (
109- make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
106+ make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
110107 templates = (
111108 'ajax_select/autocompleteselect_%s.html' % self .channel ,
112109 'ajax_select/autocompleteselect.html' )
@@ -121,7 +118,6 @@ def id_for_label(self, id_):
121118
122119
123120class AutoCompleteSelectField (forms .fields .CharField ):
124-
125121 """Form field to select a Model for a ForeignKey db field."""
126122
127123 channel = None
@@ -130,15 +126,15 @@ def __init__(self, channel, *args, **kwargs):
130126 self .channel = channel
131127
132128 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' , {})
137133 )
138134 widget_kwargs .update (kwargs .pop ('widget_options' , {}))
139135 kwargs ["widget" ] = AutoCompleteSelectWidget (** widget_kwargs )
140136 super (AutoCompleteSelectField , self ).__init__ (
141- max_length = 255 , * args , ** kwargs )
137+ max_length = 255 , * args , ** kwargs )
142138
143139 def clean (self , value ):
144140 if value :
@@ -150,7 +146,7 @@ def clean(self, value):
150146 # out of the scope of this field to do anything more than
151147 # tell you it doesn't exist
152148 raise forms .ValidationError (
153- "%s cannot find object: %s" % (lookup , value ))
149+ "%s cannot find object: %s" % (lookup , value ))
154150 return objs [0 ]
155151 else :
156152 if self .required :
@@ -171,7 +167,6 @@ def has_changed(self, initial, data):
171167
172168
173169class AutoCompleteSelectMultipleWidget (forms .widgets .SelectMultiple ):
174-
175170 """
176171 Widget to select multiple models for a ManyToMany db field.
177172 """
@@ -231,15 +226,15 @@ def render(self, name, value, attrs=None, renderer=None, **_kwargs):
231226 'current' : value ,
232227 'current_ids' : current_ids ,
233228 'current_reprs' : mark_safe (
234- json .dumps (initial , cls = json_encoder )
229+ json .dumps (initial , cls = json_encoder )
235230 ),
236231 'help_text' : help_text ,
237232 'extra_attrs' : mark_safe (flatatt (final_attrs )),
238233 'func_slug' : self .html_id .replace ("-" , "" ),
239234 'add_link' : self .add_link ,
240235 }
241236 context .update (
242- make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
237+ make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
243238 templates = ('ajax_select/autocompleteselectmultiple_%s.html' % self .channel ,
244239 'ajax_select/autocompleteselectmultiple.html' )
245240 out = render_to_string (templates , context )
@@ -254,7 +249,6 @@ def id_for_label(self, id_):
254249
255250
256251class AutoCompleteSelectMultipleField (forms .fields .CharField ):
257-
258252 """
259253 Form field to select multiple models for a ManyToMany db field.
260254 """
@@ -286,7 +280,7 @@ def __init__(self, channel, *args, **kwargs):
286280 django_default_help = _ (dh ).translate (settings .LANGUAGE_CODE )
287281 if django_default_help in translated :
288282 cleaned_help = translated .replace (
289- django_default_help , '' ).strip ()
283+ django_default_help , '' ).strip ()
290284 # probably will not show up in translations
291285 if cleaned_help :
292286 help_text = cleaned_help
@@ -327,11 +321,11 @@ def has_changed(self, initial_value, data_value):
327321 dvs = [text_type (v ) for v in (data_value or [])]
328322 return ivs != dvs
329323
324+
330325###############################################################################
331326
332327
333328class AutoCompleteWidget (forms .TextInput ):
334-
335329 """
336330 Widget to select a search result and enter the result as raw text in the
337331 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):
376370 'func_slug' : self .html_id .replace ("-" , "" ),
377371 }
378372 context .update (
379- make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
373+ make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
380374 templates = ('ajax_select/autocomplete_%s.html' % self .channel ,
381375 'ajax_select/autocomplete.html' )
382376 return mark_safe (render_to_string (templates , context ))
383377
384378
385379class AutoCompleteField (forms .CharField ):
386-
387380 """
388381 A CharField that uses an AutoCompleteWidget to lookup matching
389382 and stores the result as plain text.
@@ -394,9 +387,9 @@ def __init__(self, channel, *args, **kwargs):
394387 self .channel = channel
395388
396389 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' , {})
400393 )
401394 widget_kwargs .update (kwargs .pop ('widget_options' , {}))
402395 if 'attrs' in kwargs :
@@ -431,7 +424,7 @@ def _check_can_add(self, user, related_model):
431424 app_label = related_model ._meta .app_label
432425 model = related_model ._meta .object_name .lower ()
433426 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'
435428
436429
437430def autoselect_fields_check_can_add (form , model , user ):
@@ -466,7 +459,7 @@ def make_plugin_options(lookup, channel_name, widget_plugin_options, initial):
466459 return {
467460 'plugin_options' : mark_safe (json .dumps (po , cls = json_encoder )),
468461 'data_plugin_options' : force_escape (
469- json .dumps (po , cls = json_encoder )
462+ json .dumps (po , cls = json_encoder )
470463 )
471464 }
472465
0 commit comments