Skip to content

Pre-release improvements #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 17 additions & 31 deletions django_admin_row_actions/admin.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
from django import VERSION
from django import forms
from django.conf.urls import url
from django.urls import re_path
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _

from six import string_types
from django.utils.translation import gettext_lazy as _

from .components import Dropdown
from .views import ModelToolsView


def patterns(prefix, *args):
if VERSION < (1, 9):
from django.conf.urls import patterns as django_patterns
return django_patterns(prefix, *args)
elif prefix != '':
raise Exception("You need to update your URLConf to be a list of URL "
"objects")
else:
return list(args)


class AdminRowActionsMixin(object):
class AdminRowActionsMixin:

"""ModelAdmin mixin to add row actions just like adding admin actions"""

Expand All @@ -30,14 +17,14 @@ class AdminRowActionsMixin(object):

@property
def media(self):
return super(AdminRowActionsMixin, self).media + forms.Media(
return super().media + forms.Media(
css={'all': ["css/jquery.dropdown.min.css"]},
js=["js/jquery.dropdown.min.js"],
)

def get_list_display(self, request):
self._request = request
list_display = super(AdminRowActionsMixin, self).get_list_display(request)
list_display = super().get_list_display(request)
if '_row_actions' not in list_display:
list_display += ('_row_actions',)
return list_display
Expand All @@ -53,14 +40,14 @@ def to_dict(tool_name):
items = []

row_actions = self.get_row_actions(obj)
url_prefix = '{}/'.format(obj.pk if includePk else '')
url_prefix = f'{obj.pk if includePk else ""}/'

for tool in row_actions:
if isinstance(tool, string_types): # Just a str naming a callable
if isinstance(tool, str): # Just a str naming a callable
tool_dict = to_dict(tool)
items.append({
'label': tool_dict['label'],
'url': '{}rowactions/{}/'.format(url_prefix, tool),
'url': f'{url_prefix}rowactions/{tool}/',
'method': tool_dict.get('POST', 'GET')
})

Expand All @@ -69,9 +56,9 @@ def to_dict(tool_name):
if 'action' in tool: # If 'action' is specified then use our generic url in preference to 'url' value
if isinstance(tool['action'], tuple):
self._named_row_actions[tool['action'][0]] = tool['action'][1]
tool['url'] = '{}rowactions/{}/'.format(url_prefix, tool['action'][0])
tool['url'] = f'{url_prefix}rowactions/{tool["action"][0]}/'
else:
tool['url'] = '{}rowactions/{}/'.format(url_prefix, tool['action'])
tool['url'] = f'{url_prefix}rowactions/{tool["action"]}/'
items.append(tool)

return items
Expand Down Expand Up @@ -99,12 +86,11 @@ def get_tool_urls(self):

"""Gets the url patterns that route each tool to a special view"""

my_urls = patterns(
'',
url(r'^(?P<pk>[0-9a-f-]+)/rowactions/(?P<tool>\w+)/$',
my_urls = [
re_path(r'^(?P<pk>[0-9a-f-]+)/rowactions/(?P<tool>\w+)/$',
self.admin_site.admin_view(ModelToolsView.as_view(model=self.model))
)
)
]
return my_urls

###################################
Expand All @@ -115,7 +101,7 @@ def get_urls(self):

"""Prepends `get_urls` with our own patterns"""

urls = super(AdminRowActionsMixin, self).get_urls()
urls = super().get_urls()
return self.get_tool_urls() + urls

##################
Expand All @@ -130,7 +116,7 @@ def get_change_actions(self, request, object_id, form_url):
# If we're also using django_object_actions
# then try to reuse row actions as object actions

change_actions = super(AdminRowActionsMixin, self).get_change_actions(request, object_id, form_url)
change_actions = super().get_change_actions(request, object_id, form_url)

# Make this reuse opt-in
if getattr(self, 'reuse_row_actions_as_object_actions', False):
Expand All @@ -140,10 +126,10 @@ def get_change_actions(self, request, object_id, form_url):

for row_action in row_actions:
# Object actions only supports strings as action indentifiers
if isinstance(row_action, string_types):
if isinstance(row_action, str):
change_actions.append(row_action)
elif isinstance(row_action, dict):
if isinstance(row_action['action'], string_types):
if isinstance(row_action['action'], str):
change_actions.append(row_action['action'])
elif isinstance(row_action['action'], tuple):
change_actions.append(str(row_action['action'][1]))
Expand Down
10 changes: 3 additions & 7 deletions django_admin_row_actions/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.template.loader import render_to_string


class BaseComponent(object):
class BaseComponent:

template = None
instances = []
Expand All @@ -15,7 +15,7 @@ def __init__(self, **kwargs):

@classmethod
def get_unique_id(cls):
return "{}-{}".format(cls.__name__.lower(), len(cls.instances))
return f'{cls.__name__.lower()}-{len(cls.instances)}'

def render(self):
return render_to_string(
Expand All @@ -24,13 +24,9 @@ def render(self):
request=self.request
)

def __unicode__(self):
def __str__(self):
return self.render()


class Dropdown(BaseComponent):

template = 'django_admin_row_actions/dropdown.html'

def __init__(self, **kwargs):
super(Dropdown, self).__init__(**kwargs)
2 changes: 1 addition & 1 deletion django_admin_row_actions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, instance=None, *args, **kwargs):
# we may be able to throw away all this logic
model = instance._meta.concrete_model
self._doa_instance = instance
super(QuerySetIsh, self).__init__(model, *args, **kwargs)
super().__init__(model, *args, **kwargs)
self._result_cache = [instance]

def _clone(self, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion django_admin_row_actions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def get(self, request, **kwargs):
if isinstance(ret, HttpResponse):
response = ret
else:
back = request.META['HTTP_REFERER']
back = request.headers['referer']
response = HttpResponseRedirect(back)

return response
Expand Down
17 changes: 17 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "django-admin-row-actions"
description = "Add action buttons to individual rows in the Django Admin"
readme = "README.md"
version = "0.10.0"
authors = [
"Andy Baker <[email protected]>",
]
packages = [
{ include = "django_admin_row_actions" },
]
homepage = "https://pypi.org/project/django-admin-row-actions/"
repository = "https://github.com/DjangoAdminHackers/django-admin-row-actions"
2 changes: 0 additions & 2 deletions setup.cfg

This file was deleted.

23 changes: 0 additions & 23 deletions setup.py

This file was deleted.