forked from lhayhurst/xwlists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myapp.py
77 lines (57 loc) · 2.17 KB
/
myapp.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
import os
from flask import Flask
from sqlalchemy import create_engine
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
#see https://www.pythonanywhere.com/wiki/WebAppClientIPAddresses
from sqlalchemy.sql import ColumnElement
from sqlalchemy.sql.elements import _clause_element_as_expr
def remote_address(request):
if request.headers.has_key( 'X-Real-IP'):
return request.headers[ 'X-Real-IP' ]
else:
return request.remote_addr
#rollup help
#see https://groups.google.com/forum/#!msg/sqlalchemy/Pj5T8hO_ibQ/LrmBcIBxnNwJ
class rollup(ColumnElement):
def __init__(self, *elements):
self.elements = [_clause_element_as_expr(e) for e in elements]
@compiles(rollup, "mysql")
def _mysql_rollup(element, compiler, **kw):
return "%s WITH ROLLUP" % (', '.join([compiler.process(e, **kw) for e in element.elements]))
class MyDatabaseConnector(object):
engine = None
Session = None
connection = None
base = None
def __init__(self):
self.connect()
def connect(self):
if self.engine is None:
self.engine = create_engine(os.getenv('LOCAL_DB_URL'), echo=False, pool_size=100, pool_recycle=299,
pool_timeout=20)
if self.Session is None:
self.Session = scoped_session(sessionmaker(bind=self.engine, expire_on_commit=False))
self.Session.configure( bind=self.engine)
if self.connection is None:
self.connection = self.engine.connect()
if self.base is None:
self.base = declarative_base()
def get_base(self):
self.connect()
return self.base
def get_session(self):
self.connect()
return self.Session
def get_engine(self):
self.connect()
return self.engine
def close(self):
self.Session.remove()
db_connector = MyDatabaseConnector()
challonge_user = os.getenv('CHALLONGE_USER')
challonge_key = os.getenv('CHALLONGE_API_KEY')
def create_app():
app = Flask(__name__ , static_folder='static')
return app