2018-07-12 22:09:10 +02:00
|
|
|
from app import app, db
|
|
|
|
from models import User, Achievement
|
|
|
|
|
|
|
|
from flask import request
|
|
|
|
from flask_login import current_user, login_required
|
2018-07-12 21:37:37 +02:00
|
|
|
|
|
|
|
import json
|
|
|
|
|
2018-07-12 22:09:10 +02:00
|
|
|
@app.route("/users/<id>/add_achievement")
|
|
|
|
@login_required
|
|
|
|
def add_achievement(id):
|
|
|
|
name = request.args.get("name")
|
|
|
|
if not name:
|
|
|
|
return "No name supplied"
|
2018-07-12 22:31:08 +02:00
|
|
|
achievement = Achievement(name=name, achieved_by=id)
|
|
|
|
achievement.endorsed_by.append(current_user)
|
|
|
|
db.session.add(achievement)
|
2018-07-12 22:09:10 +02:00
|
|
|
db.session.commit()
|
|
|
|
return "OK"
|
|
|
|
|
|
|
|
@app.route("/users/<id>")
|
|
|
|
def get_user(id):
|
|
|
|
user = User.query.filter_by(id=id).first()
|
2018-07-12 21:37:37 +02:00
|
|
|
if not user:
|
|
|
|
return "{}"
|
|
|
|
return json.dumps(user.to_obj())
|
|
|
|
|
|
|
|
@app.route("/users/")
|
|
|
|
def all_users():
|
|
|
|
return json.dumps([user.to_obj() for user in User.query.all()])
|