import datetime import html import rfeed from flask import render_template, request, flash, redirect, url_for, Response from webapp.routes import * from oshipka.webapp import app 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 @app.route('/') def home(): 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('/aboutme') def aboutme(): return render_template("aboutme.html") @app.route('/blog_post/') 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, title="PiSquared Blog - {}".format(blog_post.title)) @app.route('/rss.xml') def rss(): feed_items = [] for blog_post in BlogPost.query.order_by(BlogPost.created_dt.desc()).all(): d = blog_post.created_dt pub_date = datetime.datetime(int(d[0:4]), int(d[4:6]), int(d[6:8])) item = rfeed.Item( title=blog_post.title, link="https://blog.pi2.dev/blog_post/{}".format(blog_post.filename), description="{}...".format(blog_post.body[:300]), author="Daniel", guid=rfeed.Guid("https://blog.pi2.dev/blog_post/{}".format(blog_post.filename)), pubDate=pub_date) feed_items.append(item) feed = rfeed.Feed( title="Pi2-Blog posts", link="https://blog.pi2.dev/rss.xml", description="Pisquared Blog - Recently updated posts", language="en-US", lastBuildDate=datetime.datetime.now(), items=feed_items) return Response(feed.rss(), mimetype='text/xml')