/
app.py
executable file
·359 lines (287 loc) · 10.9 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import os
import boto
from datetime import datetime
from functools import wraps
from markdown import markdown as md
from mimetypes import guess_type
from flask import *
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.login import LoginManager, login_required, current_user, login_user, logout_user
from flask.ext.bcrypt import Bcrypt
from werkzeug.utils import secure_filename
# S3 connection
s3_conn = boto.connect_s3()
s3_bucket = s3_conn.get_bucket('vineel.me')
s3_key = boto.s3.key.Key(s3_bucket)
# Database connection
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'postgresql://vineelme:vineelme@localhost/vineelme')
db = SQLAlchemy(app)
# Bcrypt
bcrypt = Bcrypt(app)
# Database object models
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
password = db.Column(db.String(120))
def __init__(self, username, password):
self.username = username
self.password = password
def __repr__(self):
return '<User %s>' % self.username
# Needed for flask-login
def is_authenticated(self):
return True
def is_active(self):
return True
def is_anonymous(self):
return False
def get_id(self):
return unicode(self.username)
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80), unique=True)
link = db.Column(db.String(80), unique=True)
body = db.Column(db.Text)
pub_date = db.Column(db.DateTime)
published = db.Column(db.Boolean)
category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
category = db.relationship('Category', backref=db.backref('posts', lazy='dynamic'))
def __init__(self, title, body, category, pub_date=None, published=False):
self.title = title
self.link = linkname(title)
self.body = body
if pub_date is None:
pub_date = datetime.utcnow()
self.pub_date = pub_date
self.published = published
self.category = category
def __repr__(self):
return '<Post %r>' % self.title
class Category(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True)
index = db.Column(db.Integer, unique=True)
def __init__(self, name, index):
self.name = name
self.index = index
def __repr__(self):
return '<Category %r>' % self.name
# Public views
@app.route('/')
def index():
posts = Post.query.filter_by(published=True).order_by(Post.pub_date.desc()).limit(6).all()
pages = [ c.name for c in Category.query.order_by(Category.index).all() ]
return render_template('index.html', pages=pages, posts=posts, current='index', admin=current_user.is_authenticated())
@app.route('/<page>/')
def go_to(page):
page = linkname(page)
category = Category.query.filter_by(name=page).first_or_404()
pages = [ c.name for c in Category.query.order_by(Category.index).all() ]
posts = category.posts.filter_by(published=True).order_by(Post.pub_date.desc()).all()
return render_template('pages/%s.html' % page, pages=pages, posts=posts, current=page, admin=current_user.is_authenticated())
@app.route('/<page>/<title>/')
def item(page, title):
post = Post.query.filter_by(link=linkname(title)).first_or_404()
if not current_user.is_authenticated() and not post.published:
abort(404)
if not post.category.name == linkname(page):
return redirect(link(post))
pages = [ c.name for c in Category.query.order_by(Category.index) ]
return render_template('/markdown.html', pages=pages, current=page, post=post, admin=current_user.is_authenticated())
# Admin views
def ssl_required(fn):
@wraps(fn)
def decorated_view(*args, **kwargs):
# Redirect to piggyback herokuapp.com SSL
# This is very hack-ish
if 'vineel' in request.url_root and not request.headers.get('X-Forwarded-Proto', 'http') == 'https':
return redirect(request.url.replace(request.url_root, 'https://vineel.herokuapp.com/'))
return fn(*args, **kwargs)
return decorated_view
@app.route('/admin/')
def admin():
if current_user.is_authenticated():
return redirect(url_for('admin_posts'))
else:
return redirect(url_for('admin_login'))
@app.route('/admin/login/', methods=['POST', 'GET'])
@ssl_required
def admin_login():
if request.method == 'POST':
user = User.query.filter_by(username=request.form['username']).first()
if user and bcrypt.check_password_hash(user.password, request.form['password']):
login_user(user)
return redirect(request.args.get('next') or url_for('admin_posts'))
if current_user.is_authenticated():
return redirect(url_for('admin_posts'))
pages = [ c.name for c in Category.query.order_by(Category.index) ]
return render_template('admin/login.html', pages=pages)
@app.route('/admin/logout/')
@login_required
@ssl_required
def admin_logout():
logout_user()
return redirect(request.referrer.replace('https://vineel.herokuapp.com', 'http://vineel.me'))
@app.route('/admin/posts/')
@login_required
@ssl_required
def admin_posts():
pages = [ c.name for c in Category.query.order_by(Category.index) ]
posts = Post.query.filter_by(published=True).order_by(Post.pub_date.desc()).all()
drafts = Post.query.filter_by(published=False).order_by(Post.pub_date.desc()).all()
return render_template('admin/posts.html', pages=pages, posts=posts, drafts=drafts, admin=current_user.is_authenticated())
@app.route('/admin/publish/<id>/', methods=['POST'])
@login_required
@ssl_required
def admin_publish(id):
if not id.isdigit():
abort(404)
post = Post.query.filter_by(id=int(id)).first_or_404()
post.published = True
db.session.commit()
return redirect(request.referrer or url_for('admin_posts'))
@app.route('/admin/unpublish/<id>/', methods=['POST'])
@login_required
@ssl_required
def admin_unpublish(id):
if not id.isdigit():
abort(404)
post = Post.query.filter_by(id=int(id)).first_or_404()
post.published = False
db.session.commit()
return redirect(request.referrer or url_for('admin_posts'))
@app.route('/admin/edit/<id>/', methods=['POST', 'GET'])
@login_required
@ssl_required
def admin_edit(id):
if not id.isdigit():
abort(404)
post = Post.query.filter_by(id=int(id)).first_or_404()
if request.method == 'POST':
post.category = Category.query.filter_by(name=request.form['category']).first_or_404()
post.title = request.form['title']
post.link = linkname(post.title)
post.body = request.form['text']
db.session.commit()
return redirect(link(post))
pages = [ c.name for c in Category.query.order_by(Category.index) ]
images = [ i.name[len(str(post.id)) + 1:] for i in s3_bucket.list(prefix='%s/' % post.id) if i.name != '%s/' % post.id]
return render_template('admin/edit.html', pages=pages, post=post, images=images, admin=current_user.is_authenticated())
@app.route('/admin/new/', methods=['POST', 'GET'])
@login_required
@ssl_required
def admin_new():
if request.method == 'POST':
category = Category.query.filter_by(name=request.form['category']).first_or_404()
post = Post(request.form['title'], request.form['text'], category)
db.session.add(post)
db.session.commit()
return redirect(link(post))
pages = [ c.name for c in Category.query.order_by(Category.index) ]
return render_template('admin/new.html', pages=pages, admin=current_user.is_authenticated())
@app.route('/admin/delete/<id>/', methods=['POST'])
@login_required
@ssl_required
def admin_delete(id):
if not id.isdigit():
abort(404)
post = Post.query.filter_by(id=int(id)).first_or_404()
for item in s3_bucket.list('%s/' % post.id):
s3_key.key = item.name
s3_key.delete()
db.session.delete(post)
db.session.commit()
return redirect(request.referrer or url_for('admin_posts'))
@app.route('/admin/image/new/<id>/', methods=['POST'])
@login_required
@ssl_required
def admin_image_new(id):
if not id.isdigit():
abort(404)
post = Post.query.filter_by(id=int(id)).first_or_404()
file = request.files['image']
filename = secure_filename(file.filename)
s3_key.key = '%s/%s' % (post.id, filename)
s3_key.set_metadata('Content-Type', guess_type(filename)[0])
s3_key.set_contents_from_file(file)
s3_key.set_acl('public-read')
return redirect(request.referrer or edit(post))
@app.route('/admin/image/delete/<id>/<image>/', methods=['POST'])
@login_required
@ssl_required
def admin_image_delete(id, image):
if not id.isdigit():
abort(404)
post = Post.query.filter_by(id=int(id)).first_or_404()
s3_key.key = '%s/%s' % (id, image)
s3_key.delete()
return redirect(request.referrer or edit(post))
# Login management
login_manager = LoginManager()
login_manager.init_app(app)
app.secret_key = os.environ.get('APP_SECRET_KEY', 'haha, nice try!')
login_manager.login_view = 'admin_login'
@login_manager.user_loader
def load_user(username):
return User.query.filter_by(username=username).first()
# Error views
@app.errorhandler(404)
def page_not_found(e):
pages = [ c.name for c in Category.query.order_by(Category.index).all() ]
return render_template('errors/404.html', pages=pages, admin=current_user.is_authenticated()), 404
@app.errorhandler(500)
def internal_server_error(e):
pages = [ c.name for c in Category.query.order_by(Category.index).all() ]
return render_template('errors/500.html', pages=pages, admin=current_user.is_authenticated()), 500
def jinja_filter(fn):
app.jinja_env.filters[fn.__name__] = fn
return fn
# converts Title into link version
# example: "O' Green World" would become "o-green-world"
@jinja_filter
def linkname(title):
title = title.strip()
title = ''.join(c for c in title if c.isalnum() or c.isspace() or c == '-')
return title.lower().replace(' ', '-')
@jinja_filter
def link(post):
return '/%s/%s/' % (post.category.name, post.link)
@jinja_filter
def thumbnail_on(post):
return 'https://s3.amazonaws.com/vineel.me/%s/thumbnail-on.jpg' % post.id
@jinja_filter
def thumbnail_off(post):
return 'https://s3.amazonaws.com/vineel.me/%s/thumbnail-off.jpg' % post.id
@jinja_filter
def icon_on(service):
return '/static/images/%s-on.png' % linkname(service)
@jinja_filter
def icon_off(service):
return '/static/images/%s-off.png' % linkname(service)
@jinja_filter
def markdown(post):
page = linkname(post.category.name)
prefix = 'https://s3.amazonaws.com/vineel.me/%s/' % post.id
return Markup(md(post.body, ['awsimage(PREFIX=%s)' % prefix, 'syntax']))
@jinja_filter
def edit(post):
return '/admin/edit/%s/' % post.id
@jinja_filter
def publish(post):
return '/admin/publish/%s/' % post.id
@jinja_filter
def unpublish(post):
return '/admin/unpublish/%s/' % post.id
@jinja_filter
def delete(post):
return '/admin/delete/%s/' % post.id
@jinja_filter
def image_url(image, post):
return 'https://s3.amazonaws.com/vineel.me/%s/%s' % (post.id, image)
@jinja_filter
def image_delete(image, post):
return '/admin/image/delete/%s/%s/' % (post.id, image)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=False)