1
0
Fork 0

fix: add notfound() View method; auto-append trailing slash

the latter provides auto-redirect to `/widgets/` when user visits
`/widgets` for example
This commit is contained in:
Lance Edgar 2024-08-05 21:58:31 -05:00
parent 9a739381ae
commit 7766ca6b12
3 changed files with 17 additions and 2 deletions

View file

@ -73,6 +73,14 @@ class View:
"""
return forms.Form(self.request, **kwargs)
def notfound(self):
"""
Convenience method, to raise a HTTP 404 Not Found exception::
raise self.notfound()
"""
return httpexceptions.HTTPNotFound()
def redirect(self, url, **kwargs):
"""
Convenience method to return a HTTP 302 response.

View file

@ -53,7 +53,10 @@ class CommonView(View):
@classmethod
def _defaults(cls, config):
# home
# auto-correct URLs which require trailing slash
config.add_notfound_view(cls, attr='notfound', append_slash=True)
# home page
config.add_route('home', '/')
config.add_view(cls, attr='home',
route_name='home',

View file

@ -3,7 +3,7 @@
from unittest import TestCase
from pyramid import testing
from pyramid.httpexceptions import HTTPFound, HTTPForbidden
from pyramid.httpexceptions import HTTPFound, HTTPForbidden, HTTPNotFound
from wuttjamaican.conf import WuttaConfig
from wuttaweb.views import base
@ -31,6 +31,10 @@ class TestView(TestCase):
form = self.view.make_form()
self.assertIsInstance(form, Form)
def test_notfound(self):
error = self.view.notfound()
self.assertIsInstance(error, HTTPNotFound)
def test_redirect(self):
error = self.view.redirect('/')
self.assertIsInstance(error, HTTPFound)