當前位置: 首頁>>代碼示例>>Python>>正文


Python flask.after_this_request方法代碼示例

本文整理匯總了Python中flask.after_this_request方法的典型用法代碼示例。如果您正苦於以下問題:Python flask.after_this_request方法的具體用法?Python flask.after_this_request怎麽用?Python flask.after_this_request使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在flask的用法示例。


在下文中一共展示了flask.after_this_request方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_after_request_processing

# 需要導入模塊: import flask [as 別名]
# 或者: from flask import after_this_request [as 別名]
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
開發者ID:chalasr,項目名稱:Flask-P2P,代碼行數:16,代碼來源:basic.py

示例2: save_page

# 需要導入模塊: import flask [as 別名]
# 或者: from flask import after_this_request [as 別名]
def save_page():
    @flask.after_this_request
    def add_headers(response):
        response.headers['Access-Control-Allow-Origin'] = '*'
        return response
    values = flask.request.values
    if values.get('type', '') == 'Serp':
        try:
            user_id = Session.get_user_id(values['url'])
        except Exception as e:
            app.logger.error(e)
            return 'Incorrect user_id used', 400
        try:
            query = Session.get_query(values['url'])
        except Exception as e:
            app.logger.error(e)
            return 'No query set?', 400
        for k in ['data', 'tab_id', 'time']:
            if k not in values:
                return 'Missing param: %s' % k, 400
        data = values['data']
        try:
            ts = Session.convert_time(values['time'])
        except Exception as e:
            app.logger.error(e)
            return 'Incorrect timestamp', 400
        session = Session(id=values['tab_id'], user_id=user_id, q=query,
                serp_html=data, start_ts=ts)
        n = len(data)
        while n > 1:
            session.serp_html = data[:n]
            try:
                session.put()
                break
            except apiproxy_errors.RequestTooLargeError as e:
                app.logger.error(e)
                n /= 2
        return 'Saved', 201
    return 'Only support saving SERPs using POST requests, sorry.', 403 
開發者ID:varepsilon,項目名稱:cas-eval,代碼行數:41,代碼來源:main.py

示例3: save_settings

# 需要導入模塊: import flask [as 別名]
# 或者: from flask import after_this_request [as 別名]
def save_settings():
    @flask.after_this_request
    def add_headers(response):
        response.headers['Access-Control-Allow-Origin'] = '*'
        return response
    values = flask.request.values
    try:
        user_id = Session.get_user_id(values['url'])
    except Exception as e:
        app.logger.error(e)
        return 'Incorrect user_id used', 400
    for k in ['data', 'tab_id', 'time']:
        if k not in values:
            return 'Missing param: %s' % k, 400
    try:
        ts = Session.convert_time(values['time'])
    except Exception as e:
        app.logger.error(e)
        return 'Incorrect timestamp', 400
    mute_period_m = 0
    for data in values['data'].split(','):
        try:
            mute_period_m = max(mute_period_m, UserSettings.convert_mute_period_m(data))
        except Exception as e:
            app.logger.error(e)
            return 'Incorrect mute period settings: %s' % data, 400
    mute_deadline = UserSettings.get_mute_deadline(ts, mute_period_m)
    settings = ndb.Key(UserSettings, user_id).get()
    if settings is None:
        # Create settings for the current user
        settings = UserSettings(id=user_id, mute_deadline=mute_deadline, ts=ts)
        settings.put()
    elif settings.mute_deadline is None or settings.mute_deadline < mute_deadline:
        settings.mute_deadline = mute_deadline
        settings.ts = ts
        settings.put()
    return 'Saved', 201 
開發者ID:varepsilon,項目名稱:cas-eval,代碼行數:39,代碼來源:main.py

示例4: ask_feedback

# 需要導入模塊: import flask [as 別名]
# 或者: from flask import after_this_request [as 別名]
def ask_feedback():
    @flask.after_this_request
    def add_headers(response):
        response.headers['Access-Control-Allow-Origin'] = '*'
        return response
    return '10', 200
    values = flask.request.values
    now = datetime.now()
    try:
        user_id = Session.get_user_id(values['url'])
    except:
        return 'Incorrect user_id used', 400
    settings = ndb.Key(UserSettings, user_id).get()
    if settings is None:
        # Create settings for the current user
        settings = UserSettings(id=user_id, ts=now)
    if settings.mute_deadline is not None and settings.mute_deadline > now:
        return '0', 200
    questionnaire_left = 10
    for prev_shown_ts in reversed(settings.questionnaire_shown_ts):
        if prev_shown_ts < now - timedelta(hours=24):
            break
        questionnaire_left -= 1
    if random.random() < 0.5:
        # Suppress the popup for 50% of all SERPs.
        questionnaire_left = 0
    if questionnaire_left > 0:
        settings.questionnaire_shown_ts.append(now)
        settings.put()
    return str(questionnaire_left), 200 
開發者ID:varepsilon,項目名稱:cas-eval,代碼行數:32,代碼來源:main.py

示例5: log

# 需要導入模塊: import flask [as 別名]
# 或者: from flask import after_this_request [as 別名]
def log():
    @flask.after_this_request
    def add_headers(response):
        response.headers['Access-Control-Allow-Origin'] = '*'
        return response
    values = flask.request.values
    tab_id = values.get('tab_id', '')
    session = ndb.Key(Session, tab_id).get()
    if session is None:
        return 'No sessions with tab_id = %s' % tab_id, 404
    elif session.shared:
        return 'Cannot update previously shared session with tab_id = %s' % tab_id, 403
    try:
        user_id = Session.get_user_id(values['url'])
    except:
        return 'Incorrect user_id used', 400
    if session.user_id != user_id:
        return 'Session does not belong to %s' % user_id, 403
    try:
        if 'buffer' in values:
            buffer = json.loads(values['buffer'])
        else:
            buffer = [flask.request.url.split('?', 1)[-1]]
        actions = []
        for log_str in buffer:
            log_item = urlparse.parse_qs(log_str)
            ts = Session.convert_time(log_item['time'][0])
            event_type = log_item.get('ev', ['UNKNOWN'])[0]
            fields = {k: v[0] for (k, v) in log_item.iteritems() if k not in ['ev', 'time']}
            actions.append(Action(ts=ts, event_type=event_type, fields=fields))
        session.actions += actions
        session.put()
        return 'Updated', 200

    except Exception as e:
        app.logger.error(e)
        app.logger.error('Buffer: %s' % values.get('buffer', ''))
        return 'Incorrect buffer contents', 400 
開發者ID:varepsilon,項目名稱:cas-eval,代碼行數:40,代碼來源:main.py

示例6: joke

# 需要導入模塊: import flask [as 別名]
# 或者: from flask import after_this_request [as 別名]
def joke():
    res = requests.get('https://icanhazdadjoke.com/', headers=dict(Accept='text/plain'))
    res.raise_for_status()

    @after_this_request
    def after_joke(response):
        print('Hook: after_this_request')
        return response

    return res.content 
開發者ID:DataDog,項目名稱:trace-examples,代碼行數:12,代碼來源:app.py


注:本文中的flask.after_this_request方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。