2020-03-29 07:05:09 +00:00
|
|
|
''' handle reading a csv from goodreads '''
|
2020-03-25 12:29:21 +00:00
|
|
|
import csv
|
2020-04-20 16:10:19 +00:00
|
|
|
from requests import HTTPError
|
2020-03-25 12:29:21 +00:00
|
|
|
|
2020-04-20 16:10:19 +00:00
|
|
|
from fedireads import outgoing
|
|
|
|
from fedireads.tasks import app
|
2020-04-21 14:09:21 +00:00
|
|
|
from fedireads.models import ImportJob, ImportItem
|
2020-04-22 11:43:10 +00:00
|
|
|
from fedireads.status import create_notification
|
2020-03-29 07:05:09 +00:00
|
|
|
|
2020-04-28 13:59:48 +00:00
|
|
|
# TODO: remove or increase once we're confident it's not causing problems.
|
|
|
|
MAX_ENTRIES = 500
|
2020-03-25 12:58:27 +00:00
|
|
|
|
2020-03-29 07:05:09 +00:00
|
|
|
|
2020-04-21 14:09:21 +00:00
|
|
|
def create_job(user, csv_file):
|
|
|
|
job = ImportJob.objects.create(user=user)
|
|
|
|
for index, entry in enumerate(list(csv.DictReader(csv_file))[:MAX_ENTRIES]):
|
|
|
|
ImportItem(job=job, index=index, data=entry).save()
|
|
|
|
return job
|
2020-03-25 12:29:21 +00:00
|
|
|
|
2020-04-21 14:09:21 +00:00
|
|
|
def start_import(job):
|
|
|
|
result = import_data.delay(job.id)
|
|
|
|
job.task_id = result.id
|
|
|
|
job.save()
|
2020-04-20 16:10:19 +00:00
|
|
|
|
|
|
|
@app.task
|
2020-04-21 14:09:21 +00:00
|
|
|
def import_data(job_id):
|
|
|
|
job = ImportJob.objects.get(id=job_id)
|
2020-04-22 11:43:10 +00:00
|
|
|
try:
|
|
|
|
results = []
|
|
|
|
reviews = []
|
|
|
|
for item in job.items.all():
|
|
|
|
try:
|
|
|
|
item.resolve()
|
|
|
|
except HTTPError:
|
|
|
|
pass
|
|
|
|
if item.book:
|
|
|
|
item.save()
|
|
|
|
results.append(item)
|
|
|
|
if item.rating or item.review:
|
|
|
|
reviews.append(item)
|
|
|
|
else:
|
|
|
|
item.fail_reason = "Could not match book on OpenLibrary"
|
|
|
|
item.save()
|
|
|
|
|
2020-04-22 13:16:46 +00:00
|
|
|
status = outgoing.handle_import_books(job.user, results)
|
2020-04-22 11:43:10 +00:00
|
|
|
for item in reviews:
|
|
|
|
review_title = "Review of {!r} on Goodreads".format(
|
|
|
|
item.book.title,
|
|
|
|
) if item.review else ""
|
|
|
|
outgoing.handle_review(
|
|
|
|
job.user,
|
|
|
|
item.book,
|
|
|
|
review_title,
|
|
|
|
item.review,
|
|
|
|
item.rating,
|
|
|
|
)
|
2020-04-22 13:16:46 +00:00
|
|
|
if status:
|
|
|
|
job.import_status = status
|
|
|
|
job.save()
|
2020-04-22 11:43:10 +00:00
|
|
|
finally:
|
|
|
|
create_notification(job.user, 'IMPORT', related_import=job)
|