-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadmin_urls.py
133 lines (113 loc) · 3.54 KB
/
admin_urls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
Helpers to build Admin URLs
"""
from urllib.parse import urlencode
from django.contrib.admin.utils import quote
from django.db import models
from django.urls import reverse
ADMIN_LINK_TYPES = ('changelist', 'add', 'history', 'delete', 'change')
def admin_model_url(
*,
model_or_instance: models.Model | type[models.Model],
action: str | None = None, # Default is 'changelist' and 'change'
admin_prefix: str = 'admin',
current_app: str | None = None,
params: dict | None = None,
):
"""
Build Admin change, add, changelist, etc. links with optional filter parameters.
'action' defaults to 'changelist' or 'change', depending on model or instance is given.
"""
if action is None:
if isinstance(model_or_instance, models.Model):
action = 'change'
else:
action = 'changelist'
assert (
action in ADMIN_LINK_TYPES
), f'Action: {action!r} is not one of {", ".join(ADMIN_LINK_TYPES)}'
opts = model_or_instance._meta
app_label = opts.app_label
model_name = opts.model_name
if action in ('history', 'delete', 'change'):
assert isinstance(
model_or_instance, models.Model
), f'{model_or_instance!r} is no model instance!'
pk = model_or_instance.pk
args = (quote(pk),)
else:
args = None
url = reverse(
f'{admin_prefix}:{app_label}_{model_name}_{action}', current_app=current_app, args=args
)
if params:
params = urlencode(params)
url = f'{url}?{params}'
return url
def admin_change_url(
instance: models.Model,
admin_prefix: str = 'admin',
current_app: str | None = None,
params: dict | None = None,
):
"""
Shortcut to generate Django admin "change" url for a model instance.
"""
assert isinstance(instance, models.Model), f'No model instance given: {instance}'
return admin_model_url(
model_or_instance=instance,
action='change',
admin_prefix=admin_prefix,
current_app=current_app,
params=params,
)
def admin_history_url(
instance: models.Model,
admin_prefix: str = 'admin',
current_app: str | None = None,
params: dict | None = None,
):
"""
Shortcut to generate Django admin "history" url for a model instance.
"""
assert isinstance(instance, models.Model), f'No model instance given: {instance}'
return admin_model_url(
model_or_instance=instance,
action='history',
admin_prefix=admin_prefix,
current_app=current_app,
params=params,
)
def admin_delete_url(
instance: models.Model,
admin_prefix: str = 'admin',
current_app: str | None = None,
params: dict | None = None,
):
"""
Shortcut to generate Django admin "delete" url for a model instance.
"""
assert isinstance(instance, models.Model), f'No model instance given: {instance}'
return admin_model_url(
model_or_instance=instance,
action='delete',
admin_prefix=admin_prefix,
current_app=current_app,
params=params,
)
def admin_changelist_url(
model_or_instance: models.Model | type[models.Model],
admin_prefix: str = 'admin',
current_app: str | None = None,
params: dict | None = None,
):
"""
Shortcut to generate Django admin "changelist" url for a model or instance.
"""
return admin_model_url(
model_or_instance=model_or_instance,
action='changelist',
admin_prefix=admin_prefix,
current_app=current_app,
params=params,
)