-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
197 lines (171 loc) · 6.89 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
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
from tornado.ncss import Server
from re_template_renderer import render_template
#uncomment later when DB is fixed
from db.plutonium import User,Post,Comment,database_connect
#to create street.db, double click create_db.py
###DECORATORS###
def loginRequired(fn):
def inner(response, *args, **kwargs):
user = response.get_secure_cookie('userCookie')
if user is None:
response.redirect('/login')
else:
return fn(response, *args, **kwargs)
return inner
def notLoginRequired(fn):
def inner(response, *args, **kwargs):
user = response.get_secure_cookie('userCookie')
if user is None:
return fn(response, *args, **kwargs)
else:
response.redirect('/home')
return inner
###REGULAR REFERENCE FUNCTIONS###
def home(response):
user = get_current_user(response)
posts = Post.get_by_recent(10)
print(posts)
html = render_template('main.html', {'user': user, 'posts': posts})
response.write(html)
def login_handler(response):
email = response.get_field("email")
password = response.get_field("password")
try:
user = User.login(email, password)
response.set_secure_cookie('userCookie', email)
response.redirect('/home')
except ValueError:
user = get_current_user(response)
html = render_template('login.html', {'user': user, 'invalidUser': "Invalid login." })
response.write(html)
def signup_handler(response):
name = response.get_field('name', '')
email = response.get_field('email', '')
password = response.get_field('password', '')
confpassword = response.get_field('confpassword', '')
usr_description = response.get_field('usr_describe', '')
#when done with mvp keep entered fields
if (not name) or (not email) or (not password) or (not confpassword):
user = get_current_user(response)
html = render_template('signup.html', {'user': user, 'errorMessage': "You must fill in all fields. Please try again." })
response.write(html)
elif password != confpassword:
user = get_current_user(response)
html = render_template('signup.html', {'user': user, 'errorMessage': "Password did not match. Please try again." })
response.write(html)
elif len(password) < 6:
user = get_current_user(response)
html = render_template('signup.html', {'user': user, 'errorMessage': "Password must be longer than 6 characters. Please try again." })
response.write(html)
elif password.lower() == 'password':
user = get_current_user(response)
html = render_template('signup.html', {'user': user, 'errorMessage': "'Password' is far to weak. Please submit a different password." })
response.write(html)
else:
try:
user = User.register(email,password,name,usr_description)
response.set_secure_cookie('userCookie', email)
response.redirect('/home')
except ValueError as e:
user = get_current_user(response)
html = render_template('signup.html', {'user': user, 'errorMessage': str(e) })
response.write(html)
def profile(response,name):
user = get_current_user(response)
print(user)
print(user.profile_picture)
html = render_template('profile.html', {'user': user})
response.write(html)
def get_current_user(response):
email = response.get_secure_cookie("userCookie") #change back to User(), later when DB is fixed
if email is not None:
email = email.decode()
user = User.get(email)
return user
return None
@loginRequired
def view_post(response, post_id):
try:
post = Post.get(post_id)
user = get_current_user(response)
comments = Post.comments(post_id)
for c in comments:
c.author = User.get_by_id(c.author)
poster = User.get_by_id(post.author_id)
html = render_template('content.html', {'user': user,'post':post,'poster':poster, 'comments': comments})
response.write(html)
except Exception as e:
print(e)
user = get_current_user(response)
html = render_template('404errorpage.html', {'user': user})
response.write(html)
def demo(response):
user = get_current_user(response)
html = render_template('demo.html', {'user': user, 'comments':['great', 'meh']})
response.write(html)
def notfound(response):
user = get_current_user(response)
html = render_template('404errorpage.html', {'user': user})
response.write(html)
###NOT LOGGED IN EXCLUSIVE PAGES###
@notLoginRequired
def login(response):
user = get_current_user(response)
html = render_template('login.html', {'user': user})
response.write(html)
@notLoginRequired
def signup(response):
user = get_current_user(response)
html = render_template('signup.html', {'user': user})
response.write(html)
###LOGIN EXCLUSIVE PAGES###
@loginRequired
def submit(response):
user = get_current_user(response)
html = render_template('new_post.html', {'user': user})
response.write(html)
@loginRequired
def logout(response):
response.clear_cookie("userCookie")
response.redirect('/home')
@loginRequired
def profile(response,name):
user = get_current_user(response)
posts = User.get_posts(user.user_id)
html = render_template('profile.html', {'user': user, 'posts': posts})
response.write(html)
@loginRequired
def submit_handler(response):
user = get_current_user(response)
title = response.get_field("title")
location = response.get_field("location")
image = response.get_file("postImage")
description = response.get_field("description")
if (not title) or (not location) or (not image) or (not description) or (image[1] is None):
html = render_template('new_post.html', {'user': user, 'invalidPost': "Please fill in all fields." })
response.write(html)
else:
pictureName = 'static/postimages/'+title+'.'+image[1].split('/')[1]
with open(pictureName,'wb') as pictureFile:
pictureFile.write(image[2])
createPost = Post.create(user.user_id,title,description,pictureName,location)
response.redirect("/post/"+str(createPost.id))
@loginRequired
def comment_post(response, post_id):
user = get_current_user(response)
comment = response.get_field("comment")
if comment:
comment = Comment.create(user.user_id, post_id, comment)
response.redirect('/post/' + post_id)
database_connect('db/street.db')
server = Server()
server.register(r'/?(?:home)?', home)
server.register(r'/profile(?:(?:/([\w\.\-]+)?)|/?)', profile)
server.register(r'/login', login, post=login_handler)
server.register(r'/signup',signup, post=signup_handler)
server.register(r'/post/(\d+)', view_post, post=comment_post)
server.register(r'/submit',submit, post=submit_handler)
server.register(r'/demo',demo)
server.register(r'/logout',logout)
server.register(r'.+',notfound)
server.run()