Commit b49720ca authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Merge branch '570-inplace-quota' into 'master'

Fix #570: Exclude in-place imported files from quota computation

See merge request funkwhale/funkwhale!687
parents 97ef1590 cc71d2bb
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
...@@ -51,11 +51,15 @@ class ActorQuerySet(models.QuerySet): ...@@ -51,11 +51,15 @@ class ActorQuerySet(models.QuerySet):
def with_current_usage(self): def with_current_usage(self):
qs = self qs = self
for s in ["pending", "skipped", "errored", "finished"]: 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( qs = qs.annotate(
**{ **{
"_usage_{}".format(s): models.Sum( "_usage_{}".format(s): models.Sum(
"libraries__uploads__size", "libraries__uploads__size", filter=uploads_query
filter=models.Q(libraries__uploads__import_status=s),
) )
} }
) )
......
...@@ -53,7 +53,25 @@ def test_actor_get_quota(factories): ...@@ -53,7 +53,25 @@ def test_actor_get_quota(factories):
audio_file__from_path=None, audio_file__from_path=None,
audio_file__data=b"aaaa", 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 assert library.actor.get_current_usage() == expected
......
Exclude in-place imported files from quota computation (#570)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment