From 8db832f03bb8b3430d19b03ecb97e488d17d92b9 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Fri, 6 Apr 2018 15:20:53 +0200
Subject: [PATCH] Now store source AP track file on track_file

---
 .../migrations/0003_auto_20180403_1921.py     |  31 ------
 .../migrations/0003_auto_20180406_1319.py     |  48 ++++++++
 .../migrations/0004_followrequest.py          |  28 -----
 .../migrations/0005_actor_followers.py        |  18 ---
 api/funkwhale_api/federation/models.py        |   8 +-
 .../migrations/0023_auto_20180405_1830.py     |  47 --------
 .../migrations/0023_auto_20180406_1319.py     | 104 ++++++++++++++++++
 .../migrations/0024_auto_20180406_1115.py     |  54 ---------
 api/funkwhale_api/music/models.py             |   6 +
 api/funkwhale_api/music/tasks.py              |   1 +
 api/tests/music/test_import.py                |   4 +
 11 files changed, 167 insertions(+), 182 deletions(-)
 delete mode 100644 api/funkwhale_api/federation/migrations/0003_auto_20180403_1921.py
 create mode 100644 api/funkwhale_api/federation/migrations/0003_auto_20180406_1319.py
 delete mode 100644 api/funkwhale_api/federation/migrations/0004_followrequest.py
 delete mode 100644 api/funkwhale_api/federation/migrations/0005_actor_followers.py
 delete mode 100644 api/funkwhale_api/music/migrations/0023_auto_20180405_1830.py
 create mode 100644 api/funkwhale_api/music/migrations/0023_auto_20180406_1319.py
 delete mode 100644 api/funkwhale_api/music/migrations/0024_auto_20180406_1115.py

