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