Skip to content

Commit 9298dc6

Browse files
fix Django admin integration including preserving compatibility (#754)
This change fixes the Django admin integration added in #747. As is, the integration adds the views, but they do not reverse properly without also including the URLs via `include("django_rq.urls")`. When using Django's `ModelAdmin`, the URLs registered will be in the "admin" namespace, but the templates were not updated to include a namespace so they fail to resolve. This change conditionally resolves with either the prefix `django_rq:` or `admin:django_rq_` depending on how the view is called (via the admin app or not) and the URLs are also registered with the corresponding prefix. In order to properly test this integration, the default URL conf has been updated to not include the django_rq URLS via `include`, and a separate `default_with_custom_mount_urls` URL conf is provided in order to test integrations using both `include` and the admin integration.
1 parent 02595e0 commit 9298dc6

28 files changed

+251
-221
lines changed

django_rq/admin.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ def wrap(view):
5353
"""Wrap view with admin_site.admin_view for permission checking"""
5454

5555
@wraps(view)
56-
def wrapper(*args, **kwargs):
57-
return self.admin_site.admin_view(view)(*args, **kwargs)
56+
def wrapper(request, *args, **kwargs):
57+
request.current_app = self.admin_site.name
58+
return self.admin_site.admin_view(view)(request, *args, **kwargs)
5859

5960
return wrapper
6061

6162
# Get both sets of URL patterns
62-
api_urls = get_api_urlpatterns() # Not wrapped - have their own auth
63-
admin_urls = get_admin_urlpatterns(view_wrapper=wrap) # Wrapped with admin auth
63+
api_urls = get_api_urlpatterns(name_prefix='django_rq_') # Not wrapped - have their own auth
64+
admin_urls = get_admin_urlpatterns(name_prefix='django_rq_', view_wrapper=wrap) # Wrapped with admin auth
6465

6566
# Combine and add to standard ModelAdmin URLs
6667
return api_urls + admin_urls + super().get_urls()

django_rq/cron_views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from django.contrib import admin
21
from django.contrib.admin.views.decorators import staff_member_required
32
from django.http import Http404
43
from django.shortcuts import render
54
from django.views.decorators.cache import never_cache
65

76
from .connection_utils import get_connection_by_index
87
from .cron import DjangoCronScheduler
8+
from .views import each_context
99

1010

1111
@never_cache
@@ -37,7 +37,7 @@ def cron_scheduler_detail(request, connection_index: int, scheduler_name: str):
3737
raise Http404(f"Scheduler '{scheduler_name}' not found")
3838

3939
context_data = {
40-
**admin.site.each_context(request),
40+
**each_context(request),
4141
"scheduler": scheduler,
4242
}
4343

django_rq/stats_views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
from typing import Optional
44

5-
from django.contrib import admin
65
from django.contrib.admin.views.decorators import staff_member_required
76
from django.http import Http404, HttpRequest, HttpResponse, JsonResponse
87
from django.shortcuts import render
98
from django.views.decorators.cache import never_cache
109

1110
from . import settings as django_rq_settings
1211
from .utils import get_cron_schedulers, get_scheduler_statistics, get_statistics
12+
from .views import each_context
1313

1414
try:
1515
import prometheus_client
@@ -64,7 +64,7 @@ def prometheus_metrics(request):
6464
@staff_member_required
6565
def stats(request: HttpRequest) -> HttpResponse:
6666
context_data = {
67-
**admin.site.each_context(request),
67+
**each_context(request),
6868
**get_statistics(run_maintenance_tasks=True),
6969
**get_scheduler_statistics(),
7070
"view_metrics": RQCollector is not None,

django_rq/templates/django_rq/clear_failed_queue.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
{% block breadcrumbs %}
1818
<div class="breadcrumbs">
1919
<a href="{% url 'admin:index' %}">Home</a> &rsaquo;
20-
<a href="{% url 'rq_home' %}">Django RQ</a> &rsaquo;
21-
<a href = "{% url 'rq_jobs' queue_index %}">{{ queue.name }}</a> &rsaquo;
20+
<a href="{% url url_prefix|add:'home' %}">Django RQ</a> &rsaquo;
21+
<a href = "{% url url_prefix|add:'jobs' queue_index %}">{{ queue.name }}</a> &rsaquo;
2222
Delete All
2323
</div>
2424
{% endblock %}
@@ -29,7 +29,7 @@
2929

3030
<div id="content-main">
3131
<p>
32-
Are you sure you want to delete {{ total_jobs }} failed job{{ total_jobs|pluralize }} in the <a href = "{% url 'rq_jobs' queue_index %}">{{ queue.name }}</a> queue?
32+
Are you sure you want to delete {{ total_jobs }} failed job{{ total_jobs|pluralize }} in the <a href = "{% url url_prefix|add:'jobs' queue_index %}">{{ queue.name }}</a> queue?
3333
This action can not be undone.
3434
</p>
3535
<form action="" method="post">

django_rq/templates/django_rq/clear_queue.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
{% block breadcrumbs %}
1818
<div class="breadcrumbs">
1919
<a href="{% url 'admin:index' %}">Home</a> &rsaquo;
20-
<a href="{% url 'rq_home' %}">Django RQ</a> &rsaquo;
21-
<a href = "{% url 'rq_jobs' queue_index %}">{{ queue.name }}</a> &rsaquo;
20+
<a href="{% url url_prefix|add:'home' %}">Django RQ</a> &rsaquo;
21+
<a href = "{% url url_prefix|add:'jobs' queue_index %}">{{ queue.name }}</a> &rsaquo;
2222
Empty
2323
</div>
2424
{% endblock %}
@@ -29,7 +29,7 @@
2929

3030
<div id="content-main">
3131
<p>
32-
Are you sure you want to clear the queue <a href = "{% url 'rq_jobs' queue_index %}">{{ queue.name }}</a>?
32+
Are you sure you want to clear the queue <a href = "{% url url_prefix|add:'jobs' queue_index %}">{{ queue.name }}</a>?
3333
This action can not be undone.
3434
</p>
3535
<form action="" method="post">

django_rq/templates/django_rq/confirm_action.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
{% block breadcrumbs %}
1818
<div class="breadcrumbs">
1919
<a href="{% url 'admin:index' %}">Home</a> &rsaquo;
20-
<a href="{% url 'rq_home' %}">Django RQ</a> &rsaquo;
21-
<a href = "{% url 'rq_jobs' queue_index %}">{{ queue.name }}</a> &rsaquo;
20+
<a href="{% url url_prefix|add:'home' %}">Django RQ</a> &rsaquo;
21+
<a href = "{% url url_prefix|add:'jobs' queue_index %}">{{ queue.name }}</a> &rsaquo;
2222
{{ action|capfirst }}
2323
</div>
2424
{% endblock %}
@@ -29,14 +29,14 @@
2929

3030
<div id="content-main">
3131
<p>
32-
Are you sure you want to <b>{{ action|capfirst }}</b> the selected jobs from <a href="{% url 'rq_jobs' queue_index %}" target="_blank">{{ queue.name }}</a>? These jobs are selected:
32+
Are you sure you want to <b>{{ action|capfirst }}</b> the selected jobs from <a href="{% url url_prefix|add:'jobs' queue_index %}" target="_blank">{{ queue.name }}</a>? These jobs are selected:
3333
</p>
3434
<ul>
3535
{% for job_id in job_ids %}
36-
<li><a href="{% url 'rq_job_detail' queue_index job_id %}" target="_blank">{{ job_id }}</a></li>
36+
<li><a href="{% url url_prefix|add:'job_detail' queue_index job_id %}" target="_blank">{{ job_id }}</a></li>
3737
{% endfor %}
3838
</ul>
39-
<form action="{% url 'rq_actions' queue_index %}" method="post">
39+
<form action="{% url url_prefix|add:'actions' queue_index %}" method="post">
4040
{% csrf_token %}
4141
<div>
4242
{% for job_id in job_ids %}

django_rq/templates/django_rq/cron_scheduler_detail.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{% block breadcrumbs %}
1515
<div class="breadcrumbs">
1616
<a href="{% url 'admin:index' %}">Home</a> &rsaquo;
17-
<a href="{% url 'rq_home' %}">Django RQ</a> &rsaquo;
17+
<a href="{% url url_prefix|add:'home' %}">Django RQ</a> &rsaquo;
1818
Cron Scheduler: {{ scheduler.name }}
1919
</div>
2020
{% endblock %}

django_rq/templates/django_rq/deferred_jobs.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
{% block breadcrumbs %}
2828
<div class="breadcrumbs">
2929
<a href="{% url 'admin:index' %}">Home</a> &rsaquo;
30-
<a href="{% url 'rq_home' %}">Django RQ</a> &rsaquo;
31-
<a href="{% url 'rq_jobs' queue_index %}">{{ queue.name }}</a>
30+
<a href="{% url url_prefix|add:'home' %}">Django RQ</a> &rsaquo;
31+
<a href="{% url url_prefix|add:'jobs' queue_index %}">{{ queue.name }}</a>
3232
</div>
3333
{% endblock %}
3434

@@ -39,7 +39,7 @@
3939
<div id="content-main">
4040
<div class="module" id="changelist">
4141
<div class="changelist-form-container">
42-
<form id="changelist-form" action="{% url 'rq_confirm_action' queue_index %}" method="post">
42+
<form id="changelist-form" action="{% url url_prefix|add:'confirm_action' queue_index %}" method="post">
4343
{% csrf_token %}
4444
<div class="actions">
4545
<label>Actions:
@@ -109,7 +109,7 @@
109109
<input class="action-select" name="_selected_action" type="checkbox" value="{{ job.id }}">
110110
</td>
111111
<th>
112-
<a href="{% url 'rq_job_detail' queue_index job.id %}">
112+
<a href="{% url url_prefix|add:'job_detail' queue_index job.id %}">
113113
{{ job.id }}
114114
</a>
115115
</th>

django_rq/templates/django_rq/delete_job.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
{% block breadcrumbs %}
1818
<div class="breadcrumbs">
1919
<a href="{% url 'admin:index' %}">Home</a> &rsaquo;
20-
<a href="{% url 'rq_home' %}">Django RQ</a> &rsaquo;
21-
<a href = "{% url 'rq_jobs' queue_index %}">{{ queue.name }}</a> &rsaquo;
22-
<a href = "{% url 'rq_job_detail' queue_index job.id %}">{{ job.id }}</a> &rsaquo;
20+
<a href="{% url url_prefix|add:'home' %}">Django RQ</a> &rsaquo;
21+
<a href = "{% url url_prefix|add:'jobs' queue_index %}">{{ queue.name }}</a> &rsaquo;
22+
<a href = "{% url url_prefix|add:'job_detail' queue_index job.id %}">{{ job.id }}</a> &rsaquo;
2323
Delete
2424
</div>
2525
{% endblock %}
@@ -30,7 +30,7 @@
3030

3131
<div id="content-main">
3232
<p>
33-
Are you sure you want to delete <a href = "{% url 'rq_job_detail' queue_index job.id %}">{{ job.id }}</a> from <a href = "{% url 'rq_jobs' queue_index %}">{{ queue.name }}</a>?
33+
Are you sure you want to delete <a href = "{% url url_prefix|add:'job_detail' queue_index job.id %}">{{ job.id }}</a> from <a href = "{% url url_prefix|add:'jobs' queue_index %}">{{ queue.name }}</a>?
3434
This action can not be undone.
3535
</p>
3636
<form action="" method="post">

django_rq/templates/django_rq/failed_jobs.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
{% block breadcrumbs %}
2828
<div class="breadcrumbs">
2929
<a href="{% url 'admin:index' %}">Home</a> &rsaquo;
30-
<a href="{% url 'rq_home' %}">Django RQ</a> &rsaquo;
31-
<a href="{% url 'rq_jobs' queue_index %}">{{ queue.name }}</a>
30+
<a href="{% url url_prefix|add:'home' %}">Django RQ</a> &rsaquo;
31+
<a href="{% url url_prefix|add:'jobs' queue_index %}">{{ queue.name }}</a>
3232
</div>
3333
{% endblock %}
3434

@@ -38,12 +38,12 @@
3838

3939
<div id="content-main">
4040
<ul class="object-tools">
41-
<li><a href="{% url 'rq_requeue_all' queue_index %}" class="requeuelink">Requeue All</a></li>
42-
<li><a href="{% url 'rq_delete_failed_jobs' queue_index %}" class="requeuelink">Delete All</a></li>
41+
<li><a href="{% url url_prefix|add:'requeue_all' queue_index %}" class="requeuelink">Requeue All</a></li>
42+
<li><a href="{% url url_prefix|add:'delete_failed_jobs' queue_index %}" class="requeuelink">Delete All</a></li>
4343
</ul>
4444
<div class="module" id="changelist">
4545
<div class="changelist-form-container">
46-
<form id="changelist-form" action="{% url 'rq_confirm_action' queue_index %}" method="post">
46+
<form id="changelist-form" action="{% url url_prefix|add:'confirm_action' queue_index %}" method="post">
4747
{% csrf_token %}
4848
<div class="actions">
4949
<label>Actions:
@@ -115,7 +115,7 @@
115115
<input class="action-select" name="_selected_action" type="checkbox" value="{{ job.id }}">
116116
</td>
117117
<th>
118-
<a href="{% url 'rq_job_detail' queue_index job.id %}">
118+
<a href="{% url url_prefix|add:'job_detail' queue_index job.id %}">
119119
{{ job.id }}
120120
</a>
121121
</th>

0 commit comments

Comments
 (0)