-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
290 lines (220 loc) · 7.29 KB
/
main.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""Main controller for app that allows users to register and connect to an
external API to retrieve data for graphing and other uses.
Author: Charlie Gorichanaz <[email protected]>
"""
# Logging
import logging
logging.basicConfig( filename='app.log', level=logging.DEBUG )
log = logging.getLogger(__name__)
log.debug("index.py loaded")
# Web.py and sessions
import web
from webpy_mongodb_sessions.session import MongoStore
import webpy_mongodb_sessions.users as users
# API interaction, database and data handling
import smartthings, processor
"""
SET UP DB AND WEBPY WITH USER LOGIN AND SESSIONS
"""
template_globals = {
'app_path': lambda p: 'https://votecharlie.com' + web.ctx.homepath + p,
}
render = web.template.render('templates/', globals=template_globals)
routes = (
'/', 'index',
'/error', 'error',
'/connect', 'connect',
'/login', 'login',
'/logout', 'logout',
'/register', 'register',
'/data/(.+)', 'data',
)
app = web.application( routes, globals() )
session = web.session.Session(app, MongoStore(smartthings.db))
users.session = session
users.collection = smartthings.db.users
"""
HELPER FUNCTIONS
"""
SHORT_KEY = 'shortcode' # db key to store shortcode
def current_user():
"""Return logged in user"""
user = users.get_user()
if user:
log.debug('user is {0}'.format(user))
return user
else:
log.debug('no user session')
return None
def new_shortcode(collection, keyname='shortcode', length=5):
"""Generate alphanumeric case sensitive codes until one is found that is not
already associated with a document in a collection.
TODO:
* If 20 attempts fail, currently returns an already used code. Should
implement a tradeoff where random attempts are made to a point, then
sort and choose next available code.
Args:
collection (pymongo.collection.Collection): Collection to check for
existing codes.
keyname (Optional[str]): Name of top level key where in use codes are
stored in each document.
length (Optional[int]): Length of each code. Since codes are ASCII
letters and numbers of any case, total combinations are 62^length.
Returns:
str: unused code
"""
log.debug('generate_shortcode(): starting')
import random, string
choices = string.ascii_letters + string.digits
attempts = 0
while True:
shortcode = ''.join(random.choice(choices) for i in range(length))
attempts += 1
log.debug(
'generate_shortcode: attempt {0}: {1}'
.format(attempts, shortcode)
)
if collection.find({keyname: shortcode}).count() == 0 or attempts > 20:
break
return shortcode
"""
WEBPY URL HANDLERS
"""
def notfound():
"""Handle 404 not found errors.
Requires `app.notfound = notfound` following definition.
"""
return web.notfound(render.error(404))
def internalerror():
"""Handle internal errors.
Requires `app.internalerror = internalerror` following definition.
"""
return web.internalerror(render.error(500))
app.notfound = notfound
app.internalerror = internalerror
class register:
"""Handle user registration."""
def GET(self):
log.debug('register.GET')
return render.register()
def POST(self):
log.debug('register.POST')
params = web.input()
username = params["username"]
password = params["password"]
user = users.register(
username=username,
password=users.pswd(password, username),
)
users.login(user)
log.debug('user is {0}'.format(user))
raise web.seeother('/')
class profile:
"""Handle user profile viewing and editing."""
log.debug('profile.GET')
@users.login_required
def GET(self):
return render.profile()
class login:
"""Handle user log in.
TODO:
* Information on log in failure.
"""
def GET(self):
log.debug('login.GET')
return render.login()
def POST(self):
log.debug('login.POST')
params = web.input()
user = users.authenticate(
params["username"],
params["password"],
)
if user:
log.debug('user is {0}'.format(user))
users.login(user)
raise web.seeother('/')
else:
log.error('login failed, so user not set')
raise web.seeother('/error')
class logout:
"""Handle user log out."""
def GET(self):
log.debug('logout.GET')
users.logout() # runs session.kill()
raise web.seeother('/')
class error:
"""Handle error page."""
def GET(self):
log.debug('error.GET')
return render.error()
class connect:
"""Handle allowing a logged in user to connect to the external API and
receive an access token, which is stored in the user's account on the local
database.
"""
def GET(self):
log.debug('connect.GET')
user = current_user()
if user:
log.debug('user is {0}'.format(user))
st = smartthings.SmartThings()
params = web.input()
log.debug('params is {0}'.format(params))
if 'code' in params:
# We just logged into SmartThings and got an OAuth code.
user['token'] = st.token(params)
user[SHORT_KEY] = new_shortcode(
collection=users.collection,
keyname=SHORT_KEY,
)
users.register(**user) # not totally sure why need **
result_url = '/data/{0}'.format(user[SHORT_KEY])
raise web.seeother(result_url)
else:
# We are about to redirect to SmartThings to authorize.
raise web.seeother(st.auth_url())
else:
log.error('/connect was accessed without a user session.')
raise web.seeother('/error')
class index:
"""Handle home page."""
def GET(self):
log.debug('index.GET')
user = current_user()
if user:
log.debug('user is {0}'.format(user))
else:
log.debug('no user session')
return render.index(user)
class data:
"""Handle displaying a user's data page."""
def GET(self, shortcode):
log.debug('data.GET')
user = users.collection.find_one({SHORT_KEY: shortcode})
if user:
log.debug('shortcode {0} matches user {1}'.format(shortcode, user))
else:
log.debug('no user found matching shortcode')
raise web.seeother('/error')
return render.data(processor.results(user["token"]))
def POST(self):
log.debug('data.POST')
params = web.input()
if "save" in params:
pass
elif "update" in params:
pass
else:
pass
user = current_user()
if user:
log.debug('user is {0}'.format(user))
result_url = '/data/{0}'.format(user[SHORT_KEY])
raise web.seeother(result_url)
else:
log.error('/data was POSTed to without a user session.')
raise web.seeother('/error')
if __name__ == "__main__":
app.run()
application = app.wsgifunc()