fixed a bunch of things, hopefully didn't break things

This commit is contained in:
pacnpal
2024-11-05 21:51:02 +00:00
parent 2e8a725933
commit eb5d2acab5
30 changed files with 944 additions and 569 deletions

View File

@@ -1,23 +1,31 @@
from typing import Any, Dict, Optional, Type, cast
from django.shortcuts import redirect
from django.urls import reverse
from django.views.generic import DetailView
from django.views import View
from django.http import HttpRequest, HttpResponse
from django.db.models import Model
class SlugRedirectMixin:
class SlugRedirectMixin(View):
"""
Mixin that handles redirects for old slugs.
Requires the model to inherit from SluggedModel and view to inherit from DetailView.
"""
def dispatch(self, request, *args, **kwargs):
model: Optional[Type[Model]] = None
slug_url_kwarg: str = 'slug'
object: Optional[Model] = None
def dispatch(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
# 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()
self.object = self.get_object() # type: ignore
# Check if we used an old slug
current_slug = kwargs.get(self.slug_url_kwarg)
if current_slug and current_slug != self.object.slug:
if current_slug and current_slug != getattr(self.object, 'slug', None):
# Get the URL pattern name from the view
url_pattern = self.get_redirect_url_pattern()
# Build kwargs for reverse()
@@ -28,10 +36,13 @@ class SlugRedirectMixin:
permanent=True
)
return super().dispatch(request, *args, **kwargs)
except (self.model.DoesNotExist, AttributeError):
except (AttributeError, Exception) as e: # type: ignore
if self.model and hasattr(self.model, 'DoesNotExist'):
if isinstance(e, self.model.DoesNotExist): # type: ignore
return super().dispatch(request, *args, **kwargs)
return super().dispatch(request, *args, **kwargs)
def get_redirect_url_pattern(self):
def get_redirect_url_pattern(self) -> str:
"""
Get the URL pattern name for redirects.
Should be overridden by subclasses.
@@ -40,9 +51,11 @@ class SlugRedirectMixin:
"Subclasses must implement get_redirect_url_pattern()"
)
def get_redirect_url_kwargs(self):
def get_redirect_url_kwargs(self) -> Dict[str, Any]:
"""
Get the kwargs for reverse() when redirecting.
Should be overridden by subclasses if they need custom kwargs.
"""
return {self.slug_url_kwarg: self.object.slug}
if not self.object:
return {}
return {self.slug_url_kwarg: getattr(self.object, 'slug', '')}