-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·102 lines (75 loc) · 2.33 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
from bottle import abort
from bottle import install
from bottle import route
from bottle import run
from bottle import static_file
from bottle.ext import sqlalchemy
from bottle import template
from sqlalchemy import Boolean
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import func
import settings
# set up sqlalchemy
AlchemyBase = declarative_base()
engine = create_engine(settings.DATABASE_CONNECTION_STRING, echo=True)
create_session = sessionmaker(bind=engine)
# set up the sqlalchemy plugin
sqlalchemy_plugin = sqlalchemy.Plugin(
engine,
AlchemyBase.metadata,
keyword="db",
create=True,
commit=True,
use_kwargs=False,
)
# set up the bottle app
install(sqlalchemy_plugin)
class Excuse(AlchemyBase):
__tablename__ = "excuses"
id = Column(Integer, primary_key=True)
excuse = Column(String, nullable=False)
published = Column(Boolean, nullable=False, default=False)
username = Column(String, nullable=False, default="admin")
team_id = Column(String, nullable=True, default=None)
def __init__(self, username, excuse):
self.username = username
self.excuse = excuse
def __repr__(self):
return "{id}: {excuse}".format(id=self.id, excuse=self.excuse)
@classmethod
def get_random_excuse(cls, db):
return (
db.query(cls).filter(cls.published == True).order_by(func.random()).first()
)
@route("/")
def hello(db):
"""Serve up a plaintext public excuse for the purposes of
"""
try:
excuse_text = Excuse.get_random_excuse(db).excuse
except AttributeError:
abort(404, "NO EXCUSE FOR YOU, but, maybe we need one :(")
return template(
"home",
excuse_text=excuse_text,
)
@route("/submit/")
def submit(db):
return template("submit")
@route("/contact/")
def contact(db):
return template("contact")
@route("/acknowledgements/")
def privacy_policy(db):
return template("acknowledgements")
@route("/static/<path:path>")
def callback(path):
return static_file(path, root=settings.STATIC_PATH)
if __name__ == "__main__":
run(host="127.0.0.1", port=8088, debug=False)