Update legacy jobs

This commit is contained in:
Mouse Reeve 2021-11-14 10:58:46 -08:00
parent 9f6796bbf5
commit 14e2960d06
5 changed files with 62 additions and 11 deletions

View file

@ -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 = {}

View file

@ -57,7 +57,7 @@
</div>
{% endif %}
{% if manual_review_count %}
{% if manual_review_count and not legacy %}
<div class="notification">
{% blocktrans trimmed count counter=manual_review_count with display_counter=manual_review_count|intcomma %}
{{ display_counter }} item needs manual approval.
@ -68,7 +68,7 @@
</div>
{% endif %}
{% if complete and fail_count and not job.retry %}
{% if job.complete and fail_count and not job.retry and not legacy %}
<div class="notification is-warning">
{% blocktrans trimmed count counter=fail_count with display_counter=fail_count|intcomma %}
{{ display_counter }} item failed to import.
@ -114,6 +114,15 @@
</th>
{% endblock %}
</tr>
{% if legacy %}
<tr>
<td colspan="8">
<p>
<em>{% trans "Import preview unavailable." %}</em>
</p>
</td>
</tr>
{% else %}
{% for item in items %}
<tr>
{% block index_col %}
@ -171,7 +180,7 @@
<span class="icon icon-dots-three" aria-hidden="true"></span>
<span class="is-sr-only-mobile">{% trans "Pending" %}</span>
{# 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 %}
<form class="ml-2" method="POST" action="{% url 'import-item-retry' job.id item.id %}" name="retry-{{ item.id }}">
{% csrf_token %}
<button class="button is-danger is-outlined is-small">{% trans "Retry" %}</button>
@ -184,13 +193,27 @@
</tr>
{% block action_row %}{% endblock %}
{% endfor %}
{% endif %}
</table>
</div>
{% if legacy %}
<div class="content">
<form name="update-import" method="POST" action="{% url 'import-status' job.id %}">
{% csrf_token %}
<p>
{% 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." %}
</p>
<button class="button">{% trans "Update import" %}</button>
</form>
</div>
{% endif %}
</div>
{% if not legacy %}
<div>
{% include 'snippets/pagination.html' with page=items %}
</div>
{% endif %}
{% endspaceless %}{% endblock %}
{% block scripts %}

View file

@ -244,7 +244,7 @@ urlpatterns = [
),
re_path(
r"^import/(?P<job_id>\d+)/retry/(?P<item_id>\d+)/?$",
views.ImportStatus.as_view(),
views.retry_item,
name="import-item-retry",
),
re_path(

View file

@ -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,

View file

@ -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)