From 1d8b2f83423a9d5aeec704be960723442f30e4f0 Mon Sep 17 00:00:00 2001 From: Matt Wright Date: Thu, 16 Aug 2012 17:25:24 -0400 Subject: [PATCH] Change urls/views to be (subjectively) simpler --- example/app.py | 4 --- example/templates/passwordless_login.html | 9 ------ flask_security/core.py | 4 +-- .../templates/security/edit_user.html | 11 -------- .../{register_user.html => register.html} | 0 flask_security/utils.py | 2 -- flask_security/views.py | 28 ++++++++++--------- tests/__init__.py | 3 +- tests/functional_tests.py | 4 +-- 9 files changed, 21 insertions(+), 44 deletions(-) delete mode 100644 example/templates/passwordless_login.html delete mode 100644 flask_security/templates/security/edit_user.html rename flask_security/templates/security/{register_user.html => register.html} (100%) diff --git a/example/app.py b/example/app.py index f67baf1..9d5d285 100644 --- a/example/app.py +++ b/example/app.py @@ -56,10 +56,6 @@ def create_app(auth_config): def index(): return render_template('index.html', content='Home Page') - @app.route('/custom_login') - def custom_login(): - return render_template('login.html', content='Custom Login Page', form=LoginForm()) - @app.route('/profile') @login_required def profile(): diff --git a/example/templates/passwordless_login.html b/example/templates/passwordless_login.html deleted file mode 100644 index 1827982..0000000 --- a/example/templates/passwordless_login.html +++ /dev/null @@ -1,9 +0,0 @@ -{% include "_messages.html" %} -{% include "_nav.html" %} -
- {{ form.hidden_tag() }} - {{ form.email.label }} {{ form.email }}
- {{ form.next }} - {{ form.submit }} -
-

{{ content }}

