bookwyrm-mastodon/bookwyrm/views/import_data.py

147 lines
4.8 KiB
Python
Raw Normal View History

2021-03-08 11:49:10 -05:00
""" import books from another app """
2021-01-12 14:28:03 -05:00
from io import TextIOWrapper
2021-11-12 16:10:52 -05:00
import math
2021-01-12 14:28:03 -05:00
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
2021-11-12 16:10:52 -05:00
from django.core.paginator import Paginator
2021-01-12 14:28:03 -05:00
from django.http import HttpResponseBadRequest
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.utils.decorators import method_decorator
from django.utils.translation import gettext_lazy as _
2021-01-12 14:28:03 -05:00
from django.views import View
2021-03-30 11:56:25 -04:00
from bookwyrm import forms, models
2021-05-10 14:56:35 -04:00
from bookwyrm.importers import (
Importer,
LibrarythingImporter,
GoodreadsImporter,
StorygraphImporter,
)
2021-11-12 16:10:52 -05:00
from bookwyrm.settings import PAGE_LENGTH
2021-01-12 14:28:03 -05:00
# pylint: disable= no-self-use
2021-03-08 11:49:10 -05:00
@method_decorator(login_required, name="dispatch")
2021-01-12 14:28:03 -05:00
class Import(View):
2021-04-26 12:15:42 -04:00
"""import view"""
2021-03-08 11:49:10 -05:00
2021-01-12 14:28:03 -05:00
def get(self, request):
2021-04-26 12:15:42 -04:00
"""load import page"""
2021-03-08 11:49:10 -05:00
return TemplateResponse(
request,
2021-09-08 10:28:42 -04:00
"import/import.html",
2021-03-08 11:49:10 -05:00
{
"import_form": forms.ImportForm(),
"jobs": models.ImportJob.objects.filter(user=request.user).order_by(
"-created_date"
),
},
)
2021-01-12 14:28:03 -05:00
def post(self, request):
2021-04-26 12:15:42 -04:00
"""ingest a goodreads csv"""
2021-01-12 14:28:03 -05:00
form = forms.ImportForm(request.POST, request.FILES)
if form.is_valid():
2021-03-08 11:49:10 -05:00
include_reviews = request.POST.get("include_reviews") == "on"
privacy = request.POST.get("privacy")
source = request.POST.get("source")
2021-02-20 11:02:36 -05:00
importer = None
2021-03-08 11:49:10 -05:00
if source == "LibraryThing":
2021-03-30 11:56:25 -04:00
importer = LibrarythingImporter()
2021-05-10 13:00:51 -04:00
elif source == "Storygraph":
importer = StorygraphImporter()
2021-02-20 11:02:36 -05:00
else:
# Default : Goodreads
2021-03-30 11:56:25 -04:00
importer = GoodreadsImporter()
2021-02-20 11:02:36 -05:00
2021-01-12 14:28:03 -05:00
try:
2021-02-20 11:02:36 -05:00
job = importer.create_job(
2021-01-12 14:28:03 -05:00
request.user,
TextIOWrapper(
2021-03-08 11:49:10 -05:00
request.FILES["csv_file"], encoding=importer.encoding
),
2021-01-12 14:28:03 -05:00
include_reviews,
privacy,
)
except (UnicodeDecodeError, ValueError, KeyError):
return HttpResponseBadRequest(_("Not a valid csv file"))
2021-02-20 11:02:36 -05:00
importer.start_import(job)
2021-09-18 14:32:00 -04:00
return redirect(f"/import/{job.id}")
2021-01-12 14:28:03 -05:00
return HttpResponseBadRequest()
2021-03-08 11:49:10 -05:00
@method_decorator(login_required, name="dispatch")
2021-01-12 14:28:03 -05:00
class ImportStatus(View):
2021-04-26 12:15:42 -04:00
"""status of an existing import"""
2021-03-08 11:49:10 -05:00
2021-01-12 14:28:03 -05:00
def get(self, request, job_id):
2021-04-26 12:15:42 -04:00
"""status of an import job"""
job = get_object_or_404(models.ImportJob, id=job_id)
2021-01-12 14:28:03 -05:00
if job.user != request.user:
2021-09-27 18:57:22 -04:00
raise PermissionDenied()
2021-11-12 16:10:52 -05:00
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,
2021-11-12 17:36:28 -05:00
"fail_count": items.filter(fail_reason__isnull=False).count(),
2021-11-12 16:10:52 -05:00
"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)
2021-01-12 14:28:03 -05:00
2021-11-12 17:36:28 -05:00
@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)
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)
2021-01-12 14:28:03 -05:00
def post(self, request, job_id):
2021-04-26 12:15:42 -04:00
"""retry lines from an import"""
2021-01-12 14:28:03 -05:00
job = get_object_or_404(models.ImportJob, id=job_id)
2021-11-12 17:36:28 -05:00
items = job.items.filter(fail_reason__isnull=False)
2021-01-12 14:28:03 -05:00
2021-03-30 11:43:38 -04:00
importer = Importer()
job = importer.create_retry_job(
2021-01-12 14:28:03 -05:00
request.user,
job,
items,
)
2021-03-30 11:43:38 -04:00
importer.start_import(job)
2021-09-18 14:32:00 -04:00
return redirect(f"/import/{job.id}")