コード例 #1
0
def property_create():
    if not request.json or not 'name' in request.json:
        abort(400)

#   Read from properties Json file
    jsonOpObj = JsonFileOperator(PROPERTIES_FILE, 'r')
    users_properties = jsonOpObj.Read()
    if len(users_properties):
        new_property_id = users_properties[-1]['id'] + 1
    else:
        new_property_id = 1


#   Read from users Json file
    jsonOpObj = JsonFileOperator(USERS_FILE, 'r')
    users = jsonOpObj.Read()
    found_user = next(
        (item for item in users if item["username"] == auth.username()), None)

    additional_params = {'id': new_property_id, 'user_id': found_user['id']}
    property_model = Helpers.PopulateModel(request, CREATE_PROPERTY_PARAMS,
                                           additional_params)
    users_properties.append(property_model)

    #   Write to Json file
    jsonOpObj = JsonFileOperator(PROPERTIES_FILE, 'w')
    jsonOpObj.Write(users_properties)
    return jsonify({'property_id': str(property_model['id'])}), 201
コード例 #2
0
def property_update():
    # request
    if not request.json or not 'property_id' in request.json:
        abort(400)


#   Read from properties Json file
    jsonOpObj = JsonFileOperator(PROPERTIES_FILE, 'r')
    users_properties = jsonOpObj.Read()

    #   Read from users Json file
    jsonOpObj = JsonFileOperator(USERS_FILE, 'r')
    users = jsonOpObj.Read()
    found_user = next(
        (item for item in users if item["username"] == auth.username()), None)

    property_id = request.json["property_id"]
    user_id = found_user['id']
    property_index = get_property_index(users_properties, property_id, user_id)

    if property_index != -1:
        done_op = True
        #     Make Property Model
        additional_params = {'id': property_id, 'user_id': found_user['id']}
        property_model = Helpers.PopulateModel(request, CREATE_PROPERTY_PARAMS,
                                               additional_params)
        print('----------Property Model -----------')
        print(property_model)
        users_properties[property_index] = property_model

        #   Updating property model
        jsonOpObj = JsonFileOperator(PROPERTIES_FILE, 'w')
        jsonOpObj.Write(users_properties)

    else:
        done_op = False

    return jsonify(done_op)
コード例 #3
0
def register():
    # request
    if not request.json or not ('username' in request.json
                                and 'password' in request.json):
        abort(400)


#   Read from users Json file
    jsonOpObj = JsonFileOperator(USERS_FILE, 'r')
    users = jsonOpObj.Read()
    if len(users):
        new_user_id = users[-1]['id'] + 1
    else:
        new_user_id = 1
    additional_params = {'id': new_user_id}
    user_model = Helpers.PopulateModel(request, CREATE_USER_PARAMS,
                                       additional_params)
    users.append(user_model)

    #   Write to users Json file
    jsonOpObj = JsonFileOperator(USERS_FILE, 'w')
    jsonOpObj.Write(users)
    return jsonify({'user_id': str(user_model['id'])}), 201