From f3d77ef7d922fe48a93b74d6b207c50ded02b443 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Wed, 21 Feb 2018 00:04:30 +0100
Subject: [PATCH] Update request status based on batch status

---
 .../0022_importbatch_import_request.py        | 20 ++++++++++++++++
 api/funkwhale_api/music/models.py             | 23 +++++++++++++++++++
 api/tests/requests/test_models.py             | 23 +++++++++++++++++++
 3 files changed, 66 insertions(+)
 create mode 100644 api/funkwhale_api/music/migrations/0022_importbatch_import_request.py
 create mode 100644 api/tests/requests/test_models.py

diff --git a/api/funkwhale_api/music/migrations/0022_importbatch_import_request.py b/api/funkwhale_api/music/migrations/0022_importbatch_import_request.py
new file mode 100644
index 000000000..d9f6f01d9
--- /dev/null
+++ b/api/funkwhale_api/music/migrations/0022_importbatch_import_request.py
@@ -0,0 +1,20 @@
+# Generated by Django 2.0.2 on 2018-02-20 22:48
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('requests', '__first__'),
+        ('music', '0021_populate_batch_status'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='importbatch',
+            name='import_request',
+            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='import_batches', to='requests.ImportRequest'),
+        ),
+    ]
diff --git a/api/funkwhale_api/music/models.py b/api/funkwhale_api/music/models.py
index 308bc43cc..97992fc8f 100644
--- a/api/funkwhale_api/music/models.py
+++ b/api/funkwhale_api/music/models.py
@@ -466,3 +466,26 @@ class ImportJob(models.Model):
 @receiver(post_save, sender=ImportJob)
 def update_batch_status(sender, instance, **kwargs):
     instance.batch.update_status()
+
+
+@receiver(post_save, sender=ImportBatch)
+def update_request_status(sender, instance, created, **kwargs):
+    update_fields = kwargs.get('update_fields', []) or []
+    if not instance.import_request:
+        return
+
+    if not created and not 'status' in update_fields:
+        return
+
+    r_status = instance.import_request.status
+    status = instance.status
+
+    if status == 'pending' and r_status == 'pending':
+        # let's mark the request as accepted since we started an import
+        instance.import_request.status = 'accepted'
+        return instance.import_request.save(update_fields=['status'])
+
+    if status == 'finished' and r_status == 'accepted':
+        # let's mark the request as imported since the import is over
+        instance.import_request.status = 'imported'
+        return instance.import_request.save(update_fields=['status'])
diff --git a/api/tests/requests/test_models.py b/api/tests/requests/test_models.py
new file mode 100644
index 000000000..797656bd7
--- /dev/null
+++ b/api/tests/requests/test_models.py
@@ -0,0 +1,23 @@
+import pytest
+
+from django.forms import ValidationError
+
+
+def test_can_bind_import_batch_to_request(factories):
+    request = factories['requests.ImportRequest']()
+
+    assert request.status == 'pending'
+
+    # when we create the import, we consider the request as accepted
+    batch = factories['music.ImportBatch'](import_request=request)
+    request.refresh_from_db()
+
+    assert request.status == 'accepted'
+
+    # now, the batch is finished, therefore the request status should be
+    # imported
+    batch.status = 'finished'
+    batch.save(update_fields=['status'])
+    request.refresh_from_db()
+
+    assert request.status == 'imported'
-- 
GitLab