Esempio n. 1
0
def common_create(appname, modelname, temp_dict):
    Model_Name = classing_model(modelname)
    try:
        temp_dict = CommonBase.clean_data(modelname, temp_dict)
    except ParamError as e:
        return json_response_error(PARAM_ERROR, msg=e.msg)
    # when save data, filter some useless data
    pop_list = [
        'id', 'last_release_ec2', 'last_release_local', 'release',
        'is_upload_local', 'is_upload_ec2']
    for key in pop_list:
        temp_dict.pop(key, None)
    if hasattr(Model_Name, "fields_check"):
        try:
            temp_dict = get_valid_params(temp_dict, Model_Name.fields_check)
        except ParamError as e:
            return json_response_error(PARAM_ERROR, msg=e.msg)
    temp_dict['first_created'] = temp_dict['last_modified'] = now_timestamp()

    #this is a factory function, to check some save data before insert if need
    (check_success, msg) = check_save_dict(appname, modelname, temp_dict)
    if not check_success:
        return json_response_error(PARAM_ERROR, msg=msg)
    insert_id = Model_Name.insert(appname, temp_dict)

    temp_dict['id'] = insert_id
    # if the model has ref icon and rule, increase the reference
    inc_icon(appname, modelname, temp_dict)
    inc_rule(appname, modelname, temp_dict)
    return json_response_ok(temp_dict)
Esempio n. 2
0
def common_update(appname, modelname, temp_dict, item_id):
    Model_Name = classing_model(modelname)
    temp_dict.pop("id", 0)
    try:
        temp_dict = CommonBase.clean_data(modelname, temp_dict)
    except ParamError as e:
        return json_response_error(PARAM_ERROR, msg=e.msg)
    if hasattr(Model_Name, "fields_check"):
        try:
            temp_dict = get_valid_params(temp_dict, Model_Name.fields_check)
        except ParamError as e:
            return json_response_error(PARAM_ERROR, msg=e.msg)
    cond = {"id": int(item_id)}
    temp_dict['last_modified'] = now_timestamp()
    # if the model has release field
    if check_release(modelname):
        temp_dict['release'] = UploadStatus.UNUPLOAD_LOCAL
    # find the old item before update, aim to track icon and rule
    old_item = Model_Name.find_one(appname, {'id': item_id}, {'_id': 0})

    #this is a factory function, to check some save data before save if need
    (check_success, msg) = check_save_dict(
        appname, modelname, temp_dict, item_id)
    if not check_success:
        return json_response_error(PARAM_ERROR, msg=msg)

    Model_Name.update(appname, cond, temp_dict)
    temp_dict['id'] = item_id
    # if model has refered icon and rule
    mod_icon(appname, modelname, temp_dict, old_item)
    mod_rule(appname, modelname, temp_dict, old_item)
    return json_response_ok(temp_dict)