You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.2 KiB

import json
from flask import Flask, Response, redirect, url_for, request, session, abort, render_template
from flask_login import LoginManager, UserMixin, login_required, login_user, logout_user
app = Flask(__name__)
# config
app.config.update(
DEBUG = True,
SECRET_KEY = 'secret_xxx'
)
class User(UserMixin):
def __init__(self, id):
self.id = id
with open('users.json') as jsonfile:
data = json.load(jsonfile)
self.name = data[str(id)]['name']
self.password = data[str(id)]['password']
def __repr__(self):
return "%d/%s/%s" % (self.id, self.name, self.password)
leaderboard = []
davide = {}
davide['name'] = 'Davide'
davide['score'] = 20
leaderboard.append(davide)
pawel = {}
pawel['name'] = 'Pawel'
pawel['score'] = 10
leaderboard.append(pawel)
mark = {}
mark['name'] = 'Marek'
mark['score'] = 1
leaderboard.append(mark)
sven = {}
sven['name'] = 'Sven'
sven['score'] = -5
leaderboard.append(sven)
wille = {}
wille['name'] = 'Wille'
wille['score'] = -15
leaderboard.append(wille)
c_davide = {}
c_davide['name'] = 'Davide'
c_davide['confirmed'] = 'no'
cook = c_davide
eaters = ["Wille", "Marek", "Sven"]
# flask-login
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"
@app.route("/")
@login_required
def index():
date = 'Friday 23rd of November'
meals = []
meals.append({'id':1, 'kuk':'davide', 'eaters':['marek','sven','wille'], 'flavor_text':'Pizza', 'date':'Tuesday 21st of November', 'selection_deadline':'23 hours 14 minutes', 'status':0})
meals.append({'id':2, 'kuk':'marek', 'eaters':['davide','sven','david', 'wille'], 'flavor_text':'Grzybowa', 'date':'Friday 23rd of November', 'selection_deadline':'3 Days 23 hours', 'status':0})
return render_template('index.html', leaderboard=leaderboard, cook=cook, eaters=eaters, date=date)
# return render_template('meal_list.html', leaderboard=leaderboard, cook=cook, eaters=eaters, meals=meals)
# somewhere to login
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
jsonfile = open('users.json')
data = json.load(jsonfile)
for i in range(1, 1 + int(data['nofusers'])):
if data[str(i)]['name'] == username and data[str(i)]['password'] == password:
id = i
jsonfile.close()
user = User(id)
login_user(user)
return redirect(request.args.get("next"))
jsonfile.close()
return abort(401)
else:
return Response('''
<form action="" method="post">
<p><input type=text name=username>
<p><input type=password name=password>
<p><input type=submit value=Login>
</form>
''')
# somewhere to logout
@app.route("/logout")
@login_required
def logout():
logout_user()
return Response('<p>Logged out</p>')
# handle login failed
@app.errorhandler(401)
def page_not_found(e):
return Response('<p>Login failed</p>')
# callback to reload the user object
@login_manager.user_loader
def load_user(userid):
return User(userid)