endorsement/user.py
2018-11-28 15:02:38 +01:00

38 lines
1.2 KiB
Python

from app import app, db
from models import User, Achievement
from flask import request, render_template, redirect, url_for
from flask_login import current_user, login_required
from wtforms import Form, TextField, validators
import json
class AchievementForm(Form):
name = TextField("Name: ", validators=[validators.required()])
@app.route("/users/<id>/addachievement", methods=["GET", "POST"])
@login_required
def add_achievement(id):
form = AchievementForm(request.form)
if request.method == "POST":
name = request.form["name"]
if form.validate():
achievement = Achievement(name=name, achieved_by=id)
achievement.endorsed_by.append(current_user)
db.session.add(achievement)
db.session.commit()
return redirect(url_for("get_user", id=id))
return render_template("newachievement.html", form=form)
@app.route("/users/<id>")
def get_user(id):
user = User.query.filter_by(id=id).first()
if not user:
return "Unknown user"
return render_template("user.html", data=user.to_obj())
@app.route("/")
@app.route("/users/")
def all_users():
return render_template("allusers.html", data=[user.to_obj() for user in User.query.all()])