-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
287 lines (255 loc) · 10.8 KB
/
tasks.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
import os
import logging
import pymongo
import datetime
from pyramid.config import Configurator
from pyramid.events import NewRequest
from pyramid.events import subscriber
from pyramid.events import ApplicationCreated
from pyramid.httpexceptions import HTTPFound
from pyramid.session import UnencryptedCookieSessionFactoryConfig
from pyramid.view import view_config
from bson.objectid import ObjectId
from pyramid.security import (
authenticated_userid,
remember,
forget,
)
from wsgiref.simple_server import make_server
from userops import *
logger = logging.getLogger(__file__)
@view_config(route_name='home')
def home_view(request):
logger.info('in home view')
#TODO select a random playlist
#for now redirect to playlists
return HTTPFound(location=request.route_url('playlists'))
#TODO this should be pulled out of the view
@view_config(route_name='playlists', renderer='playlists.mako')
def playlists_view(request):
logger.info("in playlists view")
playlists = get_all_playlists()
loggedin = False
user = authenticated_userid(request)
if user is not None:
loggedin = True
return { "playlists" : playlists, "listname":"Playlists", "loggedin": loggedin}
@view_config(route_name='playlist', renderer='playlist.mako')
def playlist_view(request):
logger.info("in playlist view")
loggedin = False
username = authenticated_userid(request)
userfollows = False
reqname = request.matchdict['name']
if reqname is None:
#TODO what is the proper behavior here?
#probably a redirect + a flash
logger.error('got a playlist request where name is None')
if username:
loggedin = True
userfollows = follows(username, reqname)
articles = get_user_articles(reqname)
return { "articles" : articles, "name" : reqname, "loggedin": loggedin, "userfollows": userfollows, "listname": "list" }
@view_config(route_name='newuser', renderer='newuser.mako')
def new_user_view(request):
logger.info("in new user view")
if request.method == 'POST':
#validate input
newusername = request.POST.get('newusername')
newuserpass = request.POST.get('newuserpass')
if newusername is None or newuserpass is None:
request.session.flash('please fill out all the fields')
elif user_exists(newusername):
request.session.flash('Username already taken :(')
elif not create_new_user(newusername, newuserpass):
request.session.flash('error creating user...strange')
else:
request.session.flash('successfuly created!')
remember(request, newusername)
return HTTPFound(location=request.route_url('edit'))
return {}
@view_config(route_name='edit', renderer="edit.mako")
def edit_view(request):
logger.info('in edit view')
username = authenticated_userid(request)
if not user_exists(username):
return HTTPFound(location=request.route_url('login'))
articles = get_user_articles(username)
if request.method == 'POST':
headline = request.POST.get('linkname')
url = request.POST.get('url')
new_user_article = insert_user_article(username, headline, url)
if new_user_article is None:
request.session.flash('Please enter a valid article')
return HTTPFound(location=request.current_route_url())
else:
#add the article in for displaying to user without a full reload
articles.insert(0,new_user_article)
logger.debug('returning from edit')
return {'name': username, 'articles': articles, "loggedin": True, "userfollows": True, "listname": "edit"}
#TODO here we need to provide better responses back
#how to respond in ajax requests?
#this is the post-only endpoint that should not be linked to, but used for sending data to
@view_config(route_name="followreq", request_method='POST')
def follow_request(request):
logger.info('got a follow request')
username = authenticated_userid(request)
followee = request.POST.get('followee')
if not user_exists(username):
request.session.flash('need to be logged in to follow')
return HTTPFound(location=request.current_route_url())
if not user_exists(followee):
request.session.flash('need to follow an actual user')
return HTTPFound(location=request.current_route_url())
if not new_follow(username, followee):
#TODO better info to user here
request.session.flash('following failed')
return HTTPFound(location=request.current_route_url())
else:
request.session.flash('followed successfully')
#and done!
return HTTPFound(location=request.current_route_url())
#TODO need to pull this into userops
#how to alert users on ajax responses?
#again, only an endpoint
@view_config(route_name='unfollowreq', request_method='POST')
def unfollow_reqeust(request):
logger.info('got an unfollow request')
#get username
username = authenticated_userid(request)
followee = request.POST.get('unfollowee')
if not user_exists(username):
request.session.flash('need to be logged in to unfollow')
return HTTPFound(location=request.current_route_url())
if not user_exists(followee):
request.session.flash('need to to unfollow an actual user')
return HTTPFound(location=request.current_route_url())
if not unfollow(username, followee):
logger.debug('unfollowed failed!')
request.session.flash('unfollowing failed')
return HTTPFound(location=request.current_route_url())
logger.debug('unfollowed successfully!')
return HTTPFound(location=request.current_route_url())
#TODO how to make this more secure?
#deletes an entry from a users list
@view_config(route_name="deleteid", request_method="POST")
def delete_req_id(request):
logger.info("got a delete id request")
username = authenticated_userid(request)
if username is None:
request.session.flash("need to be logged in to edit")
return HTTPFound(location=request.current_route_url())
targetid = request.POST.get("targetid")
if targetid is None:
return HTTPFound(location=request.current_route_url())
logger.debug("calling delete_user_article_by_id")
delete_user_article_by_id(username, targetid)
return HTTPFound(location=request.current_route_url())
#TODO how to make this more secure?
#deletes an entry from a users list
@view_config(route_name="deleteidx", request_method="POST")
def delete_req(request):
logger.info("got a delete idx request")
username = authenticated_userid(request)
if username is None:
request.session.flash("need to be logged in to edit")
return HTTPFound(location=request.current_route_url())
targetidx = request.POST.get("targetidx")
if targetidx is None:
return HTTPFound(location=request.current_route_url())
logger.debug("calling delete_user_article_by_idx")
delete_user_article_by_idx(username, int(targetidx))
return HTTPFound(location=request.current_route_url())
@view_config(route_name='followers', renderer='followers.mako')
def followers_view(request):
logger.info("in followers view")
username = request.matchdict['followee']
if not user_exists(username):
return {'followers': [], 'name': username}
followers = get_user_followers(username)
user = authenticated_userid(request)
loggedin = False
userfollows = False
if user is not None:
loggedin = True
userfollows = follows(user, username)
return {'followers': followers, 'name': username, "listname":"followers", "loggedin": loggedin, "userfollows": userfollows}
@view_config(route_name='following', renderer='following.mako')
def following_view(request):
logger.info("in following view")
username = request.matchdict['follower']
if not user_exists(username):
#TODO need better behavior here probably a redirect
return {'followers': [], 'name': username}
user = authenticated_userid(request)
loggedin = False
userfollows = False
if user is not None:
loggedin = True
userfollows = follows(user, username)
following = get_user_following(username)
return {'following': following, 'name': username, "listname": "followees", "loggedin": loggedin, "userfollows": userfollows}
@view_config(route_name='feed', renderer='feed.mako')
def feed_view(request):
logger.info('in feed view')
feeduser = request.matchdict['name']
if not user_exists(feeduser):
request.session.flash('need to look at a real users feed')
return HTTPFound(location=request.route_url('/people'))
user = authenticated_userid(request)
loggedin = False
userfollows = False
if user is not None:
loggedin = True
userfollows = follows(user, feeduser)
selections = generate_feed(feeduser)
return {"selections": selections, "name": feeduser, "listname": "feed", "loggedin": loggedin, "userfollows": userfollows}
#NOTE: assumes feeduser is a real user
def generate_feed(feeduser):
#first get all people they are following
following = get_user_following(feeduser)
#create a list of all the articles from all those users
selections = []
for user in following:
articles = get_user_articles(user['username'])
for article in articles:
article['author'] = user['username']
selections = selections + articles
#sort the list
selections.sort(key=lambda selection: selection['timestamp'])
selections.reverse()
return selections
@view_config(route_name='logout')
def logout_view(request):
logger.info('in logout view')
user = authenticated_userid(request)
if user is None:
request.session.flash("need to be logged in to log out, lol")
else:
forget(request)
return HTTPFound(location=request.route_url('playlists'))
@view_config(route_name='login', renderer='login.mako')
def login_view(request):
logger.info('in login view')
#get login info
if request.method == 'POST':
#validate input
username = request.POST.get('username')
password = request.POST.get('password')
if username is not None and password is not None:
#check authentication
check = credcheck(username, password)
if check is True:
#redirect to edit page
headers = remember(request, username)
return HTTPFound(location=request.route_url('edit'), headers=headers)
else:
#fail and flash retry
request.session.flash('Please enter valid credentials')
else:
request.session.flash('Please fill in all the fields')
return {}
@view_config(context='pyramid.exceptions.NotFound', renderer='notfound.mako')
def notfound_view(request):
request.response.status = '404 Not Found'
return {}