Permalink
Browse files

Posts are now cached (if possible) so they are not re-rendered each t…

…ime they are viewed. This should speed things up a bit. Also you can now specify that only post titles should appear on the frontpage instead of the entire post.
  • Loading branch information...
1 parent 67f564e commit 5d8dfe42c4966cdabc9838134f97ebca16c4c6ac @orf committed Sep 8, 2012
Showing with 37 additions and 10 deletions.
  1. +1 −0 .gitignore
  2. +4 −2 create_config.py
  3. +1 −1 requirements.txt
  4. +23 −3 simple.py
  5. +1 −2 static/css/layout.css
  6. +7 −2 templates/index.html
View
@@ -2,3 +2,4 @@
*.db
*.*~
/settings.py
+cache/
View
@@ -11,6 +11,7 @@ def input_with_default(prompt, default):
print "Generating a Simple config file. Please answer some questions:"
SETTINGS = (
input_with_default("Posts per page", 5),
+ input_with_default("Show the post content on the homepage","y").lower()[0] == "y",
input_with_default("Admin username","admin"),
generate_password_hash(input_with_default("Admin password","password")),
input_with_default("Google analytics ID",""),
@@ -19,13 +20,14 @@ def input_with_default(prompt, default):
input_with_default("Contact Email", ""),
input_with_default("Blog title", ""),
input_with_default("Blog tagline", ""),
- input_with_default("Blog URL (e.g. /blog)",""),
- input_with_default("Font Name: ","Source Sans Pro").replace(" ","+")
+ input_with_default("Blog URL (e.g. /blog)","/"),
+ input_with_default("Font Name (Selected from google font library): ","Source Sans Pro").replace(" ","+")
)
with open("settings.py", "w") as fd:
fd.write("""# -*- coding: utf-8 -*-\n
POSTS_PER_PAGE = %s
+POST_CONTENT_ON_HOMEPAGE = %s
ADMIN_USERNAME = '%s'
ADMIN_PASSWORD = '%s'
ANALYTICS_ID = '%s'
View
@@ -1,6 +1,6 @@
Flask==0.8
Flask-SQLAlchemy==0.15
Jinja2==2.6
-Markdown==2.1.1
SQLAlchemy==0.7.6
Werkzeug==0.8.3
+Markdown==2.2.0
View
@@ -3,6 +3,7 @@
# python imports
import re
import datetime
+import os
from functools import wraps
from unicodedata import normalize
@@ -12,10 +13,20 @@
from werkzeug.security import check_password_hash
from flask import render_template, request, Flask, flash, redirect, url_for, \
abort, jsonify, Response, make_response
+from werkzeug.contrib.cache import FileSystemCache, NullCache
app = Flask(__name__)
app.config.from_object('settings')
db = SQLAlchemy(app)
+cache_directory = os.path.dirname(__file__)
+try:
+ cache = FileSystemCache(os.path.join(cache_directory, "cache"))
+except Exception,e:
+ print "Could not create cache folder, caching will be disabled."
+ print "Error: %s"%e
+ cache = NullCache()
+
+
_punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
@@ -43,7 +54,16 @@ def __init__(self, title=None, created_at=None):
updated_at = db.Column(db.DateTime)
def render_content(self):
- return MARKDOWN_PARSER.convert(self.text)
+ _cached = cache.get("post_%s"%self.id)
+ if _cached is not None:
+ return _cached
+ text = MARKDOWN_PARSER.convert(self.text)
+ cache.set("post_%s"%self.id, text)
+ return text
+
+ def set_content(self, content):
+ self.text = content
+
try:
db.create_all()
@@ -166,7 +186,7 @@ def edit(post_id):
if post.title != request.form.get("post_title", ""):
post.title = request.form.get("post_title","")
post.slug = slugify(post.title)
- post.text = request.form.get("post_content","")
+ post.set_content(request.form.get("post_content",""))
post.updated_at = datetime.datetime.now()
if any(request.form.getlist("post_draft", type=int)):
@@ -216,7 +236,7 @@ def save_post(post_id):
if post.title != request.form.get("title", ""):
post.title = request.form.get("title","")
post.slug = slugify(post.title)
- post.text = request.form.get("content", "")
+ post.set_content(request.form.get("content", ""))
post.updated_at = datetime.datetime.now()
db.session.add(post)
db.session.commit()
View
@@ -12,11 +12,10 @@ time {
}
.post_container {
- padding: 26px 0 40px 50px;
+ padding: 26px 0 15px 50px;
}
-
.sidebar {
position:fixed;
left:1%;
View
@@ -10,8 +10,13 @@
<div class="post_header">
<h1><a href="{{ url_for('view_post_slug', slug=post.slug, pid=current_page) }}">{{ post.title }}</a></h1>
</div>
- <br/>
- {{ post.render_content()|safe }}
+
+ {% if config.get('POST_CONTENT_ON_HOMEPAGE',True) %}
+ <br/>
+ {{ post.render_content()|safe }}
+ {% else %}
+ <p style="color: #999;">{{ post.created_at.strftime('%B %d, %Y') }}</p>
+ {% endif %}
</div>
</section>
{% endfor %}

0 comments on commit 5d8dfe4

Please sign in to comment.