Exemplo n.º 1
0
def after_request_func_for_statistics(response):
    try:
        update_post_visits_for_response(response) # 因为调用了 request/g,不能放在 spawn 中处理
    except:
        if sentry_client: sentry_client.captureException()

    update_usage_statistics(response)

    return response
Exemplo n.º 2
0
def get_record_data_error_info(data):
    # record_data 必须是可以 dumps 的 dict 或者 字符串
    if isinstance(data, dict):
        try:
            data = json_dumps(data)
        except:
            if sentry_client:
                sentry_client.captureException()
            return 'py-data format error'
    if not isinstance(data, string_types):
        return 'data format error'
Exemplo n.º 3
0
 def wsgi_app(self, environ, start_response):
     try:
         return Flask.wsgi_app(self, environ, start_response)
     except InvalidHeader:
         response = get_status_response('InvalidHeader', status_code=500)
         return response(environ, start_response)
     except:
         sentry_client.captureException()
         response = get_status_response('unknown request error',
                                        status_code=500)
         return response(environ, start_response)
Exemplo n.º 4
0
def get_cookie(k, max_age=None, is_pure=False):  # 这里的max_age是服务端的判定
    try:
        raw_value = request.cookies.get(k)
        if is_pure:  # 纯 cookie
            return raw_value
    except RuntimeError:  # 基本上是因为request不能调用,因为当前不是web请求
        return ''
    if raw_value:
        try:
            return signer.loads(raw_value, max_age=max_age)
        except (BadTimeSignature, BadSignature):
            return
        except Exception:
            if sentry_client:
                sentry_client.captureException()
            return
Exemplo n.º 5
0
def context_call(self, context_obj, *args, **kwargs):
    # 一般只是本地的 jade 才会调用到,外部的实际走evn_call
    if not hasattr(context_obj, '__call__'):  # 不可 call, 可能仅仅是 property
        return context_obj
    if isinstance(context_obj, Undefined):
        return ''
    try:
        value = old_context_call(self, context_obj, *args, **kwargs)
        if value is None:
            value = Undefined()
        return value
    except HTTPException as e:
        raise e
    except TemplateDebugException as e:
        raise e
    except TypeError as e:
        if hasattr(e, 'message') and 'not callable' in e.message:
            # 被调用的函数不可被call,返回原始值
            return context_obj
        else:
            # sentry_client.captureException()
            message = getattr(e, 'message', None) or ''
            if message:
                return message
            error_info = 'context_call error:* %s, ** %s; %s' % (
                smart_unicode(args), smart_unicode(kwargs), message)
            return error_info
    except Exception as e:
        if isinstance(context_obj, Macro):
            pass
        else:
            if sentry_client:
                sentry_client.captureException()  # todo 目前这个错误还是会进行捕获

        message = getattr(e, 'message', None) or ''
        object_name = getattr(context_obj, '__name__', None) or ''
        if DEBUG:
            print 'context_call error\n', context_obj, args, kwargs
        if message:
            error_info = message
        else:
            error_info = 'context_call error: %s, * %s, ** %s; %s' % (
                object_name, smart_unicode(args), smart_unicode(kwargs),
                message)

        return '<error>%s</error>' % error_info
Exemplo n.º 6
0
def render_template_source(template_source, *args, **kwargs):
    if not template_source:
        return ''
    try:
        api_template_env = get_api_template_env()
        template_source_md5 = get_md5(template_source)
        template = template_source_cache.get(template_source_md5)
        if not template:
            template = api_template_env.from_string(template_source)
            template_source_cache[template_source_md5] = template
        html_content = template.render(*args, **kwargs)
    except HTTPException as e:  # 410 是内部用来跳转用得
        raise e
    except Exception as e:
        if DEBUG:
            raise e
        if sentry_client:
            sentry_client.captureException()
        html_content = '<div style="color:red">api template error</div>'
    return html_content
Exemplo n.º 7
0
def push_message_to_ws(ws, message):
    # ws is websocket, 一个具体的连接
    if not isinstance(message, string_types):
        message = json.dumps(message)
    if not ws:
        return
    try:
        if 'HTTP_SEC_WEBSOCKET_KEY1' in ws.environ and 'HTTP_SEC_WEBSOCKET_KEY2' in ws.environ:
            # 针对旧版 websocket 的发送协议
            try:
                if isinstance(message, unicode):
                    message = message.encode('utf8')
                raw_socket = ws.stream.handler.socket
                message = "\x00" + message + "\xFF"
                raw_socket.sendall(message)
            except:
                if sentry_client:
                    sentry_client.captureException()
        else:
            ws.send(message)
    except WebSocketError:  # socket已经关闭,此时会触发on_close事件
        pass
Exemplo n.º 8
0
def search_posts(bucket,
                 keywords,
                 sort=None,
                 status=None,
                 path='',
                 limit=None,
                 page=1,
                 search_fields=None,
                 date_start=None,
                 date_end=None,
                 return_count=False,
                 should_sync_es=True):
    es_client = get_es_client()
    if not es_client:  # es is not valid
        return []
    try:
        total, post_ids_with_highlight = es_search_posts(
            bucket,
            keywords,
            sort=sort,
            status=status,
            path=path,
            limit=limit,
            page=page,
            search_fields=search_fields,
            date_start=date_start,
            date_end=date_end,
            should_sync_es=should_sync_es)
        if return_count:  # 直接返回匹配的数量就可以了
            return total
    except:
        total = 0
        posts = []
        sentry_client.captureException()
        set_not_cache_current_request()
    else:
        post_ids_with_highlight = post_ids_with_highlight[:100]  # 最多返回的数据量
        posts = []
        for (post_id, highlight_result) in post_ids_with_highlight:
            post = get_record(bucket, post_id)
            if post:
                highlight = {}
                keyword_title_list = highlight_result.get("title")
                if keyword_title_list:
                    if isinstance(keyword_title_list, (list, tuple)):
                        highlight["title"] = keyword_title_list[0]
                    else:
                        highlight["title"] = keyword_title_list
                keyword_content_list = highlight_result.get("raw_content")
                if keyword_content_list:
                    if isinstance(keyword_content_list, (list, tuple)):
                        keyword_content_list_length = len(keyword_content_list)
                        if keyword_content_list_length == 1:
                            content = keyword_content_list[0]
                            if "---\n" in content:
                                content = content.rsplit("---\n", 1)[-1]
                            highlight["content"] = content
                        elif keyword_content_list_length >= 2:
                            highlight["content_list"] = keyword_content_list
                    else:
                        highlight["content"] = keyword_content_list
                post["highlight"] = highlight
                posts.append(post)
    return total, posts