mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-27 20:11:14 +00:00
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:
commit
06d32126ab
2 changed files with 11 additions and 4 deletions
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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()
|
||||||
|
|
Loading…
Reference in a new issue