<s> from flask import Flask , request , jsonify <EOL> from _compact import PY2 <EOL> from flask . ext import excel <EOL> import pyexcel . ext . xls <EOL> import pyexcel . ext . ods3 <EOL> import pyexcel . ext . xlsx <EOL> import pyexcel as pe <EOL> from flask . ext . sqlalchemy import SQLAlchemy <EOL> from datetime import datetime <EOL> app = Flask ( __name__ ) <EOL> data = [ <EOL> [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <EOL> [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] <EOL> ] <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> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def respond_array ( struct_type ) : <EOL> if struct_type == "<STR_LIT>" : <EOL> array = request . get_array ( field_name = '<STR_LIT:file>' ) <EOL> return jsonify ( { "<STR_LIT:result>" : array } ) <EOL> elif struct_type == "<STR_LIT>" : <EOL> adict = request . get_dict ( field_name = '<STR_LIT:file>' ) <EOL> return jsonify ( { "<STR_LIT:result>" : adict } ) <EOL> elif struct_type == "<STR_LIT>" : <EOL> records = request . get_records ( field_name = '<STR_LIT:file>' ) <EOL> return jsonify ( { "<STR_LIT:result>" : records } ) <EOL> elif struct_type == "<STR_LIT>" : <EOL> book = request . get_book ( field_name = '<STR_LIT:file>' ) <EOL> return jsonify ( { "<STR_LIT:result>" : book . to_dict ( ) } ) <EOL> elif struct_type == "<STR_LIT>" : <EOL> book_dict = request . get_book_dict ( field_name = '<STR_LIT:file>' ) <EOL> return jsonify ( { "<STR_LIT:result>" : book_dict } ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> def switch ( file_type ) : <EOL> sheet = request . get_sheet ( field_name = '<STR_LIT:file>' ) <EOL> return excel . make_response ( sheet , file_type ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> def swtich_file_name ( file_type , file_name ) : <EOL> return excel . make_response ( pe . Sheet ( [ "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ] ) , file_type , file_name = file_name ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> def upload_array ( struct_type ) : <EOL> if struct_type == "<STR_LIT>" : <EOL> array = request . get_array ( field_name = '<STR_LIT:file>' ) <EOL> return excel . make_response_from_array ( array , '<STR_LIT>' ) <EOL> elif struct_type == "<STR_LIT>" : <EOL> adict = request . get_dict ( field_name = '<STR_LIT:file>' ) <EOL> return excel . make_response_from_dict ( adict , '<STR_LIT>' ) <EOL> elif struct_type == "<STR_LIT>" : <EOL> records = request . get_records ( field_name = '<STR_LIT:file>' ) <EOL> return excel . make_response_from_records ( records , '<STR_LIT>' ) <EOL> elif struct_type == "<STR_LIT>" : <EOL> book = request . get_book ( field_name = '<STR_LIT:file>' ) <EOL> return excel . make_response ( book , '<STR_LIT>' ) <EOL> elif struct_type == "<STR_LIT>" : <EOL> book_dict = request . get_book_dict ( field_name = '<STR_LIT:file>' ) <EOL> return excel . make_response_from_book_dict ( book_dict , '<STR_LIT>' ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> def upload_categories ( ) : <EOL> def table_init_func ( row ) : <EOL> return Category ( row [ '<STR_LIT:name>' ] ) <EOL> request . save_to_database ( field_name = '<STR_LIT:file>' , session = db . session , <EOL> table = Category , initializer = table_init_func ) <EOL> return excel . make_response_from_a_table ( db . session , Category , "<STR_LIT>" ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> def upload_all ( ) : <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 excel . make_response_from_tables ( db . session , [ Category , Post ] , "<STR_LIT>" ) </s>
