mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-26 03:21:05 +00:00
Merge pull request #2449 from bookwyrm-social/empty-csv
Don't start imports with empty CSVs
This commit is contained in:
commit
df42712b8e
3 changed files with 14 additions and 4 deletions
|
@ -36,7 +36,11 @@ class Importer:
|
|||
def create_job(self, user, csv_file, include_reviews, privacy):
|
||||
"""check over a csv and creates a database entry for the job"""
|
||||
csv_reader = csv.DictReader(csv_file, delimiter=self.delimiter)
|
||||
rows = enumerate(list(csv_reader))
|
||||
rows = list(csv_reader)
|
||||
if len(rows) < 1:
|
||||
raise ValueError("CSV file is empty")
|
||||
rows = enumerate(rows)
|
||||
|
||||
job = ImportJob.objects.create(
|
||||
user=user,
|
||||
include_reviews=include_reviews,
|
||||
|
|
|
@ -8,6 +8,12 @@
|
|||
<div class="block">
|
||||
<h1 class="title">{% trans "Import Books" %}</h1>
|
||||
|
||||
{% if invalid %}
|
||||
<div class="notification is-danger">
|
||||
{% trans "Not a valid CSV file" %}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if site.imports_enabled %}
|
||||
{% if recent_avg_hours or recent_avg_minutes %}
|
||||
<div class="notification">
|
||||
|
|
|
@ -11,7 +11,6 @@ from django.shortcuts import redirect
|
|||
from django.template.response import TemplateResponse
|
||||
from django.utils import timezone
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm import forms, models
|
||||
|
@ -30,7 +29,7 @@ from bookwyrm.utils.cache import get_or_set
|
|||
class Import(View):
|
||||
"""import view"""
|
||||
|
||||
def get(self, request):
|
||||
def get(self, request, invalid=False):
|
||||
"""load import page"""
|
||||
jobs = models.ImportJob.objects.filter(user=request.user).order_by(
|
||||
"-created_date"
|
||||
|
@ -43,6 +42,7 @@ class Import(View):
|
|||
"page_range": paginated.get_elided_page_range(
|
||||
page.number, on_each_side=2, on_ends=1
|
||||
),
|
||||
"invalid": invalid,
|
||||
}
|
||||
|
||||
seconds = get_or_set("avg-import-time", get_average_import_time, timeout=86400)
|
||||
|
@ -88,7 +88,7 @@ class Import(View):
|
|||
privacy,
|
||||
)
|
||||
except (UnicodeDecodeError, ValueError, KeyError):
|
||||
return HttpResponseBadRequest(_("Not a valid csv file"))
|
||||
return self.get(request, invalid=True)
|
||||
|
||||
job.start_job()
|
||||
|
||||
|
|
Loading…
Reference in a new issue