diff --git a/api/funkwhale_api/federation/migrations/0003_auto_20180403_1921.py b/api/funkwhale_api/federation/migrations/0003_auto_20180403_1921.py
deleted file mode 100644
index aadf3257..00000000
--- a/api/funkwhale_api/federation/migrations/0003_auto_20180403_1921.py
+++ /dev/null
@@ -1,31 +0,0 @@
-# Generated by Django 2.0.3 on 2018-04-03 19:21
-
-from django.db import migrations, models
-import django.db.models.deletion
-import django.utils.timezone
-import uuid
-
-
-class Migration(migrations.Migration):
-
-    dependencies = [
-        ('federation', '0002_auto_20180403_1620'),
-    ]
-
-    operations = [
-        migrations.CreateModel(
-            name='Follow',
-            fields=[
-                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
-                ('uuid', models.UUIDField(default=uuid.uuid4, unique=True)),
-                ('creation_date', models.DateTimeField(default=django.utils.timezone.now)),
-                ('last_modification_date', models.DateTimeField(default=django.utils.timezone.now)),
-                ('actor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='emitted_follows', to='federation.Actor')),
-                ('target', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='received_follows', to='federation.Actor')),
-            ],
-        ),
-        migrations.AlterUniqueTogether(
-            name='follow',
-            unique_together={('actor', 'target')},
-        ),
-    ]
diff --git a/api/funkwhale_api/federation/migrations/0003_auto_20180406_1319.py b/api/funkwhale_api/federation/migrations/0003_auto_20180406_1319.py
new file mode 100644
index 00000000..cc653b1a
--- /dev/null
+++ b/api/funkwhale_api/federation/migrations/0003_auto_20180406_1319.py
@@ -0,0 +1,48 @@
+# Generated by Django 2.0.3 on 2018-04-06 13:19
+
+from django.db import migrations, models
+import django.db.models.deletion
+import django.utils.timezone
+import uuid
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('federation', '0002_auto_20180403_1620'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='Follow',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('uuid', models.UUIDField(default=uuid.uuid4, unique=True)),
+                ('creation_date', models.DateTimeField(default=django.utils.timezone.now)),
+                ('modification_date', models.DateTimeField(auto_now=True)),
+                ('actor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='emitted_follows', to='federation.Actor')),
+                ('target', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='received_follows', to='federation.Actor')),
+            ],
+        ),
+        migrations.CreateModel(
+            name='FollowRequest',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('uuid', models.UUIDField(default=uuid.uuid4, unique=True)),
+                ('creation_date', models.DateTimeField(default=django.utils.timezone.now)),
+                ('modification_date', models.DateTimeField(auto_now=True)),
+                ('approved', models.NullBooleanField(default=None)),
+                ('actor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='emmited_follow_requests', to='federation.Actor')),
+                ('target', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='received_follow_requests', to='federation.Actor')),
+            ],
+        ),
+        migrations.AddField(
+            model_name='actor',
+            name='followers',
+            field=models.ManyToManyField(related_name='following', through='federation.Follow', to='federation.Actor'),
+        ),
+        migrations.AlterUniqueTogether(
+            name='follow',
+            unique_together={('actor', 'target')},
+        ),
+    ]
diff --git a/api/funkwhale_api/federation/migrations/0004_followrequest.py b/api/funkwhale_api/federation/migrations/0004_followrequest.py
deleted file mode 100644
index 6ede7274..00000000
--- a/api/funkwhale_api/federation/migrations/0004_followrequest.py
+++ /dev/null
@@ -1,28 +0,0 @@
-# Generated by Django 2.0.3 on 2018-04-04 17:11
-
-from django.db import migrations, models
-import django.db.models.deletion
-import django.utils.timezone
-import uuid
-
-
-class Migration(migrations.Migration):
-
-    dependencies = [
-        ('federation', '0003_auto_20180403_1921'),
-    ]
-
-    operations = [
-        migrations.CreateModel(
-            name='FollowRequest',
-            fields=[
-                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
-                ('uuid', models.UUIDField(default=uuid.uuid4, unique=True)),
-                ('creation_date', models.DateTimeField(default=django.utils.timezone.now)),
-                ('last_modification_date', models.DateTimeField(default=django.utils.timezone.now)),
-                ('approved', models.NullBooleanField(default=None)),
-                ('actor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='emmited_follow_requests', to='federation.Actor')),
-                ('target', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='received_follow_requests', to='federation.Actor')),
-            ],
-        ),
-    ]
diff --git a/api/funkwhale_api/federation/migrations/0005_actor_followers.py b/api/funkwhale_api/federation/migrations/0005_actor_followers.py
deleted file mode 100644
index 94a1c75a..00000000
--- a/api/funkwhale_api/federation/migrations/0005_actor_followers.py
+++ /dev/null
@@ -1,18 +0,0 @@
-# Generated by Django 2.0.3 on 2018-04-05 16:35
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
-    dependencies = [
-        ('federation', '0004_followrequest'),
-    ]
-
-    operations = [
-        migrations.AddField(
-            model_name='actor',
-            name='followers',
-            field=models.ManyToManyField(related_name='following', through='federation.Follow', to='federation.Actor'),
-        ),
-    ]
diff --git a/api/funkwhale_api/federation/models.py b/api/funkwhale_api/federation/models.py
index 833b5d8f..4bf59700 100644
--- a/api/funkwhale_api/federation/models.py
+++ b/api/funkwhale_api/federation/models.py
@@ -106,8 +106,8 @@ class Follow(models.Model):
         on_delete=models.CASCADE,
     )
     creation_date = models.DateTimeField(default=timezone.now)
-    last_modification_date = models.DateTimeField(
-        default=timezone.now)
+    modification_date = models.DateTimeField(
+        auto_now=True)
 
     class Meta:
         unique_together = ['actor', 'target']
@@ -129,8 +129,8 @@ class FollowRequest(models.Model):
         on_delete=models.CASCADE,
     )
     creation_date = models.DateTimeField(default=timezone.now)
-    last_modification_date = models.DateTimeField(
-        default=timezone.now)
+    modification_date = models.DateTimeField(
+        auto_now=True)
     approved = models.NullBooleanField(default=None)
 
     def approve(self):
