Added an accessed_date field on TrackFile for easier cache deletion (#189)
This commit is contained in:
		
							parent
							
								
									2649ad88ff
								
							
						
					
					
						commit
						f343159856
					
				|  | @ -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), | ||||
|         ), | ||||
|     ] | ||||
|  | @ -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) | ||||
|  |  | |||
|  | @ -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: | ||||
|  |  | |||
|  | @ -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) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	 Eliot Berriot
						Eliot Berriot