muistox/gallery/views.py

103 lines
3.3 KiB
Python
Raw Permalink Normal View History

2025-01-13 21:09:29 +02:00
from django.core.paginator import Paginator
2025-01-04 20:04:20 +02:00
from django.shortcuts import get_object_or_404, redirect, render
from django.views.generic import DetailView, ListView, TemplateView
from .models import Album, Photo, Redir
2024-12-28 12:21:29 +02:00
2024-12-28 14:14:21 +02:00
class Main(TemplateView):
2025-01-04 20:04:20 +02:00
template_name = "gallery/main.html"
2025-01-13 21:09:29 +02:00
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
2025-01-04 20:04:20 +02:00
class AlbumsList(ListView):
model = Album
template_name = 'gallery/album_list.html'
queryset = Album.objects.filter(is_public=True)
ordering = ['-album_date']
2025-01-13 21:09:29 +02:00
paginate_by = 30
2025-01-04 20:04:20 +02:00
class AlbumDetail(DetailView):
model = Album
template_name = 'gallery/album_detail.html'
def get_object(self, queryset=None):
2025-01-05 17:26:09 +02:00
return get_object_or_404(Album, slug=self.kwargs.get('album_slug'))
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2025-01-13 21:09:29 +02:00
photos = self.object.photos.all().order_by('taken_at')
2025-01-19 21:30:23 +02:00
# Pagination
2025-01-13 21:09:29 +02:00
paginator = Paginator(photos, 30)
page_number = self.request.GET.get('page')
page_obj = paginator.get_page(page_number)
2025-01-19 21:30:23 +02:00
# Return
2025-01-13 21:09:29 +02:00
context['photos'] = page_obj.object_list
context['page_obj'] = page_obj
2025-01-05 17:26:09 +02:00
return context
2025-01-13 22:49:12 +02:00
class PhotosList(ListView):
model = Photo
paginate_by = 30
def get_queryset(self):
orderdic = {'latest': '-taken_at', 'liked': 'likes', 'popular': '-views'}
order = orderdic.get(self.request.GET.get('order', 'latest'), '-taken_at')
return Photo.objects.filter(album__is_public=True).order_by(order).select_related('album')
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['order'] = self.request.GET.get('order', 'latest')
return context
2025-01-13 22:49:12 +02:00
2025-01-15 20:31:41 +02:00
2025-01-05 17:26:09 +02:00
class PhotoDetail(DetailView):
model = Photo
2025-01-13 22:49:12 +02:00
slug_url_kwarg = 'photo_slug'
2025-01-05 17:26:09 +02:00
template_name = 'gallery/photo_detail.html'
def get_object(self, queryset=None):
2025-01-15 20:31:41 +02:00
album_slug = self.kwargs.get('album_slug')
photo_slug = self.kwargs.get('photo_slug')
photo = get_object_or_404(Photo, slug=photo_slug, album__slug=album_slug)
if photo.slug not in self.request.session:
photo.add_view()
self.request.session[photo.slug] = 0
2025-01-13 22:49:12 +02:00
return photo
2025-01-15 20:31:41 +02:00
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context["next"] = self.object.get_next()
context["prev"] = self.object.get_prev()
return context
def post(self, request, *args, **kwargs):
photo = self.get_object()
if request.POST.get("like") == "true":
if self.request.session.get(photo.slug) == 0:
photo.add_like()
self.request.session[photo.slug] = 1
if request.user.is_authenticated:
photo.is_favorite = True
photo.save()
return redirect('gallery:photo_url', album_slug=self.kwargs['album_slug'], photo_slug=self.kwargs['photo_slug'])
def redirect_to_album(request, redir_path):
redir = get_object_or_404(Redir, path=redir_path)
return redirect('gallery:album_url', album_slug=redir.album.slug)