diff --git a/api/funkwhale_api/music/migrations/0023_auto_20180405_1830.py b/api/funkwhale_api/music/migrations/0023_auto_20180405_1830.py
deleted file mode 100644
index 3cef1f42..00000000
--- a/api/funkwhale_api/music/migrations/0023_auto_20180405_1830.py
+++ /dev/null
@@ -1,47 +0,0 @@
-# Generated by Django 2.0.3 on 2018-04-05 18:30
-
-from django.conf import settings
-import django.contrib.postgres.fields.jsonb
-from django.db import migrations, models
-import django.db.models.deletion
-
-
-class Migration(migrations.Migration):
-
-    dependencies = [
-        ('federation', '0005_actor_followers'),
-        ('music', '0022_importbatch_import_request'),
-    ]
-
-    operations = [
-        migrations.AddField(
-            model_name='importbatch',
-            name='federation_actor',
-            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='import_batches', to='federation.Actor'),
-        ),
-        migrations.AddField(
-            model_name='importbatch',
-            name='federation_source',
-            field=models.URLField(blank=True, null=True),
-        ),
-        migrations.AddField(
-            model_name='importjob',
-            name='federation_source',
-            field=models.URLField(blank=True, null=True),
-        ),
-        migrations.AddField(
-            model_name='importjob',
-            name='metadata',
-            field=django.contrib.postgres.fields.jsonb.JSONField(default={}),
-        ),
-        migrations.AlterField(
-            model_name='importbatch',
-            name='source',
-            field=models.CharField(choices=[('api', 'api'), ('shell', 'shell'), ('federation', 'federation')], default='api', max_length=30),
-        ),
-        migrations.AlterField(
-            model_name='importbatch',
-            name='submitted_by',
-            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='imports', to=settings.AUTH_USER_MODEL),
-        ),
-    ]
diff --git a/api/funkwhale_api/music/migrations/0023_auto_20180406_1319.py b/api/funkwhale_api/music/migrations/0023_auto_20180406_1319.py
new file mode 100644
index 00000000..c51a7b9f
--- /dev/null
+++ b/api/funkwhale_api/music/migrations/0023_auto_20180406_1319.py
@@ -0,0 +1,104 @@
+# Generated by Django 2.0.3 on 2018-04-06 13:19
+
+from django.conf import settings
+import django.contrib.postgres.fields.jsonb
+from django.db import migrations, models
+import django.db.models.deletion
+import django.utils.timezone
+import uuid
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('federation', '0003_auto_20180406_1319'),
+        ('music', '0022_importbatch_import_request'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='album',
+            name='uuid',
+            field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True),
+        ),
+        migrations.AddField(
+            model_name='artist',
+            name='uuid',
+            field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True),
+        ),
+        migrations.AddField(
+            model_name='importbatch',
+            name='federation_actor',
+            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='import_batches', to='federation.Actor'),
+        ),
+        migrations.AddField(
+            model_name='importbatch',
+            name='federation_source',
+            field=models.URLField(blank=True, null=True),
+        ),
+        migrations.AddField(
+            model_name='importbatch',
+            name='uuid',
+            field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True),
+        ),
+        migrations.AddField(
+            model_name='importjob',
+            name='federation_source',
+            field=models.URLField(blank=True, null=True),
+        ),
+        migrations.AddField(
+            model_name='importjob',
+            name='metadata',
+            field=django.contrib.postgres.fields.jsonb.JSONField(default={}),
+        ),
+        migrations.AddField(
+            model_name='importjob',
+            name='uuid',
+            field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True),
+        ),
+        migrations.AddField(
+            model_name='lyrics',
+            name='uuid',
+            field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True),
+        ),
+        migrations.AddField(
+            model_name='track',
+            name='uuid',
+            field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True),
+        ),
+        migrations.AddField(
+            model_name='trackfile',
+            name='creation_date',
+            field=models.DateTimeField(default=django.utils.timezone.now),
+        ),
+        migrations.AddField(
+            model_name='trackfile',
+            name='federation_source',
+            field=models.URLField(blank=True, null=True),
+        ),
+        migrations.AddField(
+            model_name='trackfile',
+            name='modification_date',
+            field=models.DateTimeField(auto_now=True),
+        ),
+        migrations.AddField(
+            model_name='trackfile',
+            name='uuid',
+            field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True),
+        ),
+        migrations.AddField(
+            model_name='work',
+            name='uuid',
+            field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True),
+        ),
+        migrations.AlterField(
+            model_name='importbatch',
+            name='source',
+            field=models.CharField(choices=[('api', 'api'), ('shell', 'shell'), ('federation', 'federation')], default='api', max_length=30),
+        ),
+        migrations.AlterField(
+            model_name='importbatch',
+            name='submitted_by',
+            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='imports', to=settings.AUTH_USER_MODEL),
+        ),
+    ]
diff --git a/api/funkwhale_api/music/migrations/0024_auto_20180406_1115.py b/api/funkwhale_api/music/migrations/0024_auto_20180406_1115.py
deleted file mode 100644
index 8b655a64..00000000
--- a/api/funkwhale_api/music/migrations/0024_auto_20180406_1115.py
+++ /dev/null
@@ -1,54 +0,0 @@
-# Generated by Django 2.0.3 on 2018-04-06 11:15
-
-from django.db import migrations, models
-import uuid
-
-
-class Migration(migrations.Migration):
-
-    dependencies = [
-        ('music', '0023_auto_20180405_1830'),
-    ]
-
-    operations = [
-        migrations.AddField(
-            model_name='album',
-            name='uuid',
-            field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True),
-        ),
-        migrations.AddField(
-            model_name='artist',
-            name='uuid',
-            field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True),
-        ),
-        migrations.AddField(
-            model_name='importbatch',
-            name='uuid',
-            field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True),
-        ),
-        migrations.AddField(
-            model_name='importjob',
-            name='uuid',
-            field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True),
-        ),
-        migrations.AddField(
-            model_name='lyrics',
-            name='uuid',
-            field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True),
-        ),
-        migrations.AddField(
-            model_name='track',
-            name='uuid',
-            field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True),
-        ),
-        migrations.AddField(
-            model_name='trackfile',
-            name='uuid',
-            field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True),
-        ),
-        migrations.AddField(
-            model_name='work',
-            name='uuid',
-            field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True),
-        ),
-    ]
diff --git a/api/funkwhale_api/music/models.py b/api/funkwhale_api/music/models.py
index 5d8035f3..efffb12d 100644
--- a/api/funkwhale_api/music/models.py
+++ b/api/funkwhale_api/music/models.py
@@ -414,6 +414,12 @@ class TrackFile(models.Model):
         Track, related_name='files', on_delete=models.CASCADE)
     audio_file = models.FileField(upload_to='tracks/%Y/%m/%d', max_length=255)
     source = models.URLField(null=True, blank=True)
