forked from mirrors/bookwyrm
Separate import classes into files
This commit is contained in:
parent
be26e8363a
commit
9bff27e61f
4 changed files with 109 additions and 91 deletions
|
@ -44,7 +44,9 @@ from .shelf.shelf_actions import create_shelf, delete_shelf
|
|||
from .shelf.shelf_actions import shelve, unshelve
|
||||
|
||||
# csv import
|
||||
from .imports.import_data import Import, ImportStatus, ImportTroubleshoot
|
||||
from .imports.import_data import Import
|
||||
from .imports.import_status import ImportStatus
|
||||
from .imports.troubelshoot import ImportTroubleshoot
|
||||
|
||||
# misc views
|
||||
from .author import Author, EditAuthor
|
||||
|
|
|
@ -1,25 +1,16 @@
|
|||
""" import books from another app """
|
||||
from io import TextIOWrapper
|
||||
import math
|
||||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.core.paginator import Paginator
|
||||
from django.http import HttpResponseBadRequest
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.shortcuts import redirect
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm import forms, models
|
||||
from bookwyrm.importers import (
|
||||
Importer,
|
||||
LibrarythingImporter,
|
||||
GoodreadsImporter,
|
||||
StorygraphImporter,
|
||||
)
|
||||
from bookwyrm.settings import PAGE_LENGTH
|
||||
from bookwyrm.importers import LibrarythingImporter, GoodreadsImporter, StorygraphImporter
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
@method_decorator(login_required, name="dispatch")
|
||||
|
@ -72,82 +63,3 @@ class Import(View):
|
|||
|
||||
return redirect(f"/import/{job.id}")
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
|
||||
@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")
|
||||
pending_items = items.filter(fail_reason__isnull=True, book__isnull=True)
|
||||
item_count = items.count() or 1
|
||||
|
||||
paginated = Paginator(items, PAGE_LENGTH)
|
||||
page = paginated.get_page(request.GET.get("page"))
|
||||
data = {
|
||||
"job": job,
|
||||
"items": page,
|
||||
"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(),
|
||||
"page_range": paginated.get_elided_page_range(
|
||||
page.number, on_each_side=2, on_ends=1
|
||||
),
|
||||
"complete": not pending_items.exists(),
|
||||
"percent": math.floor( # pylint: disable=c-extension-no-member
|
||||
(item_count - pending_items.count()) / item_count * 100
|
||||
),
|
||||
}
|
||||
|
||||
return TemplateResponse(request, "import/import_status.html", data)
|
||||
|
||||
|
||||
@method_decorator(login_required, name="dispatch")
|
||||
class ImportTroubleshoot(View):
|
||||
"""problems items in 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").filter(
|
||||
fail_reason__isnull=False, book_guess__isnull=False
|
||||
)
|
||||
|
||||
paginated = Paginator(items, PAGE_LENGTH)
|
||||
page = paginated.get_page(request.GET.get("page"))
|
||||
data = {
|
||||
"job": job,
|
||||
"items": page,
|
||||
"page_range": paginated.get_elided_page_range(
|
||||
page.number, on_each_side=2, on_ends=1
|
||||
),
|
||||
"complete": True,
|
||||
}
|
||||
|
||||
return TemplateResponse(request, "import/troubleshoot.html", data)
|
||||
|
||||
def post(self, request, job_id):
|
||||
"""retry lines from an import"""
|
||||
job = get_object_or_404(models.ImportJob, id=job_id)
|
||||
items = job.items.filter(fail_reason__isnull=False)
|
||||
|
||||
importer = Importer()
|
||||
job = importer.create_retry_job(
|
||||
request.user,
|
||||
job,
|
||||
items,
|
||||
)
|
||||
importer.start_import(job)
|
||||
return redirect(f"/import/{job.id}")
|
||||
|
|
50
bookwyrm/views/imports/import_status.py
Normal file
50
bookwyrm/views/imports/import_status.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
""" 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
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm import models
|
||||
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")
|
||||
pending_items = items.filter(fail_reason__isnull=True, book__isnull=True)
|
||||
item_count = items.count() or 1
|
||||
|
||||
paginated = Paginator(items, PAGE_LENGTH)
|
||||
page = paginated.get_page(request.GET.get("page"))
|
||||
data = {
|
||||
"job": job,
|
||||
"items": page,
|
||||
"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(),
|
||||
"page_range": paginated.get_elided_page_range(
|
||||
page.number, on_each_side=2, on_ends=1
|
||||
),
|
||||
"complete": not pending_items.exists(),
|
||||
"percent": math.floor( # pylint: disable=c-extension-no-member
|
||||
(item_count - pending_items.count()) / item_count * 100
|
||||
),
|
||||
}
|
||||
|
||||
return TemplateResponse(request, "import/import_status.html", data)
|
54
bookwyrm/views/imports/troubleshoot.py
Normal file
54
bookwyrm/views/imports/troubleshoot.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
""" import books from another app """
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.core.paginator import Paginator
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm import models
|
||||
from bookwyrm.importers import Importer
|
||||
from bookwyrm.settings import PAGE_LENGTH
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
@method_decorator(login_required, name="dispatch")
|
||||
class ImportTroubleshoot(View):
|
||||
"""problems items in 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").filter(
|
||||
fail_reason__isnull=False, book_guess__isnull=False
|
||||
)
|
||||
|
||||
paginated = Paginator(items, PAGE_LENGTH)
|
||||
page = paginated.get_page(request.GET.get("page"))
|
||||
data = {
|
||||
"job": job,
|
||||
"items": page,
|
||||
"page_range": paginated.get_elided_page_range(
|
||||
page.number, on_each_side=2, on_ends=1
|
||||
),
|
||||
"complete": True,
|
||||
}
|
||||
|
||||
return TemplateResponse(request, "import/troubleshoot.html", data)
|
||||
|
||||
def post(self, request, job_id):
|
||||
"""retry lines from an import"""
|
||||
job = get_object_or_404(models.ImportJob, id=job_id)
|
||||
items = job.items.filter(fail_reason__isnull=False)
|
||||
|
||||
importer = Importer()
|
||||
job = importer.create_retry_job(
|
||||
request.user,
|
||||
job,
|
||||
items,
|
||||
)
|
||||
importer.start_import(job)
|
||||
return redirect(f"/import/{job.id}")
|
Loading…
Reference in a new issue