def profile(): """Allow the user to edit their personal details. Displays a form and processes it to update the users details. """ if flask.request.method == 'POST': valid = True flashes = [] if (flask.request.form['email'] != login.current_user.email and models.User.get_by_email( flask.request.form['email']) is not None): flashes.append('That email address is already in use. ') valid = False if ('oldpassword' in flask.request.form and flask.request.form['oldpassword'] != ''): if not login.current_user.check_password( flask.request.form['oldpassword']): flashes.append('Current password is not correct') valid = False if ('password' not in flask.request.form or 'confirm' not in flask.request.form or flask.request.form['password'] == '' or (flask.request.form['password'] != flask.request.form['confirm'])): flashes.append('New passwords do not match') valid = False if len(flask.request.form['password']) < 8: flashes.append('Password must be at least 8 characters long') valid = False if ('forenames' not in flask.request.form or flask.request.form['forenames'] == ''): flashes.append('First Name cannot be blank') valid = False if ('surname' not in flask.request.form or flask.request.form['surname'] == ''): flashes.append('Surname cannot be blank') valid = False if ('email' not in flask.request.form or flask.request.form['email'] == ''): flashes.append('Email cannot be blank') valid = False if ('phone' not in flask.request.form or flask.request.form['phone'] == ''): flashes.append('Phone cannot be blank') valid = False if ('college' not in flask.request.form or flask.request.form['college'] == '---'): flashes.append('Please select a college') valid = False if ('affiliation' not in flask.request.form or flask.request.form['affiliation'] == '---'): flashes.append('Please select an affiliation') valid = False if not valid: flask.flash( ('There were errors in your provided details. Please fix ' 'these and try again'), 'error') for msg in flashes: flask.flash(msg, 'warning') else: if flask.request.form['email'] != login.current_user.email: login.current_user.new_email = flask.request.form['email'] login.current_user.secret_key = util.generate_key(64) login.current_user.secret_key_expiry = ( datetime.datetime.utcnow() + datetime.timedelta(days=7)) APP.email_manager.send_template( flask.request.form['email'], 'Confirm your Email Address', 'email_change_confirm.email', name=login.current_user.forenames, confirmurl=flask.url_for( 'front.confirm_email', user_id=login.current_user.object_id, secret_key=login.current_user.secret_key, _external=True)) flask.flash( ('You must confirm your new email address to make ' 'sure that we can contact you if necessary. Please ' 'check your email for further instructions.'), 'info') if ('oldpassword' in flask.request.form and flask.request.form['oldpassword'] != ''): login.current_user.set_password(flask.request.form['password']) login.current_user.forenames = flask.request.form['forenames'] login.current_user.surname = flask.request.form['surname'] login.current_user.phone = flask.request.form['phone'] affiliation_logic.update_affiliation( login.current_user, models.College.get_by_id(flask.request.form['college']), models.Affiliation.get_by_id( flask.request.form['affiliation'])) DB.session.commit() APP.log_manager.log_event('Updated Details', [], login.current_user) flask.flash('Your details have been updated', 'success') affiliation_logic.maybe_verify_affiliation(login.current_user) return flask.render_template('dashboard/profile.html', colleges=models.College.query.all(), affiliations=models.Affiliation.query.all())
def update_profile(): """Allow the user to update their personal details.""" if flask.request.method != 'POST': return flask.redirect(flask.request.referrer or flask.url_for('dashboard.profile')) if not login.current_user.can_update_details(): flask.flash( flask.Markup( ('You cannot currently change your details. Please contact ' '<a href="{0}">the ticketing officer</a> for assistance.' ).format(APP.config['TICKETS_EMAIL_LINK'])), 'error') return flask.redirect(flask.request.referrer or flask.url_for('dashboard.profile')) flashes = [] if ('forenames' not in flask.request.form or flask.request.form['forenames'] == ''): flashes.append('Forename(s) cannot be blank') if ('surname' not in flask.request.form or flask.request.form['surname'] == ''): flashes.append('Surname cannot be blank') if ('phone' not in flask.request.form or flask.request.form['phone'] == ''): flashes.append('Phone cannot be blank') if ('college' not in flask.request.form or flask.request.form['college'] == '---'): flashes.append('Please select a college') if ('affiliation' not in flask.request.form or flask.request.form['affiliation'] == '---'): flashes.append('Please select an affiliation') if flashes: flask.flash(('There were errors in your provided details. Please fix ' 'these and try again'), 'error') for msg in flashes: flask.flash(msg, 'warning') return flask.redirect(flask.request.referrer or flask.url_for('dashboard.profile')) login.current_user.forenames = flask.request.form['forenames'] login.current_user.surname = flask.request.form['surname'] if login.current_user.phone != flask.request.form['phone']: login.current_user.phone_verified = False login.current_user.phone = flask.request.form['phone'] affiliation_logic.update_affiliation( login.current_user, models.College.query.get_or_404(flask.request.form['college']), models.Affiliation.query.get_or_404(flask.request.form['affiliation'])) DB.session.commit() APP.log_manager.log_event('Updated Details', user=login.current_user) flask.flash('Your details have been updated', 'success') affiliation_logic.maybe_verify_affiliation(login.current_user) return flask.redirect(flask.request.referrer or flask.url_for('dashboard.profile'))
def update_profile(): """Allow the user to update their personal details.""" if flask.request.method != "POST": return flask.redirect( flask.request.referrer or flask.url_for("dashboard.profile") ) if not login.current_user.can_update_details(): flask.flash( flask.Markup( ( "You cannot currently change your details. Please contact " '<a href="{0}">the ticketing officer</a> for assistance.' ).format(APP.config["TICKETS_EMAIL_LINK"]) ), "error", ) return flask.redirect( flask.request.referrer or flask.url_for("dashboard.profile") ) flashes = [] if "forenames" not in flask.request.form or flask.request.form["forenames"] == "": flashes.append("Forename(s) cannot be blank") if "surname" not in flask.request.form or flask.request.form["surname"] == "": flashes.append("Surname cannot be blank") if "phone" not in flask.request.form or flask.request.form["phone"] == "": flashes.append("Phone cannot be blank") if "college" not in flask.request.form or flask.request.form["college"] == "---": flashes.append("Please select a college") if ( "affiliation" not in flask.request.form or flask.request.form["affiliation"] == "---" ): flashes.append("Please select an affiliation") alumni_affiliation_ids = [ str(affiliation.object_id) for affiliation in models.Affiliation.query.all() if "Alumnus" in affiliation.name ] if flask.request.form["affiliation"] in alumni_affiliation_ids and ( "alumni-number" not in flask.request.form or not re.match(r"^8-\d{8}$", flask.request.form["alumni-number"]) ): flashes.append( "Please enter a valid alumni number, starting with '8-' and with 8 digits (e.g. 8-12345678)" ) if flashes: flask.flash( ( "There were errors in your provided details. Please fix " "these and try again" ), "error", ) for msg in flashes: flask.flash(msg, "warning") return flask.redirect( flask.request.referrer or flask.url_for("dashboard.profile") ) login.current_user.forenames = flask.request.form["forenames"] login.current_user.surname = flask.request.form["surname"] if login.current_user.phone != flask.request.form["phone"]: login.current_user.phone_verified = False login.current_user.phone = flask.request.form["phone"] login.current_user.alumni_number = flask.request.form["alumni-number"] affiliation_logic.update_affiliation( login.current_user, models.College.get_by_id(flask.request.form["college"]), models.Affiliation.get_by_id(flask.request.form["affiliation"]), ) affiliation_logic.match_to_affiliation_list(login.current_user) DB.session.commit() APP.log_manager.log_event("Updated Details", user=login.current_user) flask.flash("Your details have been updated", "success") affiliation_logic.maybe_verify_affiliation(login.current_user) return flask.redirect(flask.request.referrer or flask.url_for("dashboard.profile"))
def profile(): """Allow the user to edit their personal details. Displays a form and processes it to update the users details. """ if flask.request.method == 'POST': valid = True flashes = [] if ( flask.request.form['email'] != login.current_user.email and models.User.get_by_email( flask.request.form['email'] ) is not None ): flashes.append('That email address is already in use. ') valid = False if ( 'oldpassword' in flask.request.form and flask.request.form['oldpassword'] != '' ): if not login.current_user.check_password( flask.request.form['oldpassword'] ): flashes.append('Current password is not correct') valid = False if ( 'password' not in flask.request.form or 'confirm' not in flask.request.form or flask.request.form['password'] == '' or ( flask.request.form['password'] != flask.request.form['confirm'] ) ): flashes.append('New passwords do not match') valid = False if len(flask.request.form['password']) < 8: flashes.append('Password must be at least 8 characters long') valid = False if ( 'forenames' not in flask.request.form or flask.request.form['forenames'] == '' ): flashes.append('First Name cannot be blank') valid = False if ( 'surname' not in flask.request.form or flask.request.form['surname'] == '' ): flashes.append('Surname cannot be blank') valid = False if ( 'email' not in flask.request.form or flask.request.form['email'] == '' ): flashes.append('Email cannot be blank') valid = False if ( 'phone' not in flask.request.form or flask.request.form['phone'] == '' ): flashes.append('Phone cannot be blank') valid = False if ( 'college' not in flask.request.form or flask.request.form['college'] == '---' ): flashes.append('Please select a college') valid = False if ( 'affiliation' not in flask.request.form or flask.request.form['affiliation'] == '---' ): flashes.append('Please select an affiliation') valid = False if not valid: flask.flash( ( 'There were errors in your provided details. Please fix ' 'these and try again' ), 'error' ) for msg in flashes: flask.flash(msg, 'warning') else: if flask.request.form['email'] != login.current_user.email: login.current_user.new_email = flask.request.form['email'] login.current_user.secret_key = util.generate_key(64) login.current_user.secret_key_expiry = ( datetime.datetime.utcnow() + datetime.timedelta(days=7)) APP.email_manager.send_template( flask.request.form['email'], 'Confirm your Email Address', 'email_change_confirm.email', name=login.current_user.forenames, confirmurl=flask.url_for( 'front.confirm_email', user_id=login.current_user.object_id, secret_key=login.current_user.secret_key, _external=True ) ) flask.flash( ( 'You must confirm your new email address to make ' 'sure that we can contact you if necessary. Please ' 'check your email for further instructions.' ), 'info' ) if ( 'oldpassword' in flask.request.form and flask.request.form['oldpassword'] != '' ): login.current_user.set_password(flask.request.form['password']) login.current_user.forenames = flask.request.form['forenames'] login.current_user.surname = flask.request.form['surname'] login.current_user.phone = flask.request.form['phone'] affiliation_logic.update_affiliation( login.current_user, models.College.get_by_id(flask.request.form['college']), models.Affiliation.get_by_id(flask.request.form['affiliation']) ) DB.session.commit() APP.log_manager.log_event( 'Updated Details', [], login.current_user ) flask.flash( 'Your details have been updated', 'success' ) affiliation_logic.maybe_verify_affiliation(login.current_user) return flask.render_template( 'dashboard/profile.html', colleges=models.College.query.all(), affiliations=models.Affiliation.query.all() )
def update_profile(): """Allow the user to update their personal details.""" if flask.request.method != 'POST': return flask.redirect(flask.request.referrer or flask.url_for('dashboard.profile')) if not login.current_user.can_update_details(): flask.flash( flask.Markup( ( 'You cannot currently change your details. Please contact ' '<a href="{0}">the ticketing officer</a> for assistance.' ).format( APP.config['TICKETS_EMAIL_LINK'] ) ), 'error' ) return flask.redirect(flask.request.referrer or flask.url_for('dashboard.profile')) flashes = [] if ( 'forenames' not in flask.request.form or flask.request.form['forenames'] == '' ): flashes.append('Forename(s) cannot be blank') if ( 'surname' not in flask.request.form or flask.request.form['surname'] == '' ): flashes.append('Surname cannot be blank') if ( 'phone' not in flask.request.form or flask.request.form['phone'] == '' ): flashes.append('Phone cannot be blank') if ( 'college' not in flask.request.form or flask.request.form['college'] == '---' ): flashes.append('Please select a college') if ( 'affiliation' not in flask.request.form or flask.request.form['affiliation'] == '---' ): flashes.append('Please select an affiliation') if flashes: flask.flash( ( 'There were errors in your provided details. Please fix ' 'these and try again' ), 'error' ) for msg in flashes: flask.flash(msg, 'warning') return flask.redirect(flask.request.referrer or flask.url_for('dashboard.profile')) login.current_user.forenames = flask.request.form['forenames'] login.current_user.surname = flask.request.form['surname'] if login.current_user.phone != flask.request.form['phone']: login.current_user.phone_verified = False login.current_user.phone = flask.request.form['phone'] affiliation_logic.update_affiliation( login.current_user, models.College.get_by_id(flask.request.form['college']), models.Affiliation.get_by_id(flask.request.form['affiliation']) ) DB.session.commit() APP.log_manager.log_event( 'Updated Details', user=login.current_user ) flask.flash( 'Your details have been updated', 'success' ) affiliation_logic.maybe_verify_affiliation(login.current_user) return flask.redirect(flask.request.referrer or flask.url_for('dashboard.profile'))