Skip to content
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

Make it working in multiple admin sites context #59

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's provide a link to the Django Documentation about multiple/custom admin site (https://docs.djangoproject.com/en/stable/ref/contrib/admin/#customizing-the-adminsite-class)


.. 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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like BaseAdminSite is not defined here

Please, add definition or import of BaseAdminSite to make the example more realistic (should be obvious it BaseAdminSite is existing class or defined by user)

# ...
cutom_admin_site.register(ExportJob, ExportJobAdmin)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cutom_admin_site.register(ExportJob, ExportJobAdmin)
custom_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 @@ -42,6 +42,13 @@ class ExportJobAdmin(
"cancel_jobs",
)

def get_form(self, request: WSGIRequest, obj=None, **kwargs):
"""Push admin site to form."""
form = super().get_form(request, obj, **kwargs)

form.admin_site = self.admin_site
return form
Comment on lines +45 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really required? Why?

Please try to answer questions why? rather than what does it do? in docstring

We also made some changes to the admin mixins, so this logic can probably be put into BaseCeleryImportExportAdminMixin. Please check the latest changes when you get back to work


def export_data_action(
self,
request: WSGIRequest,
Expand All @@ -67,7 +74,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 @@ -43,6 +43,13 @@ class ImportJobAdmin(
"confirm_jobs",
)

def get_form(self, request: WSGIRequest, obj=None, **kwargs):
"""Push admin site to form."""
form = super().get_form(request, obj, **kwargs)

form.admin_site = self.admin_site
return form

def get_queryset(self, request: WSGIRequest):
"""Override `get_queryset`.

Expand All @@ -61,9 +68,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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name= "import_job_progress",
name="import_job_progress",

),
]
return import_urls + urls
Expand Down