Skip to content

Commit

Permalink
resend_confirm_email
Browse files Browse the repository at this point in the history
  • Loading branch information
lingthio committed May 30, 2014
1 parent e6c4173 commit ae92d8d
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 55 deletions.
2 changes: 2 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ UserManager()
forgot_password_form = forms.ForgotPasswordForm,
login_form = forms.LoginForm,
register_form = forms.RegisterForm,
resend_confirm_email_form = forms.ResendConfirmEmailForm,
reset_password_form = forms.ResetPasswordForm,

# Validators
Expand All @@ -93,6 +94,7 @@ UserManager()
login_view_function = views.login,
logout_view_function = views.logout,
register_view_function = views.register,
resend_confirm_email_view_function = views.resend_confirm_email_view_function,
reset_password_view_function = views.reset_password,
unauthenticated_view_function = views.unauthenticated,
unauthorized_view_function = views.unauthorized,
Expand Down
19 changes: 11 additions & 8 deletions docs/source/customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ with a username validator (at least 3 characters in "abcdefghijklmnopqrstuvwxyzA

Custom validators can be specified by setting an attribute on the Flask-User's UserManager object::

from wtforms.validators import ValidationError

def my_password_validator(form, field):
password = field.data
if len(password) < 8:
Expand Down Expand Up @@ -454,14 +456,15 @@ Custom view functions are specified by setting an attribute on the Flask-User's

# View functions
user_manager = UserManager(db_adapter,
change_password_view_function = my_view_function1,
change_username_view_function = my_view_function2,
confirm_email_view_function = my_view_function3,
forgot_password_view_function = my_view_function4,
login_view_function = my_view_function5,
logout_view_function = my_view_function6,
register_view_function = my_view_function7,
reset_password_view_function = my_view_function8)
change_password_view_function = my_view_function1,
change_username_view_function = my_view_function2,
confirm_email_view_function = my_view_function3,
forgot_password_view_function = my_view_function4,
login_view_function = my_view_function5,
logout_view_function = my_view_function6,
register_view_function = my_view_function7,
resend_confirm_email_view_function = my_view_function8,
reset_password_view_function = my_view_function9)
user_manager.init_app(app)

