This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.py
60 lines (48 loc) · 1.63 KB
/
config.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
import os
from flask import Flask, Request, g, request
from flask.ext.login import LoginManager, current_user
from database import session
from models import User
# Set application constants, and create the application
DATABASE = "/tmp/timeclock.db"
DEBUG = False
SECRET_KEY = str(os.getenv("APP_SECRET_KEY", "local_development"))
app = Flask(__name__)
app.config.from_object(__name__)
# Configure login manager
lm = LoginManager()
lm.init_app(app)
lm.login_view = "login"
# Identify the timezone lookup database
GEOIP_DATA_LOCATION = "static/geo_ip/GeoLiteCity.dat"
# Remove the database session at the end of a request or at shutdown
@app.teardown_appcontext
def shutdown_session(exception=None):
session.remove()
# Allow login manager to load a user from the database
@lm.user_loader
def load_user(id):
if id is not None:
return User.query.get(int(id))
else:
return None
# Remember the current user
@app.before_request
def before_request():
g.user = current_user
# Replace the remote_addr property so that it returns a non-proxy IP
class HerokuRequest(Request):
@property
def remote_addr(self):
forwarded_for = self.environ.get("HTTP_X_FORWARDED_FOR", None)
if forwarded_for:
# If the object contains multiple addresses, the actual IP is first
if "," in forwarded_for:
return fwd.split(",")[0]
else:
return forwarded_for
# Otherwise, return the default value
else:
return self.environ.get("REMOTE_ADDR")
# Use the HerokuRequest class so that the actual IPs are retrieved
app.request_class = HerokuRequest