<s> from flask import abort , jsonify <EOL> from flask . ext . restful import Resource , reqparse <EOL> from reststub . globals import db , encode_mongo_obj , auth <EOL> class ItemListAPI ( Resource ) : <EOL> decorators = [ auth . login_required ] <EOL> def __init__ ( self ) : <EOL> self . reqparse = reqparse . RequestParser ( ) <EOL> self . reqparse . add_argument ( '<STR_LIT:name>' , type = str , location = '<STR_LIT>' , required = True ) <EOL> self . reqparse . add_argument ( '<STR_LIT:text>' , type = str , location = '<STR_LIT>' , required = True ) <EOL> super ( ItemListAPI , self ) . __init__ ( ) <EOL> def get ( self ) : <EOL> found = [ encode_mongo_obj ( v ) for v in db [ '<STR_LIT>' ] . find ( ) ] <EOL> return found <EOL> def post ( self ) : <EOL> args = self . reqparse . parse_args ( ) <EOL> try : <EOL> new_item = { "<STR_LIT:name>" : args [ '<STR_LIT:name>' ] , "<STR_LIT:text>" : args [ '<STR_LIT:text>' ] } <EOL> inserted_id = db [ '<STR_LIT>' ] . insert_one ( new_item ) . inserted_id <EOL> return { '<STR_LIT>' : str ( inserted_id ) } , <NUM_LIT> <EOL> except Exception as e : <EOL> return { '<STR_LIT:error>' : e } , <NUM_LIT> </s>
