-
Notifications
You must be signed in to change notification settings - Fork 1
/
swift.py
353 lines (301 loc) · 10.5 KB
/
swift.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# SWIFT Taskbook
# Web Application for Task Management
# system libraries
import os
# web transaction objects
from bottle import request, response
# HTML request types
from bottle import route, get, put, post, delete, redirect
# web page template processor
from bottle import template
# static file serving
from bottle import static_file
VERSION = 0.1
#email = "[email protected]"
# development server
PYTHONANYWHERE = ("PYTHONANYWHERE_SITE" in os.environ)
if PYTHONANYWHERE:
from bottle import default_app
else:
from bottle import run
def read_file(path):
output = ''
with open(path,'r') as f:
output += f.read()
return output
import sass, re
def compile_sass(str):
if str:
if len(str) <= 3:
return str
else:
return sass.compile(string=str)
return str
def compile_sass_file(path):
compiled = ''
try:
compiled = compile_sass(read_file(path))
except sass.CompileError as e:
print('FAILED TO COMPILE', path)
print(e)
compiled = ''
return compiled
def compile_sass_tag(str):
def replacer(match):
if match.group(0)[0:11] == '<style lang':
#print(match.string)
compiled = ''
try:
compiled = '<style>' + compile_sass(match.group(0)[len(match.group(1)):][:-2]) + '</'
except sass.CompileError as e:
print('FAILED TO COMPILE', str)
print(e)
compiled = match.string
return compiled
return match.string
return re.sub('(<style lang="scss">)(.|\n)+?(?=(style))', replacer, str)
# compile_sass_tag(open('./components/task.html').read())
# Static File Including
from bottle import BaseTemplate
def include_file(path):
return compile_sass_tag(read_file(path))
def include_component(path):
ext = os.path.splitext(path)[1]
contents = read_file('./components/' + path)
if ext == '.js':
return '<script>' + contents + '</script>'
return contents
BaseTemplate.defaults["file"] = include_file
BaseTemplate.defaults["component"] = include_component
# ---------------------------
# web application routes
# ---------------------------
@route('/')
@route('/tasks')
def tasks():
#print(template("tasks.tpl"))
#return template("tasks.tpl")
return compile_sass_tag(template('tasks.tpl'))
@route('/login')
def login():
return compile_sass_tag(template("login.tpl"))
@route('/register')
def login():
return compile_sass_tag(template("register.tpl"))
@route('/about')
def about():
return compile_sass_tag(template("about.tpl"))
# ---------------------------
# database
# ---------------------------
import json
import dataset
import time
import bcrypt
taskbook_db = dataset.connect('sqlite:///taskbook.db')
@get('/api/version')
def get_version():
return {"version": VERSION}
# ---------------------------
# account REST api
# ---------------------------
@get('/api/session')
def get_account():
session = request.get_cookie('taskbook_session')
if session:
if len(session) == 40:
account_table = taskbook_db.get_table('account')
user = account_table.find_one(session=session)
if user:
user.pop('password')
return user
return False
#return bytes(os.urandom(20)).hex()
@post('/api/login')
def login_account():
email = request.forms.get('email')
password = request.forms.get('password')
if len(email) <= 3 or len(password) <= 3:
return redirect('/login')
account_table = taskbook_db.get_table('account')
user = account_table.find_one(email=email)
if user:
if bcrypt.checkpw(str.encode(password), user["password"]):
token = bytes(os.urandom(20)).hex()
response.set_cookie('taskbook_session', token, path='/')
data = dict(email=user["email"], session=token)
account_table.update(data, ['email'])
return redirect('/')
return redirect('/login')
@get('/api/logout')
def logout_account():
user = get_account()
if user:
account_table = taskbook_db.get_table('account')
data = dict(email=user["email"], session="")
account_table.update(data, ['email'])
response.set_cookie('taskbook_session', '', expires=0, path='/')
return redirect('/login')
return redirect('/login')
@get('/logout')
def logout_account2():
return logout_account();
@post('/api/signup')
def create_account():
name = request.forms.get('name')
email = request.forms.get('email')
password = request.forms.get('password')
if len(email) <= 3 or len(password) <= 3 or len(name) <= 1:
return redirect('/register')
account_table = taskbook_db.get_table('account')
user = account_table.find_one(email=email)
if not user:
data = dict(email=email, name=name, password=bcrypt.hashpw(str.encode(password), bcrypt.gensalt()), session='')
account_table.insert(data)
return login_account()
return redirect('/register')
@get('/api/session2')
def get_account2():
what = get_account()
print(what)
return what
# ---------------------------
# task REST api
# ---------------------------
@get('/api/tasks')
def get_tasks():
"""return a list of tasks sorted by submit/modify time"""
user = get_account()
if not user:
return False
response.headers['Content-Type'] = 'application/json'
response.headers['Cache-Control'] = 'no-cache'
task_table = taskbook_db.get_table('task')
tasks = [dict(x) for x in task_table.find(email=user["email"], order_by='time')]
for x in tasks:
x.pop('email', None)
return {"tasks": tasks}
@post('/api/tasks')
def create_task():
"""create a new task in the database"""
user = get_account()
if not user:
return False
try:
data = request.json
for key in data.keys():
assert key in ["name", "day", "description", "color", "completed", "date", "time"], f"Illegal key '{key}'"
assert type(data['name']) is str, "name is not a string."
assert len(data['name'].strip()) > 0, "name is length zero."
assert data['day'] in ["today", "tomorrow"], "day must be 'today' or 'tomorrow'"
assert len(data['color']) == 7, "The color must be in the format #XXXXXX"
assert data['color'][0] == '#', "The color must be in the format #XXXXXX"
#assert len(data['date']) == 25, "The date must be in the format yyyy-MM-ddThh:mm"
except Exception as e:
response.status = "400 Bad Request:" + str(e)
return
try:
task_table = taskbook_db.get_table('task')
task_table.insert({
"time": time.time(),
"name": data['name'].strip(),
"description": data['description'].strip(),
"day": data['day'],
"email": user["email"],
"completed": False,
"color": data['color'], #"#ffffff",
"date": data['date']
})
except Exception as e:
response.status = "409 Bad Request:" + str(e)
# return 200 Success
response.headers['Content-Type'] = 'application/json'
return json.dumps({'status': 200, 'success': True})
@put('/api/tasks')
def update_task():
"""update properties of an existing task in the database"""
user = get_account()
if not user:
return False
try:
data = request.json
for key in data.keys():
assert key in ["id", "name", "completed", "day", "color", "description", "subtasks", "time", "date"], f"Illegal key '{key}'"
assert type(data['id']) is int, f"id '{id}' is not int"
if "name" in request:
assert type(data['name']) is str, "name is not a string."
assert len(data['name'].strip()) > 0, "name is length zero."
if "completed" in request:
assert type(data['completed']) is bool, "Completed is not a bool."
if "day" in request:
assert data['day'] in ["today", "tomorrow"], "day must be 'today' or 'tomorrow'"
if "color" in request:
assert len(data['color']) == 7, "The color must be in the format #XXXXXX"
assert data['color'][0] == '#', "The color must be in the format #XXXXXX"
#if "date" in request:
#assert len(data['date']) == 25, "The date must be in the format yyyy-MM-ddThh:mm"
except Exception as e:
response.status = "400 Bad Request:" + str(e)
return
data['email'] = user["email"]
if 'day' in data:
data['time'] = time.time()
try:
task_table = taskbook_db.get_table('task')
task_table.update(row=data, keys=['id'])
except Exception as e:
response.status = "409 Bad Request:" + str(e)
return
# return 200 Success
response.headers['Content-Type'] = 'application/json'
return json.dumps({'status': 200, 'success': True})
@delete('/api/tasks')
def delete_task():
"""delete an existing task in the database"""
user = get_account()
if not user:
return False
try:
data = request.json
assert type(data['id']) is int, f"id '{id}' is not int"
except Exception as e:
response.status = "400 Bad Request:" + str(e)
return
try:
task_table = taskbook_db.get_table('task')
task_table.delete(email=user["email"], id=data['id'])
except Exception as e:
response.status = "409 Bad Request:" + str(e)
return
# return 200 Success
response.headers['Content-Type'] = 'application/json'
return json.dumps({'success': True})
# Serve static files
# THIS ROUTE SHOULD BE THE LAST ONE, AS IT IS A WILDCARD
import os
useCache = False
cached_files = {
}
@get('/<filepath:path>')
def server_static(filepath):
ext = os.path.splitext(filepath)[1]
if ext == '.scss':
response.content_type = "text/css"
if useCache:
if cached_files.get(filepath, False):
return cached_files[filepath]
else:
cached_files[filepath] = compile_sass_file('./public/' + filepath)
return cached_files[filepath]
else:
return compile_sass_file('./public/' + filepath)
return static_file(filepath, root='./public')
import platform
if PYTHONANYWHERE:
application = default_app()
elif platform.node() == 'ubuntu-s-1vcpu-1gb-nyc3-01':
if __name__ == "__main__":
run(host='localhost', port=6590, debug=True)
else:
if __name__ == "__main__":
run(host='0.0.0.0', port=os.environ.get('PORT', 8080), debug=False)