2021-11-12 23:50:33 +00:00
|
|
|
""" import books from another app """
|
|
|
|
import math
|
|
|
|
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.core.exceptions import PermissionDenied
|
|
|
|
from django.core.paginator import Paginator
|
2021-11-14 18:20:14 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
2021-11-12 23:50:33 +00:00
|
|
|
from django.template.response import TemplateResponse
|
2021-11-14 18:20:14 +00:00
|
|
|
from django.utils import timezone
|
2021-11-12 23:50:33 +00:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views import View
|
2021-11-14 18:58:46 +00:00
|
|
|
from django.views.decorators.http import require_POST
|
2021-11-12 23:50:33 +00:00
|
|
|
|
|
|
|
from bookwyrm import models
|
2021-11-14 18:58:46 +00:00
|
|
|
from bookwyrm.importers import GoodreadsImporter
|
2021-11-14 18:20:14 +00:00
|
|
|
from bookwyrm.importers.importer import import_item_task
|
2021-11-12 23:50:33 +00:00
|
|
|
from bookwyrm.settings import PAGE_LENGTH
|
|
|
|
|
|
|
|
# pylint: disable= no-self-use
|
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
class ImportStatus(View):
|
|
|
|
"""status of an existing import"""
|
|
|
|
|
|
|
|
def get(self, request, job_id):
|
|
|
|
"""status of an import job"""
|
|
|
|
job = get_object_or_404(models.ImportJob, id=job_id)
|
|
|
|
if job.user != request.user:
|
|
|
|
raise PermissionDenied()
|
|
|
|
|
|
|
|
items = job.items.order_by("index")
|
|
|
|
item_count = items.count() or 1
|
|
|
|
|
|
|
|
paginated = Paginator(items, PAGE_LENGTH)
|
|
|
|
page = paginated.get_page(request.GET.get("page"))
|
2021-11-15 17:34:36 +00:00
|
|
|
manual_review_count = items.filter(
|
|
|
|
fail_reason__isnull=False, book_guess__isnull=False, book__isnull=True
|
|
|
|
).count()
|
|
|
|
fail_count = items.filter(
|
|
|
|
fail_reason__isnull=False, book_guess__isnull=True
|
|
|
|
).count()
|
|
|
|
pending_item_count = job.pending_items.count()
|
2021-11-12 23:50:33 +00:00
|
|
|
data = {
|
|
|
|
"job": job,
|
|
|
|
"items": page,
|
2021-11-15 17:34:36 +00:00
|
|
|
"manual_review_count": manual_review_count,
|
|
|
|
"fail_count": fail_count,
|
2021-11-12 23:50:33 +00:00
|
|
|
"page_range": paginated.get_elided_page_range(
|
|
|
|
page.number, on_each_side=2, on_ends=1
|
|
|
|
),
|
2021-11-15 17:34:36 +00:00
|
|
|
"item_count": item_count,
|
|
|
|
"complete_count": item_count - pending_item_count,
|
2021-11-12 23:50:33 +00:00
|
|
|
"percent": math.floor( # pylint: disable=c-extension-no-member
|
2021-11-15 17:34:36 +00:00
|
|
|
(item_count - pending_item_count) / item_count * 100
|
2021-11-12 23:50:33 +00:00
|
|
|
),
|
2021-11-14 18:20:14 +00:00
|
|
|
# hours since last import item update
|
|
|
|
"inactive_time": (job.updated_date - timezone.now()).seconds / 60 / 60,
|
2021-11-14 18:58:46 +00:00
|
|
|
"legacy": not job.mappings,
|
2021-11-12 23:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TemplateResponse(request, "import/import_status.html", data)
|
2021-11-14 18:20:14 +00:00
|
|
|
|
2021-11-14 18:58:46 +00:00
|
|
|
def post(self, request, job_id):
|
|
|
|
"""bring a legacy import into the latest format"""
|
|
|
|
job = get_object_or_404(models.ImportJob, id=job_id)
|
|
|
|
if job.user != request.user:
|
|
|
|
raise PermissionDenied()
|
|
|
|
GoodreadsImporter().update_legacy_job(job)
|
2021-11-14 18:20:14 +00:00
|
|
|
return redirect("import-status", job_id)
|
2021-11-14 18:58:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@require_POST
|
|
|
|
def retry_item(request, job_id, item_id):
|
|
|
|
"""retry an item"""
|
|
|
|
item = get_object_or_404(
|
|
|
|
models.ImportItem, id=item_id, job__id=job_id, job__user=request.user
|
|
|
|
)
|
|
|
|
import_item_task.delay(item.id)
|
|
|
|
return redirect("import-status", job_id)
|