Skip to content

Commit

Permalink
Merge pull request lingthio#147 from LotosikRa/pr/render_template
Browse files Browse the repository at this point in the history
Add `render_function` to `UserManager` class
  • Loading branch information
lingthio authored Nov 10, 2016
2 parents 1e31e1b + 10fff05 commit 0920f62
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
4 changes: 3 additions & 1 deletion flask_user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
:license: Simplified BSD License, see LICENSE.txt for more details."""

from passlib.context import CryptContext
from flask import Blueprint, current_app, url_for
from flask import Blueprint, current_app, url_for, render_template
from flask_login import LoginManager, UserMixin as LoginUserMixin, make_secure_token
from flask_user.db_adapters import DBAdapter
from .db_adapters import SQLAlchemyAdapter
Expand Down Expand Up @@ -68,6 +68,7 @@ def init_app(self, app, db_adapter=None,
username_validator=forms.username_validator,
password_validator=forms.password_validator,
# View functions
render_function=render_template,
change_password_view_function=views.change_password,
change_username_view_function=views.change_username,
confirm_email_view_function=views.confirm_email,
Expand Down Expand Up @@ -109,6 +110,7 @@ def init_app(self, app, db_adapter=None,
self.username_validator = username_validator
self.password_validator = password_validator
# View functions
self.render_function = render_function
self.change_password_view_function = change_password_view_function
self.change_username_view_function = change_username_view_function
self.confirm_email_view_function = confirm_email_view_function
Expand Down
27 changes: 16 additions & 11 deletions flask_user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
:license: Simplified BSD License, see LICENSE.txt for more details."""

from datetime import datetime
from flask import current_app, flash, redirect, render_template, request, url_for
from flask import current_app, flash, redirect, request, url_for
from flask_login import current_user, login_user, logout_user
try: # Handle Python 2.x and Python 3.x
from urllib.parse import quote # Python 3.x
Expand All @@ -21,6 +21,11 @@ def _call_or_get(function_or_property):
return function_or_property() if callable(function_or_property) else function_or_property


def render(*args, **kwargs):
user_manager = current_app.user_manager
return user_manager.render_function(*args, **kwargs)


def confirm_email(token):
""" Verify email confirmation token and activate the user account."""
# Verify token
Expand Down Expand Up @@ -105,7 +110,7 @@ def change_password():
return redirect(form.next.data)

# Process GET or invalid POST
return render_template(user_manager.change_password_template, form=form)
return render(user_manager.change_password_template, form=form)

@login_required
@confirm_email_required
Expand Down Expand Up @@ -141,7 +146,7 @@ def change_username():
return redirect(form.next.data)

# Process GET or invalid POST
return render_template(user_manager.change_username_template, form=form)
return render(user_manager.change_username_template, form=form)

@login_required
@confirm_email_required
Expand Down Expand Up @@ -208,7 +213,7 @@ def forgot_password():
return redirect(_endpoint_url(user_manager.after_forgot_password_endpoint))

# Process GET or invalid POST
return render_template(user_manager.forgot_password_template, form=form)
return render(user_manager.forgot_password_template, form=form)


def login():
Expand Down Expand Up @@ -257,7 +262,7 @@ def login():
return _do_login_user(user, login_form.next.data, login_form.remember_me.data)

# Process GET or invalid POST
return render_template(user_manager.login_template,
return render(user_manager.login_template,
form=login_form,
login_form=login_form,
register_form=register_form)
Expand Down Expand Up @@ -298,7 +303,7 @@ def manage_emails():
return redirect(url_for('user.manage_emails'))

# Process GET or invalid POST request
return render_template(user_manager.manage_emails_template,
return render(user_manager.manage_emails_template,
user_emails=user_emails,
form=form,
)
Expand Down Expand Up @@ -453,7 +458,7 @@ def register():
return redirect(url_for('user.login')+'?next='+reg_next) # redirect to login page

# Process GET or invalid POST
return render_template(user_manager.register_template,
return render(user_manager.register_template,
form=register_form,
login_form=login_form,
register_form=register_form)
Expand Down Expand Up @@ -517,7 +522,7 @@ def invite():
flash(_('Invitation has been sent.'), 'success')
return redirect(next)

return render_template(user_manager.invite_template, form=invite_form)
return render(user_manager.invite_template, form=invite_form)

def resend_confirm_email():
"""Prompt for email and re-send email conformation email."""
Expand All @@ -540,7 +545,7 @@ def resend_confirm_email():
return redirect(_endpoint_url(user_manager.after_resend_confirm_email_endpoint))

# Process GET or invalid POST
return render_template(user_manager.resend_confirm_email_template, form=form)
return render(user_manager.resend_confirm_email_template, form=form)


def reset_password(token):
Expand Down Expand Up @@ -609,7 +614,7 @@ def reset_password(token):
return redirect(url_for('user.login')+'?next='+next) # redirect to login page

# Process GET or invalid POST
return render_template(user_manager.reset_password_template, form=form)
return render(user_manager.reset_password_template, form=form)


def unconfirmed():
Expand Down Expand Up @@ -652,7 +657,7 @@ def unauthorized():
@confirm_email_required
def user_profile():
user_manager = current_app.user_manager
return render_template(user_manager.user_profile_template)
return render(user_manager.user_profile_template)


def _send_registered_email(user, user_email, require_email_confirmation=True):
Expand Down

0 comments on commit 0920f62

Please sign in to comment.