mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-04 08:39:16 +02:00
See #170: now record downloads count on tracks/uploads
This commit is contained in:
parent
3674d1235d
commit
b22b9c83b0
12 changed files with 161 additions and 18 deletions
|
@ -4,6 +4,11 @@ import magic
|
|||
import mutagen
|
||||
import pydub
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.db.models import F
|
||||
|
||||
from funkwhale_api.common import throttling
|
||||
from funkwhale_api.common.search import get_fts_query # noqa
|
||||
from funkwhale_api.common.search import get_query # noqa
|
||||
from funkwhale_api.common.search import normalize_query # noqa
|
||||
|
@ -91,3 +96,25 @@ def transcode_file(input, output, input_format, output_format, **kwargs):
|
|||
def transcode_audio(audio, output, output_format, **kwargs):
|
||||
with output.open("wb"):
|
||||
return audio.export(output, format=output_format, **kwargs)
|
||||
|
||||
|
||||
def increment_downloads_count(upload, user, wsgi_request):
|
||||
ident = throttling.get_ident(user=user, request=wsgi_request)
|
||||
cache_key = "downloads_count:upload-{}:{}-{}".format(
|
||||
upload.pk, ident["type"], ident["id"]
|
||||
)
|
||||
|
||||
value = cache.get(cache_key)
|
||||
if value:
|
||||
# download already tracked
|
||||
return
|
||||
|
||||
upload.downloads_count = F("downloads_count") + 1
|
||||
upload.track.downloads_count = F("downloads_count") + 1
|
||||
|
||||
upload.save(update_fields=["downloads_count"])
|
||||
upload.track.save(update_fields=["downloads_count"])
|
||||
|
||||
duration = max(upload.duration or 0, settings.MIN_DELAY_BETWEEN_DOWNLOADS_COUNT)
|
||||
|
||||
cache.set(cache_key, 1, duration)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue