-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholpc.py
145 lines (104 loc) · 3.91 KB
/
olpc.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# all the imports
import os
from flask import Flask, render_template
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.security import Security, SQLAlchemyUserDatastore, \
UserMixin, RoleMixin, login_required
from flask.ext.security.utils import encrypt_password
from flask.ext.security.views import register
# configuration
DEBUG = True
app = Flask(__name__)
app.config['SECURITY_REGISTERABLE'] = True
app.config["SECURITY_PASSWORD_HASH"] = "bcrypt"
app.config["SECURITY_PASSWORD_SALT"] = "salty"
app.secret_key = 'D\xf04\xa9\xa8\xac`\x99?\x98m\xd7\x98j\x89\xac6\x84&\x87\xe4y\xce\xceKl\xd0E`D\xa0\n'
app.config.from_object(__name__)
app.config.from_envvar('OLPC_SETTINGS', silent=True)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get("DATABASE_URL","postgresql://olpc:!hessian!@localhost/olpc_dashboard")
db = SQLAlchemy(app)
# Define models
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
class Role(db.Model, RoleMixin):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(80), unique=True)
description = db.Column(db.String(255))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True)
password = db.Column(db.String(255))
active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime())
roles = db.relationship('Role', secondary=roles_users,
backref=db.backref('users', lazy='dynamic'))
# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)
# Create a user to test with
"""
@app.before_first_request
def create_user():
db.create_all()
user_datastore.create_user(email='[email protected]',password=encrypt_password('!olpc_1634!'))
db.session.commit()
"""
@app.teardown_request
def teardown_request(exception):
pass
@app.route('/project_stats', methods=['GET', 'POST'])
def projects_stats_page():
""" stats about project,location , size mean,max ,age"""
projects = getAllProjectsFromDB()
app.logger.debug(dir(projects[0]))
data = {}
column_headers = ["Project Name","Country",
"Date Started","Number of XO",
"Details"]
return render_template('project_stats.html',
column_headers = column_headers,
projects=projects)
@app.route('/applications_stats', methods=['GET'])
def applications_stats_page():
""" stats about application,use, time of day used """
data = {}
return render_template('application_stats.html', data=data)
@app.route('/register', methods=['GET'])
def register_wrapper():
return register()
@app.route('/about_us', methods=['GET'])
def about_us():
return "about us"
class Project:
def create(self, name="Project Name",
country = "Jamaica",
dateStarted="Date Started",
numberOfXO = "1000"):
self.name = name
self.country = country
self.dateStarted = dateStarted
self.numberOfXO = numberOfXO
def createDummyProjects(qty):
projects = []
for idx in range(1,qty):
p = Project()
p.create()
projects.append(p)
return projects
def getAllProjectsFromDB():
#need to cache this
#see if new projects have been added to the database
#if not return the projects in the cache
projects = createDummyProjects(40)
return projects
@app.route('/')
@login_required
def main_page():
return render_template('index.html', page = "FRONT_PAGE")
@app.route('/contact', methods=['GET', 'POST'])
def contact_us():
error = {}
return render_template('contact_us.html', error=error)
if __name__ == '__main__':
app.run('0.0.0.0',8000)