2015-05-28 21:48:16 +00:00
from flask import abort , g , redirect , render_template , request , url_for
2012-12-19 06:53:59 +00:00
from werkzeug . contrib . atom import AtomFeed
2014-11-15 23:54:06 +00:00
from i2p2www import BLOG_DIR , BLOG_POSTS_PER_FEED , BLOG_POSTS_PER_PAGE , cache
2013-01-16 01:41:24 +00:00
from i2p2www . blog . helpers import get_blog_posts , get_blog_feed_items , get_date_from_slug , get_metadata_from_meta , render_blog_post
2015-05-28 21:48:16 +00:00
from i2p2www . blog . shortlinks import BLOG_SHORTLINKS
2012-12-19 06:53:59 +00:00
from i2p2www . helpers import Pagination , get_for_page
############
# Blog views
2013-01-16 22:12:51 +00:00
@cache.memoize ( 600 )
2013-02-03 02:58:55 +00:00
def blog_index ( page , category = None ) :
all_posts = get_blog_posts ( category = category )
2013-01-09 00:32:32 +00:00
posts = get_for_page ( all_posts , page , BLOG_POSTS_PER_PAGE )
if not posts and page != 1 :
2012-12-19 06:53:59 +00:00
abort ( 404 )
2013-01-09 00:32:32 +00:00
pagination = Pagination ( page , BLOG_POSTS_PER_PAGE , len ( all_posts ) )
2013-02-03 02:58:55 +00:00
if category :
return render_template ( ' blog/category.html ' , pagination = pagination , posts = posts , category = category )
else :
return render_template ( ' blog/index.html ' , pagination = pagination , posts = posts )
2013-02-03 02:17:41 +00:00
2013-01-16 22:12:51 +00:00
@cache.memoize ( 600 )
2013-01-09 00:32:32 +00:00
def blog_post ( slug ) :
# try to render that blog post.. throws 404 if it does not exist
parts = render_blog_post ( slug )
2012-12-19 06:53:59 +00:00
if parts :
2013-01-08 01:33:22 +00:00
meta = get_metadata_from_meta ( parts [ ' meta ' ] )
2013-01-16 01:41:24 +00:00
meta [ ' date ' ] = meta [ ' date ' ] if meta [ ' date ' ] else get_date_from_slug ( slug )
2015-01-11 06:31:06 +00:00
# add notification to any error messages
parts [ ' fragment ' ] = parts [ ' fragment ' ] . replace ( ' Docutils System Messages ' , ' There are errors in this translation. Please comment on <a href= " https://trac.i2p2.de/ticket/1396 " >this ticket</a> with the URL of this page. ' )
2014-11-15 23:54:06 +00:00
# remove BLOG_DIR from any error messages
parts [ ' fragment ' ] = parts [ ' fragment ' ] . replace ( BLOG_DIR , ' Blog ' )
2012-12-19 06:53:59 +00:00
# now just pass to simple template file and we are done
2013-01-09 00:32:32 +00:00
return render_template ( ' blog/post.html ' , parts = parts , title = parts [ ' title ' ] , body = parts [ ' fragment ' ] , slug = slug , meta = meta )
2012-12-19 06:53:59 +00:00
else :
abort ( 404 )
2015-05-28 21:48:16 +00:00
@cache.memoize ( 600 )
def blog_post_shortlink ( shortlink ) :
lang = ' en '
if hasattr ( g , ' lang ' ) and g . lang :
lang = g . lang
try :
slug = BLOG_SHORTLINKS [ shortlink ]
except KeyError :
abort ( 404 )
return redirect ( url_for ( ' blog_post ' , lang = lang , slug = slug ) )
2012-12-19 06:53:59 +00:00
def blog_rss ( ) :
# TODO: implement
pass
2013-01-16 22:12:51 +00:00
@cache.cached ( 600 )
2013-02-03 02:53:58 +00:00
def blog_atom ( category = None ) :
feed_title = ' I2P Blog '
if category :
feed_title = ' I2P Blog Category: %s ' % category
feed = AtomFeed ( feed_title , feed_url = request . url , url = request . url_root )
2013-02-03 03:12:31 +00:00
items = get_blog_feed_items ( BLOG_POSTS_PER_FEED , category = category )
2012-12-19 06:53:59 +00:00
for item in items :
feed . add ( item [ ' title ' ] ,
item [ ' content ' ] ,
2015-09-09 01:19:14 +00:00
author = item [ ' author ' ] ,
2012-12-19 06:53:59 +00:00
content_type = ' html ' ,
url = item [ ' url ' ] ,
updated = item [ ' updated ' ] )
return feed . get_response ( )