<s> from flask import Flask <EOL> from flask . ext . sqlalchemy import SQLAlchemy <EOL> builtin_list = list <EOL> db = SQLAlchemy ( ) <EOL> def init_app ( app ) : <EOL> db . init_app ( app ) <EOL> def from_sql ( row ) : <EOL> """<STR_LIT>""" <EOL> data = row . __dict__ . copy ( ) <EOL> data [ '<STR_LIT:id>' ] = row . id <EOL> data . pop ( '<STR_LIT>' ) <EOL> return data <EOL> class Book ( db . Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> title = db . Column ( db . String ( <NUM_LIT:255> ) ) <EOL> author = db . Column ( db . String ( <NUM_LIT:255> ) ) <EOL> publishedDate = db . Column ( db . String ( <NUM_LIT:255> ) ) <EOL> imageUrl = db . Column ( db . String ( <NUM_LIT:255> ) ) <EOL> description = db . Column ( db . String ( <NUM_LIT:255> ) ) <EOL> createdBy = db . Column ( db . String ( <NUM_LIT:255> ) ) <EOL> createdById = db . Column ( db . String ( <NUM_LIT:255> ) ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . title , self . author ) <EOL> def list ( limit = <NUM_LIT:10> , cursor = None ) : <EOL> cursor = int ( cursor ) if cursor else <NUM_LIT:0> <EOL> query = ( Book . query <EOL> . order_by ( Book . title ) <EOL> . limit ( limit ) <EOL> . offset ( cursor ) ) <EOL> books = builtin_list ( map ( from_sql , query . all ( ) ) ) <EOL> next_page = cursor + limit if len ( books ) == limit else None <EOL> return ( books , next_page ) <EOL> def read ( id ) : <EOL> result = Book . query . get ( id ) <EOL> if not result : <EOL> return None <EOL> return from_sql ( result ) <EOL> def create ( data ) : <EOL> book = Book ( ** data ) <EOL> db . session . add ( book ) <EOL> db . session . commit ( ) <EOL> return from_sql ( book ) <EOL> def update ( data , id ) : <EOL> book = Book . query . get ( id ) <EOL> for k , v in data . items ( ) : <EOL> setattr ( book , k , v ) <EOL> db . session . commit ( ) <EOL> return from_sql ( book ) <EOL> def delete ( id ) : <EOL> Book . query . filter_by ( id = id ) . delete ( ) <EOL> db . session . commit ( ) <EOL> def _create_database ( ) : <EOL> """<STR_LIT>""" <EOL> app = Flask ( __name__ ) <EOL> app . config . from_pyfile ( '<STR_LIT>' ) <EOL> init_app ( app ) <EOL> with app . app_context ( ) : <EOL> db . create_all ( ) <EOL> print ( "<STR_LIT>" ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> _create_database ( ) </s>
