def edit_account(user_id): form = EditAccountForm(request.form) if not session.get('logged_in'): return "You are not logged in" user = dbOps.get_user_by_ID(user_id) if not user: return "No user account associated with that user" if request.method == 'GET': return render_template("edit_account_page.html", user_id=user_id, form=form) if request.method == 'POST' and form.validate(): errors = [] new_email = form.email.data new_pword = form.password.data if (not new_email) and (not new_pword): errors += ['Please enter a new email or password'] errors += validate_password(new_pword) + validate_email(new_email) if dbOps.user_exists(new_email): flash("Account already exists for this email") return render_template("edit_account_page.html", user_id=user_id, form=form) if len(errors) is not 0: if errors[0] !='field is required': flash(errors[0]) return render_template("edit_account_page.html", user_id=user_id, form=form) if new_email: dbOps.send_verification_email(new_email, mail_manager) flash("you will receive a confirmation email with an activation URL, to prove that the new email address belongs to you") dbOps.edit_user_account(user_id, new_email, encrypt(new_pword)) return redirect(url_for('index')) dbOps.edit_user_account(user_id, new_email, encrypt(new_pword)) flash("Account successfully updated") return redirect(url_for('show_user_page', user_id=user_id)) else: flash("Please fix any errors") return render_template("edit_account_page.html", user_id=user_id, form=form)
def show_user_page(user_id): if request.method == 'GET': if not session.get('logged_in'): return "You are not logged in" user = dbOps.get_user_by_ID(user_id) if user: sorting = request.args.get('sorting') if sorting == 'OldestFirst': sorting = 'OldestFirst' posts = dbOps.get_oldest_first_posts_by_user(user) else: sorting = 'MostRecent' posts = dbOps.get_most_recent_posts_by_user(user) page, per_page, offset = get_page_items(5) pagination = Pagination(page=page, total=len(posts), search=False, record_name='posts', per_page=5, css_framework='foundation') return render_template('user_page.html', user_id=user_id, posts=posts[offset:offset + per_page], pagination=pagination, sorting=sorting) else: return "No user account associated with that user" if request.method == 'POST': return redirect(url_for('create_post', user_id=user_id))
def edit_account(user_id): form = EditAccountForm(request.form) if not session.get('logged_in'): return "You are not logged in" user = dbOps.get_user_by_ID(user_id) if not user: return "No user account associated with that user" if request.method == 'GET': return render_template("edit_account_page.html", user_id=user_id, form=form) if request.method == 'POST' and form.validate(): errors = [] new_email = form.email.data new_pword = form.password.data if (not new_email) and (not new_pword): errors += ['Please enter a new email or password'] errors += validate_password(new_pword) + validate_email(new_email) if dbOps.user_exists(new_email): flash("Account already exists for this email") return render_template("edit_account_page.html", user_id=user_id, form=form) if len(errors) is not 0: if errors[0] != 'field is required': flash(errors[0]) return render_template("edit_account_page.html", user_id=user_id, form=form) if new_email: dbOps.send_verification_email(new_email, mail_manager) flash( "you will receive a confirmation email with an activation URL, to prove that the new email address belongs to you" ) dbOps.edit_user_account(user_id, new_email, encrypt(new_pword)) return redirect(url_for('index')) dbOps.edit_user_account(user_id, new_email, encrypt(new_pword)) flash("Account successfully updated") return redirect(url_for('show_user_page', user_id=user_id)) else: flash("Please fix any errors") return render_template("edit_account_page.html", user_id=user_id, form=form)
def show_user_page(user_id): if request.method == 'GET': if not session.get('logged_in'): return "You are not logged in" user = dbOps.get_user_by_ID(user_id) if user: sorting = request.args.get('sorting') if sorting == 'OldestFirst': sorting = 'OldestFirst' posts = dbOps.get_oldest_first_posts_by_user(user) else: sorting = 'MostRecent' posts = dbOps.get_most_recent_posts_by_user(user) page, per_page, offset = get_page_items(5) pagination = Pagination(page=page, total=len(posts), search=False, record_name='posts', per_page=5, css_framework='foundation') return render_template('user_page.html', user_id=user_id, posts=posts[offset:offset+per_page], pagination=pagination, sorting=sorting) else: return "No user account associated with that user" if request.method == 'POST': return redirect(url_for('create_post', user_id=user_id))