Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
126 lines (108 sloc) 3.94 KB
import aetycoon
import hashlib
import re
from google.appengine.ext import db
from google.appengine.ext import deferred
import config
import generators
import markup
import static
import utils
if config.default_markup in markup.MARKUP_MAP:
DEFAULT_MARKUP = config.default_markup
class BlogPost(db.Model):
# The URL path to the blog post. Posts have a path iff they are published.
path = db.StringProperty()
title = db.StringProperty(required=True, indexed=False)
body_markup = db.StringProperty(choices=set(markup.MARKUP_MAP),
body = db.TextProperty(required=True)
tags = aetycoon.SetProperty(basestring, indexed=False)
published = db.DateTimeProperty()
updated = db.DateTimeProperty(auto_now=False)
deps = aetycoon.PickleProperty()
def normalized_tags(tags):
return list(set(utils.slugify(x.lower()) for x in tags))
def tag_pairs(self):
return [(x, utils.slugify(x.lower())) for x in self.tags]
def rendered(self):
"""Returns the rendered body."""
return markup.render_body(self)
def summary(self):
"""Returns a summary of the blog post."""
return markup.render_summary(self)
def hash(self):
val = (self.title, self.body, self.published)
return hashlib.sha1(str(val)).hexdigest()
def summary_hash(self):
val = (self.title, self.summary, self.tags, self.published)
return hashlib.sha1(str(val)).hexdigest()
def publish(self):
regenerate = False
if not self.path:
num = 0
content = None
while not content:
path = utils.format_post_path(self, num)
content = static.add(path, '', config.html_mime_type)
num += 1
self.path = path
# Force regenerate on new publish. Also helps with generation of
# chronologically previous and next page.
regenerate = True
if not self.deps:
self.deps = {}
for generator_class, deps in self.get_deps(regenerate=regenerate):
for dep in deps:
if generator_class.can_defer:
deferred.defer(generator_class.generate_resource, None, dep)
generator_class.generate_resource(self, dep)
def remove(self):
if not self.is_saved():
if not self.deps:
self.deps = {}
# It is important that the get_deps() return the post dependency
# before the list dependencies as the BlogPost entity gets deleted
# while calling PostContentGenerator.
for generator_class, deps in self.get_deps(regenerate=True):
for dep in deps:
if generator_class.can_defer:
deferred.defer(generator_class.generate_resource, None, dep)
if == 'PostContentGenerator':
generator_class.generate_resource(self, dep, action='delete')
generator_class.generate_resource(self, dep)
def get_deps(self, regenerate=False):
for generator_class in generators.generator_list:
new_deps = set(generator_class.get_resource_list(self))
new_etag = generator_class.get_etag(self)
old_deps, old_etag = self.deps.get(, (set(), None))
if new_etag != old_etag or regenerate:
# If the etag has changed, regenerate everything
to_regenerate = new_deps | old_deps
# Otherwise just regenerate the changes
to_regenerate = new_deps ^ old_deps
self.deps[] = (new_deps, new_etag)
yield generator_class, to_regenerate
class VersionInfo(db.Model):
bloggart_major = db.IntegerProperty(required=True)
bloggart_minor = db.IntegerProperty(required=True)
bloggart_rev = db.IntegerProperty(required=True)
def bloggart_version(self):
return (self.bloggart_major, self.bloggart_minor, self.bloggart_rev)
Jump to Line
Something went wrong with that request. Please try again.