From 3d155bb46fedf825f5cc9dd678d3a4065a75ec59 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 27 Aug 2022 12:59:12 +0100 Subject: [PATCH] Add management command to refresh caches of spotify playlists --- website/common/tests/test_pages.py | 4 +++- .../commands/refresh_spotify_playlists.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 website/spotify/management/commands/refresh_spotify_playlists.py diff --git a/website/common/tests/test_pages.py b/website/common/tests/test_pages.py index be4fb8b..7247221 100644 --- a/website/common/tests/test_pages.py +++ b/website/common/tests/test_pages.py @@ -60,6 +60,8 @@ class ListingPageTestCase(TestCase): def test_feed_accessible(self) -> None: with self.assertNumQueries(11): - response = self.client.get(self.page.url + self.page.reverse_subpage("feed")) + response = self.client.get( + self.page.url + self.page.reverse_subpage("feed") + ) self.assertEqual(response.status_code, 200) self.assertEqual(response["Content-Type"], "application/rss+xml; charset=utf-8") diff --git a/website/spotify/management/commands/refresh_spotify_playlists.py b/website/spotify/management/commands/refresh_spotify_playlists.py new file mode 100644 index 0000000..8e6ca0a --- /dev/null +++ b/website/spotify/management/commands/refresh_spotify_playlists.py @@ -0,0 +1,17 @@ +from django.core.cache import cache +from django.core.management.base import BaseCommand + +from website.spotify.models import SpotifyPlaylistPage +from website.utils.queue import enqueue_or_sync + + +def refresh_cache(page_id: int) -> None: + page = SpotifyPlaylistPage.objects.get(id=page_id) + cache.delete(page.playlist_cache_key) + page.playlist_data # Prime cache + + +class Command(BaseCommand): + def handle(self, *args: list, **options: dict) -> None: + for page in SpotifyPlaylistPage.objects.all().defer_streamfields().iterator(): + enqueue_or_sync(refresh_cache, args=[page.id])