endorsement/user.py

30 lines
776 B
Python
Raw Normal View History

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
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"
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()
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()])