Do some checks to make sure we've got a csv file on import.

Fixes #104.
This commit is contained in:
Adam Kelly 2020-04-29 15:33:06 +01:00
parent 4f9edae05a
commit aec8e5c536
2 changed files with 11 additions and 4 deletions

View file

@ -14,6 +14,8 @@ MAX_ENTRIES = 20
def create_job(user, csv_file): def create_job(user, csv_file):
job = ImportJob.objects.create(user=user) job = ImportJob.objects.create(user=user)
for index, entry in enumerate(list(csv.DictReader(csv_file))[:MAX_ENTRIES]): 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() ImportItem(job=job, index=index, data=entry).save()
return job return job

View file

@ -418,10 +418,15 @@ def import_data(request):
''' ingest a goodreads csv ''' ''' ingest a goodreads csv '''
form = forms.ImportForm(request.POST, request.FILES) form = forms.ImportForm(request.POST, request.FILES)
if form.is_valid(): if form.is_valid():
job = goodreads_import.create_job( try:
request.user, job = goodreads_import.create_job(
TextIOWrapper(request.FILES['csv_file'], encoding=request.encoding) 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) goodreads_import.start_import(job)
return redirect('/import_status/%d' % (job.id,)) return redirect('/import_status/%d' % (job.id,))
return HttpResponseBadRequest() return HttpResponseBadRequest()