-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
248 lines (185 loc) · 6.06 KB
/
views.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
import webapp2
#import urllib
from utils import *
from models import User, Post
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
class BaseHandler(webapp2.RequestHandler):
"""
Base class for view functions, which provides basic rendering
funtionalities
"""
def render(self, template, **kw):
"""
Render a template with the given keyword arguments
"""
self.response.out.write(render_str(template, **kw))
def set_secure_cookie(self, name, val):
"""
Set an encrypted cookie on client's machine
"""
cookie_val = make_secure_val(val)
self.response.headers.add_header(
'Set-Cookie',
'%s=%s; Path=/' % (name, cookie_val)
)
def read_secure_cookie(self, name):
"""
Read a cookie and check it's integrity
"""
cookie_val = self.request.cookies.get(name)
return cookie_val and check_secure_val(cookie_val)
def initialize(self, *a, **kw):
"""
Override the constuctor for adding user information
when a request comes
"""
webapp2.RequestHandler.initialize(self, *a, **kw)
user_id = self.read_secure_cookie('user')
self.user = user_id and User.get_by_id(int(user_id))
class Home(BaseHandler):
"""
Render the homepage.
"""
def get(self):
"""
For a GET request, render the homepage.
"""
self.render("home.html", user=self.user, permalink=False)
class downloadVideo(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, post_id):
post = Post.get_by_id(int(post_id))
#resource = Post.post.blobKey
resource = post.blobKey
#resource = str(urllib.unquote(resource))
blob_info = blobstore.BlobInfo.get(resource)
self.send_blob(blob_info)
class Permalink(BaseHandler):
"""
Handle permalink for the posts
"""
def get(self, post_id):
"""
For a GET request with a valid post_id, return the post
Else, return a 404 error
"""
post = Post.get_by_id(int(post_id))
if post:
self.render("dashboard.html", user=self.user, posts=[post], video="/download/"+post_id, permalink=True)
else:
self.abort(404)
class Login(BaseHandler):
"""
Handles the login page
"""
def get(self):
"""
For a GET request, render the login page
"""
user = self.user
if user:
self.redirect('/dashboard')
self.render("login.html", user=user)
def post(self):
"""
For a POST request, perform the login. If successful, redirect
to homepage
"""
username = self.request.get('username')
password = self.request.get('password')
try:
user_id = User.authenticate(username, password)
self.set_secure_cookie('user', str(user_id))
self.redirect('/dashboard')
except Exception, e:
self.render("login.html", user=self.user, error = e)
class Signup(BaseHandler):
"""
Signup a new user
"""
def get(self):
"""
Render the signup page
"""
user = self.user
if user:
self.redirect('/')
self.render('signup.html', user=user)
def post(self):
"""
Create a new user, and redirect to homepage
"""
username = self.request.get('username')
password = self.request.get('password')
group = self.request.get('group')
college = self.request.get('college')
try:
user = User.create_user(group, college, username, password)
self.render("login.html",
success="Great! You are registered! Please log in.", user=user)
except Exception, e:
self.render("signup.html", user=self.user, error=e)
class Dashboard(BaseHandler):
"""
Handle the dashboard NOT COMPLETE
"""
def get(self):
"""
For a GET request, return the homepage
"""
top_posts = Post.top_posts()
##if not self.user:
## self.redirect('/login')
#try:
self.render("dashboard.html", posts=top_posts, user=self.user, permalink=False)
#except Exception, e:
# self.redirect('/dashboard')
class NewPost(BaseHandler, blobstore_handlers.BlobstoreUploadHandler):
"""
Handles all the new video posts
"""
def get(self):
"""
Render the form for adding new post
"""
if not self.user:
self.redirect('/login')
upload_url = blobstore.create_upload_url('/upload')
self.render("newpost.html", uploads=upload_url, user=self.user)
class UploadHandler(BaseHandler, blobstore_handlers.BlobstoreUploadHandler):
"""
Handles the upload part
"""
def post(self):
"""
Add a new post, and redirect to the permalink page
"""
if not self.user:
self.abort(403)
upload_files = self.get_uploads('file') # 'file' is file upload field in the form
blob_info = upload_files[0]
# if(blob_info):
# self.redirect('/dashboard/%s' % blob_info.key())
caption = self.request.get('caption')
blobKey = blob_info.key()
user = self.user
#author = self.request.get('group')
#college = self.request.get('college')
try:
post_id = Post.create_new(caption=caption, blobKey=blobKey, user=user)
self.redirect('/post/%s' % post_id)
except Exception, e:
self.render('newpost.html',
post={'caption' : caption, 'blobKey' : blobKey},
user=self.user,
error=e)
class Logout(BaseHandler):
"""
Log out a user
"""
def get(self):
"""
Log out the user and redirect her to homepage
"""
self.set_secure_cookie('user', '')
self.redirect('/')