diff --git a/HISTORY.rst b/HISTORY.rst index 74d6280..ed31218 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -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) diff --git a/import_export_extensions/resources.py b/import_export_extensions/resources.py index 9fbeff8..16a7f93 100644 --- a/import_export_extensions/resources.py +++ b/import_export_extensions/resources.py @@ -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 @@ -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") @@ -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( @@ -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, @@ -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( @@ -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: @@ -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]):