<s> from flask import Flask , g <EOL> from flask . ext . pushrod import Pushrod , pushrod_view <EOL> from flask . ext . sqlalchemy import SQLAlchemy , Pagination <EOL> from sqlalchemy . sql . functions import now <EOL> app = Flask ( __name__ ) <EOL> pushrod = Pushrod ( app ) <EOL> db = SQLAlchemy ( app ) <EOL> class Author ( db . Model ) : <EOL> __tablename__ = "<STR_LIT>" <EOL> id = db . Column ( db . Integer , primary_key = True , nullable = False ) <EOL> name = db . Column ( db . String ( <NUM_LIT> ) , unique = True , nullable = False ) <EOL> description = db . Column ( db . Text ( ) , nullable = False ) <EOL> def __pushrod_normalize__ ( self , pushrod ) : <EOL> return pushrod . normalize ( { <EOL> '<STR_LIT:name>' : self . name , <EOL> '<STR_LIT:description>' : self . description <EOL> } ) <EOL> class Post ( db . Model ) : <EOL> __tablename__ = "<STR_LIT>" <EOL> id = db . Column ( db . Integer , primary_key = True , nullable = False ) <EOL> timestamp = db . Column ( db . DateTime , nullable = False , default = now ( ) ) <EOL> title = db . Column ( db . String ( <NUM_LIT:255> ) , unique = True , nullable = False ) <EOL> content = db . Column ( db . Text , nullable = False ) <EOL> author_id = db . Column ( db . Integer , db . ForeignKey ( Author . id ) , nullable = False ) <EOL> author = db . relationship ( Author , backref = '<STR_LIT>' ) <EOL> def __pushrod_normalize__ ( self , pushrod ) : <EOL> data = { <EOL> '<STR_LIT:id>' : self . id , <EOL> '<STR_LIT:title>' : self . title , <EOL> '<STR_LIT>' : self . timestamp , <EOL> '<STR_LIT:content>' : self . content , <EOL> '<STR_LIT>' : self . author , <EOL> } <EOL> if not getattr ( g , '<STR_LIT>' , False ) : <EOL> data [ '<STR_LIT>' ] = self . comments <EOL> return pushrod . normalize ( data ) <EOL> class Comment ( db . Model ) : <EOL> __tablename__ = "<STR_LIT>" <EOL> id = db . Column ( db . Integer , primary_key = True , nullable = False ) <EOL> author = db . Column ( db . String ( <NUM_LIT> ) , nullable = False ) <EOL> timestamp = db . Column ( db . DateTime , nullable = False , default = now ( ) ) <EOL> content = db . Column ( db . Text , nullable = False ) <EOL> post_id = db . Column ( db . Integer , db . ForeignKey ( Post . id ) , nullable = False ) <EOL> post = db . relationship ( Post , backref = '<STR_LIT>' ) <EOL> def __pushrod_normalize__ ( self , pushrod ) : <EOL> return pushrod . normalize ( { <EOL> '<STR_LIT>' : self . author , <EOL> '<STR_LIT>' : self . timestamp , <EOL> '<STR_LIT:content>' : self . content , <EOL> } ) <EOL> def normalize_pagination ( x , pushrod ) : <EOL> return pushrod . normalize ( { <EOL> '<STR_LIT>' : x . page , <EOL> '<STR_LIT>' : x . pages , <EOL> '<STR_LIT>' : x . total , <EOL> '<STR_LIT>' : x . items , <EOL> } ) <EOL> pushrod . normalizers [ Pagination ] = normalize_pagination <EOL> @ app . route ( "<STR_LIT:/>" ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> @ pushrod_view ( ) <EOL> def list_posts ( page = <NUM_LIT:1> ) : <EOL> g . list_view = True <EOL> return Post . query . paginate ( page ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> @ pushrod_view ( ) <EOL> def blog_post ( id ) : <EOL> post = Post . query . get_or_404 ( id ) <EOL> return { '<STR_LIT>' : post } <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> app . run ( ) </s>
