Added an accessed_date field on TrackFile for easier cache deletion (#189)

This commit is contained in:
Eliot Berriot 2018-05-06 15:36:18 +02:00
parent 2649ad88ff
commit f343159856
No known key found for this signature in database
GPG Key ID: DD6965E2476E5C27
4 changed files with 38 additions and 0 deletions

View File

@ -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),
),
]

View File

@ -415,6 +415,7 @@ class TrackFile(models.Model):
source = models.URLField(null=True, blank=True, max_length=500) source = models.URLField(null=True, blank=True, max_length=500)
creation_date = models.DateTimeField(default=timezone.now) creation_date = models.DateTimeField(default=timezone.now)
modification_date = models.DateTimeField(auto_now=True) modification_date = models.DateTimeField(auto_now=True)
accessed_date = models.DateTimeField(null=True, blank=True)
duration = models.IntegerField(null=True, blank=True) duration = models.IntegerField(null=True, blank=True)
acoustid_track_id = models.UUIDField(null=True, blank=True) acoustid_track_id = models.UUIDField(null=True, blank=True)
mimetype = models.CharField(null=True, blank=True, max_length=200) mimetype = models.CharField(null=True, blank=True, max_length=200)

View File

@ -14,6 +14,7 @@ from django.db.models.functions import Length
from django.db.models import Count from django.db.models import Count
from django.http import StreamingHttpResponse from django.http import StreamingHttpResponse
from django.urls import reverse from django.urls import reverse
from django.utils import timezone
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from rest_framework import viewsets, views, mixins from rest_framework import viewsets, views, mixins
@ -264,6 +265,10 @@ class TrackFileViewSet(viewsets.ReadOnlyModelViewSet):
except models.TrackFile.DoesNotExist: except models.TrackFile.DoesNotExist:
return Response(status=404) return Response(status=404)
# we update the accessed_date
f.accessed_date = timezone.now()
f.save(update_fields=['accessed_date'])
mt = f.mimetype mt = f.mimetype
audio_file = f.audio_file audio_file = f.audio_file
try: try:

View File

@ -2,6 +2,7 @@ import io
import pytest import pytest
from django.urls import reverse from django.urls import reverse
from django.utils import timezone
from funkwhale_api.music import views from funkwhale_api.music import views
from funkwhale_api.federation import actors from funkwhale_api.federation import actors
@ -149,6 +150,19 @@ def test_can_proxy_remote_track(
assert library_track.audio_file.read() == b'test' 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( def test_can_create_import_from_federation_tracks(
factories, superuser_api_client, mocker): factories, superuser_api_client, mocker):
lts = factories['federation.LibraryTrack'].create_batch(size=5) lts = factories['federation.LibraryTrack'].create_batch(size=5)