+    creation_date = models.DateTimeField(default=timezone.now)
+    modification_date = models.DateTimeField(auto_now=True)
+
+    # points to the URL of the original trackfile ActivityPub Object
+    federation_source = models.URLField(null=True, blank=True)
+
     duration = models.IntegerField(null=True, blank=True)
     acoustid_track_id = models.UUIDField(null=True, blank=True)
     mimetype = models.CharField(null=True, blank=True, max_length=200)
diff --git a/api/funkwhale_api/music/tasks.py b/api/funkwhale_api/music/tasks.py
index e4214d99..4f85613e 100644
--- a/api/funkwhale_api/music/tasks.py
+++ b/api/funkwhale_api/music/tasks.py
@@ -102,6 +102,7 @@ def _do_import(import_job, replace, use_acoustid=True):
     track_file = track_file or models.TrackFile(
         track=track, source=import_job.source)
     track_file.acoustid_track_id = acoustid_track_id
+    track_file.federation_source = import_job.federation_source
     if from_file:
         track_file.audio_file = ContentFile(import_job.audio_file.read())
         track_file.audio_file.name = import_job.audio_file.name
diff --git a/api/tests/music/test_import.py b/api/tests/music/test_import.py
index 87e1899d..98174891 100644
--- a/api/tests/music/test_import.py
+++ b/api/tests/music/test_import.py
@@ -62,6 +62,7 @@ def test_import_job_from_federation_no_musicbrainz(factories):
 
     tf = job.track_file
     assert tf.source == job.source
+    assert tf.federation_source == job.federation_source
     assert tf.track.title == 'Ping'
     assert tf.track.artist.name == 'Hello'
     assert tf.track.album.title == 'World'
@@ -84,6 +85,7 @@ def test_import_job_from_federation_musicbrainz_recording(factories, mocker):
 
     tf = job.track_file
     assert tf.source == job.source
+    assert tf.federation_source == job.federation_source
     assert tf.track == t
     track_from_api.assert_called_once_with(
         mbid=tasks.get_mbid(job.metadata['recording'], 'recording'))
@@ -105,6 +107,7 @@ def test_import_job_from_federation_musicbrainz_release(factories, mocker):
     job.refresh_from_db()
 
     tf = job.track_file
+    assert tf.federation_source == job.federation_source
     assert tf.source == job.source
     assert tf.track.title == 'Ping'
     assert tf.track.artist == a.artist
@@ -131,6 +134,7 @@ def test_import_job_from_federation_musicbrainz_artist(factories, mocker):
 
     tf = job.track_file
     assert tf.source == job.source
+    assert tf.federation_source == job.federation_source
     assert tf.track.title == 'Ping'
     assert tf.track.artist == a
     assert tf.track.album.artist == a
-- 
GitLab