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.http import HttpResponseBadRequest
|
2021-11-12 23:50:33 +00:00
|
|
|
from django.shortcuts import redirect
|
2021-01-12 19:28:03 +00:00
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.utils.decorators import method_decorator
|
2021-05-20 21:39:33 +00:00
|
|
|
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-11-13 00:23:56 +00:00
|
|
|
from bookwyrm.importers import (
|
|
|
|
LibrarythingImporter,
|
|
|
|
GoodreadsImporter,
|
|
|
|
StorygraphImporter,
|
|
|
|
)
|
2021-01-12 19:28:03 +00:00
|
|
|
|
|
|
|
# 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)
|
2021-12-10 04:04:47 +00:00
|
|
|
if not form.is_valid():
|
|
|
|
return HttpResponseBadRequest()
|
2021-02-20 16:02:36 +00:00
|
|
|
|
2021-12-10 04:04:47 +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
|
|
|
|
2021-12-10 04:04:47 +00:00
|
|
|
importer = None
|
|
|
|
if source == "LibraryThing":
|
|
|
|
importer = LibrarythingImporter()
|
|
|
|
elif source == "Storygraph":
|
|
|
|
importer = StorygraphImporter()
|
|
|
|
else:
|
|
|
|
# Default : Goodreads
|
|
|
|
importer = GoodreadsImporter()
|
2021-02-20 16:02:36 +00:00
|
|
|
|
2021-12-10 04:04:47 +00:00
|
|
|
try:
|
|
|
|
job = importer.create_job(
|
|
|
|
request.user,
|
|
|
|
TextIOWrapper(request.FILES["csv_file"], encoding=importer.encoding),
|
|
|
|
include_reviews,
|
|
|
|
privacy,
|
|
|
|
)
|
|
|
|
except (UnicodeDecodeError, ValueError, KeyError):
|
|
|
|
return HttpResponseBadRequest(_("Not a valid csv file"))
|
2021-02-20 16:02:36 +00:00
|
|
|
|
2021-12-10 04:04:47 +00:00
|
|
|
importer.start_import(job)
|
|
|
|
|
|
|
|
return redirect(f"/import/{job.id}")
|