<s> import datetime <EOL> import pytz <EOL> from docutils . core import publish_parts <EOL> from webob import Response <EOL> from pyramid . decorator import reify <EOL> from pyramid . httpexceptions import HTTPFound <EOL> from pyramid . url import resource_url <EOL> from substanced . util import find_catalog <EOL> from pyramid . view import ( <EOL> view_config , <EOL> view_defaults , <EOL> ) <EOL> def _getentrybody ( format , entry ) : <EOL> if format == '<STR_LIT>' : <EOL> body = publish_parts ( entry , writer_name = '<STR_LIT:html>' ) [ '<STR_LIT>' ] <EOL> else : <EOL> body = entry <EOL> return body <EOL> @ view_config ( <EOL> renderer = '<STR_LIT>' , <EOL> content_type = '<STR_LIT>' , <EOL> ) <EOL> def blogview ( context , request ) : <EOL> system_catalog = find_catalog ( context , '<STR_LIT>' ) <EOL> blog_catalog = find_catalog ( context , '<STR_LIT>' ) <EOL> content_type = system_catalog [ '<STR_LIT>' ] <EOL> query = content_type . eq ( '<STR_LIT>' ) <EOL> query_result = query . execute ( ) . sort ( blog_catalog [ '<STR_LIT>' ] , reverse = True ) <EOL> blogentries = [ ] <EOL> for blogentry in query_result : <EOL> blogentries . append ( <EOL> { '<STR_LIT:url>' : resource_url ( blogentry , request ) , <EOL> '<STR_LIT:title>' : blogentry . title , <EOL> '<STR_LIT:body>' : _getentrybody ( blogentry . format , blogentry . entry ) , <EOL> '<STR_LIT>' : blogentry . pubdate , <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT:name>' : a . __name__ , '<STR_LIT:url>' : resource_url ( a , request , '<STR_LIT>' ) } <EOL> for a in blogentry [ '<STR_LIT>' ] . values ( ) ] , <EOL> '<STR_LIT>' : len ( blogentry [ '<STR_LIT>' ] . values ( ) ) , <EOL> } ) <EOL> return dict ( blogentries = blogentries ) <EOL> @ view_defaults ( <EOL> content_type = '<STR_LIT>' , <EOL> renderer = '<STR_LIT>' , <EOL> ) <EOL> class BlogEntryView ( object ) : <EOL> def __init__ ( self , context , request ) : <EOL> self . context = context <EOL> self . request = request <EOL> @ reify <EOL> def blogentry ( self ) : <EOL> return _getentrybody ( self . context . format , self . context . entry ) <EOL> @ reify <EOL> def comments ( self ) : <EOL> context = self . context <EOL> system_catalog = find_catalog ( context , '<STR_LIT>' ) <EOL> blog_catalog = find_catalog ( context , '<STR_LIT>' ) <EOL> content_type = system_catalog [ '<STR_LIT>' ] <EOL> path = system_catalog [ '<STR_LIT:path>' ] <EOL> comments_path = self . request . resource_path ( context [ '<STR_LIT>' ] ) <EOL> query = content_type . eq ( '<STR_LIT>' ) & path . eq ( comments_path ) <EOL> query_result = query . execute ( ) . sort ( blog_catalog [ '<STR_LIT>' ] ) <EOL> return query_result <EOL> @ reify <EOL> def attachments ( self ) : <EOL> return self . context [ '<STR_LIT>' ] . values ( ) <EOL> @ view_config ( request_method = '<STR_LIT:GET>' ) <EOL> def view_blogentry ( self ) : <EOL> return dict ( error_message = '<STR_LIT>' ) <EOL> @ view_config ( request_method = '<STR_LIT:POST>' ) <EOL> def add_comment ( self ) : <EOL> params = self . request . params <EOL> commenter_name = params . get ( '<STR_LIT>' ) <EOL> comment_text = params . get ( '<STR_LIT>' ) <EOL> spambot = params . get ( '<STR_LIT>' ) <EOL> if spambot : <EOL> message = '<STR_LIT>' <EOL> elif comment_text == '<STR_LIT>' and commenter_name == '<STR_LIT>' : <EOL> message = '<STR_LIT>' <EOL> elif comment_text == '<STR_LIT>' : <EOL> message = '<STR_LIT>' <EOL> elif commenter_name == '<STR_LIT>' : <EOL> message = '<STR_LIT>' <EOL> else : <EOL> pubdate = datetime . datetime . now ( ) <EOL> comment = self . request . registry . content . create ( <EOL> '<STR_LIT>' , commenter_name , comment_text , pubdate ) <EOL> self . context . add_comment ( comment ) <EOL> return HTTPFound ( location = self . request . resource_url ( self . context ) ) <EOL> return dict ( error_message = message ) <EOL> @ view_config ( <EOL> content_type = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> ) <EOL> def download_attachment ( context , request ) : <EOL> f = context . blob . open ( ) <EOL> headers = [ ( '<STR_LIT:Content-Type>' , str ( context . mimetype ) ) , <EOL> ( '<STR_LIT>' , <EOL> '<STR_LIT>' % str ( context . __name__ ) ) , <EOL> ] <EOL> response = Response ( headerlist = headers , app_iter = f ) <EOL> return response <EOL> @ view_defaults ( <EOL> content_type = '<STR_LIT>' , <EOL> ) <EOL> class FeedViews ( object ) : <EOL> def __init__ ( self , context , request ) : <EOL> self . context = context <EOL> self . request = request <EOL> def _nowtz ( self ) : <EOL> now = datetime . datetime . utcnow ( ) <EOL> y , mo , d , h , mi , s = now . timetuple ( ) [ : <NUM_LIT:6> ] <EOL> return datetime . datetime ( y , mo , d , h , mi , s , tzinfo = pytz . utc ) <EOL> def _get_feed_info ( self ) : <EOL> context = self . context <EOL> request = self . request <EOL> feed = { "<STR_LIT>" : request . application_url + "<STR_LIT>" , <EOL> "<STR_LIT>" : request . application_url + "<STR_LIT>" , <EOL> "<STR_LIT>" : request . application_url , <EOL> "<STR_LIT:title>" : context . sdi_title , <EOL> "<STR_LIT:description>" : context . description , <EOL> } <EOL> def _add_updated_strings ( updated , info ) : <EOL> if getattr ( updated , '<STR_LIT>' , None ) is None : <EOL> y , mo , d , h , mi , s = updated . timetuple ( ) [ : <NUM_LIT:6> ] <EOL> updated = datetime . datetime ( y , mo , d , h , mi , s , tzinfo = pytz . utc ) <EOL> info [ '<STR_LIT>' ] = updated . astimezone ( pytz . utc ) . isoformat ( ) <EOL> info [ '<STR_LIT>' ] = updated . strftime ( '<STR_LIT>' ) <EOL> blogentries = [ ] <EOL> for name , blogentry in context . items ( ) : <EOL> if request . registry . content . istype ( blogentry , '<STR_LIT>' ) : <EOL> updated = blogentry . pubdate <EOL> info = { '<STR_LIT:url>' : resource_url ( blogentry , request ) , <EOL> '<STR_LIT:title>' : blogentry . title , <EOL> '<STR_LIT:body>' : _getentrybody ( blogentry . format , <EOL> blogentry . entry ) , <EOL> '<STR_LIT>' : updated , <EOL> '<STR_LIT>' : updated , <EOL> } <EOL> _add_updated_strings ( updated , info ) <EOL> blogentries . append ( ( updated , info ) ) <EOL> blogentries . sort ( key = lambda x : x [ <NUM_LIT:0> ] . isoformat ( ) ) <EOL> blogentries = [ entry [ <NUM_LIT:1> ] for entry in reversed ( blogentries ) ] [ : <NUM_LIT:15> ] <EOL> updated = blogentries and blogentries [ <NUM_LIT:0> ] [ '<STR_LIT>' ] or self . _nowtz ( ) <EOL> _add_updated_strings ( updated , feed ) <EOL> return feed , blogentries <EOL> @ view_config ( <EOL> name = '<STR_LIT>' , <EOL> renderer = '<STR_LIT>' , <EOL> ) <EOL> def blog_rss ( self ) : <EOL> feed , blogentries = self . _get_feed_info ( ) <EOL> self . request . response . content_type = '<STR_LIT>' <EOL> return dict ( <EOL> feed = feed , <EOL> blogentries = blogentries , <EOL> ) <EOL> @ view_config ( <EOL> name = '<STR_LIT>' , <EOL> renderer = '<STR_LIT>' , <EOL> ) <EOL> def blog_atom ( self ) : <EOL> feed , blogentries = self . _get_feed_info ( ) <EOL> self . request . response . content_type = '<STR_LIT>' <EOL> return dict ( <EOL> feed = feed , <EOL> blogentries = blogentries , <EOL> ) </s>
