11from __future__ import unicode_literals
2+ import json
23from ajax_select .registry import registry
34from django import forms
45from django .conf import settings
56from django .contrib .contenttypes .models import ContentType
67from django .core .urlresolvers import reverse
8+ from django .db .models .query import QuerySet
79try :
810 from django .forms .utils import flatatt
911except ImportError :
1517from django .utils .safestring import mark_safe
1618from django .utils .six import text_type
1719from django .utils .translation import ugettext as _
18- import json
1920
2021
2122as_default_help = 'Enter text to search.'
@@ -49,10 +50,10 @@ def __init__(self,
4950 channel ,
5051 help_text = '' ,
5152 show_help_text = True ,
52- plugin_options = {} ,
53+ plugin_options = None ,
5354 * args ,
5455 ** kwargs ):
55- self .plugin_options = plugin_options
56+ self .plugin_options = plugin_options or {}
5657 super (forms .widgets .TextInput , self ).__init__ (* args , ** kwargs )
5758 self .channel = channel
5859 self .help_text = help_text
@@ -90,9 +91,10 @@ def render(self, name, value, attrs=None):
9091 'func_slug' : self .html_id .replace ("-" , "" ),
9192 'add_link' : self .add_link ,
9293 }
93- context .update (plugin_options (lookup , self .channel , self .plugin_options , initial ))
94- templates = ('ajax_select/autocompleteselect_%s.html' % self .channel ,
95- 'ajax_select/autocompleteselect.html' )
94+ context .update (make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
95+ templates = (
96+ 'ajax_select/autocompleteselect_%s.html' % self .channel ,
97+ 'ajax_select/autocompleteselect.html' )
9698 out = render_to_string (templates , context )
9799 return mark_safe (out )
98100
@@ -162,15 +164,15 @@ def __init__(self,
162164 channel ,
163165 help_text = '' ,
164166 show_help_text = True ,
165- plugin_options = {} ,
167+ plugin_options = None ,
166168 * args ,
167169 ** kwargs ):
168170 super (AutoCompleteSelectMultipleWidget , self ).__init__ (* args , ** kwargs )
169171 self .channel = channel
170172
171173 self .help_text = help_text
172174 self .show_help_text = show_help_text
173- self .plugin_options = plugin_options
175+ self .plugin_options = plugin_options or {}
174176
175177 def render (self , name , value , attrs = None ):
176178
@@ -182,20 +184,18 @@ def render(self, name, value, attrs=None):
182184
183185 lookup = registry .get (self .channel )
184186
185- # eg. value = [3002L, 1194L]
186- if value :
187- # |pk|pk| of current
188- current_ids = "|" + "|" .join (str (pk ) for pk in value ) + "|"
187+ if isinstance (value , QuerySet ):
188+ objects = value
189189 else :
190- current_ids = "|"
190+ objects = lookup . get_objects ( value )
191191
192- objects = lookup . get_objects ( value )
192+ current_ids = pack_ids ([ obj . pk for obj in objects ] )
193193
194194 # text repr of currently selected items
195- initial = []
196- for obj in objects :
197- display = lookup . format_item_display ( obj )
198- initial . append ([ display , obj . pk ])
195+ initial = [
196+ [ lookup . format_item_display ( obj ), obj . pk ]
197+ for obj in objects
198+ ]
199199
200200 if self .show_help_text :
201201 help_text = self .help_text
@@ -213,7 +213,7 @@ def render(self, name, value, attrs=None):
213213 'func_slug' : self .html_id .replace ("-" , "" ),
214214 'add_link' : self .add_link ,
215215 }
216- context .update (plugin_options (lookup , self .channel , self .plugin_options , initial ))
216+ context .update (make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
217217 templates = ('ajax_select/autocompleteselectmultiple_%s.html' % self .channel ,
218218 'ajax_select/autocompleteselectmultiple.html' )
219219 out = render_to_string (templates , context )
@@ -344,7 +344,7 @@ def render(self, name, value, attrs=None):
344344 'extra_attrs' : mark_safe (flatatt (final_attrs )),
345345 'func_slug' : self .html_id .replace ("-" , "" ),
346346 }
347- context .update (plugin_options (lookup , self .channel , self .plugin_options , initial ))
347+ context .update (make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
348348 templates = ('ajax_select/autocomplete_%s.html' % self .channel ,
349349 'ajax_select/autocomplete.html' )
350350 return mark_safe (render_to_string (templates , context ))
@@ -394,10 +394,9 @@ def _check_can_add(self, user, related_model):
394394 ctype = ContentType .objects .get_for_model (related_model )
395395 can_add = user .has_perm ("%s.add_%s" % (ctype .app_label , ctype .model ))
396396 if can_add :
397- self .widget .add_link = reverse ('add_popup' , kwargs = {
398- 'app_label' : related_model ._meta .app_label ,
399- 'model' : related_model ._meta .object_name .lower ()
400- })
397+ app_label = related_model ._meta .app_label
398+ model = related_model ._meta .object_name .lower ()
399+ self .widget .add_link = reverse ('admin:%s_%s_add' % (app_label , model )) + '?_popup=1'
401400
402401
403402def autoselect_fields_check_can_add (form , model , user ):
@@ -411,7 +410,7 @@ def autoselect_fields_check_can_add(form, model, user):
411410 form_field .check_can_add (user , db_field .rel .to )
412411
413412
414- def plugin_options (lookup , channel_name , widget_plugin_options , initial ):
413+ def make_plugin_options (lookup , channel_name , widget_plugin_options , initial ):
415414 """ Make a JSON dumped dict of all options for the jQuery ui plugin."""
416415 po = {}
417416 if initial :
@@ -429,3 +428,11 @@ def plugin_options(lookup, channel_name, widget_plugin_options, initial):
429428 'plugin_options' : mark_safe (json .dumps (po )),
430429 'data_plugin_options' : force_escape (json .dumps (po ))
431430 }
431+
432+
433+ def pack_ids (ids ):
434+ if ids :
435+ # |pk|pk| of current
436+ return "|" + "|" .join (str (pk ) for pk in ids ) + "|"
437+ else :
438+ return "|"
0 commit comments