예제 #1
0
def getall(table, field):
    sql = "select * from %s" % (table)
    util.Writelog('sql').info('getall:%s' % sql)
    try:
        cur.execute(sql)
        res = cur.fetchall()
        user = [{k: row[i] for i, k in enumerate(field)} for row in res]
        result = {'code': 0, 'msg': user}
    except:
        result = {'code': 1, 'msg': 'select faild'}
        util.Writelog('err').error('Execute %s error: %s' %
                                   (sql, traceback.format_exc()))
    return result
예제 #2
0
def delete(user, uid):
    sql = "delete from %s where id=%s" % (user, uid)
    try:
        res = cur.execute(sql)
        result = {'code': 0, 'msg': 'delete success'}
    except:
        result = {'code': 1, 'msg': 'delete faild'}
        util.Writelog('err').error('Execute %s error: %s' %
                                   (sql, traceback.format_exc()))
    return result
예제 #3
0
def update(table, field, data):
    sql = "update %s set %s where id=%s" % (table, ','.join(
        ["%s='%s'" % (k, data[k]) for k in data]), data['id'])
    try:
        res = cur.execute(sql)
        result = {'code': 0, 'msg': 'update success'}
    except:
        result = {'code': 1, 'msg': 'update faild'}
        util.Writelog('err').error('Execute %s error: %s' %
                                   (sql, traceback.format_exc()))
    return result
예제 #4
0
def regist(table, field, data):
    sql = 'insert into %s (%s) values(%s)' % (table, ','.join(field), ','.join(
        ['"%s"' % data[v] for v in field]))
    try:
        res = cur.execute(sql)
        result = {'code': 0, 'msg': 'reg success'}
    except:
        result = {'code': 1, 'msg': 'reg faild'}
        util.Writelog('err').error('Execute %s error: %s' %
                                   (sql, traceback.format_exc()))
    return result
예제 #5
0
def getone(table, field, data):
    if data.has_key('username'):
        sql = "select * from %s where username='******'" % (table,
                                                        data['username'])
    elif data.has_key('sr'):
        sql = "select * from %s where sr='%s'" % (table, data['sr'])
    else:
        sql = "select * from %s where id='%s'" % (table, data['id'])
    try:
        cur.execute(sql)
        res = cur.fetchone()
        user = {k: res[i] for i, k in enumerate(field)}
        result = {'code': 0, 'msg': user}
    except:
        result = {'code': 1, 'msg': 'user not is exist'}
        util.Writelog('err').error('Execute %s error: %s' %
                                   (sql, traceback.format_exc()))
    return result
예제 #6
0
def login():
    if request.method == "POST":
        data = {k: v[0] for k, v in dict(request.form).items()}
        result = utils.getone("user", field, data)
        if result['code'] == 0:
            if result['msg']['status'] == 0:
                if result['msg']['password'] == data['password']:
                    session['username'] = data['username']
                    session['role'] = result['msg']['role']
                    util.Writelog('login').info('%s login success ' %
                                                session['username'])
                    result = {'code': 0, 'msg': 'login success'}
                else:
                    result = {'code': 1, 'msg': 'passwd is wrong'}
            else:
                result = {'code': 1, 'msg': 'user is lock'}
        else:
            result = {'code': 1, 'msg': 'user not is exist'}
        return json.dumps(result)
    return render_template('login.html', msg=' ')