-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·235 lines (160 loc) · 6.69 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
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
import json
from jinja2 import StrictUndefined
from flask import Flask, render_template, request, flash, redirect, session, jsonify
# from flask_debugtoolbar import DebugToolbarExtension
import praw
from model import connect_to_db, db, User, Category, UserCategory, categories
from reddit import r,get_authorize_reddit_link, get_posts_by_interest
from main_program import get_news, get_declared_interests, personalize_name
from headlines import get_headlines
app = Flask(__name__)
app.secret_key = "ABC"
app.jinja_env.undefined = StrictUndefined
@app.route('/')
def homepage():
""" home page, going to ask pass in secret key and code to reddit in order to get OAuth code to use in authorized"""
# if session.get('user_id'):
# # check if logged into Reddit
# try:
# user = r.get_me()
# return render_template("homepage.html")
# except praw.errors.OAuthScopeRequired:
# url_for_api = get_authorize_reddit_link()
# return redirect(url_for_api)
# else:
return render_template('homepage.html')
@app.route('/authorize_callback')
def get_authorized():
"""actually get into the api after getting the callback information"""
state = request.args.get('state', '')
code = request.args.get('code', '')
info = r.get_access_information(code)
return redirect('/')
@app.route('/news_quote', methods=["POST"])
def print_news_quote():
""" Links to ajax request for updating news data"""
dict_misc = {}
category_name = request.form.get("category")
category = Category.query.filter_by(category_name=category_name).one()
if category:
subreddit_url = category.subreddit_search
more_news = get_posts_by_interest(subreddit_url,21)
dict_misc[category_name] = more_news
return jsonify(dict_misc)
else:
return "error, category not in database"
@app.route('/see-news')
def get_news_page():
"""display the news according to the users interests"""
if session.get('user_id'):
dictionary_to_unpack_in_html = get_news()
# print dictionary_to_unpack_in_html
return render_template("thenews.html",dictionary_to_unpack_in_html=dictionary_to_unpack_in_html)
else:
flash("Please login if you're a member or register to become a member to see your news")
return redirect('/')
@app.route('/register_form')
def get_form_to_fill_in():
"""display the registration form to fill in"""
return render_template("register_form.html")
@app.route('/register_form', methods=['POST'])
def get_registration_info():
"""Process registration"""
firstname = request.form.get("firstname")
lastname = request.form.get("lastname")
email = request.form.get("email")
password = request.form.get("password")
flash("Welcome %s %s. Please check the categories you're interested in." % (firstname,lastname))
#instantiate a user in the User database.
new_user = User.instantiate_user(firstname,lastname,email,password)
session['user_id'] = new_user.user_id
return redirect('/declare_interests')
@app.route("/declare_interests")
def user_interests_form():
""" present a list of interests that the user can check. Displayes interests checked before"""
user_id = session['user_id']
category_names = categories.keys()
category_names.sort()
print category_names
category_list = get_declared_interests(user_id)
return render_template("declare_interests.html",
category_names=category_names,
category_list=category_list,
)
@app.route("/declare_interests" , methods=['POST'])
def register_interests():
""""Get the user's interests and put them in the UserCategory Table"""
user_id = session['user_id']
if db.session.query(UserCategory):
db.session.query(UserCategory).filter_by(user_id=user_id).delete()
db.session.commit()
interests = request.form.getlist("interest_change")
interest_list = []
#look up the category id on every category(interest) chosen to put in the association table
for interest in interests:
interest_add_on = db.session.query(Category.category_id).filter_by(category_name=interest).first()
interest_list.append(interest_add_on[0])
UserCategory.instantiate_usercategory(user_id,interest_list)
return redirect("/")
@app.route("/login")
def login():
"""Log in Form"""
return render_template("login.html")
@app.route('/login',methods=['POST'])
def login_process():
""" Process login."""
email = request.form.get('email')
password = request.form.get('password')
user = User.query.filter_by(email=email).first()
if not user:
flash ("Please Register First")
return render_template('homepage.html')
if user.password != password:
flash('Incorrect Password. Please try again.')
return redirect('/login')
session['user_id'] = user.user_id
session['user_name'] = personalize_name(user.user_id)
flash('Logged in')
return redirect('/')
@app.route('/news/<newssource>')
def show_bbc_hl(newssource):
""" to get the Headline news from BBC,CNN,API,Google or New York Times"""
bbc_dict = get_headlines(newssource)
if newssource == 'bbc-news':
headline_string = "BBC Headline News"
elif newssource == 'bloomberg':
headline_string = "Bloomberg Headline News"
elif newssource == 'cnn':
headline_string = "CNN Headline News"
elif newssource == 'associated-press':
headline_string = 'Associated Press Headline News'
elif newssource =='google-news':
headline_string = "Google Headline News"
url = bbc_dict['url']
title = bbc_dict['title']
description = bbc_dict['description']
return render_template("headlines.html",headline_string=headline_string,url=url,title=title,description=description)
@app.route('/logout')
def logout():
"""Log out."""
session.clear()
# del session['user_id']
# del session['user_name']
flash('Logged out. Please log in to see your news')
# return render_template('homepage.html',user_name="My")
return redirect('/')
@app.route("/important")
def important():
"""Important info for logged in users."""
if ("user_id" in session):
if ("user_name" in session):
return render_template("important.html")
else:
flash("You must be logged in to view the important page")
return redirect("/login")
if __name__ == '__main__':
app.debug = True
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
connect_to_db(app)
# DebugToolbarExtension(app)
app.run(debug=True, host="0.0.0.0", port=65010)