added code for rendering simple blog entries
This commit is contained in:
parent
0cf168b393
commit
57caa5d05e
1 changed files with 58 additions and 0 deletions
|
@ -8,6 +8,7 @@ BASE_DIR = os.path.dirname(os.path.abspath(os.path.join(__file__, os.path.pardir
|
|||
import sys
|
||||
sys.path.insert(0, os.path.join(BASE_DIR, "src"))
|
||||
|
||||
import tools
|
||||
import bobo
|
||||
import forms
|
||||
import sqlobject
|
||||
|
@ -34,6 +35,8 @@ db_uri = config.get("database", "uri")
|
|||
sqlobject.sqlhub.processConnection = sqlobject.connectionForURI(db_uri)
|
||||
loader = genshi.template.TemplateLoader(os.path.join(BASE_DIR, 'templates'), auto_reload=False)
|
||||
|
||||
BLOG_DIR = os.path.join(BASE_DIR, "blog")
|
||||
|
||||
|
||||
BASE_DICT = {
|
||||
"base_url": "/", # the trailing slash is necessary
|
||||
|
@ -556,6 +559,61 @@ def admin_poll(cancel=False, submit=None, admin_hash_key=None, author=None,
|
|||
else:
|
||||
return render("poll_admin_edit.html", input_data=data, **value_dict)
|
||||
|
||||
def render_blog_entry(blog_id):
|
||||
blog_info = get_blog_info(blog_id)
|
||||
if blog_info is None:
|
||||
return None
|
||||
else:
|
||||
value_dict = get_default_values()
|
||||
value_dict["title"] = blog_info[0]
|
||||
value_dict["date"] = blog_info[1]
|
||||
value_dict["link"] = blog_info[2]
|
||||
value_dict["body"] = blog_info[3]
|
||||
return render("blog_entry.html", **value_dict)
|
||||
|
||||
def get_blog_info(blog_id):
|
||||
blog_file = os.path.join(BLOG_DIR, blog_id)
|
||||
try:
|
||||
input = open(blog_file)
|
||||
title = input.readline()
|
||||
body = input.read()
|
||||
input.close()
|
||||
except IOError:
|
||||
return None
|
||||
date = "%s.%s.%s %s:%s" % (blog_id[6:8], blog_id[4:6], blog_id[0:4],
|
||||
blog_id[8:10], blog_id[10:12])
|
||||
link = "%sblog/%s" % (get_default_values()["base_url"], blog_id)
|
||||
body = tools.creole2html(body.decode("utf-8"))
|
||||
return title, date, link, body
|
||||
|
||||
def get_blog_ids():
|
||||
def add_files_to_list(file_list, dirname, fnames):
|
||||
for fname in fnames:
|
||||
if re.match(r"^[0-9]{12}$", fname) \
|
||||
and os.path.isfile(os.path.join(dirname, fname)):
|
||||
file_list.append(fname)
|
||||
# remove all entries (e.g. sub-directories)
|
||||
while len(fnames) > 0:
|
||||
del fnames[0]
|
||||
file_list = []
|
||||
os.path.walk(BLOG_DIR, add_files_to_list, file_list)
|
||||
return file_list
|
||||
|
||||
@bobo.query('/blog')
|
||||
@bobo.query('/blog/')
|
||||
@bobo.query('/blog/:blog_id')
|
||||
@bobo.query('/blog/:blog_id/')
|
||||
def serve_blog(blog_id=None):
|
||||
value_dict = get_default_values()
|
||||
if blog_id and re.match(r"^[0-9]{12}$", blog_id):
|
||||
# the blog_id should consist of 12 digits
|
||||
result = render_blog_entry(blog_id)
|
||||
if not result is None:
|
||||
return result
|
||||
# if anything fails: render the blog list
|
||||
value_dict["blog_list"] = [get_blog_info(blog_id) for blog_id in get_blog_ids()]
|
||||
return render("blog_list.html", **value_dict)
|
||||
|
||||
@bobo.query('')
|
||||
def base():
|
||||
return bobo.redirect(BASE_DICT["base_url"])
|
||||
|
|
Loading…
Reference in a new issue