Ejemplo n.º 1
0
 def login(self, conn, request: Request):
     if len(request.body) == 0:
         return response.json({"err_code": -1, "err_msg": "no body"})
     info = json.loads(request.body)
     u = info.get("user_name")
     p = info.get("passwd")
     project_id = request.headers.get("projectid")
     if project_id is None or project_id == "":
         project_id = info.get("projectid")
     if u is None or p is None or project_id is None or u == "" or p == "":
         return response.json({"err_code": -1, "err_msg": "login fail"})
     s = '''select * from uhrs.employee where disabled = 0 and account_id = %s and password_hash = %s and (account_id = 'admin' or id in 
     (select employee_id from uhrs.employee_project where project_id = %s))'''
     if pretty.get_log_number(logconsts.LOG_SQL):
         logger.info(s % (u, p, project_id))
     ret = pymysqlutil.select(conn, s, u, p, project_id)
     if len(ret) == 0:
         return response.json({"err_code": -1, "err_msg": "login fail"})
     entry = Login(ret[0])
     token = "%d-%s-%s-%d-%f" % (entry.id, entry.account_id, entry.password_hash, int(time.time()), random.random())
     token = hashlib.md5(bytes(token, encoding="utf-8")).hexdigest()
     entry.token = token
     entry.check_in_time = int(time.time())
     entry.check_in_ua = hashlib.md5(bytes(pretty.user_agent(request), encoding="utf-8")).hexdigest()
     entry.check_in_ip = pretty.remote_ip(request)
     entry.project_id = project_id
     self._set_entry(token, entry)
     self._save()
     s = "update uhrs.employee set check_in_time = %s, check_in_ip = %s, check_in_ua = %s where id = %s"
     if pretty.get_log_number(logconsts.LOG_SQL):
         logger.info(s % (entry.check_in_time, entry.check_in_ip, entry.check_in_ua, entry.id))
     pymysqlutil.execute(conn, s, entry.check_in_time, entry.check_in_ip, entry.check_in_ua, entry.id, commit=True)
     return response.json({"err_code": 0, "err_msg": "", "token": token})
Ejemplo n.º 2
0
    def insert(self, conn, entity: dict, update_when_exists=False):
        # return effected rows, inserted rowid
        self._before_insert(conn, entity)
        sql, values = self._insert_sql(entity, update_when_exists)
        cur = conn.cursor()
        df = pretty.Defer(cur.close)
        if pretty.get_log_number(logconsts.LOG_SQL):
            logger.info(cur.mogrify(sql, values))
        cur.execute(sql, values)

        self._after_insert(conn, entity)
        conn.commit()
        effected_rows, inserted_id = cur.rowcount, cur.lastrowid
        return effected_rows, inserted_id
Ejemplo n.º 3
0
    def delete(self, conn, entity: dict):
        # return effected rows
        self._before_delete(conn, entity)
        sql, values = self._delete_sql(entity)
        cur = conn.cursor()
        df = pretty.Defer(cur.close)
        if pretty.get_log_number(logconsts.LOG_SQL):
            logger.info(cur.mogrify(sql, values))
        cur.execute(sql, values)
        effected_rows = cur.rowcount

        self._after_delete(conn, entity)
        conn.commit()

        return effected_rows
Ejemplo n.º 4
0
def execute(conn, sql, *args, commit=False):
    '''
    insert inton xxx values(%s,%s)
    pymysql sql execution
    :param conn: pymysql connection
    :param commit: bool, commit transction
    :param sql: sql with %param
    :param args: param values
    :return: effected records count
    '''
    cur = conn.cursor()
    defer = pretty.Defer(cur.close)
    if pretty.get_log_number(logconsts.LOG_SQL):
        logger.info(cur.mogrify(sql, args))
    ret = cur.execute(sql, args)
    if commit:
        conn.commit()
    return ret
Ejemplo n.º 5
0
    def select(self, conn, entity: dict):
        # return [entities]
        self._before_select(conn, entity)
        sql, values = self._select_sql(entity)
        cur = conn.cursor()
        df = pretty.Defer(cur.close)
        if pretty.get_log_number(logconsts.LOG_SQL):
            logger.info(cur.mogrify(sql, values))
        cur.execute(sql, values)

        self._after_select(conn, entity)
        conn.commit()
        data = cur.fetchall()
        key_list = []
        desc = cur.description
        for v in desc:
            key_list.append(v[0])
        ret = []
        for v in data:
            ret.append(dict(zip(key_list, v)))
        return ret
Ejemplo n.º 6
0
def select(conn, sql, *args):
    '''
    pymysql sql query
    :param conn: pymysql connection
    :param sql: sql with %param
    :param args: param values
    :return: [{field_name1: field_value1, field_name2: field_value2},{...}]
    '''
    cur_data = conn.cursor()
    defer = pretty.Defer(cur_data.close)
    if pretty.get_log_number(logconsts.LOG_SQL):
        logger.info(cur_data.mogrify(sql, args))
    cur_data.execute(sql, args)
    data = cur_data.fetchall()
    key_list = []
    desc = cur_data.description
    for v in desc:
        key_list.append(v[0])
    ret = []
    for v in data:
        ret.append(dict(zip(key_list, v)))
    return ret
Ejemplo n.º 7
0
async def log_response(request, response):
    if pretty.get_log_number(logconsts.LOG_HTTP):
        logger.info("%s:\n%s" % (request.url, str(response.output())))
Ejemplo n.º 8
0
async def log_request(request: sanic.request.Request):
    if pretty.get_log_number(logconsts.LOG_HTTP):
        logger.info("%s\n%s" % (str(request.headers), str(request.body)))