-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler_story.py
84 lines (61 loc) · 2.69 KB
/
handler_story.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
import template
from dbapi.user import User
from dbapi.story import Story
from dbapi.rules import Rules
def view_story(response, id):
user = response.get_secure_cookie('username')
story = Story.find('id', id)
author = User.find('id', story[0].author_id)
if user is not None:
context = {'current_user':User.find('username', str(user, 'utf-8'))[0], 'story':story[0], 'author':author[0]}
else:
context = {'current_user':None, 'story':story[0], 'author':author[0]}
html = template.render_file('templates/viewingstory.html', context)
response.write(html)
def view_story_list(response):
user = response.get_secure_cookie('username')
stories = Story.find('all', '')
if user is not None:
context = {'current_user':User.find('username', str(user, 'utf-8'))[0], 'stories':stories}
else:
context = {'current_user':None, 'stories':stories}
html = template.render_file('templates/storylist.html', context)
response.write(html)
def add_to_story(response, id):
username = response.get_secure_cookie('username')
user = User.find('username', str(username, 'utf-8'))
if not user:
raise Exception("Expected user account when adding to story")
user = user[0]
addition_to_story = response.get_argument('paragraph')
story = Story.find('id', id)[0]
if not Rules.check(addition_to_story, story.id):
#reject the story
print('Rejected')
#return to story view without updating.... TODO: show error
response.redirect('/view_story/{}'.format(id))
return
added_paragraph = story.add_paragraph(user, addition_to_story)
added_paragraph.save()
response.redirect('/view_story/{}'.format(id))
#html = template.render_file('templates/viewingstory.html', context)
#response.write(html)
def process_new_story(response):
username = response.get_secure_cookie('username')
user = User.find('username', str(username, 'utf-8'))[0]
title = response.get_argument('title')
story_text = response.get_argument('story')
rule = response.get_argument('rule')
comment = response.get_argument('comment')
story = Story.create(user, title, comment)
story.save()
story.add_paragraph(user, story_text).save()
response.redirect('/view_story/{}'.format(story.id))
def new_story(response):
user = response.get_secure_cookie('username')
if user is not None:
context = {'current_user':User.find('username', str(user, 'utf-8'))[0]}
else:
context = {'current_user':None}
html = template.render_file('templates/newstory.html', context)
response.write(html)