Skip to content

Commit

Permalink
Make it working in multiple admin sites context
Browse files Browse the repository at this point in the history
  • Loading branch information
maisim committed Oct 25, 2024
1 parent 6e02e18 commit 322cbdf
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 11 deletions.
18 changes: 18 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,24 @@ Prepare view sets to import/export via API
resource_class = resources.BookResource
If you use multiple or custom admin site, register required models to them

.. code-block:: python
# app/admin.py
from import_export_extensions.models import ExportJob
from import_export_extensions.admin.model_admins.export_job_admin import ExportJobAdmin
from django.apps import AppConfig
custom_admin_site = BaseAdminSite(name="custom_admin")
# ...
cutom_admin_site.register(ExportJob, ExportJobAdmin)
Don't forget to `configure Celery <https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html>`_
if you want to run import/export in background

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(
):
"""Provide `export_progressbar` widget the `ExportJob` instance."""
super().__init__(*args, instance=instance, **kwargs)
url_name = "admin:export_job_progress"
url_name = f"{self.admin_site.name}:export_job_progress"
self.fields["export_progressbar"].widget = ProgressBarWidget(
job=instance,
url=reverse(url_name, args=(instance.id,)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(
):
"""Provide `import_progressbar` widget the ``ImportJob`` instance."""
super().__init__(*args, instance=instance, **kwargs)
url_name = "admin:import_job_progress"
url_name = f"{self.admin_site.name}:import_job_progress"
self.fields["import_progressbar"].label = (
"Import progress" if
instance.import_status == models.ImportJob.ImportStatus.IMPORTING
Expand Down
6 changes: 3 additions & 3 deletions import_export_extensions/admin/mixins/export_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def export_job_status_view(
)

context = self.get_context_data(request)
job_url = reverse("admin:export_job_progress", args=(job.id,))
job_url = reverse(f"{self.admin_site.name}:export_job_progress", args=(job.id,))

context["title"] = _("Export status")
context["opts"] = self.model_info.meta
Expand Down Expand Up @@ -289,7 +289,7 @@ def _redirect_to_export_status_page(
) -> HttpResponse:
"""Shortcut for redirecting to job's status page."""
url_name = (
f"admin:{self.model_info.app_model_name}_export_job_status"
f"{self.admin_site.name}:{self.model_info.app_model_name}_export_job_status"
)
url = reverse(url_name, kwargs=dict(job_id=job.id))
query = request.GET.urlencode()
Expand All @@ -304,7 +304,7 @@ def _redirect_to_export_results_page(
) -> HttpResponse:
"""Shortcut for redirecting to job's results page."""
url_name = (
f"admin:{self.model_info.app_model_name}_export_job_results"
f"{self.admin_site.name}:{self.model_info.app_model_name}_export_job_results"
)
url = reverse(url_name, kwargs=dict(job_id=job.id))
query = request.GET.urlencode()
Expand Down
6 changes: 3 additions & 3 deletions import_export_extensions/admin/mixins/import_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def celery_import_job_status_view(
)

context = self.get_context_data(request)
job_url = reverse("admin:import_job_progress", args=(job.id,))
job_url = reverse(f"{self.admin_site.name}:import_job_progress", args=(job.id,))
context.update(
dict(
title=_("Import status"),
Expand Down Expand Up @@ -362,7 +362,7 @@ def _redirect_to_import_status_page(
) -> HttpResponseRedirect:
"""Shortcut for redirecting to job's status page."""
url_name = (
f"admin:{self.model_info.app_model_name}_import_job_status"
f"{self.admin_site.name}:{self.model_info.app_model_name}_import_job_status"
)
url = reverse(url_name, kwargs=dict(job_id=job.id))
query = request.GET.urlencode()
Expand All @@ -376,7 +376,7 @@ def _redirect_to_results_page(
) -> HttpResponseRedirect:
"""Shortcut for redirecting to job's results page."""
url_name = (
f"admin:{self.model_info.app_model_name}_import_job_results"
f"{self.admin_site.name}:{self.model_info.app_model_name}_import_job_results"
)
url = reverse(url_name, kwargs=dict(job_id=job.id))
query = request.GET.urlencode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def get_urls(self):
urls = super().get_urls()
export_urls = [
re_path(
route=r"^(?P<job_id>\d+)/progress/$",
route=r"^celery-export/(?P<job_id>\d+)/progress/$",
view=self.admin_site.admin_view(self.export_job_progress_view),
name="export_job_progress",
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ def get_urls(self):
urls = super().get_urls()
import_urls = [
re_path(
r"^(?P<job_id>\d+)/progress/$",
r"^celery-import/(?P<job_id>\d+)/progress/$",
self.admin_site.admin_view(self.import_job_progress_view),
name="import_job_progress",
name= "import_job_progress",
),
]
return import_urls + urls
Expand Down

0 comments on commit 322cbdf

Please sign in to comment.