1
1
from django import VERSION
2
2
from django import forms
3
- from django .conf . urls import url
3
+ from django .urls import re_path
4
4
from django .utils .safestring import mark_safe
5
- from django .utils .translation import ugettext_lazy as _
6
-
7
- from six import string_types
5
+ from django .utils .translation import gettext_lazy as _
8
6
9
7
from .components import Dropdown
10
8
from .views import ModelToolsView
11
9
12
10
13
- def patterns (prefix , * args ):
14
- if VERSION < (1 , 9 ):
15
- from django .conf .urls import patterns as django_patterns
16
- return django_patterns (prefix , * args )
17
- elif prefix != '' :
18
- raise Exception ("You need to update your URLConf to be a list of URL "
19
- "objects" )
20
- else :
21
- return list (args )
22
-
23
-
24
- class AdminRowActionsMixin (object ):
11
+ class AdminRowActionsMixin :
25
12
26
13
"""ModelAdmin mixin to add row actions just like adding admin actions"""
27
14
@@ -30,14 +17,14 @@ class AdminRowActionsMixin(object):
30
17
31
18
@property
32
19
def media (self ):
33
- return super (AdminRowActionsMixin , self ).media + forms .Media (
20
+ return super ().media + forms .Media (
34
21
css = {'all' : ["css/jquery.dropdown.min.css" ]},
35
22
js = ["js/jquery.dropdown.min.js" ],
36
23
)
37
24
38
25
def get_list_display (self , request ):
39
26
self ._request = request
40
- list_display = super (AdminRowActionsMixin , self ).get_list_display (request )
27
+ list_display = super ().get_list_display (request )
41
28
if '_row_actions' not in list_display :
42
29
list_display += ('_row_actions' ,)
43
30
return list_display
@@ -53,14 +40,14 @@ def to_dict(tool_name):
53
40
items = []
54
41
55
42
row_actions = self .get_row_actions (obj )
56
- url_prefix = '{}/' . format ( obj .pk if includePk else '' )
43
+ url_prefix = f' { obj .pk if includePk else "" } /'
57
44
58
45
for tool in row_actions :
59
- if isinstance (tool , string_types ): # Just a str naming a callable
46
+ if isinstance (tool , str ): # Just a str naming a callable
60
47
tool_dict = to_dict (tool )
61
48
items .append ({
62
49
'label' : tool_dict ['label' ],
63
- 'url' : '{ }rowactions/{}/'. format ( url_prefix , tool ) ,
50
+ 'url' : f' { url_prefix } rowactions/{ tool } /' ,
64
51
'method' : tool_dict .get ('POST' , 'GET' )
65
52
})
66
53
@@ -69,9 +56,9 @@ def to_dict(tool_name):
69
56
if 'action' in tool : # If 'action' is specified then use our generic url in preference to 'url' value
70
57
if isinstance (tool ['action' ], tuple ):
71
58
self ._named_row_actions [tool ['action' ][0 ]] = tool ['action' ][1 ]
72
- tool ['url' ] = '{ }rowactions/{}/' . format ( url_prefix , tool [' action' ][0 ])
59
+ tool ['url' ] = f' { url_prefix } rowactions/{ tool [" action" ][0 ]} /'
73
60
else :
74
- tool ['url' ] = '{ }rowactions/{}/' . format ( url_prefix , tool [' action' ])
61
+ tool ['url' ] = f' { url_prefix } rowactions/{ tool [" action" ] } /'
75
62
items .append (tool )
76
63
77
64
return items
@@ -99,12 +86,11 @@ def get_tool_urls(self):
99
86
100
87
"""Gets the url patterns that route each tool to a special view"""
101
88
102
- my_urls = patterns (
103
- '' ,
104
- url (r'^(?P<pk>[0-9a-f-]+)/rowactions/(?P<tool>\w+)/$' ,
89
+ my_urls = [
90
+ re_path (r'^(?P<pk>[0-9a-f-]+)/rowactions/(?P<tool>\w+)/$' ,
105
91
self .admin_site .admin_view (ModelToolsView .as_view (model = self .model ))
106
92
)
107
- )
93
+ ]
108
94
return my_urls
109
95
110
96
###################################
@@ -115,7 +101,7 @@ def get_urls(self):
115
101
116
102
"""Prepends `get_urls` with our own patterns"""
117
103
118
- urls = super (AdminRowActionsMixin , self ).get_urls ()
104
+ urls = super ().get_urls ()
119
105
return self .get_tool_urls () + urls
120
106
121
107
##################
@@ -130,7 +116,7 @@ def get_change_actions(self, request, object_id, form_url):
130
116
# If we're also using django_object_actions
131
117
# then try to reuse row actions as object actions
132
118
133
- change_actions = super (AdminRowActionsMixin , self ).get_change_actions (request , object_id , form_url )
119
+ change_actions = super ().get_change_actions (request , object_id , form_url )
134
120
135
121
# Make this reuse opt-in
136
122
if getattr (self , 'reuse_row_actions_as_object_actions' , False ):
@@ -140,10 +126,10 @@ def get_change_actions(self, request, object_id, form_url):
140
126
141
127
for row_action in row_actions :
142
128
# Object actions only supports strings as action indentifiers
143
- if isinstance (row_action , string_types ):
129
+ if isinstance (row_action , str ):
144
130
change_actions .append (row_action )
145
131
elif isinstance (row_action , dict ):
146
- if isinstance (row_action ['action' ], string_types ):
132
+ if isinstance (row_action ['action' ], str ):
147
133
change_actions .append (row_action ['action' ])
148
134
elif isinstance (row_action ['action' ], tuple ):
149
135
change_actions .append (str (row_action ['action' ][1 ]))
0 commit comments