Пример #1
0
def get_zipcodes():
    zipcode = request.args.get('zipcode')
    maxrange = request.args.get('range')
    try:
        len(zipcode) == 5
        zipcode = int(zipcode)
    except (RuntimeError, ValueError):
        return {"users_list": User().find_all()}
    if zipcode:
        user = User().find_all()
        zipcode = zcdb[int(zipcode)]
        l1 = []
        for x in range(len(user)):
            l1.append(user[x]['zipcode'])
        locations = []
        for x in range(len(l1)):
            locations.append(zcdb[int(l1[x])])
        filtered = []
        for x in range(len(locations)):
            distance = calculateDistance(zipcode.latitude, zipcode.longitude,
                                         locations[x].latitude,
                                         locations[x].longitude)
            if math.floor(distance) <= int(maxrange):
                filtered.append(user[x])
        return {"users_list": filtered}
    return {"users_list": User().find_all()}
Пример #2
0
def get_users():
    if request.method == 'GET':
        result = User().find_all()
        return {"users_list": result}
    elif request.method == 'POST':
        userToAdd = request.get_json()
        userToAdd['latitude'] = zcdb[int(userToAdd['zipcode'])].latitude
        userToAdd['longitude'] = zcdb[int(userToAdd['zipcode'])].longitude
        newUser = User(userToAdd)
        newUser.save()
        resp = jsonify(newUser), 201
        return resp
def get_user(id):
    if request.method == 'GET':
        return User(db_client).find_by_id(id)

    elif request.method == 'DELETE':
        user = User(db_client).find_by_id(id)
        if user:
            db_resp = User(db_client).remove(db_client, id)

        print(db_resp)

        if db_resp['n'] == 1:
            http_resp = jsonify(succes=True, status_code=204)

            return http_resp

        else:
            return jsonify(success=False, status_code=404)
    elif request.method == 'PUT':
        user = User(db_client).find_by_id(id)
        if user:
            usr_to_add = User(db_client, json=request.get_json())

            db_resp = usr_to_add.reload(id, db_client)

            http_resp = jsonify(usr_to_add.json, 200)

            return http_resp
        else:
            return jsonify(204)
Пример #4
0
def get_user(id):
    if request.method == 'GET':
        user = User({"_id": id})
        if user.reload():
            return user
        else:
            return jsonify({"error": "User not found"}), 404
    elif request.method == 'DELETE':
        user = User({"_id": id})
        resp = user.remove()
        if resp['n'] == 1:
            return jsonify({"success": "User was removed"}), 204
        return jsonify({"error": "User not found"}), 404
    elif request.method == 'PATCH':
        user = User({"_id": id})
        data = request.get_json()
        for key in data:
            user.update_user(key, data[key])
        return jsonify({"Success": "User was patched"}), 200
Пример #5
0
def get_user(id):
    if request.method == 'GET':
        user = User({"_id": id})
        if user.reload():
            return user
        else:
            return jsonify({"error": "User not found"}), 404
    elif request.method == 'DELETE':
        user = User({"_id": id})
        resp = user.remove()
        if resp.hasWriteConcernError() or resp.hasWriteError():
            return 404
        return {}, 204
Пример #6
0
def get_user(id):
    if request.method == 'GET':
        user = User({"_id": id})
        if user.reload():
            return user
        else:
            return jsonify({"error": "User not found"}), 404
    elif request.method == 'DELETE':  ## still the old version. Turn it into the DB version
        user = User({"_id": id})
        if user.remove():
            resp = jsonify(), 204
            return resp
        return jsonify({"error": "User not found"}), 404
Пример #7
0
def get_users():
    if request.method == 'GET':
        search_username = request.args.get('name')
        search_job = request.args.get('job')
        if search_username and search_job:
            result = User().find_by_name_and_job(search_username, search_job)
        elif search_username:
            result = User().find_by_name(search_username)
        else:
            result = User().find_all()
        return {"users_list": result}
    elif request.method == 'POST':
        userToAdd = request.get_json()
        newUser = User(userToAdd)
        newUser.save()
        resp = jsonify(newUser), 201
        return resp
Пример #8
0
def get_users():
    if request.method == 'GET':
        search_username = request.args.get('name')
        search_job = request.args.get('job')
        if search_username and search_job:
            users = User().find_by_name_job(
                search_username, search_job)  # not converted to DB access yet
        elif search_username:
            # return find_users_by_name(search_username) #old code left here for comparuson
            users = User().find_by_name(search_username)
        else:
            users = User().find_all()
        return {"users_list": users}
    elif request.method == 'POST':
        userToAdd = request.get_json()
        # userToAdd['id'] = generate_id() #old code left here for comparuson
        # users['users_list'].append(userToAdd) #old code left here for comparuson
        newUser = User(userToAdd)
        newUser.save()
        resp = jsonify(newUser), 201
        return resp
def get_users():
    if request.method == 'GET':

        search_id = request.args.get('id')
        search_name = request.args.get('name')

        if search_id:
            return User(db_client).find_by_id(search_id)

        elif search_name:
            return User(db_client).find_by_name(search_name)

        else:
            return User(db_client).find_all()

    elif request.method == 'POST':
        user_to_add = User(db_client, json=request.get_json())
        db_resp = user_to_add.save(db_client)

        http_resp = jsonify(user_to_add.json, 201)

        return http_resp