<s> """<STR_LIT>""" <EOL> from flask import Flask , request , jsonify <EOL> from flask . ext import excel <EOL> from flask . ext . sqlalchemy import SQLAlchemy <EOL> from datetime import datetime <EOL> import pyexcel . ext . xls <EOL> app = Flask ( __name__ ) <EOL> app . config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> db = SQLAlchemy ( app ) <EOL> class Post ( db . Model ) : <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> title = db . Column ( db . String ( <NUM_LIT> ) ) <EOL> body = db . Column ( db . Text ) <EOL> pub_date = db . Column ( db . DateTime ) <EOL> category_id = db . Column ( db . Integer , db . ForeignKey ( '<STR_LIT>' ) ) <EOL> category = db . relationship ( '<STR_LIT>' , <EOL> backref = db . backref ( '<STR_LIT>' , lazy = '<STR_LIT>' ) ) <EOL> def __init__ ( self , title , body , category , pub_date = None ) : <EOL> self . title = title <EOL> self . body = body <EOL> if pub_date is None : <EOL> pub_date = datetime . utcnow ( ) <EOL> self . pub_date = pub_date <EOL> self . category = category <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . title <EOL> class Category ( db . Model ) : <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> name = db . Column ( db . String ( <NUM_LIT:50> ) ) <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . name <EOL> db . create_all ( ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def upload_file ( ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> return jsonify ( { "<STR_LIT:result>" : request . get_array ( '<STR_LIT:file>' ) } ) <EOL> return '''<STR_LIT>''' <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:GET>' ] ) <EOL> def download_file ( ) : <EOL> return excel . make_response_from_array ( [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ] , "<STR_LIT>" ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def doimport ( ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> def category_init_func ( row ) : <EOL> c = Category ( row [ '<STR_LIT:name>' ] ) <EOL> c . id = row [ '<STR_LIT:id>' ] <EOL> return c <EOL> def post_init_func ( row ) : <EOL> c = Category . query . filter_by ( name = row [ '<STR_LIT>' ] ) . first ( ) <EOL> p = Post ( row [ '<STR_LIT:title>' ] , row [ '<STR_LIT:body>' ] , c , row [ '<STR_LIT>' ] ) <EOL> return p <EOL> request . save_book_to_database ( field_name = '<STR_LIT:file>' , session = db . session , <EOL> tables = [ Category , Post ] , <EOL> initializers = [ category_init_func , post_init_func ] ) <EOL> return "<STR_LIT>" <EOL> return '''<STR_LIT>''' <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:GET>' ] ) <EOL> def doexport ( ) : <EOL> return excel . make_response_from_tables ( db . session , [ Category , Post ] , "<STR_LIT>" ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:GET>' ] ) <EOL> def docustomexport ( ) : <EOL> query_sets = Category . query . filter_by ( id = <NUM_LIT:1> ) . all ( ) <EOL> column_names = [ '<STR_LIT:id>' , '<STR_LIT:name>' ] <EOL> return excel . make_response_from_query_sets ( query_sets , column_names , "<STR_LIT>" ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> app . run ( ) </s>
