From 60dabe442eca32328655084dbdb432a1fe4a9543 Mon Sep 17 00:00:00 2001
From: Adam Kelly <adam@cthulahoops.org>
Date: Wed, 22 Apr 2020 14:16:46 +0100
Subject: [PATCH] Link import job to import status and display status on status
 page.

---
 fedireads/goodreads_import.py                 |  5 ++++-
 .../0034_importjob_import_status.py           | 19 +++++++++++++++++++
 fedireads/models/import_job.py                |  2 ++
 fedireads/outgoing.py                         |  3 ++-
 fedireads/templates/import_status.html        |  4 ++++
 5 files changed, 31 insertions(+), 2 deletions(-)
 create mode 100644 fedireads/migrations/0034_importjob_import_status.py

diff --git a/fedireads/goodreads_import.py b/fedireads/goodreads_import.py
index fb95f3fc..7d4abdd5 100644
--- a/fedireads/goodreads_import.py
+++ b/fedireads/goodreads_import.py
@@ -42,7 +42,7 @@ def import_data(job_id):
                 item.fail_reason = "Could not match book on OpenLibrary"
                 item.save()
 
-        outgoing.handle_import_books(job.user, results)
+        status = outgoing.handle_import_books(job.user, results)
         for item in reviews:
             review_title = "Review of {!r} on Goodreads".format(
                 item.book.title,
@@ -54,5 +54,8 @@ def import_data(job_id):
                 item.review,
                 item.rating,
             )
+        if status:
+            job.import_status = status
+            job.save()
     finally:
         create_notification(job.user, 'IMPORT', related_import=job)
diff --git a/fedireads/migrations/0034_importjob_import_status.py b/fedireads/migrations/0034_importjob_import_status.py
new file mode 100644
index 00000000..94cc65f6
--- /dev/null
+++ b/fedireads/migrations/0034_importjob_import_status.py
@@ -0,0 +1,19 @@
+# Generated by Django 3.0.3 on 2020-04-22 13:12
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('fedireads', '0033_auto_20200422_1249'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='importjob',
+            name='import_status',
+            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='fedireads.Status'),
+        ),
+    ]
diff --git a/fedireads/models/import_job.py b/fedireads/models/import_job.py
index b979c012..fcb21654 100644
--- a/fedireads/models/import_job.py
+++ b/fedireads/models/import_job.py
@@ -36,6 +36,8 @@ class ImportJob(models.Model):
     user = models.ForeignKey(User, on_delete=models.CASCADE)
     created_date = models.DateTimeField(default=timezone.now)
     task_id = models.CharField(max_length=100, null=True)
+    import_status = models.ForeignKey(
+        'Status', null=True, on_delete=models.PROTECT)
 
 class ImportItem(models.Model):
     job = models.ForeignKey(
diff --git a/fedireads/outgoing.py b/fedireads/outgoing.py
index 975a5fe6..9b44057c 100644
--- a/fedireads/outgoing.py
+++ b/fedireads/outgoing.py
@@ -158,7 +158,7 @@ def handle_shelve(user, book, shelf):
     activity = activitypub.get_status(status)
     create_activity = activitypub.get_create(user, activity)
 
-    broadcast(user, create_activity, recipients)
+    broadcast(user, create_activity)
 
 
 def handle_unshelve(user, book, shelf):
@@ -206,6 +206,7 @@ def handle_import_books(user, items):
         create_activity = activitypub.get_create(
             user, activitypub.get_status(status))
         broadcast(user, create_activity)
+        return status
 
 
 def handle_rate(user, book, rating):
diff --git a/fedireads/templates/import_status.html b/fedireads/templates/import_status.html
index dada9951..34abeb8f 100644
--- a/fedireads/templates/import_status.html
+++ b/fedireads/templates/import_status.html
@@ -16,6 +16,10 @@
         <p>
         {{ task.info }}
         {% endif %}
+
+        {% if job.import_status %}
+            {% include 'snippets/status.html' with status=job.import_status %}
+        {% endif %}
         {% else %}
         Import still in progress.
         <p>