-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
336 lines (273 loc) · 13.2 KB
/
app.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# coding=utf-8
import sys, os, re, json
from flask import Flask, Response, render_template, redirect, url_for, request, flash, session
from flask.ext.login import LoginManager, login_required, current_user, login_user, logout_user
from flask.ext.bcrypt import Bcrypt
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.orm import joinedload
from requests_oauthlib import OAuth1Session
import pytumblr
from sanitize import sanitize
app = Flask(__name__)
app.config.from_object(os.environ['ENV_SETTINGS'])
print('ΨΨΨΨΨΨΨΨΨΨΨΨ SQLALCHEMY_DATABASE_URI', app.config.get('SQLALCHEMY_DATABASE_URI'))
db = SQLAlchemy(app)
# app.tumblr_request_url
# app.tumblr_auth_base_url
# app.tumblr_access_token_url
oauth_sessions = {}
if __name__ == '__main__': # to avoid import loops
#{
def create_tumblr_client(user):
if user.tumblr_key and user.tumblr_secret:
return pytumblr.TumblrRestClient(
app.config['TUMBLR_CLIENT'],
app.config['TUMBLR_SECRET'],
user.tumblr_key,
user.tumblr_secret
)
return pytumblr.TumblrRestClient(
app.config['TUMBLR_CLIENT'],
app.config['TUMBLR_SECRET']) # I think this works?
def get_tumblr_user_info(user, client=None):
#{
global oauth_sessions
if client == None:
client = create_tumblr_client(user)
if user.tumblr_key and user.tumblr_secret:
tumblr_info = client.info()
if not 'user' in tumblr_info: # ouath failed
tumblr_info['user'] = None
elif user != current_user: # filter data to only show public blogs
tumblr_info['user']['blogs'] = [blog for blog in tumblr_info['user']['blogs'] if blog['type'] == 'public']
return tumblr_info
elif user == current_user:
tumblr_oauth = OAuth1Session(app.config['TUMBLR_CLIENT'], client_secret=app.config['TUMBLR_SECRET'], callback_uri=app.config['TUMBLR_CALLBACK_URL'])
fetch_response = tumblr_oauth.fetch_request_token(app.config['TUMBLR_REQUEST_URL'])
authorization_url = tumblr_oauth.authorization_url(app.config['TUMBLR_AUTH_BASE_URL'])
# store this OAuth1Session to use again in the callback route (which will receive the same oauth_token)
oauth_sessions[fetch_response['oauth_token']] = tumblr_oauth
return { 'user': None, 'oauth_url': authorization_url }
else:
return { 'user': None }
#}
# from models import User
import models
# Setup login
bcrypt = Bcrypt(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
@login_manager.user_loader
def load_user(user_id):
return models.User.query.options(joinedload('works')).get(user_id)
####### ROUTES #########
########################
# MAIN INDEX
@app.route('/')
def index():
return render_template('index.html', works=models.Work.query.all())
# LOGIN
@app.route('/login', methods=['GET', 'POST'])
def login():
#{
if request.method == 'POST':
try:
user = models.User.query.filter_by(email=request.form['email']).first()
# user = models.User.filter(email==request.form['email']).first()
if not user:
flash('E-mail address not recognized.', 'error')
return redirect(url_for('login'))
if bcrypt.check_password_hash(user.password, request.form['password']):
login_user(user)
flash('login-success', 'login success')
return redirect(url_for('user_dash'))
else:
flash('Password was incorrect.', 'error')
return redirect(url_for('login'))
except NameError:
flash('There were missing fields in the data you submitted.', 'error')
return redirect(url_for('login'))
elif request.method == 'GET':
return render_template('user/login.html')
#}
# NEW USER
@app.route('/signup', methods=['GET', 'POST'])
def signup():
#{
if request.method == 'POST': # Form data submitted
try:
assert len(request.form['password']) >= 8
new_user_data = \
{
'name': request.form['name'],
'email': request.form['email'],
'password': bcrypt.generate_password_hash(request.form['password']),
'about': sanitize(request.form.get('about', ""))
}
new_user = models.User(**new_user_data)
db.session.add(new_user)
db.session.commit()
flash('Your account has been created and you can now log in.', 'success')
return redirect(url_for('login'))
except NameError:
flash('There were missing fields in the data you submitted.', 'error')
return redirect(url_for('signup'))
except AssertionError:
flash('Password not long enough.', 'error')
return redirect(url_for('signup'))
# except IntegrityError:
# flash('That e-mail address or display name is already in use.');
# not sure it was IntegrityError when uniqueness violated...
elif request.method == 'GET': # Show sign up form
if not current_user.is_authenticated: # but not if they're already logged in
return render_template('user/new.html')
else:
return redirect(url_for('user_dash'))
#}
# USER LOG OUT
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('index'))
# SHOW USER
@app.route('/user/<int:id>')
def user_show(id):
user = models.User.query.options(joinedload('works')).get_or_404(id)
if user == current_user:
return redirect(url_for('user_dash'))
tumblr_info = get_tumblr_user_info(user)
return render_template('user/show.html', user=user, tumblr_info=tumblr_info)
# USER DASHBOARD
@app.route('/user/dash')
@login_required
def user_dash():
tumblr_info = get_tumblr_user_info(current_user)
return render_template('user/show.html', user=current_user, tumblr_info=tumblr_info)
# TUMBLR CALLBACK
@app.route('/user/dash/tumblr')
@login_required
def tumblr_callback():
try:
# retrieve the OAuth1Session matching this oauth_token
tumblr_oauth = oauth_sessions[request.args['oauth_token']]
# oauth_sessions[request.args['oauth_token']] = None # It's been used, remove it from the list
tumblr_oauth.parse_authorization_response(request.url)
access_tokens = tumblr_oauth.fetch_access_token(app.config['TUMBLR_ACCESS_TOKEN_URL'])
current_user.tumblr_key = access_tokens['oauth_token']
current_user.tumblr_secret = access_tokens['oauth_token_secret']
db.session.merge(current_user)
db.session.commit()
flash('Tumblr authorization successful.', 'success')
except NameError as err:
print("NAME_ERROR NAME_ERROR NAME_ERROR", err)
flash('Tumblr authorization failed.', 'error')
return redirect(url_for('user_dash'))
# NEW WORK
@app.route('/work/new', methods=['GET', 'POST'])
@login_required
def new_work():
#{
if request.method == 'POST':
try:
new_work_data = \
{
'title': request.form['title'],
'fandom_tags': re.split('\s*,\s*', request.form['fandom_tags']),
'content_tags': re.split('\s*,\s*', request.form['content_tags']),
'char_tags': re.split('\s*,\s*', request.form['char_tags']),
'ship_tags': re.split('\s*,\s*', request.form['ship_tags']),
'summary': sanitize(request.form['summary'])
}
new_work = models.Work(**new_work_data)
new_work.author_id = current_user.id
db.session.add(new_work)
db.session.commit()
flash('New work created.', 'success')
return redirect('/work/'+str(new_work.id)+'/add')
except NameError as err:
flash('There were missing fields in the data you submitted.', 'error')
print(err)
return redirect(url_for('new_work'))
elif request.method == 'GET':
return render_template('work/new.html')
#}
# ADD CHAPTER
@app.route('/work/<work_id>/add', methods=['GET','POST'])
@login_required
def add_chapter(work_id):
#{
if request.method == 'POST':
try:
work = models.Work.query.options(joinedload('chapters')).get_or_404(work_id)
new_chapter_data = \
{
'title': request.form['title'],
'numeral': request.form['numeral'],
'body': sanitize(request.form['body'])
}
new_chapter = models.Chapter(**new_chapter_data)
new_chapter.work_id = work.id
new_chapter.position = len(work.chapters)
db.session.add(new_chapter)
db.session.commit()
#### DEBUG FOO ####
try:
chapter_from_db = models.Chapter.query.get(new_chapter.id)
print('ΨΨΨΨΨΨΨΨΨΨΨΨ ADD CHAPTER // chapter_from_db success :', chapter_from_db)
except:
print('ΨΨΨΨΨΨΨΨΨΨΨΨ ADD CHAPTER // chapter_from_db fail with error :', sys.exc_info()[0])
raise
#### END DEBUG FOO ####
flash('Chapter added.', 'success')
return redirect('/work/'+work_id+'/'+str(new_chapter.position+1))
except NameError as e:
flash('There were missing fields in the data you submitted.', 'error')
print('!!!!!!!!!!!!!!!!!', e)
return redirect('/work/'+work_id+'/add')
elif request.method == 'GET':
work = models.Work.query.options(joinedload('chapters')).get_or_404(work_id)
default_numeral = "Chapter " + str(len(work.chapters) + 1)
tumblr = current_user.tumblr_key and current_user.tumblr_secret # true or false whether to offer Tumblr import
return render_template('work/add.html', work=work, default_numeral=default_numeral, tumblr=tumblr)
#}
# SHOW WORK + CHAPTER
@app.route('/work/<work_id>/<int:position>')
def show_work(work_id, position):
#{
work = models.Work.query.options(joinedload('chapters')).get_or_404(work_id)
print('ΨΨΨΨΨΨΨΨΨΨΨΨ SHOW WORK // Work loaded :', work)
position -= 1
chapter = None
print('ΨΨΨΨΨΨΨΨΨΨΨΨ SHOW WORK // work.chapters', work.chapters)
try:
chapter = work.chapters[position]
print('ΨΨΨΨΨΨΨΨΨΨΨΨ SHOW WORK // Chapter retrieved :', chapter)
except:
print('ΨΨΨΨΨΨΨΨΨΨΨΨ SHOW WORK // Chapter retrieval failed with error:', sys.exc_info()[0])
raise
return render_template('work/show.html', work=work, chapter=chapter, chapter_count=len(work.chapters), chapter_position=chapter.position+1)
#}
# FRONT-END REQUESTING TUMBLR IMPORT DATA
@app.route('/api/tumblr-import.json')
@login_required
def tumblr_import():
tumblr_imports = None
if current_user.tumblr_key and current_user.tumblr_secret:
client = create_tumblr_client(current_user)
user_info = get_tumblr_user_info(current_user, client)
# print('user_info', user_info)
# collect/trim just the Tumblr data the front end might need
blog_names = [blog['name'] for blog in user_info['user']['blogs']]
tumblr_imports = {}
for name in blog_names:
blog_posts = []
for post in client.posts(name, type='text')['posts']:
blog_posts.append({ 'title': post['title'], 'tags': post['tags'], 'body': post['body'] })
tumblr_imports[name] = blog_posts
# print(json.dumps(tumblr_imports))
return Response(json.dumps(tumblr_imports), mimetype='application/json')
# return Response(tumblr_imports, mimetype='application/json')
app.run(host='0.0.0.0', port=int(os.environ.get("PORT", 5000)))
#}