From 86d9ca5066c26017245f52239affb8d8227aca06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9DNyymix=E2=80=9D?= Date: Sat, 26 Apr 2025 20:39:57 +0300 Subject: [PATCH] Optimize templates & clean code --- gallery/admin.py | 3 ++- gallery/context_processors.py | 3 +-- gallery/models/album.py | 2 +- gallery/models/location.py | 1 - gallery/models/photo.py | 9 +++---- gallery/sitemaps.py | 2 +- gallery/templates/gallery/album_list.html | 22 ++++++++------- gallery/templates/gallery/album_search.html | 22 ++++++++------- gallery/templates/gallery/main.html | 27 ++++++++++++------- gallery/templatetags/image_tags.py | 25 ++++++++++++----- static/css/muistox.css | 30 ++++++++++++++++++--- 11 files changed, 98 insertions(+), 48 deletions(-) diff --git a/gallery/admin.py b/gallery/admin.py index 0c23b74..e671ec5 100644 --- a/gallery/admin.py +++ b/gallery/admin.py @@ -61,6 +61,7 @@ class LocationAdmin(admin.ModelAdmin): search_fields = ('place', 'city__name') list_per_page = 30 + class AlbumAdmin(admin.ModelAdmin): prepopulated_fields = {'slug': ('name',)} list_display = ('name', 'location', 'album_date', 'is_public', 'upload_link', 'thumbnail') @@ -145,4 +146,4 @@ admin.site.register(City, CityAdmin) admin.site.register(Location, LocationAdmin) admin.site.register(Album, AlbumAdmin) admin.site.register(Photo, PhotoAdmin) -admin.site.register(Redir, RedirAdmin) \ No newline at end of file +admin.site.register(Redir, RedirAdmin) diff --git a/gallery/context_processors.py b/gallery/context_processors.py index c7a7d46..5c38e85 100644 --- a/gallery/context_processors.py +++ b/gallery/context_processors.py @@ -13,9 +13,8 @@ def site_config(request): try: with open(config_path, 'r') as f: config = json.load(f) - cache.set('site_config', config, 3600) + cache.set('site_config', config, 3600) except FileNotFoundError: config = {} return {'site_config': config} - diff --git a/gallery/models/album.py b/gallery/models/album.py index 97310bb..a4062fc 100644 --- a/gallery/models/album.py +++ b/gallery/models/album.py @@ -65,4 +65,4 @@ class Album(models.Model): @receiver(post_save, sender=Album) def delete_album_cover_cache(sender, instance, **kwargs): - cache.delete(f'cover_image_data_{instance.pk}') \ No newline at end of file + cache.delete(f'photo_md_image_data_{instance.pk}') diff --git a/gallery/models/location.py b/gallery/models/location.py index 7338d60..f6772a8 100644 --- a/gallery/models/location.py +++ b/gallery/models/location.py @@ -20,7 +20,6 @@ class Location(models.Model): unique_together = ('place', "city") ordering = ['city'] - def __str__(self): if self.place: return '{0}, {1}'.format(self.city, self.place) diff --git a/gallery/models/photo.py b/gallery/models/photo.py index 76f455e..9c34cce 100644 --- a/gallery/models/photo.py +++ b/gallery/models/photo.py @@ -38,10 +38,10 @@ class Photo(models.Model): @property def orientation(self): return "Portrait" if self.height > self.width else "Landscape" - + @property def aspect_ratio(self): - return self.width / self.height + return self.width / self.height def save(self, *args, **kwargs): if not self.slug: @@ -59,7 +59,7 @@ class Photo(models.Model): def get_next(self): return self.__class__.objects.filter(taken_at__gt=self.taken_at, album=self.album).order_by('taken_at').first() - + def get_prev(self): return self.__class__.objects.filter(taken_at__lt=self.taken_at, album=self.album).order_by('-taken_at').first() @@ -77,7 +77,6 @@ class Photo(models.Model): def __str__(self): return f'{self.slug} ({self.orientation}) {self.is_favorite}' - @receiver(post_save, sender=Photo) @@ -95,4 +94,4 @@ def handle_photo_creation(sender, instance, created, **kwargs): # Aseta cover, jos albumilla ei ole vielä sellaista if instance.album and not instance.album.cover: instance.album.cover = instance - instance.album.save(update_fields=['cover']) \ No newline at end of file + instance.album.save(update_fields=['cover']) diff --git a/gallery/sitemaps.py b/gallery/sitemaps.py index cf2e3a9..a6a0f3e 100644 --- a/gallery/sitemaps.py +++ b/gallery/sitemaps.py @@ -15,4 +15,4 @@ class AlbumSitemap(Sitemap): return obj.album_date def location(self, obj): - return reverse('gallery:album_url', kwargs={'album_slug': obj.slug}) \ No newline at end of file + return reverse('gallery:album_url', kwargs={'album_slug': obj.slug}) diff --git a/gallery/templates/gallery/album_list.html b/gallery/templates/gallery/album_list.html index 2c47d64..40d6655 100644 --- a/gallery/templates/gallery/album_list.html +++ b/gallery/templates/gallery/album_list.html @@ -30,15 +30,19 @@
- {% with album|cover_image_data as img %} - Cover image for {{ album.name }} + {% with album.cover|photo_image_data as img %} +
+ Cover image for {{ album.name }} +
{% endwith %}
diff --git a/gallery/templates/gallery/album_search.html b/gallery/templates/gallery/album_search.html index 1026350..fd13000 100644 --- a/gallery/templates/gallery/album_search.html +++ b/gallery/templates/gallery/album_search.html @@ -31,15 +31,19 @@
- {% with album|cover_image_data as img %} - Cover image for {{ album.name }} + {% with album.cover|photo_image_data as img %} +
+ Cover image for {{ album.name }} +
{% endwith %}
diff --git a/gallery/templates/gallery/main.html b/gallery/templates/gallery/main.html index 9b56b01..cacd2fb 100644 --- a/gallery/templates/gallery/main.html +++ b/gallery/templates/gallery/main.html @@ -15,6 +15,7 @@ {% load top_tags %} + {% load image_tags %} {% random_favorite_photos_portrait 10 as top_portrait_photos %} {% if top_portrait_photos %} @@ -22,11 +23,13 @@
{% for photo in top_portrait_photos %} + {% with photo|photo_image_data as data %}
- {{ photo.album.slug }} + {{ photo.album.slug }}
+ {% endwith %} {% endfor %}
@@ -46,15 +49,19 @@ diff --git a/gallery/templatetags/image_tags.py b/gallery/templatetags/image_tags.py index 387d8ba..c92bae8 100644 --- a/gallery/templatetags/image_tags.py +++ b/gallery/templatetags/image_tags.py @@ -6,24 +6,36 @@ from django.templatetags.static import static register = template.Library() -@register.filter -def cover_image_data(album): - cache_key = f'cover_image_data_{album.pk}' +@register.filter +def photo_image_data(photo): + """ + Generate resized image data for Photo objects. + """ + if not photo: + return { + "url": static("img/placeholder.png"), + "width": 1200, + "height": 800, + "aspect_ratio": round(1200 / 800, 3), + "is_placeholder": True, + } + + cache_key = f'photo_md_image_data_{photo.pk}' data = cache.get(cache_key) if data: return data - photo = getattr(album, "cover", None) max_w, max_h = 720, 720 - if not photo or not photo.width or not photo.height: + if not photo.width or not photo.height: data = { "url": static("img/placeholder.png"), "width": 1200, "height": 800, "aspect_ratio": round(1200 / 800, 3), + "is_placeholder": True, } else: aspect = photo.aspect_ratio @@ -39,7 +51,8 @@ def cover_image_data(album): "width": w, "height": h, "aspect_ratio": round(aspect, 3), + "is_placeholder": False, } cache.set(cache_key, data, 60 * 60 * 24) - return data \ No newline at end of file + return data diff --git a/static/css/muistox.css b/static/css/muistox.css index 6a6ae8c..634fc49 100644 --- a/static/css/muistox.css +++ b/static/css/muistox.css @@ -1,8 +1,10 @@ body { - font: 400 14px/20px "Helvetica Neue",Helvetica,Arial,sans-serif; + font: 400 14px/20px "Helvetica Neue", Helvetica, Arial, sans-serif; } -.uk-navbar-item, .uk-navbar-nav>li>a, .uk-navbar-toggle { +.uk-navbar-item, +.uk-navbar-nav>li>a, +.uk-navbar-toggle { min-height: 50px; } @@ -12,5 +14,27 @@ body { } .uk-breadcrumb>:nth-child(n+2):not(.uk-first-column)::before { - margin: 0 10px 0 calc(10px - 4px); + margin: 0 10px 0 calc(10px - 4px); +} + +.uk-blur img { + filter: blur(8px); + transform: scale(1.05); + transition: filter 0.5s ease, transform 0.5s ease; +} + +.fade-in { + animation: fadeIn 0.8s ease forwards; +} + +@keyframes fadeIn { + 0% { + opacity: 0; + transform: scale(1.02); + } + + 100% { + opacity: 1; + transform: scale(1); + } } \ No newline at end of file