series of tests added with built-in django test support

This commit is contained in:
pacnpal
2024-11-05 18:40:39 +00:00
parent ba226c861a
commit 2e8a725933
60 changed files with 2108 additions and 274 deletions

View File

@@ -1,18 +1,23 @@
from django.shortcuts import redirect
from django.urls import reverse
from django.views.generic import DetailView
class SlugRedirectMixin:
"""
Mixin that handles redirects for old slugs.
Requires the model to inherit from SluggedModel.
Requires the model to inherit from SluggedModel and view to inherit from DetailView.
"""
def get(self, request, *args, **kwargs):
def dispatch(self, request, *args, **kwargs):
# Only apply slug redirect logic to DetailViews
if not isinstance(self, DetailView):
return super().dispatch(request, *args, **kwargs)
# Get the object using current or historical slug
try:
self.object = self.get_object()
# Check if we used an old slug
current_slug = kwargs.get(self.slug_url_kwarg)
if current_slug != self.object.slug:
if current_slug and current_slug != self.object.slug:
# Get the URL pattern name from the view
url_pattern = self.get_redirect_url_pattern()
# Build kwargs for reverse()
@@ -22,9 +27,9 @@ class SlugRedirectMixin:
reverse(url_pattern, kwargs=reverse_kwargs),
permanent=True
)
return super().get(request, *args, **kwargs)
except self.model.DoesNotExist:
return super().get(request, *args, **kwargs)
return super().dispatch(request, *args, **kwargs)
except (self.model.DoesNotExist, AttributeError):
return super().dispatch(request, *args, **kwargs)
def get_redirect_url_pattern(self):
"""