| 1 |
from typing import Callable, Optional |
| 2 |
|
| 3 |
from django.urls import URLPattern, path, re_path |
| 4 |
|
| 5 |
from . import cron_views, stats_views, views |
| 6 |
from .contrib.prometheus import RQCollector |
| 7 |
|
| 8 |
|
| 9 |
def get_api_urlpatterns(name_prefix: str = '') -> list[URLPattern]: |
| 10 |
""" |
| 11 |
Get URL patterns for views that have their own authentication (API tokens). |
| 12 |
|
| 13 |
These views should NOT be wrapped with admin_view because they support |
| 14 |
API token authentication that must work without Django session auth. |
| 15 |
|
| 16 |
Returns: |
| 17 |
List of URL patterns for API-authenticated views. |
| 18 |
""" |
| 19 |
# Conditional metrics view (only if prometheus_client is installed) |
| 20 |
metrics_view = ( |
| 21 |
[ |
| 22 |
re_path(r'^metrics/?$', stats_views.prometheus_metrics, name=f'{name_prefix}metrics'), |
| 23 |
] |
| 24 |
if RQCollector is not None |
| 25 |
else [] |
| 26 |
) |
| 27 |
|
| 28 |
return [ |
| 29 |
# Stats JSON (supports API token authentication) |
| 30 |
re_path(r'^stats.json/?$', stats_views.stats_json, name=f'{name_prefix}home_json'), |
| 31 |
re_path(r'^stats.json/(?P<token>[\w]+)?/?$', stats_views.stats_json, name=f'{name_prefix}home_json'), |
| 32 |
# Prometheus metrics (supports API token authentication) |
| 33 |
*metrics_view, |
| 34 |
] |
| 35 |
|
| 36 |
|
| 37 |
def get_admin_urlpatterns(name_prefix: str = '', view_wrapper: Optional[Callable] = None) -> list[URLPattern]: |
| 38 |
""" |
| 39 |
Get URL patterns for views that should be wrapped with admin authentication. |
| 40 |
|
| 41 |
Args: |
| 42 |
view_wrapper: Optional function to wrap each view (e.g., admin_site.admin_view). |
| 43 |
|
| 44 |
Returns: |
| 45 |
List of URL patterns for admin-authenticated views. |
| 46 |
""" |
| 47 |
|
| 48 |
def maybe_wrap(view: Callable) -> Callable: |
| 49 |
"""Apply wrapper if provided, otherwise return view as-is""" |
| 50 |
return view_wrapper(view) if view_wrapper else view |
| 51 |
|
| 52 |
return [ |
| 53 |
# Dashboard |
| 54 |
path('', maybe_wrap(stats_views.stats), name=f'{name_prefix}home'), |
| 55 |
# Queue views |
| 56 |
path('queues/<int:queue_index>/', maybe_wrap(views.jobs), name=f'{name_prefix}jobs'), |
| 57 |
path('queues/<int:queue_index>/details/', maybe_wrap(views.queue_details), name=f'{name_prefix}queue_details'), |
| 58 |
path('queues/<int:queue_index>/finished/', maybe_wrap(views.finished_jobs), name=f'{name_prefix}finished_jobs'), |
| 59 |
path('queues/<int:queue_index>/failed/', maybe_wrap(views.failed_jobs), name=f'{name_prefix}failed_jobs'), |
| 60 |
path('queues/<int:queue_index>/failed/clear/', maybe_wrap(views.delete_failed_jobs), name=f'{name_prefix}delete_failed_jobs'), |
| 61 |
path('queues/<int:queue_index>/scheduled/', maybe_wrap(views.scheduled_jobs), name=f'{name_prefix}scheduled_jobs'), |
| 62 |
path('queues/<int:queue_index>/started/', maybe_wrap(views.started_jobs), name=f'{name_prefix}started_jobs'), |
| 63 |
path('queues/<int:queue_index>/deferred/', maybe_wrap(views.deferred_jobs), name=f'{name_prefix}deferred_jobs'), |
| 64 |
path('queues/<int:queue_index>/empty/', maybe_wrap(views.clear_queue), name=f'{name_prefix}clear'), |
| 65 |
path('queues/<int:queue_index>/requeue-all/', maybe_wrap(views.requeue_all), name=f'{name_prefix}requeue_all'), |
| 66 |
# Job detail and actions |
| 67 |
path('queues/<int:queue_index>/<str:job_id>/', maybe_wrap(views.job_detail), name=f'{name_prefix}job_detail'), |
| 68 |
path( |
| 69 |
'queues/<int:queue_index>/<str:job_id>/results/<str:result_id>/', |
| 70 |
maybe_wrap(views.result_detail), |
| 71 |
name=f'{name_prefix}result_detail', |
| 72 |
), |
| 73 |
path('queues/<int:queue_index>/<str:job_id>/delete/', maybe_wrap(views.delete_job), name=f'{name_prefix}delete_job'), |
| 74 |
path('queues/<int:queue_index>/<str:job_id>/requeue/', maybe_wrap(views.requeue_job_view), name=f'{name_prefix}requeue_job'), |
| 75 |
path('queues/<int:queue_index>/<str:job_id>/enqueue/', maybe_wrap(views.enqueue_job), name=f'{name_prefix}enqueue_job'), |
| 76 |
path('queues/<int:queue_index>/<str:job_id>/stop/', maybe_wrap(views.stop_job), name=f'{name_prefix}stop_job'), |
| 77 |
# Bulk actions |
| 78 |
path('queues/confirm-action/<int:queue_index>/', maybe_wrap(views.confirm_action), name=f'{name_prefix}confirm_action'), |
| 79 |
path('queues/actions/<int:queue_index>/', maybe_wrap(views.actions), name=f'{name_prefix}actions'), |
| 80 |
# Workers |
| 81 |
path('workers/<int:queue_index>/', maybe_wrap(views.workers), name=f'{name_prefix}workers'), |
| 82 |
path('workers/<int:queue_index>/<str:key>/', maybe_wrap(views.worker_details), name=f'{name_prefix}worker_details'), |
| 83 |
# Schedulers |
| 84 |
path('schedulers/<int:scheduler_index>/', maybe_wrap(views.scheduler_jobs), name=f'{name_prefix}scheduler_jobs'), |
| 85 |
path( |
| 86 |
'cron-schedulers/<int:connection_index>/<str:scheduler_name>/', |
| 87 |
maybe_wrap(cron_views.cron_scheduler_detail), |
| 88 |
name=f'{name_prefix}cron_scheduler_detail', |
| 89 |
), |
| 90 |
# `path` rather than `str` because a cron job's name is user supplied and may contain slashes |
| 91 |
path( |
| 92 |
'cron-schedulers/<int:connection_index>/<str:scheduler_name>/jobs/<path:cron_job_name>/', |
| 93 |
maybe_wrap(cron_views.cron_job_detail), |
| 94 |
name=f'{name_prefix}cron_job_detail', |
| 95 |
), |
| 96 |
] |
| 97 |
|
| 98 |
|
| 99 |
# Standalone URL patterns (for use with include('django_rq.urls')) |
| 100 |
# Combines both API and admin patterns without wrapping |
| 101 |
app_name = "django_rq" |
| 102 |
urlpatterns = get_api_urlpatterns() + get_admin_urlpatterns() |