Skip to content

Commit

Permalink
Update codestyle, history, add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Eg0ra committed Dec 13, 2024
1 parent 1979ac6 commit 5a23598
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ UNRELEASED
------------------
* Fix issue with slow export duration (https://github.com/saritasa-nest/django-import-export-extensions/issues/79):

* Add setting ``STATUS_UPDATE_ROW_COUNT`` which defines the number of rows after import/export of which the task status is updated;
* Add setting ``STATUS_UPDATE_ROW_COUNT`` (default: 100) which defines the number of rows after import/export of which the task status is updated;
* Add ability to specify ``status_update_row_count`` for each resource;

1.1.0 (2024-12-06)
Expand Down
39 changes: 21 additions & 18 deletions import_export_extensions/resources.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import collections
import enum
import functools
import typing
from enum import Enum
from functools import cached_property

from django.conf import settings
from django.db.models import QuerySet
Expand All @@ -19,7 +19,7 @@
from .results import Error, Result, RowResult


class TaskState(Enum):
class TaskState(enum.Enum):
"""Class with possible task state values."""

IMPORTING = _("Importing")
Expand Down Expand Up @@ -47,7 +47,7 @@ def __init__(
self.current_object_number = 0
super().__init__()

@cached_property
@functools.cached_property
def status_update_row_count(self):
"""Rows count after which to update celery task status."""
return getattr(
Expand Down Expand Up @@ -208,7 +208,10 @@ def export(
if queryset is None:
queryset = self.get_queryset()

# Necessary for correct calculation of the total, this method is called
# later inside parent resource class
queryset = self.filter_export(queryset, **kwargs)

self.initialize_task_state(
state=TaskState.EXPORTING.name,
queryset=queryset,
Expand Down Expand Up @@ -243,17 +246,18 @@ def initialize_task_state(
if not current_task or current_task.request.called_directly:
return

if isinstance(queryset, QuerySet):
self.total_objects_count = queryset.count()
else:
self.total_objects_count = len(queryset)
self.total_objects_count = (
queryset.count()
if isinstance(queryset, QuerySet)
else len(queryset)
)

self._update_current_task_state(
state=state,
meta=dict(
current=self.current_object_number,
total=self.total_objects_count,
),
meta={
"current": self.current_object_number,
"total": self.total_objects_count,
},
)

def update_task_state(
Expand All @@ -262,8 +266,7 @@ def update_task_state(
):
"""Update state of the current event.
Receives meta of the current task and increase the `current`
field by 1.
Receives meta of the current task and increase the `current`.
"""
if not current_task or current_task.request.called_directly:
Expand All @@ -276,10 +279,10 @@ def update_task_state(
):
self._update_current_task_state(
state=state,
meta=dict(
current=self.current_object_number,
total=self.total_objects_count,
),
meta={
"current": self.current_object_number,
"total": self.total_objects_count,
},
)

def _update_current_task_state(self, state: str, meta: dict[str, int]):
Expand Down

0 comments on commit 5a23598

Please sign in to comment.