Update old URL redirect
This commit is contained in:
parent
2b6135f26d
commit
0c1c888547
1 changed files with 27 additions and 2 deletions
|
@ -1,3 +1,6 @@
|
|||
import os
|
||||
import re
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.core.paginator import Paginator
|
||||
|
@ -19,7 +22,6 @@ class Main(TemplateView):
|
|||
context = super().get_context_data(**kwargs)
|
||||
context['canonical_url'] = self.request.build_absolute_uri(reverse('gallery:main_url'))
|
||||
context['latest_albums'] = Album.objects.filter(is_public=True).order_by('-album_date')[:6]
|
||||
|
||||
return context
|
||||
|
||||
|
||||
|
@ -34,6 +36,29 @@ class About(TemplateView):
|
|||
|
||||
|
||||
def redirect_to_album(request, redir_path):
|
||||
"""Handles redirect logic for shortened/legacy album URLs."""
|
||||
"""Redirect old album or photo URLs to new structure, including pagination and photos."""
|
||||
photo_extensions = ['.jpg', '.jpeg']
|
||||
|
||||
# 1. Tarkista vanha pagination-muoto: Album_Name/page/3/
|
||||
page_match = re.match(r'^(?P<album_path>.+)/page/(?P<page_number>\d+)/?$', redir_path)
|
||||
if page_match:
|
||||
album_path = page_match.group('album_path')
|
||||
page_number = page_match.group('page_number')
|
||||
redir = get_object_or_404(Redir, path=album_path)
|
||||
return redirect(f'/albums/{redir.album.slug}/?page={page_number}')
|
||||
|
||||
# 2. Tarkista, onko kyseessä kuvan URL (tiedoston pääte)
|
||||
_, ext = os.path.splitext(redir_path.lower())
|
||||
if ext in photo_extensions:
|
||||
# Erotellaan albumin nimi ja kuvan tiedostonimi
|
||||
album_path, photo_filename = redir_path.rsplit('/', 1)
|
||||
redir = get_object_or_404(Redir, path=album_path)
|
||||
album = redir.album
|
||||
photo = Photo.objects.filter(album=album, slug=photo_filename).first()
|
||||
if photo:
|
||||
return redirect('gallery:photo_url', album_slug=album.slug, photo_slug=photo.slug)
|
||||
return redirect('gallery:album_url', album_slug=album.slug)
|
||||
|
||||
# 3. Oletus: pelkkä albuminimi
|
||||
redir = get_object_or_404(Redir, path=redir_path)
|
||||
return redirect('gallery:album_url', album_slug=redir.album.slug)
|
||||
|
|
Loading…
Add table
Reference in a new issue