From 30b400005aa7db1329237e10e29dda9b46bf4df7 Mon Sep 17 00:00:00 2001 From: kelepirci Date: Sun, 31 Jul 2016 18:44:54 +0300 Subject: [PATCH] Profile page and password update New profile page added. Also added new password update functionality I need to figure out how to update all elements of user profile. --- dash/__init__.py | 4 + dash/profile/__init__.py | 5 + dash/profile/forms.py | 40 ++ dash/profile/views.py | 46 ++ dash/templates/index.html | 2 +- .../profile/email/password_changed.html | 5 + .../profile/email/password_changed.txt | 8 + dash/templates/profile/index.html | 404 ++++++++++++++++++ 8 files changed, 513 insertions(+), 1 deletion(-) create mode 100644 dash/profile/__init__.py create mode 100644 dash/profile/forms.py create mode 100644 dash/profile/views.py create mode 100644 dash/templates/profile/email/password_changed.html create mode 100644 dash/templates/profile/email/password_changed.txt create mode 100644 dash/templates/profile/index.html diff --git a/dash/__init__.py b/dash/__init__.py index 64838d0..c07e539 100644 --- a/dash/__init__.py +++ b/dash/__init__.py @@ -50,4 +50,8 @@ def create_app(config_name): from .auth import auth as auth_blueprint dash.register_blueprint(auth_blueprint, url_prefix='/auth') + # user profile application + from .profile import profile as profile_blueprint + dash.register_blueprint(profile_blueprint, url_prefix='/profile') + return dash \ No newline at end of file diff --git a/dash/profile/__init__.py b/dash/profile/__init__.py new file mode 100644 index 0000000..8797116 --- /dev/null +++ b/dash/profile/__init__.py @@ -0,0 +1,5 @@ +from flask import Blueprint + +profile = Blueprint('profile', __name__) + +from . import views \ No newline at end of file diff --git a/dash/profile/forms.py b/dash/profile/forms.py new file mode 100644 index 0000000..46b5f17 --- /dev/null +++ b/dash/profile/forms.py @@ -0,0 +1,40 @@ +from flask_wtf import Form +from flask import flash +from wtforms import StringField, PasswordField, BooleanField, SubmitField, ValidationError +from wtforms.validators import Required, Length, Email, Regexp, EqualTo +from ..models import User + +class ChangePasswordForm(Form): + old_password = PasswordField('Password', validators=[Required()]) + password = PasswordField('Password', validators=[ + Required(), EqualTo('password2', message='Passwords must match.')]) + password2 = PasswordField('Confirm password', validators=[Required()]) + type = StringField() + +class UpdateProfileForm(Form): + full_name = StringField('Full name', validators=[Required(), Length(1, 255)]) + type = StringField() + + def validate_full_name(self, field): + if User.query.filter_by(full_name=field.data).first(): + raise ValidationError('You have not changed your full name.') + +class ChangeUserNameForm(Form): + username = StringField('Username', validators=[ + Required(), Length(1, 64), Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0, + 'Usernames must have only letters, ' + 'numbers, dots or underscores')]) + type = StringField() + + def validate_username(self, field): + if User.query.filter_by(username=field.data).first(): + raise ValidationError('Username already in use.') + +class UpdateEmailForm(Form): + email = StringField('Email', validators=[Required(), Length(1, 128), + Email()]) + type = StringField() + + def validate_email(self, field): + if User.query.filter_by(email=field.data).first(): + raise ValidationError('Email already registered.') \ No newline at end of file diff --git a/dash/profile/views.py b/dash/profile/views.py new file mode 100644 index 0000000..1ee052a --- /dev/null +++ b/dash/profile/views.py @@ -0,0 +1,46 @@ +import datetime +from flask import render_template, redirect, request, url_for, flash +from flask_login import login_user, logout_user, login_required, \ + current_user +from . import profile +from .. import db +from ..models import User +from ..email import send_email +from .forms import ChangePasswordForm, UpdateProfileForm, ChangeUserNameForm + + +@profile.route('/', methods=['GET', 'POST']) +@login_required +def index(): + formChangePassword = ChangePasswordForm() + formChangeUserName = ChangeUserNameForm() + formUpdateProfile = UpdateProfileForm() + + if formChangePassword.type.data == 'formChangePassword': + if formChangePassword.validate_on_submit(): + if current_user.verify_password(formChangePassword.old_password.data): + current_user.password = formChangePassword.password.data + db.session.add(current_user) + flash('You password has been changed.') + send_email(current_user.email, 'You Password has Changed', + 'profile/email/password_changed', user=current_user) + return redirect(url_for('profile.index')) + else: + flash('Invalid password.') + return redirect(url_for('profile.index')) + if formChangeUserName.type.data == 'formChangeUserName': + if formChangeUserName.validate_on_submit(): + current_user.username = formChangeUserName.username.data + db.session.add(current_user) + flash('formChangeUserName') + return redirect(url_for('profile.index')) + if formUpdateProfile.type.data == 'formUpdateProfile': + if formUpdateProfile.validate_on_submit(): + current_user.full_name = formUpdateProfile.full_name.data + db.session.add(current_user) + flash('formUpdateProfile') + return redirect(url_for('profile.index')) + return render_template('profile/index.html', + formChangePassword=formChangePassword, + formChangeUserName=formChangeUserName, + formUpdateProfile=formUpdateProfile) diff --git a/dash/templates/index.html b/dash/templates/index.html index 16ff7a3..9d60073 100644 --- a/dash/templates/index.html +++ b/dash/templates/index.html @@ -230,7 +230,7 @@