Skip to content

Commit

Permalink
change to redirect view
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahframe committed Aug 20, 2024
1 parent d78788f commit 376950a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
15 changes: 8 additions & 7 deletions cdhweb/blog/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from typing import Any
from django.contrib.syndication.views import Feed
from django.utils.feedgenerator import Atom1Feed
from django.views.generic.detail import DetailView
from django.views.generic.base import RedirectView
from django.shortcuts import get_object_or_404

from cdhweb.blog.models import BlogPost

Expand Down Expand Up @@ -62,11 +65,9 @@ class AtomBlogPostFeed(RssBlogPostFeed):
feed_type = Atom1Feed
subtitle = RssBlogPostFeed.description

class BlogPostRedirectView(DetailView):

def get_object(self):
slug = self.kwargs["slug"]
return BlogPost.objects.live().filter(slug=slug).first()
class BlogPostRedirectView(RedirectView):
pattern_name = "blog-detail"

def get(self, request, *args, **kwargs):
return self.get_object().serve(request)
def get_redirect_url(self, *args, **kwargs):
post = get_object_or_404(BlogPost, slug=kwargs["slug"])
return post.url
4 changes: 2 additions & 2 deletions cdhweb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
# wagtail paths
path("cms/", include(wagtailadmin_urls)),
path("documents/", include(wagtaildocs_urls)),
re_path(r"updates/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[\w-]+)", BlogPostRedirectView.as_view(), name='updates-redirect-with-date'),
path("updates/<slug>/", BlogPostRedirectView.as_view(), name='updates-redirect'),
re_path(r"updates/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[\w-]+)", BlogPostRedirectView.as_view(), name='blog-detail'),
path("updates/<slug>/", BlogPostRedirectView.as_view(), name='blog-detail'),
]

if settings.DEBUG:
Expand Down

0 comments on commit 376950a

Please sign in to comment.