Added possibility to add achievements

This commit is contained in:
Robbe Van Herck 2018-07-12 22:09:10 +02:00
parent 8b85bc3dad
commit 8e9e1b6e8f
No known key found for this signature in database
GPG key ID: 4D1C8E8FBD1C7188
4 changed files with 31 additions and 19 deletions

View file

@ -23,4 +23,4 @@ def logout():
if 'zeus_token' in session: if 'zeus_token' in session:
session.pop('zeus_token', None) session.pop('zeus_token', None)
logout_user() logout_user()
return redirect(url_for('home')) return redirect(url_for('all_users'))

View file

@ -1,21 +1,21 @@
from app import db from app import db
endorsed = db.Table('endorses', endorsed = db.Table('endorses',
db.Column('user_id', db.String, db.ForeignKey('user.username'), primary_key=True), db.Column('user_id', db.String, db.ForeignKey('user.id'), primary_key=True),
db.Column('achievement_id', db.Integer, db.ForeignKey('achievement.id'), primary_key=True) db.Column('achievement_id', db.Integer, db.ForeignKey('achievement.id'), primary_key=True)
) )
class User(db.Model): class User(db.Model):
username = db.Column(db.String(80), unique=True, nullable=False, primary_key=True) id = db.Column(db.String(80), unique=True, nullable=False, primary_key=True)
def to_obj(self): def to_obj(self):
return { return {
"username": self.username, "id": self.id,
"achievements": [x.to_obj() for x in Achievement.query.filter_by(achieved_by=self.username).all()] "achievements": [x.to_obj() for x in Achievement.query.filter_by(achieved_by=self.id).all()]
} }
def __repr__(self): def __repr__(self):
return '<User %r>' % self.username return '<User %r>' % self.id
# For login # For login
def is_authenticated(self): def is_authenticated(self):
@ -24,19 +24,16 @@ class User(db.Model):
def is_active(self): def is_active(self):
return True return True
def is_admin(self):
return self.admin
def is_anonymous(self): def is_anonymous(self):
return False return False
def get_id(self): def get_id(self):
return str(self.username) return str(self.id)
class Achievement(db.Model): class Achievement(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128), nullable=False) name = db.Column(db.String(128), nullable=False)
achieved_by = db.Column(db.Integer, db.ForeignKey("user.username"), nullable=False) achieved_by = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
endorsed_by = db.relationship('User', secondary=endorsed, lazy='subquery', backref=db.backref('user', lazy=True)) endorsed_by = db.relationship('User', secondary=endorsed, lazy='subquery', backref=db.backref('user', lazy=True))
@ -44,7 +41,7 @@ class Achievement(db.Model):
return { return {
"id": self.id, "id": self.id,
"name": self.name, "name": self.name,
"endorsed_by": [x.username for x in self.endorsed_by], # Don't show full user object to avoid infinite loops "endorsed_by": [x.id for x in self.endorsed_by], # Don't show full user object to avoid infinite loops
} }
def __repr__(self): def __repr__(self):

25
user.py
View file

@ -1,11 +1,26 @@
from app import app from app import app, db
from models import User from models import User, Achievement
from flask import request
from flask_login import current_user, login_required
import json import json
@app.route("/users/<username>") @app.route("/users/<id>/add_achievement")
def get_user(username): @login_required
user = User.query.filter_by(username=username).first() def add_achievement(id):
name = request.args.get("name")
if not name:
return "No name supplied"
a = Achievement(name=name, achieved_by=current_user.id)
db.session.add(a)
db.session.commit()
return "OK"
@app.route("/users/<id>")
def get_user(id):
user = User.query.filter_by(id=id).first()
if not user: if not user:
return "{}" return "{}"
return json.dumps(user.to_obj()) return json.dumps(user.to_obj())

View file

@ -41,7 +41,7 @@ def authorized():
me = zeus.get('current_user/') me = zeus.get('current_user/')
username = me.data.get('username', '').lower() username = me.data.get('username', '').lower()
user = User.query.filter_by(username=username).first() user = User.query.filter_by(id=username).first()
if len(username) > 0 and user: if len(username) > 0 and user:
return login_and_redirect_user(user) return login_and_redirect_user(user)
elif len(username) > 0: elif len(username) > 0:
@ -63,7 +63,7 @@ def login_and_redirect_user(user):
def create_user(username): def create_user(username):
user = User(username=username) user = User(id=username)
db.session.add(user) db.session.add(user)
db.session.commit() db.session.commit()
return user return user