-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
308 lines (247 loc) · 9.5 KB
/
Copy pathviews.py
File metadata and controls
308 lines (247 loc) · 9.5 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/python
# -*- coding: utf-8 -*-
from app import app, db
from flask import Flask, render_template, request, jsonify, session, redirect, url_for, flash
from twitter_lib import TwitterClient
from newsapi import _news
import requests
from reduction import reduce
from models import User, news_data, user_vote_data,Post
@app.route('/')
def main():
return render_template('index.html', title='Index')
# This is the login route
@app.route('/login', methods=['GET', 'POST'])
def login():
if session.get('logged_in'):
return redirect(url_for('main'))
error = None
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = User.query.filter_by(username=username).first()
if user:
if user.verify_password(password):
session['logged_in'] = True
session['uid'] = user.id
session['username'] = user.username
check_user = Post.query.filter_by(user_id=username).first()
user_id=username
upvotes=0
downvotes=0
check_votes=0
if not check_user:
for i in xrange(1,10):
news_id='G'+str(i)
c = Post(user_id, upvotes, downvotes, news_id, check_votes)
db.session.add(c)
for i in xrange(1,10):
news_id='S'+str(i)
c = Post(user_id, upvotes, downvotes, news_id, check_votes)
db.session.add(c)
for i in xrange(1,10):
news_id='B'+str(i)
c = Post(user_id, upvotes, downvotes, news_id, check_votes)
db.session.add(c)
for i in xrange(1,10):
news_id='T'+str(i)
c = Post(user_id, upvotes, downvotes, news_id, check_votes)
db.session.add(c)
for i in xrange(1,10):
news_id='X'+str(i)
c = Post(user_id, upvotes, downvotes, news_id, check_votes)
db.session.add(c)
for i in xrange(1,10):
news_id='Y'+str(i)
c = Post(user_id, upvotes, downvotes, news_id, check_votes)
db.session.add(c)
db.session.commit()
flash('You are successfully logged in!')
return redirect(url_for('main'))
error = 'Invalid credentials!'
return render_template('login.html', error=error)
# This is the signup route
@app.route('/register', methods=['GET', 'POST'])
def register():
if session.get('logged_in'):
return redirect(url_for('dash'))
error = None
user = None
if request.method == 'POST':
username = request.form['username']
email = request.form['email']
password = request.form['password']
confirm_pass = request.form['confirm_pass']
user = User.query.filter_by(username=username).first()
if user:
error = 'Username already used.'
else:
user = User.query.filter_by(email=email).first()
if user:
error = 'Email already used.'
elif password != confirm_pass:
error = "Passwords don't match."
else:
u = User(username, password, email)
db.session.add(u)
db.session.commit()
flash('Registration successfull')
return redirect(url_for('register'))
return render_template('signup.html', error=error)
@app.route('/forgot')
def forgot():
return 'forgot'
@app.route('/settings')
def settings():
return 'settings'
@app.route('/api/general')
def general():
news_obj = _news()
news_dict = news_obj.get_json('the-telegraph')
for news in news_dict['articles']:
author = news['author']
title = news['title']
desc = news['description']
url = news['url']
urlToImage = news['urlToImage']
publishedAt = news['publishedAt']
category = 'general'
news_obj = news_data.query.filter_by(title=title).first()
if not news_obj:
news_obj = news_data(author,title,desc,url,urlToImage,publishedAt, category)
db.session.add(news_obj)
db.session.commit()
news_obj = news_data.query.filter_by(category='general').all()
return render_template('general.html', news_dict=news_dict,news_obj=news_obj)
@app.route('/api/sports')
def sports():
news_obj = _news()
news_dict = news_obj.get_json('fox-sports')
for news in news_dict['articles']:
author = news['author']
title = news['title']
desc = news['description']
url = news['url']
urlToImage = news['urlToImage']
publishedAt = news['publishedAt']
category = 'sports'
news_obj = news_data.query.filter_by(title=title).first()
if not news_obj:
news_obj = news_data(author,title,desc,url,urlToImage,publishedAt, category)
db.session.add(news_obj)
db.session.commit()
return render_template('sports.html', news_dict=news_dict)
@app.route('/api/business')
def business():
news_obj = _news()
news_dict = news_obj.get_json('business-insider')
for news in news_dict['articles']:
author = news['author']
title = news['title']
desc = news['description']
url = news['url']
urlToImage = news['urlToImage']
publishedAt = news['publishedAt']
category = 'business'
news_obj = news_data.query.filter_by(title=title).first()
if not news_obj:
news_obj = news_data(author,title,desc,url,urlToImage,publishedAt, category)
db.session.add(news_obj)
db.session.commit()
return render_template('bussiness.html', news_dict=news_dict)
@app.route('/api/technology')
def technology():
news_obj = _news()
news_dict = news_obj.get_json('engadget')
for news in news_dict['articles']:
author = news['author']
title = news['title']
desc = news['description']
url = news['url']
urlToImage = news['urlToImage']
publishedAt = news['publishedAt']
category = 'technology'
news_obj = news_data.query.filter_by(title=title).first()
if not news_obj:
news_obj = news_data(author,title,desc,url,urlToImage,publishedAt, category)
db.session.add(news_obj)
db.session.commit()
news_obj = news_data.query.filter_by(title=title).first()
if not news_obj:
db.session.add(news_obj)
db.session.commit()
return render_template('technology.html', news_dict=news_dict)
@app.route('/api/gaming')
def gaming():
news_obj = _news()
news_dict = news_obj.get_json('ign')
for news in news_dict['articles']:
author = news['author']
title = news['title']
desc = news['description']
url = news['url']
urlToImage = news['urlToImage']
publishedAt = news['publishedAt']
category = 'gaming'
news_obj = news_data.query.filter_by(title=title).first()
if not news_obj:
news_obj = news_data(author,title,desc,url,urlToImage,publishedAt, category)
db.session.add(news_obj)
db.session.commit()
return render_template('gaming.html', news_dict=news_dict)
@app.route('/api/science_nature')
def science_nature():
news_obj = _news()
news_dict = news_obj.get_json('daily-mail')
for news in news_dict['articles']:
author = news['author']
title = news['title']
desc = news['description']
url = news['url']
urlToImage = news['urlToImage']
publishedAt = news['publishedAt']
category = 'science_nature'
news_obj = news_data.query.filter_by(title=title).first()
if not news_obj:
news_obj = news_data(author,title,desc,url,urlToImage,publishedAt, category)
db.session.add(news_obj)
db.session.commit()
return render_template('science_nature.html', news_dict=news_dict)
# This is the logout route
@app.route('/logout')
def logout():
if not session.get('logged_in'):
flash("You are not logged in!")
return redirect(url_for('index'))
session['logged_in'] = False
session['uid'] = ""
session['username'] = ""
flash("You are successfully logged out!")
return redirect(url_for('index'))
@app.route('/api/upvote/id/<string:id>/cat/<cat>')
def upvote(id,cat):
if not session.get('logged_in'):
flash("You are not logged in!")
return redirect(url_for(cat))
print id
print session.get('uid')
n = Post.query.filter_by(news_id=id).filter_by(user_id='jose')
n = Post('jose',1,0,'G1',1)
db.session.add(n)
db.session.commit()
flash("Upvotted..!")
return redirect(url_for(cat))
@app.route('/api/downvote/id/<int:id>/cat/<cat>')
def downvote(id,cat):
if not session.get('logged_in'):
flash("You are not logged in!")
return redirect(url_for(cat))
print id
print session.get('uid')
n = Post.query.filter_by(id=id).filter_by(user_id=session.get('uid')).first()
if not n:
n = Post(1,None,session.get('uid'),id)
db.session.add(n)
db.session.commit()
flash("Downvotted..!")
return redirect(url_for(cat))