From 63ddb51e89e491eb9642555fe6bd9cdc37da71b8 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Thu, 30 Nov 2023 23:37:42 +0100 Subject: [PATCH] Add even more imagekit gloriousness --- vrobbler/apps/boardgames/models.py | 26 +++++++++ vrobbler/apps/books/models.py | 28 +++++++++- vrobbler/apps/music/models.py | 16 +++--- vrobbler/apps/videogames/models.py | 54 ++++++++++++++++++- vrobbler/apps/videos/models.py | 36 ++++++++++++- vrobbler/templates/scrobbles/_media_box.html | 6 +-- .../templates/scrobbles/scrobble_list.html | 8 +-- 7 files changed, 153 insertions(+), 21 deletions(-) diff --git a/vrobbler/apps/boardgames/models.py b/vrobbler/apps/boardgames/models.py index a37a314..c417db0 100644 --- a/vrobbler/apps/boardgames/models.py +++ b/vrobbler/apps/boardgames/models.py @@ -10,6 +10,8 @@ from django.core.files.base import ContentFile from django.db import models from django.urls import reverse from django_extensions.db.models import TimeStampedModel +from imagekit.models import ImageSpecField +from imagekit.processors import ResizeToFit from scrobbles.mixins import ScrobblableMixin from vrobbler.apps.boardgames.bgg import lookup_boardgame_id_from_bgg @@ -54,7 +56,31 @@ class BoardGame(ScrobblableMixin): uuid = models.UUIDField(default=uuid4, editable=False, **BNULL) description = models.TextField(**BNULL) cover = models.ImageField(upload_to="boardgames/covers/", **BNULL) + cover_small = ImageSpecField( + source="cover", + processors=[ResizeToFit(100, 100)], + format="JPEG", + options={"quality": 60}, + ) + cover_medium = ImageSpecField( + source="cover", + processors=[ResizeToFit(300, 300)], + format="JPEG", + options={"quality": 75}, + ) layout_image = models.ImageField(upload_to="boardgames/layouts/", **BNULL) + layout_image_small = ImageSpecField( + source="layout_image", + processors=[ResizeToFit(100, 100)], + format="JPEG", + options={"quality": 60}, + ) + layout_image_medium = ImageSpecField( + source="layout_image", + processors=[ResizeToFit(300, 300)], + format="JPEG", + options={"quality": 75}, + ) rating = models.FloatField(**BNULL) max_players = models.PositiveSmallIntegerField(**BNULL) min_players = models.PositiveSmallIntegerField(**BNULL) diff --git a/vrobbler/apps/books/models.py b/vrobbler/apps/books/models.py index 1c4c87d..a1224ca 100644 --- a/vrobbler/apps/books/models.py +++ b/vrobbler/apps/books/models.py @@ -13,6 +13,8 @@ from django.core.files.base import ContentFile from django.db import models from django.urls import reverse from django_extensions.db.models import TimeStampedModel +from imagekit.models import ImageSpecField +from imagekit.processors import ResizeToFit from scrobbles.mixins import ( LongPlayScrobblableMixin, ObjectWithGenres, @@ -37,6 +39,18 @@ class Author(TimeStampedModel): uuid = models.UUIDField(default=uuid4, editable=False, **BNULL) openlibrary_id = models.CharField(max_length=255, **BNULL) headshot = models.ImageField(upload_to="books/authors/", **BNULL) + headshot_small = ImageSpecField( + source="headshot", + processors=[ResizeToFit(100, 100)], + format="JPEG", + options={"quality": 60}, + ) + headshot_medium = ImageSpecField( + source="headshot", + processors=[ResizeToFit(300, 300)], + format="JPEG", + options={"quality": 75}, + ) bio = models.TextField(**BNULL) wikipedia_url = models.CharField(max_length=255, **BNULL) isni = models.CharField(max_length=255, **BNULL) @@ -88,6 +102,18 @@ class Book(LongPlayScrobblableMixin): openlibrary_id = models.CharField(max_length=255, **BNULL) locg_slug = models.CharField(max_length=255, **BNULL) cover = models.ImageField(upload_to="books/covers/", **BNULL) + cover_small = ImageSpecField( + source="cover", + processors=[ResizeToFit(100, 100)], + format="JPEG", + options={"quality": 60}, + ) + cover_medium = ImageSpecField( + source="cover", + processors=[ResizeToFit(300, 300)], + format="JPEG", + options={"quality": 75}, + ) summary = models.TextField(**BNULL) genre = TaggableManager(through=ObjectWithGenres) @@ -103,7 +129,7 @@ class Book(LongPlayScrobblableMixin): def primary_image_url(self) -> str: url = "" if self.cover: - url = self.cover.url + url = self.cover_medium.url return url def get_start_url(self): diff --git a/vrobbler/apps/music/models.py b/vrobbler/apps/music/models.py index e9dba31..64286db 100644 --- a/vrobbler/apps/music/models.py +++ b/vrobbler/apps/music/models.py @@ -13,7 +13,7 @@ from django.urls import reverse from django.utils.translation import gettext_lazy as _ from django_extensions.db.models import TimeStampedModel from imagekit.models import ImageSpecField -from imagekit.processors import ResizeToFill +from imagekit.processors import ResizeToFit from music.allmusic import get_allmusic_slug, scrape_data_from_allmusic from music.bandcamp import get_bandcamp_slug from music.theaudiodb import lookup_album_from_tadb, lookup_artist_from_tadb @@ -36,13 +36,13 @@ class Artist(TimeStampedModel): thumbnail = models.ImageField(upload_to="artist/", **BNULL) thumbnail_small = ImageSpecField( source="thumbnail", - processors=[ResizeToFill(100)], + processors=[ResizeToFit(100, 100)], format="JPEG", options={"quality": 60}, ) thumbnail_medium = ImageSpecField( source="thumbnail", - processors=[ResizeToFill(300)], + processors=[ResizeToFit(300, 300)], format="JPEG", options={"quality": 75}, ) @@ -168,13 +168,13 @@ class Album(TimeStampedModel): cover_image = models.ImageField(upload_to="albums/", **BNULL) cover_image_small = ImageSpecField( source="cover_image", - processors=[ResizeToFill(100)], + processors=[ResizeToFit(100)], format="JPEG", options={"quality": 60}, ) cover_image_medium = ImageSpecField( source="cover_image", - processors=[ResizeToFill(300)], + processors=[ResizeToFit(300)], format="JPEG", options={"quality": 75}, ) @@ -213,7 +213,7 @@ class Album(TimeStampedModel): @property def primary_image_url(self) -> str: if self.cover_image.url: - return self.cover_image.url + return self.cover_image_medium.url return "" @property @@ -440,9 +440,9 @@ class Track(ScrobblableMixin): def primary_image_url(self) -> str: url = "" if self.artist.thumbnail: - url = self.artist.thumbnail.url + url = self.artist.thumbnail_medium.url if self.album and self.album.cover_image: - url = self.album.cover_image.url + url = self.album.cover_image_medium.url return url @classmethod diff --git a/vrobbler/apps/videogames/models.py b/vrobbler/apps/videogames/models.py index 5ff2289..5de53aa 100644 --- a/vrobbler/apps/videogames/models.py +++ b/vrobbler/apps/videogames/models.py @@ -6,6 +6,8 @@ from django.contrib.auth import get_user_model from django.db import models from django.urls import reverse from django_extensions.db.models import TimeStampedModel +from imagekit.models import ImageSpecField +from imagekit.processors import ResizeToFit from scrobbles.mixins import LongPlayScrobblableMixin from scrobbles.utils import get_scrobbles_for_media from videogames.igdb import lookup_game_id_from_gdb @@ -34,6 +36,18 @@ class VideoGameCollection(TimeStampedModel): name = models.CharField(max_length=255) uuid = models.UUIDField(default=uuid4, editable=False, **BNULL) cover = models.ImageField(upload_to="games/series-covers/", **BNULL) + cover_small = ImageSpecField( + source="cover", + processors=[ResizeToFit(100, 100)], + format="JPEG", + options={"quality": 60}, + ) + cover_medium = ImageSpecField( + source="cover", + processors=[ResizeToFit(300, 300)], + format="JPEG", + options={"quality": 75}, + ) igdb_id = models.IntegerField(**BNULL) def __str__(self): @@ -72,9 +86,45 @@ class VideoGame(LongPlayScrobblableMixin): alternative_name = models.CharField(max_length=255, **BNULL) uuid = models.UUIDField(default=uuid4, editable=False, **BNULL) cover = models.ImageField(upload_to="games/covers/", **BNULL) + cover_small = ImageSpecField( + source="cover", + processors=[ResizeToFit(100, 100)], + format="JPEG", + options={"quality": 60}, + ) + cover_medium = ImageSpecField( + source="cover", + processors=[ResizeToFit(300, 300)], + format="JPEG", + options={"quality": 75}, + ) screenshot = models.ImageField(upload_to="games/screenshots/", **BNULL) + screenshot_small = ImageSpecField( + source="screenshot", + processors=[ResizeToFit(100, 100)], + format="JPEG", + options={"quality": 60}, + ) + screenshot_medium = ImageSpecField( + source="screenshot", + processors=[ResizeToFit(300, 300)], + format="JPEG", + options={"quality": 75}, + ) summary = models.TextField(**BNULL) hltb_cover = models.ImageField(upload_to="games/hltb_covers/", **BNULL) + hltb_cover_small = ImageSpecField( + source="htlb_cover", + processors=[ResizeToFit(100, 100)], + format="JPEG", + options={"quality": 60}, + ) + hltb_cover_medium = ImageSpecField( + source="hltb_cover", + processors=[ResizeToFit(300, 300)], + format="JPEG", + options={"quality": 75}, + ) rating = models.FloatField(**BNULL) rating_count = models.IntegerField(**BNULL) release_date = models.DateTimeField(**BNULL) @@ -97,9 +147,9 @@ class VideoGame(LongPlayScrobblableMixin): def primary_image_url(self) -> str: url = "" if self.cover: - url = self.cover.url + url = self.cover_medium.url if self.hltb_cover: - url = self.hltb_cover.url + url = self.hltb_cover_medium.url return url def get_absolute_url(self): diff --git a/vrobbler/apps/videos/models.py b/vrobbler/apps/videos/models.py index 13f330b..f8b054d 100644 --- a/vrobbler/apps/videos/models.py +++ b/vrobbler/apps/videos/models.py @@ -9,9 +9,10 @@ from django.db import models from django.urls import reverse from django.utils.translation import gettext_lazy as _ from django_extensions.db.models import TimeStampedModel +from imagekit.models import ImageSpecField +from imagekit.processors import ResizeToFit from scrobbles.mixins import ObjectWithGenres, ScrobblableMixin from taggit.managers import TaggableManager - from videos.imdb import lookup_video_from_imdb logger = logging.getLogger(__name__) @@ -25,6 +26,18 @@ class Series(TimeStampedModel): imdb_id = models.CharField(max_length=20, **BNULL) imdb_rating = models.FloatField(**BNULL) cover_image = models.ImageField(upload_to="videos/series/", **BNULL) + cover_small = ImageSpecField( + source="cover_image", + processors=[ResizeToFit(100, 100)], + format="JPEG", + options={"quality": 60}, + ) + cover_medium = ImageSpecField( + source="cover_image", + processors=[ResizeToFit(300, 300)], + format="JPEG", + options={"quality": 75}, + ) preferred_source = models.CharField(max_length=100, **BNULL) genre = TaggableManager(through=ObjectWithGenres) @@ -41,6 +54,13 @@ class Series(TimeStampedModel): def imdb_link(self): return f"https://www.imdb.com/title/tt{self.imdb_id}" + @property + def primary_image_url(self) -> str: + url = "" + if self.cover_image: + url = self.cover_image_medium.url + return url + def scrobbles_for_user(self, user_id: int, include_playing=False): from scrobbles.models import Scrobble @@ -120,6 +140,18 @@ class Video(ScrobblableMixin): imdb_id = models.CharField(max_length=20, **BNULL) imdb_rating = models.FloatField(**BNULL) cover_image = models.ImageField(upload_to="videos/video/", **BNULL) + cover_small = ImageSpecField( + source="cover_image", + processors=[ResizeToFit(100, 100)], + format="JPEG", + options={"quality": 60}, + ) + cover_medium = ImageSpecField( + source="cover_image", + processors=[ResizeToFit(300, 300)], + format="JPEG", + options={"quality": 75}, + ) tvrage_id = models.CharField(max_length=20, **BNULL) tvdb_id = models.CharField(max_length=20, **BNULL) plot = models.TextField(**BNULL) @@ -158,7 +190,7 @@ class Video(ScrobblableMixin): def primary_image_url(self) -> str: url = "" if self.cover_image: - url = self.cover_image.url + url = self.cover_image_medium.url return url def fix_metadata(self, force_update=False): diff --git a/vrobbler/templates/scrobbles/_media_box.html b/vrobbler/templates/scrobbles/_media_box.html index b049d31..91fa991 100644 --- a/vrobbler/templates/scrobbles/_media_box.html +++ b/vrobbler/templates/scrobbles/_media_box.html @@ -1,11 +1,7 @@
{{media.title}}
- {% if media.hltb_cover %} -
- {% elif media.cover %} -
- {% endif %} +
{% if media.is_long_play_in_progress %}Playing{% else %}Resume{% endif %} Finish diff --git a/vrobbler/templates/scrobbles/scrobble_list.html b/vrobbler/templates/scrobbles/scrobble_list.html index 8e04a78..bf01980 100644 --- a/vrobbler/templates/scrobbles/scrobble_list.html +++ b/vrobbler/templates/scrobbles/scrobble_list.html @@ -162,6 +162,7 @@ Time + Cover Title Series @@ -170,6 +171,7 @@ {% for scrobble in video_scrobble_list %} {{scrobble.timestamp|naturaltime}} + {% if scrobble.video.tv_series%}S{{scrobble.video.season_number}}E{{scrobble.video.episode_number}} -{%endif %} {{scrobble.video.title}} {% if scrobble.video.tv_series %}{{scrobble.video.tv_series}}{% endif %} @@ -250,9 +252,9 @@ {{scrobble.timestamp|naturaltime}} {% if scrobble.videogame_screenshot %} - + {% else %} - + {% endif %} {{scrobble.media_obj.title}} {{scrobble.playback_position_seconds|natural_duration}} @@ -282,7 +284,7 @@ {% for scrobble in boardgame_scrobble_list %} {{scrobble.timestamp|naturaltime}} - + {{scrobble.media_obj.title}} {{scrobble.playback_position_seconds|natural_duration}}