-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.py
602 lines (514 loc) · 22.9 KB
/
page.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
import web
import os
import simplejson as json
import utils
import time
import textile
import re
from datetime import datetime
import logging
import pytc
urls = (
'/new_page','new_page',
'/([\w|-]+)/copy', 'copy',
'/([\w|-]+)/share', 'share',
'/([\w|-]+)/delete', 'delete',
'/([\w|-]+)/rename', 'rename',
'/([\w|-]+)/(\d+)/reorder', 'reorder_component',
'/([\w|-]+)/reorder', 'reorder',
'/([\w|-]+)/(\d+)/edit/(\d+)', 'edit_list_item',
'/([\w|-]+)/(\d+)/edit', 'edit',
'/([\w|-]+)/(\d+)/remove/(\d+)', 'remove_list_item',
'/([\w|-]+)/(\d+)/remove', 'remove',
'/([\w|-]+)/(\d+)', 'get_component',
'/([\w|-]+)/(\d+)/change/(\d+)', 'change',
'/([\w|-]+)/new', 'new_component',
'/([\w|-]+)/(\d+)/clear', 'clear_completed',
'/([\w|-]+)', 'index'
)
app_page = web.application(urls, globals())
class index:
"""Return the obj for this page"""
def GET(self,page_name):
access = utils.page_access(page_name)
if access is not None: return access
content = utils.fetch_file(page_name)
## We have to textile some fields
try:
obj = json.loads(content)
for component in obj["components"]:
if obj["components"][component]["type"] == "note":
obj["components"][component]["description"] = textile.textile(obj["components"][component]["description"])
elif obj["components"][component]["type"] == "list":
if obj["components"][component].has_key("completed"):
for c in obj["components"][component]["completed"]:
date = datetime.fromtimestamp(obj["components"][component]["completed"][c]["completed"])
obj["components"][component]["completed"][c]["completed"] = date.strftime("%b %d")
return utils.callback(json.dumps(obj))
except:
utils.handle_error("failed to read file")
class share:
def GET(self,page_name):
data = web.input(email=None, status="public")
access = utils.page_access(page_name)
if access is not None: return access
db = pytc.HDB()
db.open(utils.DBNAME, pytc.HDBOWRITER | pytc.HDBOCREAT)
if data.email is not None:
email = ""
else:
if data.status == "public":
db.put(page_name + ":public","1")
return utils.callback('{ "status":"public"}')
else:
if db.has_key(page_name + ":public"):
db.out(page_name + ":public")
return utils.callback('{ "status":"private"}')
class delete:
def GET(self,page_name):
referer = web.ctx.env.get('HTTP_REFERER', '/')
access = utils.page_access(page_name,utils.PERM_WRITE,referer)
if access is not None: return access
if utils.delete_page(page_name) is not None:
raise web.seeother('http://%s/' % (web.ctx.get('host')))
else:
return 'Failed to delete page'
class copy:
def GET(self,page_name):
access = utils.page_access(page_name)
if access is not None: return access
data = web.input(new_name="Default Page")
page_name = utils.create_page(data.new_name,page_name)
if page_name is None:
return 'FATAL'
else:
raise web.seeother('http://%s/%s/test.html' % (web.ctx.get('host'),page_name))
class new_page:
def POST(self):
session = web.config._session
if session is None:
raise web.seeother('http://%s/login' % (web.ctx.get('host')))
if session.userid is None:
raise web.seeother('http://%s/login' % (web.ctx.get('host')))
data = web.input(page_name="")
page_name = utils.create_page(data.page_name)
if page_name is None:
return 'FATAL'
else:
raise web.seeother('http://%s/%s/test.html' % (web.ctx.get('host'),page_name))
class new_component:
def GET(self,page_name):
access = utils.page_access(page_name,utils.PERM_WRITE)
if access is not None: return access
data = web.input(title="",description="",type="",list_id="",after=0)
try:
content = utils.fetch_file(page_name)
try:
obj = json.loads(content)
## list
if (data.type == "list"):
component = {}
## Move the last id pointer one
last_id = obj["last_id"]
last_id = last_id + 1
obj["last_id"] = last_id
component["type"] = "list"
component["title"] = data.title;
## Add to order
current_order = obj["order"]
if current_order != "":
current_order = obj['order'].split(',')
new_order = []
## Put it after
if (int(data.after) == 0):
new_order.append(str(last_id))
for o in current_order:
if (int(o) == int(data.after)):
new_order.append(o)
new_order.append(str(last_id))
else:
new_order.append(o)
else:
new_order = [str(last_id)]
obj["components"][str(last_id)] = component
obj["order"] = ",".join(new_order)
## Save
try:
utils.save_file(page_name,json.dumps(obj))
component["id"] = last_id
return utils.callback(json.dumps(component))
except IOError:
utils.handle_error("failed to save file")
## Divider
elif (data.type == "divider"):
component = {}
## Move the last id pointer one
last_id = obj["last_id"]
last_id = last_id + 1
obj["last_id"] = last_id
component["type"] = "divider"
component["title"] = data.title;
## Add to order
current_order = obj["order"]
if current_order != "":
current_order = obj['order'].split(',')
new_order = []
## Put it after
if (int(data.after) == 0):
new_order.append(str(last_id))
for o in current_order:
if (int(o) == int(data.after)):
new_order.append(o)
new_order.append(str(last_id))
else:
new_order.append(o)
else:
new_order = [str(last_id)]
obj["components"][str(last_id)] = component
obj["order"] = ",".join(new_order)
## Save
try:
utils.save_file(page_name,json.dumps(obj))
component["id"] = last_id
return utils.callback(json.dumps(component))
except IOError:
utils.handle_error("failed to save file")
## Note
elif (data.type == "note"):
component = {}
## Move the last id pointer one
last_id = obj["last_id"]
last_id = last_id + 1
obj["last_id"] = last_id
component["type"] = "note"
component["title"] = data.title;
component["description"] = data.description
## Add to order
current_order = obj["order"]
if current_order != "":
current_order = obj['order'].split(',')
new_order = []
if (int(data.after) == 0):
new_order.append(str(last_id))
## Put it after
for o in current_order:
if (int(o) == int(data.after)):
new_order.append(o)
new_order.append(str(last_id))
else:
new_order.append(o)
else:
new_order = [str(last_id)]
obj["components"][str(last_id)] = component
obj["order"] = ",".join(new_order)
## Save
try:
utils.save_file(page_name,json.dumps(obj))
component["id"] = last_id
component["description"] = textile.textile(component["description"])
return utils.callback(json.dumps(component))
except IOError:
utils.handle_error("failed to save file")
elif (data.type == "list_item"):
component = obj["components"][data.list_id]
# Fine the list and find the next id in the order
if component.has_key("order"):
current_order = component["order"]
else:
current_order = ""
if component.has_key("last_id"):
last_id = int(component["last_id"])
else:
last_id = 0
if current_order != "":
current_order = component['order'].split(',')
else:
current_order = []
last_id = int(last_id) + 1
component["last_id"] = last_id
if component.has_key("items"):
component["items"][last_id] = {"title" : data.title}
else:
component["items"] = {}
component["items"][last_id] = {"title" : data.title}
current_order.append(str(last_id))
component["order"] = ",".join(current_order)
## Save
try:
utils.save_file(page_name,json.dumps(obj))
component["items"][last_id]["order"] = ",".join(current_order)
component["items"][last_id]["id"] = data.list_id
component["items"][last_id]["item_id"] = last_id
return utils.callback(json.dumps(component["items"][last_id]))
except IOError:
utils.handle_error("failed to save file")
except:
utils.handle_error("failed to read file")
except IOError:
utils.handle_error("file not found")
class get_component:
def GET(self,page_name,component):
access = utils.page_access(page_name)
if access is not None: return access
try:
content = utils.fetch_file(page_name)
## We have to textile some fields
try:
obj = json.loads(content)
return utils.callback(json.dumps(obj["components"][component]))
except:
utils.handle_error("failed to read file")
except IOError:
utils.handle_error("file not found")
class edit_list_item:
def GET(self,page_name,id,list_id):
access = utils.page_access(page_name,utils.PERM_WRITE)
if access is not None: return access
data = web.input(title="")
try:
content = utils.fetch_file(page_name)
try:
obj = json.loads(content)
obj["components"][id]["items"][list_id]["title"] = data.title
try:
utils.save_file(page_name,json.dumps(obj))
component = obj["components"][id]["items"][list_id]
component["id"] = id;
component["list_item_id"] = list_id
return utils.callback(json.dumps(component))
except IOError:
utils.handle_error("failed to save file")
except:
utils.handle_error("failed to read file")
except IOError:
utils.handle_error("file not found")
class clear_completed:
def GET(self,page_name,id):
access = utils.page_access(page_name,utils.PERM_WRITE)
if access is not None: return access
try:
content = utils.fetch_file(page_name)
try:
obj = json.loads(content)
if obj["components"][id].has_key("completed"):
del obj["components"][id]["completed"]
else:
return "{'error': 'no completed items'}"
try:
utils.save_file(page_name,json.dumps(obj))
return utils.callback("{'id': " + id + " }")
except IOError:
utils.handle_error("failed to save file")
except:
utils.handle_error("failed to read file")
except IOError:
utils.handle_error("file not found")
class change:
def GET(self,page_name,id,list_item_id):
access = utils.page_access(page_name,utils.PERM_WRITE)
if access is not None: return access
data = web.input(status="")
try:
content = utils.fetch_file(page_name)
try:
obj = json.loads(content)
if data.status == "completed":
item = obj["components"][id]["items"][list_item_id]
# remove the item from items and put in completed
if obj["components"][id].has_key("order"):
current_order = obj["components"][id]['order'].split(',')
else:
current_order = ""
# remove from the order
x = 0
for i in current_order:
if int(i) == int(list_item_id):
del current_order[x]
x = x + 1
current_order = ",".join(current_order)
obj["components"][id]['order'] = current_order
item["completed"] = time.time()
if obj["components"][id].has_key("completed"):
obj["components"][id]["completed"][list_item_id] = item
else:
obj["components"][id]["completed"] = {}
obj["components"][id]["completed"][list_item_id] = item
del obj["components"][id]["items"][list_item_id]
else:
item = obj["components"][id]["completed"][list_item_id]
del item["completed"]
current_order = obj["components"][id]['order'].split(',')
current_order.append(list_item_id)
current_order = ",".join(current_order)
## How does this happen, an extra comma?
current_order = current_order.strip(",")
obj["components"][id]['order'] = current_order
obj["components"][id]["items"][list_item_id] = item
del obj["components"][id]["completed"][list_item_id]
try:
utils.save_file(page_name,json.dumps(obj))
item["id"] = id
item["list_item_id"] = list_item_id
item["status"] = data.status
if item.has_key("completed"):
date = datetime.fromtimestamp(item["completed"])
item["completed"] = date.strftime("%b %d")
return utils.callback(json.dumps(item))
except IOError:
utils.handle_error("failed to save file")
except:
utils.handle_error("failed to read file")
except IOError:
utils.handle_error("file not found")
class remove_list_item:
def GET(self,page_name,id,list_item_id):
access = utils.page_access(page_name,utils.PERM_WRITE)
if access is not None: return access
try:
content = utils.fetch_file(page_name)
try:
obj = json.loads(content)
component = obj["components"][id]
## Check items first
if component["items"].has_key(list_item_id):
del component["items"][list_item_id]
## Remove from order
if component['order'] != "":
current_order = component['order'].split(',')
else:
current_order = []
x = 0
for i in current_order:
if int(i) == int(list_item_id):
del current_order[x]
x = x + 1
current_order = ",".join(current_order)
component['order'] = current_order
else:
## In the completed section
del component["completed"][list_item_id]
## We don't need to remove from order since it's in completed
try:
utils.save_file(page_name,json.dumps(obj))
return utils.callback('{"id":"' + id + '", "list_item_id":"' + list_item_id +'"}')
except IOError:
utils.handle_error("failed to save file")
except:
utils.handle_error("failed to read file")
except IOError:
utils.handle_error("file not found")
class remove:
def GET(self,page_name,id):
access = utils.page_access(page_name,utils.PERM_WRITE)
if access is not None: return access
try:
content = utils.fetch_file(page_name)
try:
obj = json.loads(content)
if obj["components"].has_key(id):
type = obj["components"][id]["type"]
del obj["components"][id]
## Remove from order
current_order = obj['order'].split(',')
x = 0
for i in current_order:
if int(i) == int(id):
del current_order[x]
x = x + 1
current_order = ",".join(current_order)
obj['order'] = current_order
try:
utils.save_file(page_name,json.dumps(obj))
return utils.callback('{"id":"' + id + '", "type":"' + type +'"}')
except IOError:
utils.handle_error("failed to save file")
else:
utils.handle_error("key not found")
except:
utils.handle_error("failed to read file")
except IOError:
utils.handle_error("file not found")
class edit:
def GET(self,page_name,id):
access = utils.page_access(page_name,utils.PERM_WRITE)
if access is not None: return access
data = web.input(title="",description="")
try:
content = utils.fetch_file(page_name)
try:
obj = json.loads(content)
if obj["components"].has_key(id):
obj["components"][id]["title"] = data.title;
if ( obj["components"][id]["type"] == "note"):
obj["components"][id]["description"] = data.description
try:
utils.save_file(page_name,json.dumps(obj))
if obj["components"][id]["type"] == "note":
obj["components"][id]["description"] = textile.textile(obj["components"][id]["description"])
obj["components"][id]["id"] = id
return utils.callback(json.dumps(obj["components"][id]))
except IOError:
utils.handle_error("failed to save file")
except:
utils.handle_error("failed to read file")
except IOError:
utils.handle_error("file not found")
class reorder_component:
def GET(self,page_name,component):
access = utils.page_access(page_name,utils.PERM_WRITE)
if access is not None: return access
data = web.input(order="")
try:
content = utils.fetch_file(page_name)
try:
obj = json.loads(content)
obj["components"][component]["order"] = data.order
try:
utils.save_file(page_name,json.dumps(obj))
return utils.callback('{"success":"1"}')
except IOError:
utils.handle_error("failed to save file")
except:
utils.handle_error("failed to read file")
except IOError:
utils.handle_error("file not found")
class reorder:
def GET(self,page_name):
access = utils.page_access(page_name,utils.PERM_WRITE)
if access is not None: return access
data = web.input(order="")
try:
content = utils.fetch_file(page_name)
try:
obj = json.loads(content)
obj["order"] = data.order
try:
utils.save_file(page_name,json.dumps(obj))
return utils.callback('{"success":"1"}')
except IOError:
utils.handle_error("failed to save file")
except:
utils.handle_error("failed to read file")
except IOError:
utils.handle_error("file not found")
class rename:
def GET(self,page_name):
access = utils.page_access(page_name,utils.PERM_WRITE)
if access is not None: return access
data = web.input(new_name="")
## Fix name
try:
content = utils.fetch_file(page_name)
try:
obj = json.loads(content)
obj["name"] = data.new_name
try:
utils.save_file(page_name,json.dumps(obj))
new_name = { 'name' : data.new_name }
return utils.callback(json.dumps(new_name))
except IOError:
utils.handle_error("failed to save file")
except:
utils.handle_error("failed to read file")
except IOError:
utils.handle_error("file not found")