mirror of
https://github.com/wassname/flask-security.git
synced 2026-07-28 11:20:10 +08:00
Change urls/views to be (subjectively) simpler
This commit is contained in:
@@ -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():
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
{% include "_messages.html" %}
|
||||
{% include "_nav.html" %}
|
||||
<form action="{{ url_for_security('send_login') }}" method="POST" name="login_form">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ form.email.label }} {{ form.email }}<br/>
|
||||
{{ form.next }}
|
||||
{{ form.submit }}
|
||||
</form>
|
||||
<p>{{ content }}</p>
|
||||
@@ -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)
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
{% from "security/_macros.html" import render_field_with_errors, render_field %}
|
||||
{% include "security/_messages.html" %}
|
||||
<h1>Edit Account</h1>
|
||||
<form action="{{ url_for_security('edit_user') }}" method="POST" name="edit_user_form">
|
||||
{{ 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) }}
|
||||
</form>
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
+15
-13
@@ -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'
|
||||
|
||||
+2
-1
@@ -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 = """
|
||||
|
||||
@@ -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("<h1>Login</h1>", r.data)
|
||||
|
||||
def test_authenticate(self):
|
||||
r = self.authenticate(endpoint="/custom_auth")
|
||||
|
||||
Reference in New Issue
Block a user