blog/webapp/app.py

51 lines
1.7 KiB
Python
Raw Normal View History

2020-07-13 11:54:16 +02:00
import html
from flask import render_template, request, flash, redirect, url_for
2020-07-08 14:36:13 +02:00
from webapp.routes import *
from oshipka.webapp import app
2020-07-13 11:54:16 +02:00
from webapp.models import BlogPost
from flaskext.markdown import Markdown
Markdown(app,
extensions=['markdown.extensions.fenced_code',
'markdown.extensions.codehilite',
'markdown.extensions.sane_lists',
'markdown.extensions.tables'],
safe_mode='escape')
@app.template_filter('rawhtmlparse')
def rawhtmlparse(text):
splitted = text.split("{% rawhtml %}")
final_text = splitted[0]
for idx, entry in enumerate(splitted[1:]):
splitted_entry = entry.split("{% endrawhtml %}")
unescaped_entry = html.unescape(splitted_entry[0])
final_text = "{}{}{}".format(final_text, unescaped_entry, splitted_entry[1])
return final_text
2020-07-08 14:36:13 +02:00
@app.route('/')
def home():
2020-07-13 11:54:16 +02:00
per_page = request.args.get('per_page', 5)
blog_post_pagination = BlogPost.query.order_by(BlogPost.created_dt.desc()).paginate(per_page=per_page,
error_out=False)
return render_template("home.html", blog_post_pagination=blog_post_pagination)
@app.route('/index')
def index():
blog_posts = BlogPost.query.order_by(BlogPost.created_dt.desc()).all()
return render_template("index.html", blog_posts=blog_posts)
@app.route('/blog_post/<filename>')
def blog_post_filename(filename):
blog_post = BlogPost.query.filter_by(filename=filename).first()
if not blog_post:
flash("Blog post doesn't exist")
return redirect(url_for('index'))
return render_template("blog_post/get.html", instance=blog_post)