1
- from __future__ import unicode_literals
2
-
3
1
import json
4
- import sys
5
2
6
- from ajax_select .registry import registry
7
3
from django import forms
8
4
from django .conf import settings
9
5
from django .contrib .contenttypes .models import ContentType
10
6
from django .db .models import Model
11
7
from django .forms .utils import flatatt
12
8
from django .template .defaultfilters import force_escape
13
9
from django .template .loader import render_to_string
14
- from django .utils .encoding import force_text
10
+ from django .urls import reverse
11
+ from django .utils .encoding import force_str
15
12
from django .utils .module_loading import import_string
16
13
from django .utils .safestring import mark_safe
14
+ from django .utils .translation import gettext as _
17
15
18
- try :
19
- from six import text_type
20
- except ImportError :
21
- from django .utils .six import text_type
22
-
23
-
24
- if sys .version_info .major >= 3 :
25
- from django .utils .translation import gettext as _
26
- else :
27
- from django .utils .translation import ugettext as _
28
-
29
- try :
30
- from django .urls import reverse
31
- except ImportError :
32
- # < django 1.10
33
- from django .core .urlresolvers import reverse
16
+ from ajax_select .registry import registry
34
17
35
18
as_default_help = 'Enter text to search.'
36
19
@@ -72,7 +55,7 @@ def __init__(self,
72
55
* args ,
73
56
** kwargs ):
74
57
self .plugin_options = plugin_options or {}
75
- super (forms . widgets . TextInput , self ).__init__ (* args , ** kwargs )
58
+ super ().__init__ (* args , ** kwargs )
76
59
self .channel = channel
77
60
self .help_text = help_text
78
61
self .show_help_text = show_help_text
@@ -93,7 +76,7 @@ def render(self, name, value, attrs=None, renderer=None, **_kwargs):
93
76
try :
94
77
obj = objs [0 ]
95
78
except IndexError :
96
- raise Exception ("%s cannot find object:%s" % ( lookup , value ) )
79
+ raise Exception (f" { lookup } cannot find object:{ value } " )
97
80
current_repr = lookup .format_item_display (obj )
98
81
initial = [current_repr , obj .pk ]
99
82
@@ -115,7 +98,7 @@ def render(self, name, value, attrs=None, renderer=None, **_kwargs):
115
98
context .update (
116
99
make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
117
100
templates = (
118
- 'ajax_select/autocompleteselect_%s.html' % self .channel ,
101
+ f 'ajax_select/autocompleteselect_{ self .channel } .html' ,
119
102
'ajax_select/autocompleteselect.html' )
120
103
out = render_to_string (templates , context )
121
104
return mark_safe (out )
@@ -124,7 +107,7 @@ def value_from_datadict(self, data, files, name):
124
107
return data .get (name , None )
125
108
126
109
def id_for_label (self , id_ ):
127
- return '%s_text' % id_
110
+ return f' { id_ } _text'
128
111
129
112
130
113
class AutoCompleteSelectField (forms .fields .CharField ):
@@ -143,8 +126,7 @@ def __init__(self, channel, *args, **kwargs):
143
126
)
144
127
widget_kwargs .update (kwargs .pop ('widget_options' , {}))
145
128
kwargs ["widget" ] = AutoCompleteSelectWidget (** widget_kwargs )
146
- super (AutoCompleteSelectField , self ).__init__ (
147
- max_length = 255 , * args , ** kwargs )
129
+ super ().__init__ (max_length = 255 , * args , ** kwargs )
148
130
149
131
def clean (self , value ):
150
132
if value :
@@ -156,7 +138,7 @@ def clean(self, value):
156
138
# out of the scope of this field to do anything more than
157
139
# tell you it doesn't exist
158
140
raise forms .ValidationError (
159
- "%s cannot find object: %s" % ( lookup , value ) )
141
+ f" { lookup } cannot find object: { value } " )
160
142
return objs [0 ]
161
143
else :
162
144
if self .required :
@@ -170,7 +152,7 @@ def has_changed(self, initial, data):
170
152
# 1 vs u'1'
171
153
initial_value = initial if initial is not None else ''
172
154
data_value = data if data is not None else ''
173
- return text_type (initial_value ) != text_type (data_value )
155
+ return str (initial_value ) != str (data_value )
174
156
175
157
176
158
###############################################################################
@@ -192,7 +174,7 @@ def __init__(self,
192
174
plugin_options = None ,
193
175
* args ,
194
176
** kwargs ):
195
- super (AutoCompleteSelectMultipleWidget , self ).__init__ (* args , ** kwargs )
177
+ super ().__init__ (* args , ** kwargs )
196
178
self .channel = channel
197
179
198
180
self .help_text = help_text
@@ -245,7 +227,7 @@ def render(self, name, value, attrs=None, renderer=None, **_kwargs):
245
227
}
246
228
context .update (
247
229
make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
248
- templates = ('ajax_select/autocompleteselectmultiple_%s.html' % self .channel ,
230
+ templates = (f 'ajax_select/autocompleteselectmultiple_{ self .channel } .html' ,
249
231
'ajax_select/autocompleteselectmultiple.html' )
250
232
out = render_to_string (templates , context )
251
233
return mark_safe (out )
@@ -254,8 +236,8 @@ def value_from_datadict(self, data, files, name):
254
236
# eg. 'members': ['|229|4688|190|']
255
237
return [val for val in data .get (name , '' ).split ('|' ) if val ]
256
238
257
- def id_for_label (self , id_ ):
258
- return '%s_text' % id_
239
+ def id_for_label (self , id_ , index = '0' ):
240
+ return f' { id_ } _text'
259
241
260
242
261
243
class AutoCompleteSelectMultipleField (forms .fields .CharField ):
@@ -275,13 +257,13 @@ def __init__(self, channel, *args, **kwargs):
275
257
# '' will cause translation to fail
276
258
# should be ''
277
259
if isinstance (help_text , str ):
278
- help_text = force_text (help_text )
260
+ help_text = force_str (help_text )
279
261
# django admin appends "Hold down "Control",..." to the help text
280
262
# regardless of which widget is used. so even when you specify an
281
263
# explicit help text it appends this other default text onto the end.
282
264
# This monkey patches the help text to remove that
283
265
if help_text != '' :
284
- if not isinstance (help_text , text_type ):
266
+ if not isinstance (help_text , str ):
285
267
# ideally this could check request.LANGUAGE_CODE
286
268
translated = help_text .translate (settings .LANGUAGE_CODE )
287
269
else :
@@ -315,7 +297,7 @@ def __init__(self, channel, *args, **kwargs):
315
297
kwargs ['widget' ] = AutoCompleteSelectMultipleWidget (** widget_kwargs )
316
298
kwargs ['help_text' ] = help_text
317
299
318
- super (AutoCompleteSelectMultipleField , self ).__init__ (* args , ** kwargs )
300
+ super ().__init__ (* args , ** kwargs )
319
301
320
302
def clean (self , value ):
321
303
if not value and self .required :
@@ -327,8 +309,8 @@ def check_can_add(self, user, model):
327
309
328
310
def has_changed (self , initial_value , data_value ):
329
311
# [1, 2] vs [u'1', u'2']
330
- ivs = [text_type (v ) for v in (initial_value or [])]
331
- dvs = [text_type (v ) for v in (data_value or [])]
312
+ ivs = [str (v ) for v in (initial_value or [])]
313
+ dvs = [str (v ) for v in (data_value or [])]
332
314
return ivs != dvs
333
315
334
316
@@ -354,7 +336,7 @@ def __init__(self, channel, *args, **kwargs):
354
336
self .show_help_text = kwargs .pop ('show_help_text' , True )
355
337
self .plugin_options = kwargs .pop ('plugin_options' , {})
356
338
357
- super (AutoCompleteWidget , self ).__init__ (* args , ** kwargs )
339
+ super ().__init__ (* args , ** kwargs )
358
340
359
341
def render (self , name , value , attrs = None , renderer = None , ** _kwargs ):
360
342
@@ -381,7 +363,7 @@ def render(self, name, value, attrs=None, renderer=None, **_kwargs):
381
363
}
382
364
context .update (
383
365
make_plugin_options (lookup , self .channel , self .plugin_options , initial ))
384
- templates = ('ajax_select/autocomplete_%s.html' % self .channel ,
366
+ templates = (f 'ajax_select/autocomplete_{ self .channel } .html' ,
385
367
'ajax_select/autocomplete.html' )
386
368
return mark_safe (render_to_string (templates , context ))
387
369
@@ -409,7 +391,7 @@ def __init__(self, channel, *args, **kwargs):
409
391
defaults = {'max_length' : 255 , 'widget' : widget }
410
392
defaults .update (kwargs )
411
393
412
- super (AutoCompleteField , self ).__init__ (* args , ** defaults )
394
+ super ().__init__ (* args , ** defaults )
413
395
414
396
415
397
###############################################################################
@@ -429,12 +411,12 @@ def _check_can_add(self, user, related_model):
429
411
can_add = lookup .can_add (user , related_model )
430
412
else :
431
413
ctype = ContentType .objects .get_for_model (related_model )
432
- can_add = user .has_perm ("%s.add_%s" % ( ctype .app_label , ctype .model ) )
414
+ can_add = user .has_perm (f" { ctype .app_label } .add_ { ctype .model } " )
433
415
if can_add :
434
416
app_label = related_model ._meta .app_label
435
417
model = related_model ._meta .object_name .lower ()
436
418
self .widget .add_link = reverse (
437
- 'admin:%s_%s_add' % ( app_label , model ) ) + '?_popup=1'
419
+ f 'admin:{ app_label } _ { model } _add' ) + '?_popup=1'
438
420
439
421
440
422
def autoselect_fields_check_can_add (form , model , user ):
0 commit comments