diff --git a/bookwyrm/importers/importer.py b/bookwyrm/importers/importer.py index 438ff7db..94e6734e 100644 --- a/bookwyrm/importers/importer.py +++ b/bookwyrm/importers/importer.py @@ -57,6 +57,20 @@ class Importer: self.create_item(job, index, entry) return job + def update_legacy_job(self, job): + """patch up a job that was in the old format""" + items = job.items + headers = list(items.first().data.keys()) + job.mappings = self.create_row_mappings(headers) + job.updated_date = timezone.now() + job.save() + + for item in items.all(): + normalized = self.normalize_row(item.data, job.mappings) + normalized["shelf"] = self.get_shelf(normalized) + item.normalized_data = normalized + item.save() + def create_row_mappings(self, headers): """guess what the headers mean""" mappings = {} diff --git a/bookwyrm/templates/import/import_status.html b/bookwyrm/templates/import/import_status.html index 6c7d54b9..6370b866 100644 --- a/bookwyrm/templates/import/import_status.html +++ b/bookwyrm/templates/import/import_status.html @@ -57,7 +57,7 @@ {% endif %} - {% if manual_review_count %} + {% if manual_review_count and not legacy %}
{% blocktrans trimmed count counter=manual_review_count with display_counter=manual_review_count|intcomma %} {{ display_counter }} item needs manual approval. @@ -68,7 +68,7 @@
{% endif %} - {% if complete and fail_count and not job.retry %} + {% if job.complete and fail_count and not job.retry and not legacy %}
{% blocktrans trimmed count counter=fail_count with display_counter=fail_count|intcomma %} {{ display_counter }} item failed to import. @@ -114,6 +114,15 @@ {% endblock %} + {% if legacy %} + + +

+ {% trans "Import preview unavailable." %} +

+ + + {% else %} {% for item in items %} {% block index_col %} @@ -171,7 +180,7 @@ {% trans "Pending" %} {# retry option if an item appears to be hanging #} - {% if job.created_date != job.updated_date and inactive_time > 0.24 %} + {% if job.created_date != job.updated_date and inactive_time > 24 %}
{% csrf_token %} @@ -184,13 +193,27 @@ {% block action_row %}{% endblock %} {% endfor %} + {% endif %}
+ {% if legacy %} +
+ + {% csrf_token %} +

+ {% trans "This import is in an old format that is no longer supported. If you would like to troubleshoot missing items from this import, click the button below to update the import format." %} +

+ + +
+ {% endif %} +{% if not legacy %}
{% include 'snippets/pagination.html' with page=items %}
+{% endif %} {% endspaceless %}{% endblock %} {% block scripts %} diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index 6f658016..514bb7e6 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -244,7 +244,7 @@ urlpatterns = [ ), re_path( r"^import/(?P\d+)/retry/(?P\d+)/?$", - views.ImportStatus.as_view(), + views.retry_item, name="import-item-retry", ), re_path( diff --git a/bookwyrm/views/__init__.py b/bookwyrm/views/__init__.py index 1a6fbdc6..d79de424 100644 --- a/bookwyrm/views/__init__.py +++ b/bookwyrm/views/__init__.py @@ -45,7 +45,7 @@ from .shelf.shelf_actions import shelve, unshelve # csv import from .imports.import_data import Import -from .imports.import_status import ImportStatus +from .imports.import_status import ImportStatus, retry_item from .imports.troubleshoot import ImportTroubleshoot from .imports.manually_review import ( ImportManualReview, diff --git a/bookwyrm/views/imports/import_status.py b/bookwyrm/views/imports/import_status.py index 54174082..8e07a171 100644 --- a/bookwyrm/views/imports/import_status.py +++ b/bookwyrm/views/imports/import_status.py @@ -9,8 +9,10 @@ from django.template.response import TemplateResponse from django.utils import timezone from django.utils.decorators import method_decorator from django.views import View +from django.views.decorators.http import require_POST from bookwyrm import models +from bookwyrm.importers import GoodreadsImporter from bookwyrm.importers.importer import import_item_task from bookwyrm.settings import PAGE_LENGTH @@ -47,14 +49,26 @@ class ImportStatus(View): ), # hours since last import item update "inactive_time": (job.updated_date - timezone.now()).seconds / 60 / 60, + "legacy": not job.mappings, } return TemplateResponse(request, "import/import_status.html", data) - def post(self, request, job_id, item_id): - """retry an item""" - item = get_object_or_404( - models.ImportItem, id=item_id, job__id=job_id, job__user=request.user - ) - import_item_task.delay(item.id) + def post(self, request, job_id): + """bring a legacy import into the latest format""" + job = get_object_or_404(models.ImportJob, id=job_id) + if job.user != request.user: + raise PermissionDenied() + GoodreadsImporter().update_legacy_job(job) return redirect("import-status", job_id) + + +@login_required +@require_POST +def retry_item(request, job_id, item_id): + """retry an item""" + item = get_object_or_404( + models.ImportItem, id=item_id, job__id=job_id, job__user=request.user + ) + import_item_task.delay(item.id) + return redirect("import-status", job_id)