-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
139 lines (106 loc) · 3.85 KB
/
server.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
from tornado.ncss import Server, ncssbook_log
import tornadotesting
from template import render_template
from db import User, Squad
from datetime import date
def render_file(filename, context):
"""
Reads a template file and pases it through the template engine.
>>> render_file('test-render-file.html', {"name":"sandy"})
'My name is sandy'
"""
with open("templates/" + filename) as f:
template = f.read()
return render_template(template, context)
def view_profile(request, username):
"""
>>> html = tornadotesting.run(view_profile, 'alice')
>>> assert "James" in html, html
>>> assert "42" in html, html
>>> assert "Sydney" in html, html
"""
user = User.get_by_username(username)
#Age code for when DB sends an actual date object
#age = date.today().year - user.birthdate.year
context = {'username':user.username, 'age':'42', 'loc':user.location, 'description':user.description }
request.write(render_file('profile.html', context))
def create_profile_page(request):
"""
>>> html = tornadotesting.run(create_profile_page)
>>> assert "description" in html, html
>>> assert "submit" in html, html
"""
context={}
request.write(render_file("test-register.html", context))
def create_profile(request):
"""
>>> tornadotesting.run(create_profile, fields={'username': 'alice'})
'You created a user called alice'
"""
user_data = {}
user_data["username"] = request.get_field("username")
user_data["password"] = request.get_field("password")
user_data["description"] = request.get_field("description")
user_data["location"] = request.get_field("location")
user_data["birthdate"] = request.get_field("birthdate")
user_data["image"] = request.get_field("image")
#print(user_data)
user = User.create(**user_data)
request.write("You created a user called {}".format(user.username))
def list_squads(request):
"""
>>> tornadotesting.run(list_squads)
'aaa'
"""
all_squads = Squad.get_all()
names = []
for squad in all_squads:
names.append(squad.name)
request.write(','.join(names))
def view_squad(request, name):
"""
>>> tornadotesting.run(view_squad, 'ateam')
'This is the squad page for ateam'
"""
request.write('This is the squad page for {}'.format(name))
def show_create_squad_page(request):
"""
>>> tornadotesting.run(show_create_squad_page)
'This page creates a "create a squad" form'
"""
request.write('This page creates a "create a squad" form')
def create_squad(request):
"""
>>> tornadotesting.run(create_squad)
'This page creates a squad'
"""
request.write('This page creates a squad')
def accept_squad_member(request, name):
"""
>>> tornadotesting.run(accept_squad_member, 'ateam')
'This page accepts ateam'
"""
request.write('This page accepts {}'.format(name))
def reject_squad_member(request, name):
"""
>>> tornadotesting.run(reject_squad_member, 'ateam')
'This page reject ateam'
"""
request.write('This page reject {}'.format(name))
def apply_to_squad(request, name):
"""
>>> tornadotesting.run(apply_to_squad, 'ateam')
'This the page to apply to join ateam'
"""
request.write('This the page to apply to join {}'.format(name))
server = Server()
server.register(r'/profiles/([a-z]+)/', view_profile)
server.register(r'/register/', create_profile_page, post=create_profile)
server.register(r'/squads/', list_squads, post=create_squad)
server.register(r'/squads/([a-z]+)/', view_squad)
server.register(r'/create-squad/', show_create_squad_page)
server.register(r'/squads/([a-z]+)/accept/', accept_squad_member)
server.register(r'/squads/([a-z]+)/reject/', reject_squad_member)
server.register(r'/squads/([a-z]+)/apply/', apply_to_squad)
if __name__ == '__main__':
server.run()