Skip to content

Commit

Permalink
Fixes Issue #52: Use a catch-all in urls.py instead of PageFallbackMi…
Browse files Browse the repository at this point in the history
…ddleware
  • Loading branch information
mvdwaeter committed Jan 19, 2012
1 parent 2f81f4f commit 46799b0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -47,7 +47,6 @@ settings.py
MIDDLEWARE_CLASSES = DEFAULT_SETTINGS.MIDDLEWARE_CLASSES + (
'fiber.middleware.ObfuscateEmailAddressMiddleware',
'fiber.middleware.AdminPageMiddleware',
'fiber.middleware.PageFallbackMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
Expand Down Expand Up @@ -87,6 +86,7 @@ urls.py
(r'^admin/fiber/', include('fiber.admin_urls')),
(r'^jsi18n/$', 'django.views.i18n.javascript_catalog', {'packages': ('fiber',),}),
...
(r'', 'fiber.views.page'),
)

if settings.DEBUG:
Expand Down
28 changes: 12 additions & 16 deletions fiber/middleware.py
@@ -1,9 +1,9 @@
import re
import random

from django.conf import settings
from django.core.exceptions import MiddlewareNotUsed
from django.core.urlresolvers import reverse
from django.http import Http404, HttpResponseRedirect
from django.http import HttpResponseRedirect
from django.template import loader, RequestContext
from django.utils.encoding import smart_unicode
from django.utils import simplejson
Expand All @@ -12,24 +12,20 @@

from app_settings import EXCLUDE_URLS, EDITOR
from models import Page, ContentItem
from views import page


class PageFallbackMiddleware(object):
"""
This middleware has been removed; see the django-fiber 0.9.6 release notes
(README.rst) for details.
"""

def process_response(self, request, response):
if response.status_code != 404:
return response # No need to check for a page for non-404 responses.
try:
return page(request, request.path_info)
# Return the original response if any errors happened. Because this
# is a middleware, we can't assume the errors will be caught elsewhere.
except Http404:
return response
except:
if settings.DEBUG:
raise
return response
def __init__(self):
import warnings
warnings.warn("PageFallbackMiddleware has been removed. "
"See the django-fiber 0.9.6 release notes (README.rst) for details.",
category=DeprecationWarning)
raise MiddlewareNotUsed()


class AdminPageMiddleware(object):
Expand Down
11 changes: 3 additions & 8 deletions fiber/views.py
Expand Up @@ -2,21 +2,16 @@
from django.core.xheaders import populate_xheaders
from django.http import HttpResponse, HttpResponsePermanentRedirect, Http404
from django.template import loader, RequestContext
from django.views.decorators.csrf import csrf_protect

from app_settings import DEFAULT_TEMPLATE
from models import Page


# This view is called from PageFallbackMiddleware.process_response
# when a 404 is raised, which often means CsrfViewMiddleware.process_view
# has not been called even if CsrfViewMiddleware is installed. So we need
# to use @csrf_protect, in case the template needs {% csrf_token %}.
def page(request):
url = request.path_info

@csrf_protect
def page(request, url):
if not url.endswith('/') and settings.APPEND_SLASH:
return HttpResponsePermanentRedirect('%s/' % request.path)
return HttpResponsePermanentRedirect('%s/' % url)

context = RequestContext(request)
if 'fiber_page' not in context:
Expand Down

3 comments on commit 46799b0

@vad
Copy link

@vad vad commented on 46799b0 Jan 19, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uhm... i already have a catch-all in my urls in a project of mine! With the middleware i can let a view to catch the request, then raise a 404 if there are no matching items in the DB, and then middleware catches the 404 and it will serve the Fiber page. What's the plus of this removal?

@dbunskoek
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#52 shows why we want to do this... Do you really need your own catch-all? Maybe you can use something like get_object_or_404 for what you are trying to achieve?

@vad
Copy link

@vad vad commented on 46799b0 Jan 19, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i understand... i'll modify my catch-all to manage both fiber and my own view. Maybe it would be good to write docs about this choice. Thank you!

Please sign in to comment.