Пример #1
0
def get_post_or_patch_carts():
    carts_collection = app.db.carts
    prams = request.args
    user = prams.get('user', type=str)
    brand = prams.get('brand', type=str)
    quantity = prams.get('quantity', type=int)
    if request.method == 'GET':
        if user is None:
            result = carts_collection.find()
        else:
            result = carts_collection.find_one({'user': user})
        response_json = dumps(result)
        if response_json == 'null':
            return ('Error: There is no user named {}'.format(user), 400, None)
        else:
            return (response_json, 200, None)
    elif request.method == 'POST':
        carts_dict = request.json
        carts_collection.insert_one(carts_dict)
        json_result = JSONEncoder().encode(carts_dict)
        return (json_result, 201, None)
    elif request.method == 'PATCH':
        if user is not None and brand is not None and quantity is not None:
            result = carts_collection.find_one_and_update(
                {
                    'user': user,
                    'items.brand': brand
                }, {'$inc': {
                    'items.$.quantity': quantity
                }},
                return_document=ReturnDocument.AFTER)
            response_json = JSONEncoder().encode(result)
            if response_json == 'null':
                result = carts_collection.find_one_and_update(
                    {'user': user}, {
                        '$addToSet': {
                            "items": {
                                'brand': brand,
                                'quantity': quantity
                            }
                        }
                    },
                    return_document=ReturnDocument.AFTER)
        response_json = JSONEncoder().encode(result)
        if response_json == 'null':
            return ('Update failed', 400, None)
        else:
            return (response_json, 200, None)
Пример #2
0
def get_or_post_courses():
    # Our users collection
    courses_collection = app.db.courses
    if request.method == 'POST':
        # Users from POST request
        courses_dict = request.json

        # Inserting one user into our users collection
        courses_collection.insert_one(courses_dict)
        json_result = JSONEncoder().encode(courses_dict)
        return (json_result, 201, None)
    elif request.method == 'GET':
        prams = request.args
        number = prams.get('number', type=int)
        if number is None:
            result = courses_collection.find()
        else:
            result = courses_collection.find_one({'number': number})
        response_json = dumps(result)
        if response_json == 'null':
            return (
                "Error: There is no class assgined number:{}".format(number),
                400, None)
        else:
            return (response_json, 200, None)
Пример #3
0
def add_coin(db, user_id, data):
    try:
        price = coinmarketcap.get_price(data['cmc_id'])
        id = db.coins.insert_one({
            'user_id': ObjectId(user_id),
            'cmc_id': data['cmc_id'],
            'amount': float(data['amount']),
            'price': float(price),
            'total': float(data['amount']) * price,
            'created': datetime.datetime.utcnow(),
            'updated': datetime.datetime.utcnow()
        }).inserted_id

        db.users.update({'_id': ObjectId(user_id)},
                        {'$inc': {
                            'total': float(data['amount']) * price
                        }})
        return JSONEncoder().encode({
            'status': 'success',
            'message': 'Successfully added coin',
            'id': id
        })
    except Exception as e:
        print(e)
        return json.dumps({'status': 'fail', 'message': 'adding coin failed'})
Пример #4
0
def get_or_post_users():
    # Our users collection
    users_collection = app.db.users
    if request.method == 'POST':
        # Users from POST request
        users_dict = request.json

        # Inserting one user into our users collection
        users_collection.insert_one(users_dict)
        json_result = JSONEncoder().encode(users_dict)
        return (json_result, 201, None)
    elif request.method == 'GET':
        name = request.args.get('name', type=str)
        result = users_collection.find_one({'name': name})
        response_json = JSONEncoder().encode(result)
        pdb.set_trace()
        return (response_json, 200, None)
Пример #5
0
def get_user():
    name = request.args.get('name', type=str)
    users_collection = app.db.users
    result = users_collection.find_one({'name': name})

    response_json = JSONEncoder().encode(result)

    # response_json = dumps(result)
    return (response_json, 200, None)
Пример #6
0
def get_coin(db, id):
    try:
        coin = db.coins.find_one({'_id': ObjectId(id)})
        return JSONEncoder().encode({
            'status': 'success',
            'message': 'Successfully got coin',
            'coin': coin
        })
    except Exception as e:
        print(e)
        return json.dumps({'status': 'fail', 'message': 'getting coin failed'})
Пример #7
0
def post_users():
    # Users from POST request
    users_dict = request.json
    # Our users collection
    users_collection = app.db.users

    # Inserting one user into our users collection
    users_collection.insert_one(users_dict)

    json_result = JSONEncoder().encode(users_dict)
    # json_result = dumps(result)

    return (json_result, 201, None)
Пример #8
0
def get_coins(db, user_id):
    try:
        coins = list(db.coins.find({'user_id': ObjectId(user_id)}))
        return JSONEncoder().encode({
            'status': 'success',
            'message': 'Successfully got coins',
            'coins': coins
        })
    except Exception as e:
        print(e)
        return json.dumps({
            'status': 'fail',
            'message': 'getting coins failed'
        })
Пример #9
0
def output_json(data, code, headers=None):
    resp = make_response(JSONEncoder().encode(data), code)
    resp.headers.extend(headers or {})
    return resp