Added add achievement field
This commit is contained in:
parent
39eb8b703c
commit
d266062856
2 changed files with 33 additions and 11 deletions
15
templates/newachievement.html
Normal file
15
templates/newachievement.html
Normal file
|
@ -0,0 +1,15 @@
|
|||
<html>
|
||||
<body>
|
||||
<form action="" method="post">
|
||||
{{ form.csrf }}
|
||||
|
||||
<div class="input text">
|
||||
{{ form.name.label }} {{ form.name }}
|
||||
</div>
|
||||
|
||||
<div class="input submit">
|
||||
<input type="submit" value="Add" />
|
||||
</div>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
19
user.py
19
user.py
|
@ -1,22 +1,28 @@
|
|||
from app import app, db
|
||||
from models import User, Achievement
|
||||
|
||||
from flask import request, render_template
|
||||
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
|
||||
|
||||
@app.route("/users/<id>/addachievement")
|
||||
class AchievementForm(Form):
|
||||
name = TextField("Name: ", validators=[validators.required()])
|
||||
|
||||
@app.route("/users/<id>/addachievement", methods=["GET", "POST"])
|
||||
@login_required
|
||||
def add_achievement(id):
|
||||
name = request.args.get("name")
|
||||
if not name:
|
||||
return "No name supplied"
|
||||
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 "OK"
|
||||
return redirect(url_for("get_user", id=id))
|
||||
return render_template("newachievement.html", form=form)
|
||||
|
||||
@app.route("/users/<id>")
|
||||
def get_user(id):
|
||||
|
@ -25,6 +31,7 @@ def get_user(id):
|
|||
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()])
|
Loading…
Reference in a new issue