moviewyrm/bookwyrm/views/import_data.py

93 lines
3.3 KiB
Python
Raw Normal View History

2021-01-12 19:28:03 +00:00
''' import books from another app '''
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.views import View
2021-02-20 16:02:36 +00:00
from bookwyrm import forms, goodreads_import, librarything_import, models
2021-01-12 19:28:03 +00:00
from bookwyrm.tasks import app
# pylint: disable= no-self-use
@method_decorator(login_required, name='dispatch')
class Import(View):
''' import view '''
def get(self, request):
''' load import page '''
return TemplateResponse(request, 'import.html', {
'import_form': forms.ImportForm(),
'jobs': models.ImportJob.
objects.filter(user=request.user).order_by('-created_date'),
})
def post(self, request):
''' ingest a goodreads csv '''
form = forms.ImportForm(request.POST, request.FILES)
if form.is_valid():
include_reviews = request.POST.get('include_reviews') == 'on'
privacy = request.POST.get('privacy')
2021-02-20 16:02:36 +00:00
source = request.POST.get('source')
importer = None
if source == 'LibraryThing':
importer = librarything_import.LibrarythingImporter()
else:
# Default : GoodReads
importer = goodreads_import.GoodreadsImporter()
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(
request.FILES['csv_file'],
2021-02-20 16:02:36 +00:00
encoding=importer.encoding),
2021-01-12 19:28:03 +00:00
include_reviews,
privacy,
)
except (UnicodeDecodeError, ValueError):
return HttpResponseBadRequest('Not a valid csv file')
2021-02-20 16:02:36 +00:00
importer.start_import(job)
2021-01-19 15:15:08 +00:00
return redirect('/import/%d' % job.id)
2021-01-12 19:28:03 +00:00
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 = models.ImportJob.objects.get(id=job_id)
if job.user != request.user:
raise PermissionDenied
task = app.AsyncResult(job.task_id)
items = job.items.order_by('index').all()
failed_items = [i for i in items if i.fail_reason]
items = [i for i in items if not i.fail_reason]
return TemplateResponse(request, 'import_status.html', {
'job': job,
'items': items,
'failed_items': failed_items,
'task': task
})
def post(self, request, job_id):
''' retry lines from an import '''
job = get_object_or_404(models.ImportJob, id=job_id)
items = []
for item in request.POST.getlist('import_item'):
items.append(get_object_or_404(models.ImportItem, id=item))
job = goodreads_import.create_retry_job(
request.user,
job,
items,
)
goodreads_import.start_import(job)
2021-01-19 15:15:08 +00:00
return redirect('/import/%d' % job.id)