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
@@ -39,6 +37,7 @@ def _media(self):
3937
4038 return forms .Media (css = {'all' : ('ajax_select/css/ajax_select.css' ,)}, js = js )
4139
40+
4241json_encoder = import_string (getattr (settings , 'AJAX_SELECT_JSON_ENCODER' ,
4342 'django.core.serializers.json.DjangoJSONEncoder' ))
4443
@@ -47,7 +46,6 @@ def _media(self):
4746
4847
4948class AutoCompleteSelectWidget (forms .widgets .TextInput ):
50-
5149 """
5250 Widget to search for a model and return it as text for use in a CharField.
5351 """
@@ -105,7 +103,7 @@ def render(self, name, value, attrs=None, renderer=None, **_kwargs):
105103 'add_link' : self .add_link ,
106104 }
107105 context .update (
108- make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
106+ make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
109107 templates = (
110108 'ajax_select/autocompleteselect_%s.html' % self .channel ,
111109 'ajax_select/autocompleteselect.html' )
@@ -120,7 +118,6 @@ def id_for_label(self, id_):
120118
121119
122120class AutoCompleteSelectField (forms .fields .CharField ):
123-
124121 """Form field to select a Model for a ForeignKey db field."""
125122
126123 channel = None
@@ -129,15 +126,15 @@ def __init__(self, channel, *args, **kwargs):
129126 self .channel = channel
130127
131128 widget_kwargs = dict (
132- channel = channel ,
133- help_text = kwargs .get ('help_text' , _ (as_default_help )),
134- show_help_text = kwargs .pop ('show_help_text' , True ),
135- 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' , {})
136133 )
137134 widget_kwargs .update (kwargs .pop ('widget_options' , {}))
138135 kwargs ["widget" ] = AutoCompleteSelectWidget (** widget_kwargs )
139136 super (AutoCompleteSelectField , self ).__init__ (
140- max_length = 255 , * args , ** kwargs )
137+ max_length = 255 , * args , ** kwargs )
141138
142139 def clean (self , value ):
143140 if value :
@@ -149,7 +146,7 @@ def clean(self, value):
149146 # out of the scope of this field to do anything more than
150147 # tell you it doesn't exist
151148 raise forms .ValidationError (
152- "%s cannot find object: %s" % (lookup , value ))
149+ "%s cannot find object: %s" % (lookup , value ))
153150 return objs [0 ]
154151 else :
155152 if self .required :
@@ -170,7 +167,6 @@ def has_changed(self, initial, data):
170167
171168
172169class AutoCompleteSelectMultipleWidget (forms .widgets .SelectMultiple ):
173-
174170 """
175171 Widget to select multiple models for a ManyToMany db field.
176172 """
@@ -230,15 +226,15 @@ def render(self, name, value, attrs=None, renderer=None, **_kwargs):
230226 'current' : value ,
231227 'current_ids' : current_ids ,
232228 'current_reprs' : mark_safe (
233- json .dumps (initial , cls = json_encoder )
229+ json .dumps (initial , cls = json_encoder )
234230 ),
235231 'help_text' : help_text ,
236232 'extra_attrs' : mark_safe (flatatt (final_attrs )),
237233 'func_slug' : self .html_id .replace ("-" , "" ),
238234 'add_link' : self .add_link ,
239235 }
240236 context .update (
241- make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
237+ make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
242238 templates = ('ajax_select/autocompleteselectmultiple_%s.html' % self .channel ,
243239 'ajax_select/autocompleteselectmultiple.html' )
244240 out = render_to_string (templates , context )
@@ -253,7 +249,6 @@ def id_for_label(self, id_):
253249
254250
255251class AutoCompleteSelectMultipleField (forms .fields .CharField ):
256-
257252 """
258253 Form field to select multiple models for a ManyToMany db field.
259254 """
@@ -285,7 +280,7 @@ def __init__(self, channel, *args, **kwargs):
285280 django_default_help = _ (dh ).translate (settings .LANGUAGE_CODE )
286281 if django_default_help in translated :
287282 cleaned_help = translated .replace (
288- django_default_help , '' ).strip ()
283+ django_default_help , '' ).strip ()
289284 # probably will not show up in translations
290285 if cleaned_help :
291286 help_text = cleaned_help
@@ -326,11 +321,11 @@ def has_changed(self, initial_value, data_value):
326321 dvs = [text_type (v ) for v in (data_value or [])]
327322 return ivs != dvs
328323
324+
329325###############################################################################
330326
331327
332328class AutoCompleteWidget (forms .TextInput ):
333-
334329 """
335330 Widget to select a search result and enter the result as raw text in the
336331 text input field. The user may also simply enter text and ignore any
@@ -375,14 +370,13 @@ def render(self, name, value, attrs=None, renderer=None, **_kwargs):
375370 'func_slug' : self .html_id .replace ("-" , "" ),
376371 }
377372 context .update (
378- make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
373+ make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
379374 templates = ('ajax_select/autocomplete_%s.html' % self .channel ,
380375 'ajax_select/autocomplete.html' )
381376 return mark_safe (render_to_string (templates , context ))
382377
383378
384379class AutoCompleteField (forms .CharField ):
385-
386380 """
387381 A CharField that uses an AutoCompleteWidget to lookup matching
388382 and stores the result as plain text.
@@ -393,9 +387,9 @@ def __init__(self, channel, *args, **kwargs):
393387 self .channel = channel
394388
395389 widget_kwargs = dict (
396- help_text = kwargs .get ('help_text' , _ (as_default_help )),
397- show_help_text = kwargs .pop ('show_help_text' , True ),
398- 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' , {})
399393 )
400394 widget_kwargs .update (kwargs .pop ('widget_options' , {}))
401395 if 'attrs' in kwargs :
@@ -430,7 +424,7 @@ def _check_can_add(self, user, related_model):
430424 app_label = related_model ._meta .app_label
431425 model = related_model ._meta .object_name .lower ()
432426 self .widget .add_link = reverse (
433- 'admin:%s_%s_add' % (app_label , model )) + '?_popup=1'
427+ 'admin:%s_%s_add' % (app_label , model )) + '?_popup=1'
434428
435429
436430def autoselect_fields_check_can_add (form , model , user ):
@@ -465,7 +459,7 @@ def make_plugin_options(lookup, channel_name, widget_plugin_options, initial):
465459 return {
466460 'plugin_options' : mark_safe (json .dumps (po , cls = json_encoder )),
467461 'data_plugin_options' : force_escape (
468- json .dumps (po , cls = json_encoder )
462+ json .dumps (po , cls = json_encoder )
469463 )
470464 }
471465
@@ -475,4 +469,4 @@ def pack_ids(ids):
475469 # |pk|pk| of current
476470 return "|" + "|" .join (str (pk ) for pk in ids ) + "|"
477471 else :
478- return "|"
472+ return "|"
0 commit comments