From f3431598564e84817f7bbdddcae6053fa5df1ebd Mon Sep 17 00:00:00 2001 From: Eliot Berriot Date: Sun, 6 May 2018 15:36:18 +0200 Subject: [PATCH] Added an accessed_date field on TrackFile for easier cache deletion (#189) --- .../migrations/0026_trackfile_accessed_date.py | 18 ++++++++++++++++++ api/funkwhale_api/music/models.py | 1 + api/funkwhale_api/music/views.py | 5 +++++ api/tests/music/test_views.py | 14 ++++++++++++++ 4 files changed, 38 insertions(+) create mode 100644 api/funkwhale_api/music/migrations/0026_trackfile_accessed_date.py diff --git a/api/funkwhale_api/music/migrations/0026_trackfile_accessed_date.py b/api/funkwhale_api/music/migrations/0026_trackfile_accessed_date.py new file mode 100644 index 000000000..1d5327d93 --- /dev/null +++ b/api/funkwhale_api/music/migrations/0026_trackfile_accessed_date.py @@ -0,0 +1,18 @@ +# Generated by Django 2.0.3 on 2018-05-06 12:47 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('music', '0025_auto_20180419_2023'), + ] + + operations = [ + migrations.AddField( + model_name='trackfile', + name='accessed_date', + field=models.DateTimeField(blank=True, null=True), + ), + ] diff --git a/api/funkwhale_api/music/models.py b/api/funkwhale_api/music/models.py index 18f181e88..655d38755 100644 --- a/api/funkwhale_api/music/models.py +++ b/api/funkwhale_api/music/models.py @@ -415,6 +415,7 @@ class TrackFile(models.Model): source = models.URLField(null=True, blank=True, max_length=500) creation_date = models.DateTimeField(default=timezone.now) modification_date = models.DateTimeField(auto_now=True) + accessed_date = models.DateTimeField(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/views.py b/api/funkwhale_api/music/views.py index f53de1b0a..76fc8bc3e 100644 --- a/api/funkwhale_api/music/views.py +++ b/api/funkwhale_api/music/views.py @@ -14,6 +14,7 @@ from django.db.models.functions import Length from django.db.models import Count from django.http import StreamingHttpResponse from django.urls import reverse +from django.utils import timezone from django.utils.decorators import method_decorator from rest_framework import viewsets, views, mixins @@ -264,6 +265,10 @@ class TrackFileViewSet(viewsets.ReadOnlyModelViewSet): except models.TrackFile.DoesNotExist: return Response(status=404) + # we update the accessed_date + f.accessed_date = timezone.now() + f.save(update_fields=['accessed_date']) + mt = f.mimetype audio_file = f.audio_file try: diff --git a/api/tests/music/test_views.py b/api/tests/music/test_views.py index 2cdee4e8c..b22ab7fd5 100644 --- a/api/tests/music/test_views.py +++ b/api/tests/music/test_views.py @@ -2,6 +2,7 @@ import io import pytest from django.urls import reverse +from django.utils import timezone from funkwhale_api.music import views from funkwhale_api.federation import actors @@ -149,6 +150,19 @@ def test_can_proxy_remote_track( assert library_track.audio_file.read() == b'test' +def test_serve_updates_access_date(factories, settings, api_client): + settings.PROTECT_AUDIO_FILES = False + track_file = factories['music.TrackFile']() + now = timezone.now() + assert track_file.accessed_date is None + + response = api_client.get(track_file.path) + track_file.refresh_from_db() + + assert response.status_code == 200 + assert track_file.accessed_date > now + + def test_can_create_import_from_federation_tracks( factories, superuser_api_client, mocker): lts = factories['federation.LibraryTrack'].create_batch(size=5)