This repository has been archived by the owner on Jan 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
340 lines (242 loc) · 9.85 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
337
338
339
340
import logging
import os
import re
from datetime import datetime
from logging.handlers import TimedRotatingFileHandler
from flask import Flask, flash, g, redirect, render_template, request, session, url_for
from flask_mail import Mail, Message
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from markupsafe import Markup, escape
from mastodon import MastodonIllegalArgumentError, MastodonUnauthorizedError
from pymysql import InternalError
from sqlalchemy import exc
from tr.forms import MastodonIDForm, SubmissionForm
from tr.helpers import get_or_create_host, mastodon_api
from tr.models import Post, Settings, User, metadata
app = Flask(__name__)
FORMAT = "%(asctime)-15s [%(filename)s:%(lineno)s : %(funcName)s()] %(message)s"
formatter = logging.Formatter(FORMAT)
# initialize the log handler
logHandler = TimedRotatingFileHandler('logs/app.log', when='D', backupCount=7)
logHandler.setFormatter(formatter)
# set the log handler level
app.logger.setLevel(logging.INFO)
app.logger.addHandler(logHandler)
app.logger.info("Starting up...")
config = os.environ.get('TR_CONFIG', 'config.DevelopmentConfig')
app.config.from_object(config)
mail = Mail(app)
if app.config['SENTRY_DSN']:
from raven.contrib.flask import Sentry
sentry = Sentry(app, dsn=app.config['SENTRY_DSN'])
db = SQLAlchemy(metadata=metadata)
migrate = Migrate(app, db)
db.init_app(app)
@app.before_request
def before_request():
try:
db.engine.execute('SELECT 1 from users')
except exc.SQLAlchemyError as e:
return f"Song Delivery is unavailable at the moment: {e}", 503
app.logger.debug(session)
@app.route('/', methods=["GET", "POST"])
def index():
posts = db.session.query(Post).order_by(Post.updated.desc()).filter_by(posted=True)
for p in posts:
p.fetch_metadata()
db.session.commit()
return render_template('community.html.j2',
app=app,
posts=posts
)
@app.route('/post', methods=["GET", "POST"])
def post():
if app.config['MAINTENANCE_MODE']:
return render_template('maintenance.html.j2')
sform = SubmissionForm()
is_preview = False
post = None
if request.method == 'POST':
if sform.validate_on_submit():
post = Post()
sform.populate_obj(post)
if request.form["task"] == 'Preview':
post.fetch_metadata()
sform.share_link.data = post.share_link
is_preview = True
if sform.comment.data == "":
sform.comment.data = f'{post.title}\n\nSent from https://tusk.rocks 🐘🎸\n'
elif request.form["task"] == 'Send':
uid = session.get('user_id', None)
if uid:
user = db.session.query(User).filter_by(id=uid).first()
if not user:
flash("An error occurred. User not found")
return redirect(url_for('post'))
else:
# For some reason sometimes user_id isn't set
flash("An error occurred. Please log in again.")
return redirect(url_for('logout'))
post.user_id = user.id
post.fetch_metadata()
db.session.add(post)
try:
db.session.commit()
except InternalError as e:
app.logger.error(e)
flash(f"Oh no, there was a problem posting this. We'll try to figure out the problem.")
else:
flash(f"Thank you! Your post will appear soon.")
return redirect(url_for('index'))
else:
for e in sform.errors.items():
flash(e[1][0])
return render_template('post.html.j2',
sform=sform,
app=app,
post=post,
is_preview=is_preview
)
@app.route('/mastodon_login', methods=['GET', 'POST'])
def mastodon_login():
form = MastodonIDForm()
if request.method == 'POST' and form.validate_on_submit():
user_id = form.mastodon_id.data
if "@" not in user_id:
flash('Invalid Mastodon ID')
return redirect(url_for('index'))
if user_id[0] == '@':
user_id = user_id[1:]
username, host = user_id.split('@')
session['mastodon_host'] = host
api = mastodon_api(db, app, host)
if api:
return redirect(
api.auth_request_url(
scopes=['read', 'write'],
redirect_uris=url_for("mastodon_oauthorized", _external=True)
)
)
else:
flash(f"There was a problem connecting to the mastodon server.")
elif request.method == 'GET':
return render_template('m_login.html.j2',
mform=form,
app=app,
)
elif request.method == 'POST':
flash("Invalid Mastodon ID")
return redirect(url_for('index'))
@app.route('/mastodon_oauthorized')
def mastodon_oauthorized():
authorization_code = request.args.get('code')
if authorization_code is None:
flash('You denied the request to sign in to Mastodon.')
else:
host = session.get('mastodon_host', None)
app.logger.info(f"Authorization code {authorization_code} for {host}")
if not host:
flash('There was an error. Please ensure you allow this site to use cookies.')
return redirect(url_for('index'))
session.pop('mastodon_host', None)
api = mastodon_api(db, app, host)
try:
access_code = api.log_in(
code=authorization_code,
scopes=["read", "write"],
redirect_uri=url_for("mastodon_oauthorized", _external=True)
)
except MastodonIllegalArgumentError as e:
flash(f"There was a problem connecting to the mastodon server. The error was {e}")
return redirect(url_for('index'))
# app.logger.info(f"Access code {access_code}")
api.access_code = access_code
try:
creds = api.account_verify_credentials()
except MastodonUnauthorizedError as e:
flash(f"There was a problem connecting to the mastodon server. The error was {e}")
return redirect(url_for('index'))
mastodon_host = get_or_create_host(db, app, host)
session['mastodon'] = {
'host': host,
'username': creds["username"],
}
# first look up by account id
user = db.session.query(User).filter_by(
mastodon_account_id=creds["id"],
mastodon_host_id=mastodon_host.id
).first()
if not user:
# fall back to looking up by username
user = db.session.query(User).filter_by(
mastodon_user=creds["username"],
mastodon_host_id=mastodon_host.id
).first()
if user:
app.logger.debug("Existing settings found")
session['user_id'] = user.id
if user.mastodon_access_code != access_code:
user.mastodon_access_code = access_code
user.updated = datetime.now()
db.session.commit()
if user.mastodon_account_id == 0:
user.mastodon_account_id = creds["id"]
user.updated = datetime.now()
db.session.commit()
else:
user = User()
user.settings = Settings()
user.mastodon_access_code = access_code
user.mastodon_user = creds["username"]
user.mastodon_host = mastodon_host
user.mastodon_account_id = creds["id"]
user.updated = datetime.now()
db.session.add(user.settings)
db.session.add(user)
db.session.commit()
session['user_id'] = user.id
if app.config.get('MAIL_SERVER', None):
body = render_template('email/new_user_email.txt.j2',
user=user)
msg = Message(subject=f"New {app.config.get('SITE_NAME', None)} user",
body=body,
recipients=[app.config.get('MAIL_TO', None)])
try:
mail.send(msg)
except Exception as e:
app.logger.error(e)
return redirect(url_for('index'))
@app.route('/delete_post/<post_id>', methods=["GET"])
def delete_post(post_id):
post_to_delete = db.session.query(Post).filter_by(id=post_id).first()
if not post_to_delete:
flash("No post found")
return redirect(url_for('index'))
uid = session.get('user_id', None)
if uid:
user = db.session.query(User).filter_by(id=uid).first()
if post_to_delete.user_id != user.id:
flash("Permission Denied")
return redirect(url_for('index'))
db.session.delete(post_to_delete)
db.session.commit()
flash("Deleted")
return redirect(url_for('index'))
@app.route('/logout', methods=["GET", "POST"])
def logout():
session.pop('mastodon', None)
session.pop('user_id', None)
return redirect(url_for('index'))
@app.route('/privacy')
def privacy():
return render_template('privacy.html.j2',
app=app)
@app.template_filter('nl2br')
def nl2br(value):
_paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}')
result = u'\n\n'.join(u'<p>%s</p>' % p.replace('\n', Markup('<br>\n'))
for p in _paragraph_re.split(escape(value)))
return result
if __name__ == '__main__':
app.run()