1
Fork 0
mirror of https://github.com/RYGhub/song.git synced 2024-11-22 04:54:20 +00:00
song/singasong.py

46 lines
1.4 KiB
Python
Raw Normal View History

2017-09-23 16:51:49 +00:00
import datetime as dt
from flask import Flask, session, request, render_template, abort, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
db = SQLAlchemy(app)
app.secret_key = "4pippo"
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
class Song(db.Model):
__tablename__ = "song"
id = db.Column(db.Integer, primary_key=True)
time = db.Column(db.DateTime, nullable=False)
word = db.Column(db.String, nullable=False)
@app.route('/', methods=["GET", "POST"])
def page_main():
if request.method == "GET":
if session.get("last_entry") and session["last_entry"] < dt.datetime.now() + dt.timedelta(1):
done = True
else:
done = False
session["last_entry"] = None
2017-09-24 12:41:56 +00:00
session.permanent = True
2017-09-23 17:36:43 +00:00
last_words = db.engine.execute("SELECT * FROM song ORDER BY time").fetchall()
2017-09-23 16:51:49 +00:00
return render_template("song.html", last_words=last_words, done=done)
elif request.method == "POST":
if session.get("last_entry") and session["last_entry"] < dt.datetime.now() + dt.timedelta(1):
abort(403)
new_word = Song(time=dt.datetime.now(), word=request.form["word"].split(" ")[0])
db.session.add(new_word)
db.session.commit()
session["last_entry"] = dt.datetime.now()
2017-09-24 12:41:56 +00:00
session.permanent = True
2017-09-23 16:51:49 +00:00
return redirect(url_for("page_main"))
if __name__ == '__main__':
db.create_all()
app.run()