endorsement/user.py

39 lines
1.2 KiB
Python
Raw Normal View History

2018-07-12 22:09:10 +02:00
from app import app, db
from models import User, Achievement
2018-07-18 19:10:55 +02:00
from flask import request, render_template, redirect, url_for
2018-07-12 22:09:10 +02:00
from flask_login import current_user, login_required
2018-07-18 19:10:55 +02:00
from wtforms import Form, TextField, validators
import json
2018-07-18 19:10:55 +02:00
class AchievementForm(Form):
name = TextField("Name: ", validators=[validators.required()])
@app.route("/users/<id>/addachievement", methods=["GET", "POST"])
2018-07-12 22:09:10 +02:00
@login_required
def add_achievement(id):
2018-07-18 19:10:55 +02:00
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)
2018-07-12 22:09:10 +02:00
@app.route("/users/<id>")
def get_user(id):
user = User.query.filter_by(id=id).first()
if not user:
2018-07-17 09:34:36 +02:00
return "Unknown user"
return render_template("user.html", data=user.to_obj())
2018-07-18 19:10:55 +02:00
@app.route("/")
@app.route("/users/")
def all_users():
2018-07-18 19:10:55 +02:00
return render_template("allusers.html", data=[user.to_obj() for user in User.query.all()])
2018-11-28 14:34:34 +01:00