Merge pull request #157 from cthulahoops/import_errors

Do some checks to make sure we've got a csv file on import.
This commit is contained in:
Mouse Reeve 2020-04-29 10:16:31 -07:00 committed by GitHub
commit 06d32126ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 4 deletions

View file

@ -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

View file

@ -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()