-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
88 lines (72 loc) · 3.12 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
from flask import Flask, send_file, request, render_template, redirect, url_for, Response
from database import *
from view_article import article_blueprint
from view_author import author_blueprint
from view_authentication import auth_blueprint
from view_file import file_blueprint
import os
import traceback
import pypandoc
from article_list_utils import *
import auth
application = Flask(__name__)
app = application
app.secret_key = b'super secret super spoopy'
app.register_blueprint(article_blueprint, url_prefix='/article')
app.register_blueprint(author_blueprint, url_prefix='/author')
app.register_blueprint(auth_blueprint, url_prefix='/login')
app.register_blueprint(file_blueprint, url_prefix='/file')
@app.route('/')
def index():
query = Article.select().where(Article.listed).order_by(-Article.date)
cur_page = int(request.args.get('page') or 1)
def goto_page(num):
return url_for('index', page=num)
last_page = ceil(len(query)/ELEMENTS_PER_PAGE)
if cur_page > last_page:
return redirect(goto_page(last_page))
return render_template('article/list-article.html', articles=query.paginate(cur_page, ELEMENTS_PER_PAGE),
get_preview=get_preview, cur_page=cur_page,
last_page=last_page, goto_page=goto_page)
@app.route('/atom.xml')
@app.route('/atom/')
@app.route('/feed.xml')
@app.route('/feed/')
def feed():
query = Article.select().where(Article.listed).order_by(-Article.date).limit(5)
return Response(render_template('atom-syndication.xml', what_here='all posts',
url_here=url_for('index', _external=True), articles=list(query),
get_content=get_content),
mimetype='application/atom+xml')
@app.route('/database-backup.sqlite')
def fetch_database():
file = DB_PATH+'.backup'
try:
os.unlink(file)
except FileNotFoundError:
pass
db.execute_sql('vacuum into ?;', (file, ))
return send_file(file)
@app.route('/pandoc/', methods=['GET', 'POST'])
def pandoc_debug():
if request.method == 'GET':
return render_template('pandoc-debug.html', format='md', source='Write your markup here...')
elif request.method == 'POST':
try:
result = pypandoc.convert_text(request.form['source'], 'html', request.form['format'])
return render_template('pandoc-debug.html', format=request.form['format'], source=request.form['source'], result=result)
except:
return render_template('pandoc-debug.html', format=request.form['format'], source=request.form['source'], error=traceback.format_exc())
@app.route('/creation-tools/')
def creation_tools():
if auth.can_create():
return render_template('creation-tools.html', is_editor=auth.is_editor(), db_size=os.path.getsize(DB_PATH))
return abort(403)
@app.route('/vacuum/', methods=['POST'])
def vacuum_db():
if not auth.can_create():
return abort(403)
db.execute_sql('vacuum;')
return redirect(url_for('creation_tools'))
if __name__ == '__main__':
app.run('0.0.0.0', 5000, debug=True)