Merge pull request #2449 from bookwyrm-social/empty-csv

Don't start imports with empty CSVs
This commit is contained in:
Mouse Reeve 2022-11-25 12:31:13 -08:00 committed by GitHub
commit df42712b8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 4 deletions

View file

@ -36,7 +36,11 @@ class Importer:
def create_job(self, user, csv_file, include_reviews, privacy): def create_job(self, user, csv_file, include_reviews, privacy):
"""check over a csv and creates a database entry for the job""" """check over a csv and creates a database entry for the job"""
csv_reader = csv.DictReader(csv_file, delimiter=self.delimiter) 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( job = ImportJob.objects.create(
user=user, user=user,
include_reviews=include_reviews, include_reviews=include_reviews,

View file

@ -8,6 +8,12 @@
<div class="block"> <div class="block">
<h1 class="title">{% trans "Import Books" %}</h1> <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 site.imports_enabled %}
{% if recent_avg_hours or recent_avg_minutes %} {% if recent_avg_hours or recent_avg_minutes %}
<div class="notification"> <div class="notification">

View file

@ -11,7 +11,6 @@ from django.shortcuts import redirect
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from django.utils import timezone from django.utils import timezone
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.utils.translation import gettext_lazy as _
from django.views import View from django.views import View
from bookwyrm import forms, models from bookwyrm import forms, models
@ -30,7 +29,7 @@ from bookwyrm.utils.cache import get_or_set
class Import(View): class Import(View):
"""import view""" """import view"""
def get(self, request): def get(self, request, invalid=False):
"""load import page""" """load import page"""
jobs = models.ImportJob.objects.filter(user=request.user).order_by( jobs = models.ImportJob.objects.filter(user=request.user).order_by(
"-created_date" "-created_date"
@ -43,6 +42,7 @@ class Import(View):
"page_range": paginated.get_elided_page_range( "page_range": paginated.get_elided_page_range(
page.number, on_each_side=2, on_ends=1 page.number, on_each_side=2, on_ends=1
), ),
"invalid": invalid,
} }
seconds = get_or_set("avg-import-time", get_average_import_time, timeout=86400) seconds = get_or_set("avg-import-time", get_average_import_time, timeout=86400)
@ -88,7 +88,7 @@ class Import(View):
privacy, privacy,
) )
except (UnicodeDecodeError, ValueError, KeyError): except (UnicodeDecodeError, ValueError, KeyError):
return HttpResponseBadRequest(_("Not a valid csv file")) return self.get(request, invalid=True)
job.start_job() job.start_job()