From cc71d2bb20e4d646d568ccc2a2b80a852cb1a899 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Thu, 21 Mar 2019 10:32:12 +0100
Subject: [PATCH] Fix #570: Exclude in-place imported files from quota
 computation

---
 api/funkwhale_api/common/models.py     | 13 +++++++++++++
 api/funkwhale_api/federation/models.py |  8 ++++++--
 api/tests/federation/test_models.py    | 20 +++++++++++++++++++-
 changes/changelog.d/570.enhancement    |  1 +
 4 files changed, 39 insertions(+), 3 deletions(-)
 create mode 100644 api/funkwhale_api/common/models.py
 create mode 100644 changes/changelog.d/570.enhancement

diff --git a/api/funkwhale_api/common/models.py b/api/funkwhale_api/common/models.py
new file mode 100644
index 0000000000..9a9d858d80
--- /dev/null
+++ b/api/funkwhale_api/common/models.py
@@ -0,0 +1,13 @@
+from django.db.models import Lookup
+from django.db.models.fields import Field
+
+
+@Field.register_lookup
+class NotEqual(Lookup):
+    lookup_name = "ne"
+
+    def as_sql(self, compiler, connection):
+        lhs, lhs_params = self.process_lhs(compiler, connection)
+        rhs, rhs_params = self.process_rhs(compiler, connection)
+        params = lhs_params + rhs_params
+        return "%s <> %s" % (lhs, rhs), params
diff --git a/api/funkwhale_api/federation/models.py b/api/funkwhale_api/federation/models.py
index 59360aea10..843d538e0d 100644
--- a/api/funkwhale_api/federation/models.py
+++ b/api/funkwhale_api/federation/models.py
@@ -51,11 +51,15 @@ class ActorQuerySet(models.QuerySet):
     def with_current_usage(self):
         qs = self
         for s in ["pending", "skipped", "errored", "finished"]:
+            uploads_query = models.Q(
+                libraries__uploads__import_status=s,
+                libraries__uploads__audio_file__isnull=False,
+                libraries__uploads__audio_file__ne="",
+            )
             qs = qs.annotate(
                 **{
                     "_usage_{}".format(s): models.Sum(
-                        "libraries__uploads__size",
-                        filter=models.Q(libraries__uploads__import_status=s),
+                        "libraries__uploads__size", filter=uploads_query
                     )
                 }
             )
diff --git a/api/tests/federation/test_models.py b/api/tests/federation/test_models.py
index f59293b678..6eeebd660a 100644
--- a/api/tests/federation/test_models.py
+++ b/api/tests/federation/test_models.py
@@ -53,7 +53,25 @@ def test_actor_get_quota(factories):
         audio_file__from_path=None,
         audio_file__data=b"aaaa",
     )
-    expected = {"total": 10, "pending": 1, "skipped": 2, "errored": 3, "finished": 4}
+
+    # this one is imported in place and don't count
+    factories["music.Upload"](
+        library=library,
+        import_status="finished",
+        source="file://test",
+        audio_file=None,
+        size=42,
+    )
+    # this one is imported in place but count because there is a mapped file
+    factories["music.Upload"](
+        library=library,
+        import_status="finished",
+        source="file://test2",
+        audio_file__from_path=None,
+        audio_file__data=b"aaaa",
+    )
+
+    expected = {"total": 14, "pending": 1, "skipped": 2, "errored": 3, "finished": 8}
 
     assert library.actor.get_current_usage() == expected
 
diff --git a/changes/changelog.d/570.enhancement b/changes/changelog.d/570.enhancement
new file mode 100644
index 0000000000..7cddc4f3f5
--- /dev/null
+++ b/changes/changelog.d/570.enhancement
@@ -0,0 +1 @@
+Exclude in-place imported files from quota computation (#570)
-- 
GitLab