Skip to content
Snippets Groups Projects
Verified Commit f3431598 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

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

parent 2649ad88
Branches
Tags
No related merge requests found
# 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),
),
]
...@@ -415,6 +415,7 @@ class TrackFile(models.Model): ...@@ -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)
......
...@@ -14,6 +14,7 @@ from django.db.models.functions import Length ...@@ -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): ...@@ -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:
......
...@@ -2,6 +2,7 @@ import io ...@@ -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( ...@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment