Beispiel #1
0
def post_add(request, kwargs):
    assert isinstance(request, HttpRequest)
    result = ResultModel()
    result.msg = ''
    formdata = request.POST.dict()
    tableid = kwargs.get('id', '')
    tablecolumns = None
    table = None
    if tableid != '':
        table = SysTableList.objects.get(id=int(tableid))
        if table.allowadd != 1:
            return JsonResponse(result.tojson())
        tablecolumns = list(
            SysTableColumn.objects.filter(
                Q(tableid=int(tableid)) & Q(addvisible=1)))
    addmodel = {}
    primarykey_cols = SysHelper.get_column_names(tableid, 'PrimaryKey=1',
                                                 'ListOrder')
    for col in tablecolumns:
        #如果这个列属于主键,判断是否已经有值存在
        if col.name in formdata.keys():
            colvalue = ''
            #这个情况下可能选多个值
            if col.datatype == 'checkbox':
                colvalue = ','.join(request.POST.getlist(col.name, ''))
            else:
                colvalue = formdata.get(col.name, '')
            value_exist = '0'
            if col.name in primarykey_cols:
                value_exist = SqlHelper.single(
                    'select count(*) from {0} where {1}=\'{2}\''.format(
                        table.name, col.name, colvalue))
            if value_exist != '0':
                result.msg += col.description + '字段为主键,值"' + colvalue + '"已存在,'
            else:
                addmodel[col.name] = colvalue
    if result.msg != '':
        return JsonResponse(result.tojson())
    addmodel['CreateDateTime'] = time.strftime("%Y-%m-%d %H:%M:%S")
    addmodel['ModifyDateTime'] = time.strftime("%Y-%m-%d %H:%M:%S")
    addmodel['Creator'] = str(request.user.id)
    addmodel['Modifier'] = str(request.user.id)
    sql = 'insert into {0}({1}) values({2})'
    values = ''
    for v in addmodel.values():
        values += "'" + v + "',"
    values = values.strip(',')
    sql = sql.format(table.name, ','.join(addmodel.keys()), values)
    SqlHelper.execute(sql)
    affect_rows = SqlHelper.execute(sql)
    result.msg = '影响数据条数' + str(affect_rows)
    result.flag = affect_rows == 1
    return JsonResponse(result.tojson())
Beispiel #2
0
def post_edit(request, kwargs):
    assert isinstance(request, HttpRequest)
    result = ResultModel()
    formdata = request.POST.dict()
    id = kwargs.get('id', '')
    tableid = kwargs.get('value', '')
    tablecolumns = None
    table = None
    if tableid != '':
        table = SysTableList.objects.get(id=int(tableid))
        if table.allowedit != 1:
            return JsonResponse(result.tojson())
        update_filter = SysTableList.objects.get(
            id=int(tableid)).forbiddenupdatefilter
        condition = '1=1'
        if update_filter != '':
            condition = update_filter.replace('{UserId}', str(request.user.id))
        tablecolumns = list(
            SysTableColumn.objects.filter(
                Q(tableid=int(tableid)) & Q(editvisible=1)))
    editmodel = {}
    primarykey_cols = SysHelper.get_column_names(tableid, 'PrimaryKey=1',
                                                 'ListOrder')

    for col in tablecolumns:
        colvalue = ''
        #这个情况下可能选多个值
        if col.datatype == 'checkbox':
            colvalue = ','.join(request.POST.getlist(col.name, ''))
        else:
            colvalue = formdata.get(col.name, '')
        value_exist = '0'
        if col.name in primarykey_cols:
            value_exist = SqlHelper.single(
                'select count(*) from {0} where {1}=\'{2}\' and Id != {3}'.
                format(table.name, col.name, colvalue, id))
        if value_exist != '0':
            result.msg += col.description + '字段为主键,值"' + colvalue + '"已存在,'
        else:
            editmodel[col.name] = colvalue
    if result.msg != '':
        return JsonResponse(result.tojson())
    editmodel['ModifyDateTime'] = time.strftime("%Y-%m-%d %H:%M:%S")
    editmodel['Modifier'] = str(request.user.id)
    sql = 'update {0} set {1} where {2}'
    newvalues = ''
    for key, value in editmodel.items():
        newvalues += "{0}='{1}',".format(key, value)
    newvalues = newvalues.rstrip(',')
    sql = sql.format(table.name, newvalues,
                     'Id=' + str(id) + ' and ' + condition)
    affect_rows = SqlHelper.execute(sql)
    result.msg = '影响数据条数' + str(affect_rows)
    result.flag = affect_rows == 1
    return JsonResponse(result.tojson())