diff --git a/fedireads/goodreads_import.py b/fedireads/goodreads_import.py index 7d4abdd5..c5f9001d 100644 --- a/fedireads/goodreads_import.py +++ b/fedireads/goodreads_import.py @@ -14,6 +14,8 @@ MAX_ENTRIES = 20 def create_job(user, csv_file): job = ImportJob.objects.create(user=user) for index, entry in enumerate(list(csv.DictReader(csv_file))[:MAX_ENTRIES]): + if not all(x in entry for x in ('ISBN13', 'Title', 'Author')): + raise ValueError("Author, title, and isbn must be in data.") ImportItem(job=job, index=index, data=entry).save() return job diff --git a/fedireads/view_actions.py b/fedireads/view_actions.py index 6195750a..bf7918a3 100644 --- a/fedireads/view_actions.py +++ b/fedireads/view_actions.py @@ -418,10 +418,15 @@ def import_data(request): ''' ingest a goodreads csv ''' form = forms.ImportForm(request.POST, request.FILES) if form.is_valid(): - job = goodreads_import.create_job( - request.user, - TextIOWrapper(request.FILES['csv_file'], encoding=request.encoding) - ) + try: + job = goodreads_import.create_job( + request.user, + TextIOWrapper( + request.FILES['csv_file'], + encoding=request.encoding) + ) + except (UnicodeDecodeError, ValueError): + return HttpResponseBadRequest('Not a valid csv file') goodreads_import.start_import(job) return redirect('/import_status/%d' % (job.id,)) return HttpResponseBadRequest()