diff --git a/flask_security/core.py b/flask_security/core.py index 07986ef..903ebf0 100644 --- a/flask_security/core.py +++ b/flask_security/core.py @@ -43,11 +43,11 @@ _default_config = { 'RESET_URL': '/reset', 'CONFIRM_URL': '/confirm', 'LOGIN_VIEW': '/login', - 'CONFIRM_ERROR_VIEW': '/confirm', 'POST_LOGIN_VIEW': '/', 'POST_LOGOUT_VIEW': '/', 'POST_FORGOT_VIEW': '/', 'RESET_PASSWORD_ERROR_VIEW': '/', + 'CONFIRM_ERROR_VIEW': None, 'POST_REGISTER_VIEW': None, 'POST_CONFIRM_VIEW': None, 'POST_RESET_VIEW': None, @@ -129,7 +129,7 @@ def _on_identity_loaded(sender, identity): def _get_login_manager(app): lm = LoginManager() lm.anonymous_user = AnonymousUser - lm.login_view = cv('LOGIN_VIEW', app=app) + lm.login_view = '%s.login' % cv('BLUEPRINT_NAME', app=app) lm.user_loader(_user_loader) lm.token_loader(_token_loader) lm.init_app(app) diff --git a/flask_security/templates/security/edit_user.html b/flask_security/templates/security/edit_user.html deleted file mode 100644 index 7169a2c..0000000 --- a/flask_security/templates/security/edit_user.html +++ /dev/null @@ -1,11 +0,0 @@ -{% from "security/_macros.html" import render_field_with_errors, render_field %} -{% include "security/_messages.html" %} -

Edit Account

-
- {{ edit_user_form.hidden_tag() }} - {{ render_field_with_errors(edit_user_form.email) }} - {{ render_field_with_errors(edit_user_form.password) }} - {{ render_field_with_errors(edit_user_form.password_confirm) }} - {{ render_field_with_errors(edit_user_form.current_password) }} - {{ render_field(edit_user_form.submit) }} -
\ No newline at end of file diff --git a/flask_security/templates/security/register_user.html b/flask_security/templates/security/register.html similarity index 100% rename from flask_security/templates/security/register_user.html rename to flask_security/templates/security/register.html diff --git a/flask_security/utils.py b/flask_security/utils.py index 42a28c7..07636eb 100644 --- a/flask_security/utils.py +++ b/flask_security/utils.py @@ -145,8 +145,6 @@ def find_redirect(key): result = (get_url(session.pop(key.lower(), None)) or get_url(current_app.config[key.upper()] or None) or '/') - session.pop(key.lower(), None) - return result diff --git a/flask_security/views.py b/flask_security/views.py index 9fe14cd..63c9453 100644 --- a/flask_security/views.py +++ b/flask_security/views.py @@ -26,7 +26,7 @@ from flask_security.recoverable import reset_by_token, \ reset_password_reset_token from flask_security.signals import user_registered from flask_security.utils import get_url, get_post_login_redirect, do_flash, \ - get_message, config_value, login_user, logout_user + get_message, config_value, login_user, logout_user, url_for_security # Convenient references @@ -90,7 +90,7 @@ def authenticate(): do_flash(msg, 'error') - return redirect(request.referrer or _security.login_manager.login_view) + return redirect(request.referrer or url_for_security('login')) def login(): @@ -107,7 +107,7 @@ def logout(): _logger.debug('User logged out') return redirect(request.args.get('next', None) or - _security.post_logout_view) + get_url(_security.post_logout_view)) def register_user(): @@ -131,8 +131,8 @@ def register_user(): if not _security.confirmable or _security.login_without_confirmation: login_user(u) - return redirect(_security.post_register_view or - _security.post_login_view) + return redirect(get_url(_security.post_register_view) or + get_url(_security.post_login_view)) return render_template('security/register_user.html', register_user_form=form) @@ -154,7 +154,7 @@ def send_login(): def token_login(token): if current_user.is_authenticated(): - return redirect(_security.post_login_view) + return redirect(get_url(_security.post_login_view)) try: user, next = login_by_token(token) @@ -167,11 +167,11 @@ def token_login(token): do_flash(msg, cat) - return redirect(request.referrer or _security.login_manager.login_view) + return redirect(request.referrer or url_for_security('login')) do_flash(*get_message('PASSWORDLESS_LOGIN_SUCCESSFUL')) - return redirect(next or _security.post_login_view) + return redirect(next or get_url(_security.post_login_view)) def send_confirmation(): @@ -208,11 +208,13 @@ def confirm_email(token): do_flash(msg, cat) - return redirect(get_url(_security.confirm_error_view)) + return redirect(get_url(_security.confirm_error_view) or + url_for_security('send_confirmation')) do_flash(*get_message('EMAIL_CONFIRMED')) - return redirect(_security.post_confirm_view or _security.post_login_view) + return redirect(get_url(_security.post_confirm_view) or + get_url(_security.post_login_view)) def forgot_password(): @@ -231,7 +233,7 @@ def forgot_password(): do_flash(msg, cat) - return redirect(_security.post_forgot_view) + return redirect(get_url(_security.post_forgot_view)) else: for key, value in form.errors.items(): @@ -256,8 +258,8 @@ def reset_password(token): login_user(user) - return redirect(_security.post_reset_view or - _security.post_login_view) + return redirect(get_url(_security.post_reset_view) or + get_url(_security.post_login_view)) except ResetPasswordError, e: msg, cat = str(e), 'error' diff --git a/tests/__init__.py b/tests/__init__.py index 3c005c4..57a6cb7 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -35,7 +35,8 @@ class SecurityTest(TestCase): def authenticate(self, email="matt@lp.com", password="password", endpoint=None, **kwargs): data = dict(email=email, password=password, remember='y') - return self._post(endpoint or '/auth', data=data, **kwargs) + r = self._post(endpoint or '/auth', data=data, **kwargs) + return r def json_authenticate(self, email="matt@lp.com", password="password", endpoint=None): data = """ diff --git a/tests/functional_tests.py b/tests/functional_tests.py index 80df7fd..f14d730 100644 --- a/tests/functional_tests.py +++ b/tests/functional_tests.py @@ -207,7 +207,7 @@ class ConfiguredSecurityTests(SecurityTest): 'SECURITY_REGISTERABLE': True, 'SECURITY_AUTH_URL': '/custom_auth', 'SECURITY_LOGOUT_URL': '/custom_logout', - 'SECURITY_LOGIN_VIEW': '/custom_login', + 'SECURITY_LOGIN_URL': '/custom_login', 'SECURITY_POST_LOGIN_VIEW': '/post_login', 'SECURITY_POST_LOGOUT_VIEW': '/post_logout', 'SECURITY_POST_REGISTER_VIEW': '/post_register', @@ -217,7 +217,7 @@ class ConfiguredSecurityTests(SecurityTest): def test_login_view(self): r = self._get('/custom_login') - self.assertIn("Custom Login Page", r.data) + self.assertIn("

Login

", r.data) def test_authenticate(self): r = self.authenticate(endpoint="/custom_auth")