Esempio n. 1
0
def new_inventory(args):
    inventory = Inventory()
    inventory.name = args['name']
    inventory.description = args['description']
    print current_user.user
    current_user.user.inventories.append(inventory)
    db_add(inventory)
    db_add(current_user.user)
    return index()
Esempio n. 2
0
def make_inventory():
    verify_args = {
        'password': fields.Str(validate=password_length_validator, required=True, use=lambda val: val.lower()),
        'username': fields.Str(required=True)
    }
    args = parser.parse(verify_args, request)

    print args

    user = verify_user(args['username'], args['password'])
    if user is False:
        return make_response(jsonify({'success': False, 'error': 'Password incorrect'}), 401)

    username = args['username']

    request_args = {
        'name': fields.Str(required=True),
        'description': fields.Str()
    }

    args = parser.parse(request_args, request)

    inventory = Inventory()
    inventory.name = args['name']
    inventory.description = args['description']
    inventory.updated = datetime.datetime.utcnow()

    user.inventories.append(inventory)

    if db_add(inventory) is True:
        if db_add(user) is True:
            return response_success(inventory_schema_simple(inventory), 201)
        db_delete(inventory)
        return make_response(
            jsonify({'success': False, 'error': 'Inventory was not added to user {}'.format(username)}))

    return make_response(jsonify({'success': False, 'error': 'Inventory {} was not created'.format(args['name'])}), 400)