From 2711f571704a9042ab0efe638ea1d9f7a9a89931 Mon Sep 17 00:00:00 2001 From: nikitaazanov Date: Mon, 11 Sep 2023 13:07:18 +0700 Subject: [PATCH] Improve code style --- import_export_extensions/admin/mixins/export_mixin.py | 8 ++++++-- import_export_extensions/admin/mixins/import_mixin.py | 8 ++++++-- .../admin/model_admins/export_job_admin.py | 7 ++++--- .../admin/model_admins/import_job_admin.py | 9 +++++---- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/import_export_extensions/admin/mixins/export_mixin.py b/import_export_extensions/admin/mixins/export_mixin.py index 0596eee..ac3bcc0 100644 --- a/import_export_extensions/admin/mixins/export_mixin.py +++ b/import_export_extensions/admin/mixins/export_mixin.py @@ -315,7 +315,7 @@ def _redirect_to_export_results_page( url = f"{url}?{query}" return HttpResponseRedirect(redirect_to=url) - def has_export_permission(self, request): + def has_export_permission(self, request: WSGIRequest): """Return whether a request has export permission.""" EXPORT_PERMISSION_CODE = getattr( settings, @@ -329,7 +329,11 @@ def has_export_permission(self, request): codename = get_permission_codename(EXPORT_PERMISSION_CODE, opts) return request.user.has_perm("%s.%s" % (opts.app_label, codename)) - def changelist_view(self, request, context=None): + def changelist_view( + self, + request: WSGIRequest, + context: typing.Optional[dict[str, typing.Any]] = None, + ): """Add the check for permission to changelist template context.""" context = context or {} context["has_export_permission"] = True diff --git a/import_export_extensions/admin/mixins/import_mixin.py b/import_export_extensions/admin/mixins/import_mixin.py index ccfbcf4..5f8f5b9 100644 --- a/import_export_extensions/admin/mixins/import_mixin.py +++ b/import_export_extensions/admin/mixins/import_mixin.py @@ -372,7 +372,7 @@ def _redirect_to_results_page( return HttpResponseRedirect(redirect_to=url) - def has_import_permission(self, request): + def has_import_permission(self, request: WSGIRequest): """Return whether a request has import permission.""" IMPORT_PERMISSION_CODE = getattr( settings, @@ -386,7 +386,11 @@ def has_import_permission(self, request): codename = get_permission_codename(IMPORT_PERMISSION_CODE, opts) return request.user.has_perm("%s.%s" % (opts.app_label, codename)) - def changelist_view(self, request, context=None): + def changelist_view( + self, + request: WSGIRequest, + context: typing.Optional[dict[str, typing.Any]] = None, + ): """Add the check for permission to changelist template context.""" context = context or {} context["has_import_permission"] = self.has_import_permission(request) diff --git a/import_export_extensions/admin/model_admins/export_job_admin.py b/import_export_extensions/admin/model_admins/export_job_admin.py index 82abdd7..6a72c50 100644 --- a/import_export_extensions/admin/model_admins/export_job_admin.py +++ b/import_export_extensions/admin/model_admins/export_job_admin.py @@ -2,6 +2,7 @@ from django.contrib import admin, messages from django.core.handlers.wsgi import WSGIRequest +from django.db.models import QuerySet from django.http import JsonResponse from django.urls import re_path from django.utils.translation import gettext_lazy as _ @@ -36,9 +37,9 @@ class ExportJobAdmin( export_job_model = models.ExportJob list_filter = ("export_status",) list_select_related = ("created_by",) - actions = [ + actions = ( "cancel_jobs", - ] + ) def export_data_action( self, @@ -215,7 +216,7 @@ def get_fieldsets( return [status, traceback_fields, export_params] @admin.action(description="Cancel selected jobs") - def cancel_jobs(self, request, queryset): + def cancel_jobs(self, request: WSGIRequest, queryset: QuerySet): """Admin action for cancelling data export.""" for job in queryset: try: diff --git a/import_export_extensions/admin/model_admins/import_job_admin.py b/import_export_extensions/admin/model_admins/import_job_admin.py index a884acd..e25ec17 100644 --- a/import_export_extensions/admin/model_admins/import_job_admin.py +++ b/import_export_extensions/admin/model_admins/import_job_admin.py @@ -2,6 +2,7 @@ from django.contrib import admin, messages from django.core.handlers.wsgi import WSGIRequest +from django.db.models import QuerySet from django.http import JsonResponse from django.template.loader import render_to_string from django.urls import re_path @@ -38,10 +39,10 @@ class ImportJobAdmin( import_job_model = models.ImportJob list_filter = ("import_status",) list_select_related = ("created_by",) - actions = [ + actions = ( "cancel_jobs", "confirm_jobs", - ] + ) def get_queryset(self, request: WSGIRequest): """Override `get_queryset`. @@ -260,7 +261,7 @@ def get_fieldsets( return [status, traceback_, import_params] @admin.action(description="Cancel selected jobs") - def cancel_jobs(self, request, queryset): + def cancel_jobs(self, request: WSGIRequest, queryset: QuerySet): """Admin action for cancelling data import.""" for job in queryset: try: @@ -274,7 +275,7 @@ def cancel_jobs(self, request, queryset): self.message_user(request, str(error), messages.ERROR) @admin.action(description="Confirm selected jobs") - def confirm_jobs(self, request, queryset): + def confirm_jobs(self, request: WSGIRequest, queryset: QuerySet): """Admin action for confirming data import.""" for job in queryset: try: