contentdb/app/views/users.py

53 lines
1.4 KiB
Python
Raw Normal View History

2018-03-18 19:05:53 +01:00
from flask import *
from flask_user import *
from flask_login import login_user, logout_user
from flask.ext import menu
from app import app
from app.models import *
# Define the User registration form
# It augments the Flask-User RegisterForm with additional fields
from flask_user.forms import RegisterForm
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, validators
class MyRegisterForm(RegisterForm):
2018-03-20 04:31:49 +01:00
display_name = StringField("Display name")
2018-03-18 19:05:53 +01:00
# Define the User profile form
class UserProfileForm(FlaskForm):
2018-03-20 04:31:49 +01:00
display_name = StringField("Display name")
2018-03-21 23:03:37 +01:00
submit = SubmitField("Save")
2018-03-18 19:05:53 +01:00
2018-03-21 23:03:37 +01:00
@app.route("/user/", methods=["GET"])
2018-03-20 20:07:20 +01:00
@login_required
def self_user_profile_page():
return redirect(url_for("user_profile_page", username=current_user.username))
2018-03-21 23:03:37 +01:00
@app.route("/user/<username>/", methods=["GET", "POST"])
2018-03-20 20:07:20 +01:00
def user_profile_page(username):
user = User.query.filter_by(username=username).first()
if not user:
abort(404)
2018-03-18 19:05:53 +01:00
2018-03-20 20:07:20 +01:00
form = None
2018-03-18 19:05:53 +01:00
if user == current_user:
# Initialize form
2018-03-18 19:14:55 +01:00
form = UserProfileForm(formdata=request.form, obj=current_user)
2018-03-18 19:05:53 +01:00
# Process valid POST
2018-03-21 23:03:37 +01:00
if request.method=="POST" and form.validate():
2018-03-18 19:05:53 +01:00
# Copy form fields to user_profile fields
form.populate_obj(current_user)
# Save user_profile
db.session.commit()
# Redirect to home page
2018-03-21 23:03:37 +01:00
return redirect(url_for("home_page"))
2018-03-18 19:05:53 +01:00
# Process GET or invalid POST
2018-03-21 23:03:37 +01:00
return render_template("users/user_profile_page.html",
2018-03-18 19:05:53 +01:00
user=user, form=form)