muistox/gallery/views.py
2025-01-13 22:49:12 +02:00

63 lines
1.9 KiB
Python

from django.core.paginator import Paginator
from django.shortcuts import get_object_or_404, redirect, render
from django.views.generic import DetailView, ListView, TemplateView
from .models import Album, Photo
class Main(TemplateView):
template_name = "gallery/main.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
session_expiry = self.request.session.get_expiry_date()
context['session_expiry'] = session_expiry
return context
class AlbumsList(ListView):
model = Album
template_name = 'gallery/album_list.html'
queryset = Album.objects.filter(is_public=True)
ordering = ['-album_date']
paginate_by = 30
class AlbumDetail(DetailView):
model = Album
template_name = 'gallery/album_detail.html'
def get_object(self, queryset=None):
return get_object_or_404(Album, slug=self.kwargs.get('album_slug'))
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
photos = self.object.photos.all().order_by('taken_at')
paginator = Paginator(photos, 30)
page_number = self.request.GET.get('page')
page_obj = paginator.get_page(page_number)
context['photos'] = page_obj.object_list
context['page_obj'] = page_obj
return context
class PhotosList(ListView):
model = Photo
paginate_by = 30
ordering = ['-taken_at']
class PhotoDetail(DetailView):
model = Photo
slug_url_kwarg = 'photo_slug'
template_name = 'gallery/photo_detail.html'
def get_object(self, queryset=None):
photo = get_object_or_404(Photo, slug=self.kwargs.get(self.slug_url_kwarg))
photo_key = f'photo_{photo.slug}'
if photo_key not in self.request.session:
photo.add_view()
self.request.session[photo_key] = 0
return photo