-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
134 lines (117 loc) · 4.1 KB
/
app.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import bottle
import uuid
import user
import counters
import datetime
bottle.debug(False)
bottle.TEMPLATE_PATH.append(os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi', 'template'))
def get_session():
session = bottle.request.get_cookie('session', secret='secret')
return session;
def save_session(uid):
session = {}
session['uid'] = uid
session['sid'] = uuid.uuid4().hex
bottle.response.set_cookie('session', session, secret='secret')
return session;
def invalidate_session():
bottle.response.delete_cookie('session', secret='secret')
return
@bottle.route('/')
def index():
session = get_session()
if session:
return bottle.template('index',auth=True)
return bottle.template('index',auth=False)
@bottle.route('/edit')
def edit():
session = get_session()
if not session: bottle.redirect('/')
luser = user.find(session['uid'])
if not luser: bottle.redirect('/logout')
return bottle.template('edit')
@bottle.route('/signup', method='POST')
def post_signup():
if 'login' in bottle.request.POST and 'password' in bottle.request.POST and 'apartment' in bottle.request.POST and 'home' in bottle.request.POST:
login = bottle.request.POST['login']
password = bottle.request.POST['password']
home = bottle.request.POST['home']
apartment = bottle.request.POST['apartment']
if user.add_user(login,password,home,apartment):
save_session(login)
bottle.redirect('/edit')
return bottle.redirect('/')
@bottle.route('/login', method='POST')
def post_login():
if 'login' in bottle.request.POST and 'password' in bottle.request.POST:
login = bottle.request.POST['login']
password = bottle.request.POST['password']
if user.auth(login,password):
save_session(login)
bottle.redirect('/edit')
return bottle.redirect('/')
@bottle.route('/logout')
def logout():
invalidate_session()
bottle.redirect('/')
@bottle.route('/edit', method='POST')
def post_edit():
session = get_session()
if not session: bottle.redirect('/')
luser = user.find(session['uid'])
if not luser: bottle.redirect('/logout')
data = user.get_data(session['uid'])
cold = bottle.request.POST['cold']
hot = bottle.request.POST['hot']
counters.update(data[0],data[1],cold,hot,datetime.date.today(),session['uid'])
return bottle.redirect('/report')
@bottle.route('/report')
def report():
session = get_session()
if not session: bottle.redirect('/')
luser = user.find(session['uid'])
if not luser: bottle.redirect('/logout')
return bottle.template('report',List=0,home=False)
@bottle.route('/report', method='POST')
def post_report():
apartment = bottle.request.POST['apartment']
home = bottle.request.POST['home']
if apartment !='' and home !='':
data = counters.get_apartment(home,apartment)
return bottle.template('report',List=data,home=False)
if apartment =='' and home !='':
data = counters.get_home(home)
cold = 0
hot = 0
for x in data:
cold += x[3]
hot += x[4]
return bottle.template('report',List=data,home=True,sumcold=cold,sumhot=hot)
return bottle.template('report',List=data,home=False)
@bottle.route('/DEBUG/cwd')
def dbg_cwd():
return "<tt>cwd is %s</tt>" % os.getcwd()
@bottle.route('/DEBUG/env')
def dbg_env():
env_list = ['%s: %s' % (key, value)
for key, value in sorted(os.environ.items())]
return "<pre>env is\n%s</pre>" % '\n'.join(env_list)
@bottle.route('/template/js/:filename')
def static_file(filename):
bottle.send_file(filename,
root= os.path.join(os.environ['OPENSHIFT_REPO_DIR'],
'wsgi', 'template','js'))
@bottle.route('/template/img/:filename')
def static_file(filename):
bottle.send_file(filename,
root= os.path.join(os.environ['OPENSHIFT_REPO_DIR'],
'wsgi', 'template','img'))
@bottle.route('/template/css/:filename')
def static_file(filename):
bottle.send_file(filename,
root= os.path.join(os.environ['OPENSHIFT_REPO_DIR'],
'wsgi', 'template','css'))
application = bottle.default_app()