Skip to content

Commit

Permalink
Fix ruff issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Eg0ra committed Sep 4, 2024
1 parent d516c13 commit 870c2eb
Show file tree
Hide file tree
Showing 46 changed files with 330 additions and 611 deletions.
14 changes: 9 additions & 5 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import os
import pathlib
import sys

import django

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.append(os.path.abspath("."))
sys.path.append(os.path.abspath(".."))
sys.path.append(os.path.abspath("../tests"))
sys.path.append(str(pathlib.Path.cwd()))
sys.path.append(str(pathlib.Path("..").resolve()))
sys.path.append(str(pathlib.Path("../tests").resolve()))
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"

django.setup()
Expand Down Expand Up @@ -106,7 +107,7 @@
]


# -- Options for Texinfo output ------------------------------------------------
# -- Options for Texinfo output -----------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
Expand All @@ -129,5 +130,8 @@
# intersphinx documentation
intersphinx_mapping = {
"tablib": ("https://tablib.readthedocs.io/en/stable/", None),
"django-import-export": ("https://django-import-export.readthedocs.io/en/latest/", None),
"django-import-export": (
"https://django-import-export.readthedocs.io/en/latest/",
None,
),
}
1 change: 1 addition & 0 deletions import_export_extensions/admin/forms/import_admin_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class ForceImportForm(base_forms.ImportForm):
"""Import form with `force_import` option."""

