-
-
-
-
-
{{ album.album_date|date:"d.m.Y" }} • {{ album.photos_in_album }} photos
+ {% if results %}
+ {% for album in results %}
+
+
+
+
+
+
{{ album.album_date|date:"d.m.Y" }} • {{ album.photos_in_album }} photos
+
-
- {% endfor %}
+ {% endfor %}
+ {% else %}
+
Not found: "{{ query }}".
+ {% endif %}
{% include "./partials/pagination.html" %}
diff --git a/gallery/views.py b/gallery/views.py
index 35329a7..5fa8bb7 100644
--- a/gallery/views.py
+++ b/gallery/views.py
@@ -8,14 +8,15 @@ from .models import Album, Photo, Redir
class AlbumsList(ListView):
+ """Displays a paginated list of public albums."""
model = Album
template_name = 'gallery/album_list.html'
- queryset = Album.objects.filter(is_public=True)
- ordering = ['-album_date']
+ queryset = Album.objects.filter(is_public=True).order_by('-album_date')
paginate_by = 30
class AlbumDetail(DetailView):
+ """Shows a single album and its paginated photos."""
model = Album
template_name = 'gallery/album_detail.html'
@@ -26,33 +27,39 @@ class AlbumDetail(DetailView):
context = super().get_context_data(**kwargs)
photos = self.object.photos.all().order_by('taken_at')
- # Pagination
paginator = Paginator(photos, 30)
- page_number = self.request.GET.get('page')
- page_obj = paginator.get_page(page_number)
+ page_obj = paginator.get_page(self.request.GET.get('page'))
- # Return
- context['photos'] = page_obj.object_list
- context['page_obj'] = page_obj
+ context.update({
+ 'photos': page_obj.object_list,
+ 'page_obj': page_obj
+ })
return context
+
class PhotosList(ListView):
+ """Shows all public photos sorted by user-selected ordering."""
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')
+ ordering_options = {
+ 'latest': '-taken_at',
+ 'liked': 'likes',
+ 'popular': '-views'
+ }
+ order = ordering_options.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)
+ def get_context_data(self, **kwargs):
+ context = super().get_context_data(**kwargs)
context['order'] = self.request.GET.get('order', 'latest')
return context
class PhotoDetail(DetailView):
+ """Shows a single photo and handles liking functionality."""
model = Photo
slug_url_kwarg = 'photo_slug'
template_name = 'gallery/photo_detail.html'
@@ -62,43 +69,48 @@ class PhotoDetail(DetailView):
photo_slug = self.kwargs.get('photo_slug')
photo = get_object_or_404(Photo, slug=photo_slug, album__slug=album_slug)
+ # Track views using session to avoid duplicate counts
if photo.slug not in self.request.session:
photo.add_view()
self.request.session[photo.slug] = 0
return photo
- 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()
+ def get_context_data(self, **kwargs):
+ context = super().get_context_data(**kwargs)
+ context.update({
+ "next": self.object.get_next(),
+ "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.POST.get("like") == "true" and 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()
+ 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'])
class About(TemplateView):
+ """Static about page."""
template_name = "gallery/about.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
- context["about"] = settings.ABOUT_PAGE_SETTINGS # Haetaan asetukset
+ context["about"] = settings.ABOUT_PAGE_SETTINGS
return context
+
class Search(TemplateView):
+ """Search view for public albums by name, place or city."""
template_name = "gallery/search.html"
def get_context_data(self, **kwargs):
@@ -115,17 +127,19 @@ class Search(TemplateView):
Q(location__city__name__icontains=query)
).order_by('-album_date').distinct()
- paginator = Paginator(results, 30)
- page_number = self.request.GET.get('page')
- page_obj = paginator.get_page(page_number)
+ paginator = Paginator(results, 30)
+ page_obj = paginator.get_page(self.request.GET.get('page'))
- context['results'] = page_obj.object_list
- context['page_obj'] = page_obj
- context['paginator'] = paginator
- context['is_paginated'] = page_obj.has_other_pages()
+ context.update({
+ 'results': page_obj.object_list,
+ 'page_obj': page_obj,
+ 'paginator': paginator,
+ 'is_paginated': page_obj.has_other_pages()
+ })
return context
+
def redirect_to_album(request, redir_path):
+ """Handles redirect logic for shortened/legacy album URLs."""
redir = get_object_or_404(Redir, path=redir_path)
return redirect('gallery:album_url', album_slug=redir.album.slug)
-
diff --git a/templates/base.html b/templates/base.html
deleted file mode 100644
index bdee05b..0000000
--- a/templates/base.html
+++ /dev/null
@@ -1,86 +0,0 @@
-
-
-
-
- {% load static %}
-
-
-
-
-
-
-
-
-
-
-
{% block title %} Gallery {% endblock %}
-
-
-
-
-
- {% block parallax %}
- {% endblock %}
-
-
- {% block navbar %}
-
-
-
-
-
-
- {% endblock %}
-
-
-
-
- {% block breadcrumb %} {% endblock %}
-
-
-
-
- {% block content %} No Content {% endblock %}
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file