Skip to content

Commit

Permalink
Merge pull request #56 from saritasa-nest/feature/improve-coverage
Browse files Browse the repository at this point in the history
Add tests for export job admin progress
  • Loading branch information
Eg0ra authored Oct 24, 2024
2 parents f4ea71e + 06ffe14 commit 6e02e18
Show file tree
Hide file tree
Showing 5 changed files with 363 additions and 53 deletions.
37 changes: 20 additions & 17 deletions import_export_extensions/admin/model_admins/export_job_admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

import http

from django.contrib import admin, messages
from django.core.handlers.wsgi import WSGIRequest
from django.db.models import QuerySet
Expand Down Expand Up @@ -92,7 +94,10 @@ def export_job_progress_view(
id=job_id,
)
except self.export_job_model.DoesNotExist as error:
return JsonResponse(dict(validation_error=error.args[0]))
return JsonResponse(
dict(validation_error=error.args[0]),
status=http.HTTPStatus.NOT_FOUND,
)

response_data = dict(status=job.export_status.title())

Expand All @@ -102,16 +107,17 @@ def export_job_progress_view(
percent = 0
total = 0
current = 0
info = job.progress["info"]
job_progress = job.progress
progress_info = job_progress["info"]

if info and info["total"]:
percent = int(100 / info["total"] * info["current"])
total = info["total"]
current = info["current"]
if progress_info and progress_info["total"]:
total = progress_info["total"]
current = progress_info["current"]
percent = int(100 / total * current)

response_data.update(
dict(
state=job.progress["state"],
state=job_progress["state"],
percent=percent,
total=total,
current=current,
Expand All @@ -125,7 +131,9 @@ def get_readonly_fields(self, request, obj=None):
Some fields are editable for new ExportJob.
"""
readonly_fields = [
base_readonly_fields = super().get_readonly_fields(request, obj)
readonly_fields = (
*base_readonly_fields,
"export_status",
"traceback",
"file_format_path",
Expand All @@ -134,15 +142,10 @@ def get_readonly_fields(self, request, obj=None):
"export_finished",
"error_message",
"_model",
]
if obj:
readonly_fields.extend(
[
"resource_path",
"data_file",
"resource_kwargs",
],
)
"resource_path",
"data_file",
"resource_kwargs",
)

return readonly_fields

Expand Down
12 changes: 11 additions & 1 deletion import_export_extensions/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,17 @@ class Meta:


class TaskStateInfo(typing.TypedDict):
"""Class representing task state dict."""
"""Class representing task state dict.
Possible states:
1. PENDING
2. STARTED
3. SUCCESS
4. EXPORTING - custom status that also set export info
https://docs.celeryproject.org/en/latest/userguide/tasks.html#states
"""

state: str
info: dict[str, int] | None
Expand Down
43 changes: 8 additions & 35 deletions import_export_extensions/models/export_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,41 +157,14 @@ def export_filename(self) -> str:

@property
def progress(self) -> TaskStateInfo | None:
"""Return dict with parsing state.
Example for sync mode::
{
'state': 'EXPORTING',
'info': None
}
Example for celery (celery) mode::
{
'state': 'EXPORTING',
'info': {'current': 15, 'total': 100}
}
Possible states:
1. PENDING
2. STARTED
3. SUCCESS
4. EXPORTING - custom status that also set export info
https://docs.celeryproject.org/en/latest/userguide/tasks.html#states
"""
if self.export_status not in (self.ExportStatus.EXPORTING,):
return None

if not self.export_task_id or current_app.conf.task_always_eager:
return dict(
state=self.export_status.upper(),
info=None,
)

return self._get_task_state(self.export_task_id)
"""Return dict with export state."""
if (
self.export_task_id
and self.export_status == self.ExportStatus.EXPORTING
):
return self._get_task_state(self.export_task_id)

return None

def _check_export_status_correctness(
self,
Expand Down
Loading

0 comments on commit 6e02e18

Please sign in to comment.