force_import = forms.BooleanField(
required=False,
initial=False,
Expand Down
24 changes: 14 additions & 10 deletions import_export_extensions/admin/mixins/export_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,24 @@ class CeleryExportAdminMixin(
If errors - traceback and error message.
"""

# export data encoding
to_encoding = "utf-8"

# template used to display ExportForm
celery_export_template_name = "admin/import_export/export.html"

export_status_template_name = "admin/import_export_extensions/celery_export_status.html"
export_status_template_name = (
"admin/import_export_extensions/celery_export_status.html"
)

export_results_template_name = "admin/import_export_extensions/celery_export_results.html"
export_results_template_name = (
"admin/import_export_extensions/celery_export_results.html"
)

import_export_change_list_template = "admin/import_export/change_list_export.html"
import_export_change_list_template = (
"admin/import_export/change_list_export.html"
)

# Statuses that should be displayed on 'results' page
export_results_statuses = models.ExportJob.export_finished_statuses
Expand Down Expand Up @@ -262,11 +269,8 @@ def create_export_job(
job = models.ExportJob.objects.create(
resource_path=resource_class.class_path,
resource_kwargs=resource_kwargs,
file_format_path=".".join(
[
file_format.__module__,
file_format.__name__,
],
file_format_path=(
f"{file_format.__module__}.{file_format.__name__}"
),
)
return job
Expand All @@ -278,7 +282,7 @@ def get_export_job(
) -> models.ExportJob:
"""Get ExportJob instance.
Raises:
Raises
Http404
"""
Expand Down Expand Up @@ -317,7 +321,7 @@ def _redirect_to_export_results_page(
def changelist_view(
self,
request: WSGIRequest,
context: typing.Optional[dict[str, typing.Any]] = None,
context: dict[str, typing.Any] | None = None,
):
"""Add the check for permission to changelist template context."""
context = context or {}
Expand Down
4 changes: 3 additions & 1 deletion import_export_extensions/admin/mixins/import_export_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ class CeleryImportExportMixin(
"""Import and export mixin."""

# template for change_list view
import_export_change_list_template = "admin/import_export/change_list_import_export.html"
import_export_change_list_template = (
"admin/import_export/change_list_import_export.html"
)
25 changes: 18 additions & 7 deletions import_export_extensions/admin/mixins/import_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class CeleryImportAdminMixin(
with progress bar and import totals.
"""

# Import data encoding
from_encoding = "utf-8"

Expand All @@ -59,12 +60,18 @@ class CeleryImportAdminMixin(
celery_import_template = "admin/import_export/import.html"

# Template used to display status of import jobs
import_status_template = "admin/import_export_extensions/celery_import_status.html"
import_status_template = (
"admin/import_export_extensions/celery_import_status.html"
)

# template used to display results of import jobs
import_result_template_name = "admin/import_export_extensions/celery_import_results.html"
import_result_template_name = (
"admin/import_export_extensions/celery_import_results.html"
)

import_export_change_list_template = "admin/import_export/change_list_import.html"
import_export_change_list_template = (
"admin/import_export/change_list_import.html"
)

skip_admin_log = None
# Copy methods of mixin from original package to reuse it here
Expand Down Expand Up @@ -323,7 +330,11 @@ def create_import_job(
data_file=form.cleaned_data["import_file"],
resource_kwargs=resource.resource_init_kwargs,
created_by=request.user,
skip_parse_step=getattr(settings, "IMPORT_EXPORT_SKIP_ADMIN_CONFIRM", False),
skip_parse_step=getattr(
settings,
"IMPORT_EXPORT_SKIP_ADMIN_CONFIRM",
False,
),
force_import=form.cleaned_data["force_import"],
)

Expand All @@ -334,7 +345,7 @@ def get_import_job(
) -> models.ImportJob:
"""Get ImportJob instance.
Raises:
Raises
Http404
"""
Expand Down Expand Up @@ -366,7 +377,7 @@ def _redirect_to_results_page(
url = reverse(url_name, kwargs=dict(job_id=job.id))
query = request.GET.urlencode()
url = f"{url}?{query}" if query else url
if not job.import_status == models.ImportJob.ImportStatus.PARSED:
if job.import_status != models.ImportJob.ImportStatus.PARSED:
return HttpResponseRedirect(redirect_to=url)

# Redirections add one by one links to `redirect_to`
Expand All @@ -388,7 +399,7 @@ def _redirect_to_results_page(
def changelist_view(
self,
request: WSGIRequest,
context: typing.Optional[dict[str, typing.Any]] = None,
context: dict[str, typing.Any] | None = None,
):
"""Add the check for permission to changelist template context."""
context = context or {}
Expand Down
10 changes: 3 additions & 7 deletions import_export_extensions/admin/mixins/types.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import dataclasses
import typing

from django.db.models.options import Options

from import_export.formats import base_formats

from ... import resources

ResourceObj = typing.Union[
resources.CeleryResource,
resources.CeleryModelResource,
]
ResourceType = typing.Type[ResourceObj]
FormatType = typing.Type[base_formats.Format]
ResourceObj = resources.CeleryResource | resources.CeleryModelResource
ResourceType = type[ResourceObj]
FormatType = type[base_formats.Format]


@dataclasses.dataclass
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import typing

from django.contrib import admin, messages
from django.core.handlers.wsgi import WSGIRequest
Expand Down Expand Up @@ -150,7 +149,7 @@ def get_readonly_fields(self, request, obj=None):
def get_fieldsets(
self,
request: WSGIRequest,
obj: typing.Optional[models.ExportJob] = None,
obj: models.ExportJob | None = None,
):
"""Get fieldsets depending on object status."""
status = (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import typing

from django.contrib import admin, messages
from django.core.handlers.wsgi import WSGIRequest
Expand Down Expand Up @@ -118,7 +117,7 @@ def import_job_progress_view(
def get_readonly_fields(
self,
request: WSGIRequest,
obj: typing.Optional[models.ImportJob] = None,
obj: models.ImportJob | None = None,
) -> list[str]:
"""Return readonly fields.
Expand Down Expand Up @@ -151,7 +150,7 @@ def get_readonly_fields(

def _show_results(
self,
obj: typing.Optional[models.ImportJob] = None,
obj: models.ImportJob | None = None,
) -> str:
"""Show results totals.
Expand Down Expand Up @@ -189,7 +188,7 @@ def _input_errors(self, job: models.ImportJob):
def get_fieldsets(
self,
request: WSGIRequest,
obj: typing.Optional[models.ImportJob] = None,
obj: models.ImportJob | None = None,
):
"""Get fieldsets depending on object status."""
status = (
Expand Down
5 changes: 2 additions & 3 deletions import_export_extensions/admin/model_admins/mixins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import typing

from django.contrib.contenttypes.models import ContentType
from django.core.handlers.wsgi import WSGIRequest
Expand Down Expand Up @@ -45,8 +44,8 @@ def _model(self, obj: models.ImportJob) -> str:

def get_from_content_type(
self,
obj: typing.Union[models.ImportJob, models.ExportJob],
) -> typing.Union[ContentType, None]:
obj: models.ImportJob | models.ExportJob,
) -> ContentType | None:
"""Shortcut to get object from content_type."""
content_type = obj.resource_kwargs.get("content_type")
obj_id = obj.resource_kwargs.get("object_id")
Expand Down
16 changes: 8 additions & 8 deletions import_export_extensions/api/serializers/export_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class ExportProgressSerializer(ProgressSerializer):
"""Serializer to show ExportJob progress."""

state = serializers.ChoiceField(
choices=models.ExportJob.ExportStatus.values
+ [
choices=[
*models.ExportJob.ExportStatus.values,
states.PENDING,
states.STARTED,
states.SUCCESS,
Expand Down Expand Up @@ -48,13 +48,13 @@ class CreateExportJob(serializers.Serializer):
"""

resource_class: typing.Type[resources.CeleryModelResource]
resource_class: type[resources.CeleryModelResource]

def __init__(
self,
*args,
filter_kwargs: typing.Optional[dict[str, typing.Any]] = None,
resource_kwargs: typing.Optional[dict[str, typing.Any]] = None,
filter_kwargs: dict[str, typing.Any] | None = None,
resource_kwargs: dict[str, typing.Any] | None = None,
**kwargs,
):
"""Set filter kwargs and current user."""
Expand Down Expand Up @@ -99,14 +99,14 @@ def update(self, instance, validated_data):


def get_create_export_job_serializer(
resource: typing.Type[resources.CeleryModelResource],
) -> typing.Type: # type: ignore
resource: type[resources.CeleryModelResource],
) -> type:
"""Create serializer for ExportJobs creation."""

class _CreateExportJob(CreateExportJob):
"""Serializer to start export job."""

resource_class: typing.Type[resources.CeleryModelResource] = resource
resource_class: type[resources.CeleryModelResource] = resource
file_format = serializers.ChoiceField(
required=True,
choices=[
Expand Down
14 changes: 7 additions & 7 deletions import_export_extensions/api/serializers/import_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class ImportProgressSerializer(ProgressSerializer):
"""Serializer to show ImportJob progress."""

state = serializers.ChoiceField(
choices=models.ImportJob.ImportStatus.values
+ [
choices=[
*models.ImportJob.ImportStatus.values,
states.PENDING,
states.STARTED,
states.SUCCESS,
Expand Down Expand Up @@ -90,7 +90,7 @@ class CreateImportJob(serializers.Serializer):
"""

resource_class: typing.Type[resources.CeleryModelResource]
resource_class: type[resources.CeleryModelResource]

file = serializers.FileField(required=True)
force_import = serializers.BooleanField(default=False, required=False)
Expand All @@ -99,7 +99,7 @@ class CreateImportJob(serializers.Serializer):
def __init__(
self,
*args,
resource_kwargs: typing.Optional[dict[str, typing.Any]] = None,
resource_kwargs: dict[str, typing.Any] | None = None,
**kwargs,
):
"""Set filter kwargs and current user."""
Expand Down Expand Up @@ -127,14 +127,14 @@ def update(self, instance, validated_data):


def get_create_import_job_serializer(
resource: typing.Type[resources.CeleryModelResource],
) -> typing.Type: # type: ignore
resource: type[resources.CeleryModelResource],
) -> type:
"""Create serializer for ImportJobs creation."""

class _CreateImportJob(CreateImportJob):
"""Serializer to start import job."""

resource_class: typing.Type[resources.CeleryModelResource] = resource
resource_class: type[resources.CeleryModelResource] = resource

return type(
f"{resource.__name__}CreateImportJob",
Expand Down
Loading

0 comments on commit 870c2eb

Please sign in to comment.