Beispiel #1
0
def realIp(request):
    """ 获取请求中的真实ip
    """
    if isEmpty(request):
        return ''
    try:
        return request.headers['X-Forwarded-For']
    except Exception:
        try:
            return request.headers['X-Real-IP']
        except Exception:
            return request.remote_addr
Beispiel #2
0
    def retrieveWholes(sql: str,
                       column_names: tuple = None,
                       fetchone=False,
                       bind=None):
        """ 查询整张表字段数据,返回JSON
        :param sql
        :param column_names 需要返回的json key ,需和sql中查询字段对于
        :param fetchone 只取一个
         """
        if isEmpty(column_names):
            import re
            re1 = re.search('from.\w*', sql)
            if isEmpty(re1):
                raise Exception('正则错误,获取表名失败,不支持的sql语句 %s' % sql)
            table_name = re1.group().replace('from ', '')
            t = DB.retrieve(
                ConstantSql.column_name(table_name, app.config['DATABASE_NAME']
                                        )  # todo appConfig中需要增加DATABASE_NAME
                ,
                bind)  # 获取表字段
            if isEmpty(t):
                i('查询表列名出错 %s' % traceback.format_exc())
                raise Exception('查询表列名出错')
            column_names = tuple(s[0] for s in t)
        elif '*' in sql:
            sql = sql.replace(
                '*',
                str(column_names).replace('(',
                                          '').replace(')',
                                                      '').replace("'", ''))

        r = DB.retrieve(sql, fetchone, bind)
        if isEmpty(r):
            return None if fetchone else []
        if fetchone:
            return DBSup.__tuple_cov_json(column_names, r)
        else:
            return [DBSup.__tuple_cov_json(column_names, d) for d in r]
Beispiel #3
0
 def wrapper(*args, **kw):
     __call_size = kw.get('__call_size')
     if isNotEmpty(__call_size) and __call_size >= loop_size:
         kw.pop('__call_size')
         return func(*args, **kw)
     try:
         if isNotEmpty(__call_size):
             kw.pop('__call_size')
         return func(*args, **kw)
     except Exception as e:
         i('死循环 Exception??%s' % e)
         if isEmpty(__call_size):
             __call_size = 0
         __call_size += 1
         kw.update({'__call_size': __call_size})
         time.sleep(1)
         return wrapper(*args, **kw)
Beispiel #4
0
 def retrieveFixCN(sql: str,
                   column_names: tuple,
                   fetchone=False,
                   bind=None):
     """ 查询返回固定的字段名数据
     :param sql 必须查询*
     :param column_names
     :param fetchone 只取一个
     """
     r = DB.retrieve(
         sql.replace(
             '*',
             str(column_names).replace('(',
                                       '').replace(')',
                                                   '').replace("'", '')),
         fetchone, bind)
     if isEmpty(r):
         return None if fetchone else []
     if fetchone:
         return DBSup.__tuple_cov_json(column_names, r)
     else:
         return [DBSup.__tuple_cov_json(column_names, d) for d in r]
Beispiel #5
0
 def form_where(form, *keys, add_where=True):
     """ form中直接提取where条件
     :param form Form
     :param keys {'key','='}(key),{'key','like'},{'key','<='}...
     :param add_where 是否增加where关键字
     :returns: '' / 'where k=v and k=v' / 'k=v and k=v'
     """
     s = []
     for ks in keys:
         if isinstance(ks, dict):
             k = list(ks.keys())[0]
             j = ks[k]
         elif isinstance(ks, str):
             k = ks
             j = '='
         else:
             raise Exception('未知类型')
         v = form.__dict__[k].data
         if isNotEmpty(v):
             s.append("%s %s '%s'" %
                      ('ctime' if k == 'stime' or k == 'etime' else k, j,
                       '%{}%'.format(v) if j == 'like' else v))
     r = ' and '.join(s) if isNotEmpty(s) else ''
     return '' if isEmpty(r) else 'where %s' % r if add_where else r