greyli / helloflask

Hello, Flask!
https://docs.helloflask.com
MIT License
1.87k stars 2.53k forks source link

【勘误】第 15 章:在移动设备上隐藏 #223

Closed yuxiaoy1 closed 3 years ago

yuxiaoy1 commented 3 years ago

Meta

错误详细信息

应增加针对 platform 是否为 None 的判断,在不人为更改请求报文中 User Agent 的情况下,该值在运行单元测试时为 None,原始代码无法通过单元测试。 如下代码应该由:

if current_app.config['SHARE_HIDE_ON_MOBILE']:
    platform = request.user_agent.platform
    mobile_pattern = re.compile('android|fennec|iemobile|iphone|opera (?:mini|mobi)')
    m = re.match(mobile_pattern, platform)
    if m is not None:
        return ''

更改为:

if current_app.config['SHARE_HIDE_ON_MOBILE']:
    platform = request.user_agent.platform
    if platform is not None:
        mobile_pattern = re.compile('android|fennec|iemobile|iphone|opera (?:mini|mobi)')
        m = re.match(mobile_pattern, platform)
        if m is not None:
            return ''
greyli commented 3 years ago

看了下源码是对的,这里大概忘记同步更改了。已添加到勘误表,谢谢!