mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 10:11:09 -05:00
series of tests added with built-in django test support
This commit is contained in:
Binary file not shown.
@@ -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):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user