Added possibility to add achievements
This commit is contained in:
parent
8b85bc3dad
commit
8e9e1b6e8f
4 changed files with 31 additions and 19 deletions
2
login.py
2
login.py
|
@ -23,4 +23,4 @@ def logout():
|
|||
if 'zeus_token' in session:
|
||||
session.pop('zeus_token', None)
|
||||
logout_user()
|
||||
return redirect(url_for('home'))
|
||||
return redirect(url_for('all_users'))
|
19
models.py
19
models.py
|
@ -1,21 +1,21 @@
|
|||
from app import db
|
||||
|
||||
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)
|
||||
)
|
||||
|
||||
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):
|
||||
return {
|
||||
"username": self.username,
|
||||
"achievements": [x.to_obj() for x in Achievement.query.filter_by(achieved_by=self.username).all()]
|
||||
"id": self.id,
|
||||
"achievements": [x.to_obj() for x in Achievement.query.filter_by(achieved_by=self.id).all()]
|
||||
}
|
||||
|
||||
def __repr__(self):
|
||||
return '<User %r>' % self.username
|
||||
return '<User %r>' % self.id
|
||||
|
||||
# For login
|
||||
def is_authenticated(self):
|
||||
|
@ -24,19 +24,16 @@ class User(db.Model):
|
|||
def is_active(self):
|
||||
return True
|
||||
|
||||
def is_admin(self):
|
||||
return self.admin
|
||||
|
||||
def is_anonymous(self):
|
||||
return False
|
||||
|
||||
def get_id(self):
|
||||
return str(self.username)
|
||||
return str(self.id)
|
||||
|
||||
class Achievement(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
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))
|
||||
|
||||
|
@ -44,7 +41,7 @@ class Achievement(db.Model):
|
|||
return {
|
||||
"id": self.id,
|
||||
"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):
|
||||
|
|
25
user.py
25
user.py
|
@ -1,11 +1,26 @@
|
|||
from app import app
|
||||
from models import User
|
||||
from app import app, db
|
||||
from models import User, Achievement
|
||||
|
||||
from flask import request
|
||||
from flask_login import current_user, login_required
|
||||
|
||||
import json
|
||||
|
||||
@app.route("/users/<username>")
|
||||
def get_user(username):
|
||||
user = User.query.filter_by(username=username).first()
|
||||
@app.route("/users/<id>/add_achievement")
|
||||
@login_required
|
||||
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:
|
||||
return "{}"
|
||||
return json.dumps(user.to_obj())
|
||||
|
|
4
zeus.py
4
zeus.py
|
@ -41,7 +41,7 @@ def authorized():
|
|||
me = zeus.get('current_user/')
|
||||
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:
|
||||
return login_and_redirect_user(user)
|
||||
elif len(username) > 0:
|
||||
|
@ -63,7 +63,7 @@ def login_and_redirect_user(user):
|
|||
|
||||
|
||||
def create_user(username):
|
||||
user = User(username=username)
|
||||
user = User(id=username)
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return user
|
Loading…
Reference in a new issue