Skip to content

Commit

Permalink
merge backout of d6ee9a2e916b
Browse files Browse the repository at this point in the history
--HG--
branch : 0.10
  • Loading branch information
pjenvey committed Jan 19, 2010
2 parents 0caee64 + 6191a29 commit 17b9f0c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Pylons Changelog
session/cache and routes middleware into the project template. This will
require projects to be updated to include those 3 middleware in the projects
middleware.py.
* Added redirect, preferred over redirect_to. Takes an explicit url instead of
url_for like arguments
* Changed to using WebTest instead of paste.fixture for app testing.
* Added render_mako_def to render def blocks within a mako template.
* Changes to cache_decorator and cached_template to support Beaker API
Expand Down
2 changes: 1 addition & 1 deletion pylons/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def command(self):

exec ('from pylons import app_globals, c, config, g, request, '
'response, session, tmpl_context, url') in locs
exec ('from pylons.controllers.util import abort, redirect_to') in locs
exec ('from pylons.controllers.util import abort, redirect') in locs
exec 'from pylons.i18n import _, ungettext, N_' in locs
exec 'from pylons.templating import render' in locs

Expand Down
9 changes: 9 additions & 0 deletions pylons/controllers/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,16 @@ def redirect_to(*args, **kargs):
the redirect, i.e.::
redirect_to(controller='home', action='index', _code=303)
.. warning::
This function is pending deprecation. Pass the result of
:func:`url` to :func:`redirect` instead.
"""
import warnings
warnings.warn('redirect_to is pending deprecation, use '
'redirect(url(*args, **kwargs)) instead.',
PendingDeprecationWarning, 2)
code = kargs.pop('_code', 302)
return redirect(url_for(*args, **kargs), code)
50 changes: 35 additions & 15 deletions pylons/decorators/secure.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Security related decorators"""
import logging
import warnings

from decorator import decorator
from routes import url_for
from webhelpers.html import secure_form

from pylons.controllers.util import abort, redirect_to
from pylons.controllers.util import abort, redirect
from pylons.decorators.util import get_pylons

__all__ = ['authenticate_form', 'https']
Expand Down Expand Up @@ -44,29 +46,38 @@ def authenticate_form(func, *args, **kwargs):
authenticate_form = decorator(authenticate_form)

def https(*redirect_args, **redirect_kwargs):
"""Decorator to redirect to the SSL version of a page if not currently
using HTTPS. Takes as arguments the parameters to pass to redirect_to.
(Specify no arguments necessary to redirect the current page). Apply this
decorator to controller methods (actions).
"""Decorator to redirect to the SSL version of a page if not
currently using HTTPS. Takes a url argument to redirect to. Apply
this decorator to controller methods (actions).
Non-https POST requests are aborted (405 response code) by this decorator.
Non-https POST requests are aborted (405 response code) by this
decorator.
Example:
.. code-block:: python
@https('/pylons') # redirect to HTTPS /pylons
# redirect to HTTPS /pylons
@https('/pylons')
def index(self):
#...
do_secure()
# redirect to HTTPS /auth/login
@https(controller='auth', action='login')
@https(url(controller='auth', action='login'))
def login(self):
#...
do_secure()
@https() # redirect to HTTPS version of myself
# redirect to HTTPS version of myself
@https(url.current())
def get(self):
#...
do_secure()
.. warning::
Arguments as would be passed to the
:func:`url_for`/:func:`redirect_to` functions are also accepted,
but that functionality is pending deprecation. Explicitly
specify the url instead.
"""
def wrapper(func, *args, **kwargs):
Expand All @@ -76,11 +87,20 @@ def wrapper(func, *args, **kwargs):
return func(*args, **kwargs)
else:
if request.method.upper() != 'POST':
redirect_kwargs['protocol'] = 'https' # ensure https
# ensure https
redirect_kwargs['protocol'] = 'https'
log.debug('Redirecting non-https request: %s to redirect '
'args: *%r, **%r', request.path_info, redirect_args,
redirect_kwargs)
redirect_to(*redirect_args, **redirect_kwargs)
if len(redirect_kwargs):
# XXX: Not the best detection; this function will
# just have to break one day (probably for 1.0)
msg = ('Calling https with url_for args is pending '
'deprecation, use https(url(*args, **kwargs)) '
'instead')
warnings.warn(msg, PendingDeprecationWarning, 2)
redirect(url_for(*redirect_args, **redirect_kwargs))
else:
abort(405, headers=[('Allow', 'GET')]) # don't allow POSTs.
# don't allow POSTs
abort(405, headers=[('Allow', 'GET')])
return decorator(wrapper)
4 changes: 2 additions & 2 deletions pylons/templates/controller.py_tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect_to
from pylons import request, response, session, tmpl_context as c, url
from pylons.controllers.util import abort, redirect

{{importstatement}}

Expand Down
4 changes: 2 additions & 2 deletions pylons/templates/restcontroller.py_tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect_to
from pylons import request, response, session, tmpl_context as c, url
from pylons.controllers.util import abort, redirect

{{importstatement}}

Expand Down

0 comments on commit 17b9f0c

Please sign in to comment.