moviewyrm/bookwyrm/views/import_data.py

116 lines
3.8 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" import books from another app """
2021-01-12 19:28:03 +00:00
from io import TextIOWrapper
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
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 19:28:03 +00:00
from django.views import View
2021-03-30 15:56:25 +00:00
from bookwyrm import forms, models
2021-05-10 18:56:35 +00:00
from bookwyrm.importers import (
Importer,
LibrarythingImporter,
GoodreadsImporter,
StorygraphImporter,
)
2021-01-12 19:28:03 +00:00
from bookwyrm.tasks import app
# pylint: disable= no-self-use
2021-03-08 16:49:10 +00:00
@method_decorator(login_required, name="dispatch")
2021-01-12 19:28:03 +00:00
class Import(View):
2021-04-26 16:15:42 +00:00
"""import view"""
2021-03-08 16:49:10 +00:00
2021-01-12 19:28:03 +00:00
def get(self, request):
2021-04-26 16:15:42 +00:00
"""load import page"""
2021-03-08 16:49:10 +00:00
return TemplateResponse(
request,
2021-09-08 14:28:42 +00:00
"import/import.html",
2021-03-08 16:49:10 +00:00
{
"import_form": forms.ImportForm(),
"jobs": models.ImportJob.objects.filter(user=request.user).order_by(
"-created_date"
),
},
)
2021-01-12 19:28:03 +00:00
def post(self, request):
2021-04-26 16:15:42 +00:00
"""ingest a goodreads csv"""
2021-01-12 19:28:03 +00:00
form = forms.ImportForm(request.POST, request.FILES)
if form.is_valid():
2021-03-08 16:49:10 +00:00
include_reviews = request.POST.get("include_reviews") == "on"
privacy = request.POST.get("privacy")
source = request.POST.get("source")
2021-02-20 16:02:36 +00:00
importer = None
2021-03-08 16:49:10 +00:00
if source == "LibraryThing":
2021-03-30 15:56:25 +00:00
importer = LibrarythingImporter()
2021-05-10 17:00:51 +00:00
elif source == "Storygraph":
importer = StorygraphImporter()
2021-02-20 16:02:36 +00:00
else:
# Default : GoodReads
2021-03-30 15:56:25 +00:00
importer = GoodreadsImporter()
2021-02-20 16:02:36 +00:00
2021-01-12 19:28:03 +00:00
try:
2021-02-20 16:02:36 +00:00
job = importer.create_job(
2021-01-12 19:28:03 +00:00
request.user,
TextIOWrapper(
2021-03-08 16:49:10 +00:00
request.FILES["csv_file"], encoding=importer.encoding
),
2021-01-12 19:28:03 +00:00
include_reviews,
privacy,
)
except (UnicodeDecodeError, ValueError, KeyError):
return HttpResponseBadRequest(_("Not a valid csv file"))
2021-02-20 16:02:36 +00:00
importer.start_import(job)
2021-03-08 16:49:10 +00:00
return redirect("/import/%d" % job.id)
2021-01-12 19:28:03 +00:00
return HttpResponseBadRequest()
2021-03-08 16:49:10 +00:00
@method_decorator(login_required, name="dispatch")
2021-01-12 19:28:03 +00:00
class ImportStatus(View):
2021-04-26 16:15:42 +00:00
"""status of an existing import"""
2021-03-08 16:49:10 +00:00
2021-01-12 19:28:03 +00:00
def get(self, request, job_id):
2021-04-26 16:15:42 +00:00
"""status of an import job"""
job = get_object_or_404(models.ImportJob, id=job_id)
2021-01-12 19:28:03 +00:00
if job.user != request.user:
raise PermissionDenied
try:
task = app.AsyncResult(job.task_id)
# triggers attribute error if the task won't load
2021-06-28 20:54:38 +00:00
task.status # pylint: disable=pointless-statement
except (ValueError, AttributeError):
task = None
2021-03-08 16:49:10 +00:00
items = job.items.order_by("index").all()
2021-01-12 19:28:03 +00:00
failed_items = [i for i in items if i.fail_reason]
items = [i for i in items if not i.fail_reason]
2021-03-08 16:49:10 +00:00
return TemplateResponse(
request,
2021-09-08 14:28:42 +00:00
"import/import_status.html",
2021-03-08 16:49:10 +00:00
{"job": job, "items": items, "failed_items": failed_items, "task": task},
)
2021-01-12 19:28:03 +00:00
def post(self, request, job_id):
2021-04-26 16:15:42 +00:00
"""retry lines from an import"""
2021-01-12 19:28:03 +00:00
job = get_object_or_404(models.ImportJob, id=job_id)
items = []
2021-03-08 16:49:10 +00:00
for item in request.POST.getlist("import_item"):
2021-01-12 19:28:03 +00:00
items.append(get_object_or_404(models.ImportItem, id=item))
2021-03-30 15:43:38 +00:00
importer = Importer()
job = importer.create_retry_job(
2021-01-12 19:28:03 +00:00
request.user,
job,
items,
)
2021-03-30 15:43:38 +00:00
importer.start_import(job)
2021-03-08 16:49:10 +00:00
return redirect("/import/%d" % job.id)