URLs
Expand Down
4 changes: 3 additions & 1 deletion flask_user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(self, db_adapter, app=None,
forgot_password_form=forms.ForgotPasswordForm,
login_form=forms.LoginForm,
register_form=forms.RegisterForm,
resend_confirm_email_form=forms.ResendConfirmEmailForm,
reset_password_form=forms.ResetPasswordForm,
# Validators
username_validator=forms.username_validator,
Expand Down Expand Up @@ -74,6 +75,7 @@ def __init__(self, db_adapter, app=None,
self.forgot_password_form = forgot_password_form
self.login_form = login_form
self.register_form = register_form
self.resend_confirm_email_form = resend_confirm_email_form
self.reset_password_form = reset_password_form
# Validators
self.username_validator = username_validator
Expand Down Expand Up @@ -154,7 +156,7 @@ def add_url_routes(self, app):
""" Add URL Routes"""
if self.enable_confirm_email:
app.add_url_rule(self.confirm_email_url, 'user.confirm_email', self.confirm_email_view_function)
app.add_url_rule(self.resend_confirm_email_url, 'user.resend_confirm_email', self.resend_confirm_email_view_function)
app.add_url_rule(self.resend_confirm_email_url, 'user.resend_confirm_email', self.resend_confirm_email_view_function, methods=['GET', 'POST'])
if self.enable_change_password:
app.add_url_rule(self.change_password_url, 'user.change_password', self.change_password_view_function, methods=['GET', 'POST'])
if self.enable_change_username:
Expand Down
7 changes: 7 additions & 0 deletions flask_user/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,13 @@ def validate(self):
# All is well
return True

class ResendConfirmEmailForm(Form):
email = StringField(_('Email'), validators=[
validators.Required(_('Email is required')),
validators.Email(_('Invalid Email')),
])
submit = SubmitField(_('Resend email confirmation email'))

class ResetPasswordForm(Form):
new_password = PasswordField(_('New Password'), validators=[
validators.Required(_('New Password is required'))])
Expand Down
2 changes: 1 addition & 1 deletion flask_user/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def set_default_settings(user_manager, app_config):
um.login_url = sd('USER_LOGIN_URL', '/user/sign-in')
um.logout_url = sd('USER_LOGOUT_URL', '/user/sign-out')
um.register_url = sd('USER_REGISTER_URL', '/user/register')
um.resend_confirm_email_url = sd('USER_RESEND_CONFIRM_EMAIL_URL', '/user/resend-confirmation-email')
um.resend_confirm_email_url = sd('USER_RESEND_CONFIRM_EMAIL_URL', '/user/resend-confirm-email')
um.reset_password_url = sd('USER_RESET_PASSWORD_URL', '/user/reset-password/<token>')
um.unauthenticated_url = sd('USER_UNAUTHENTICATED_URL', um.login_url)
um.unauthorized_url = sd('USER_UNAUTHORIZED_URL', '/')
Expand Down
20 changes: 6 additions & 14 deletions flask_user/templates/flask_user/resend_confirm_email.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
{% extends 'flask_user/base.html' %}
{% extends 'flask_user/public_base.html' %}

{% block content %}
{% from "flask_user/_macros.html" import render_field, render_submit_field %}
<h1>{%trans%}Register{%endtrans%}</h1>
<h1>{%trans%}Resend Confirmation Email{%endtrans%}</h1>

<form action="" method="POST" novalidate formnovalidate class="form" role="form">
<form action="" method="POST" class="form" role="form">
{{ form.hidden_tag() }}
{% if user_manager.enable_username %}
{{ render_field(form.username) }}
{% endif %}
{% if user_manager.enable_email %}
{{ render_field(form.email) }}
{% endif %}
{{ render_field(form.password) }}
{% if user_manager.enable_retype_password %}
{{ render_field(form.retype_password) }}
{% endif %}
{{ render_submit_field(form.submit) }}
{{ render_field(form.email, tabindex=10) }}
{{ render_submit_field(form.submit, tabindex=90) }}
</form>

{% endblock %}
89 changes: 58 additions & 31 deletions flask_user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,40 +279,11 @@ def register():

db_adapter.commit()

# Send 'confirm_email' or 'registered' email
if user_manager.enable_email:
email_address = register_form.email.data

try:
if user_manager.enable_confirm_email:
# Send 'confirm_email' email

# Generate confirm email link
token = user_manager.generate_token(user.id)
confirm_email_link = url_for('user.confirm_email', token=token, _external=True)

# Send email
emails.send_confirm_email_email(email_address, user, confirm_email_link)
else:
if user_manager.send_registered_email:
# Send 'registered' email
emails.send_registered_email(email_address, user)

except Exception as e:
# delete newly registered user if send email fails
db_adapter.delete_object(user)
db_adapter.commit()
raise e
send_confirm_email_or_registered_email(user)

# Send user_registered signal
signals.user_registered.send(current_app._get_current_object(), user=user)

# Prepare one-time system message
if user_manager.enable_confirm_email:
flash(_('A confirmation email has been sent to %(email)s with instructions to complete your registration.', email=email_address), 'success')
else:
flash(_('You have registered successfully. Please sign in.'), 'success')

# Redirect to the login page
return redirect(url_for('user.login'))

Expand All @@ -322,10 +293,66 @@ def register():
login_form=login_form,
register_form=register_form)

def send_confirm_email_or_registered_email(user):
user_manager = current_app.user_manager
db_adapter = user_manager.db_adapter

# Send 'confirm_email' or 'registered' email
if user_manager.enable_email:
email_address = user.email

try:
if user_manager.enable_confirm_email:
# Send 'confirm_email' email

# Generate confirm email link
token = user_manager.generate_token(user.id)
confirm_email_link = url_for('user.confirm_email', token=token, _external=True)

# Send email
emails.send_confirm_email_email(email_address, user, confirm_email_link)
else:
if user_manager.send_registered_email:
# Send 'registered' email
emails.send_registered_email(email_address, user)

except Exception as e:
# delete newly registered user if send email fails
db_adapter.delete_object(user)
db_adapter.commit()
raise e

# Prepare one-time system message
if user_manager.enable_confirm_email:
flash(_('A confirmation email has been sent to %(email)s with instructions to complete your registration.', email=email_address), 'success')
else:
flash(_('You have registered successfully. Please sign in.'), 'success')


# TODO:
def resend_confirm_email():
pass
"""Prompt for email and re-send email conformation email."""
user_manager = current_app.user_manager
db_adapter = user_manager.db_adapter

# Initialize form
form = user_manager.resend_confirm_email_form(request.form)

# Process valid POST
if request.method=='POST' and form.validate():
email = form.email.data

# Find user by email
user = user_manager.find_user_by_email(email)
if user:
send_confirm_email_or_registered_email(user)

# Redirect to the login page
return redirect(url_for('user.login'))

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


def reset_password(token):
""" Verify the password reset token, Prompt for new password, and set the user's password."""
Expand Down

0 comments on commit ae92d8d

Please sign in to comment.