Skip to content

Commit

Permalink
Issue #1030. Rewrite get_browser.
Browse files Browse the repository at this point in the history
Default None arg.
Return "Unknown" by default.
Switch position of "(Tablet)" modifier to come before the version string.
Normalize "Other" result to "Unknown" for weird values.
  • Loading branch information
Mike Taylor committed Apr 29, 2016
1 parent 459346c commit 5eb5505
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions webcompat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,28 +99,37 @@ def get_user_info():
session['avatar_url'] = gh_user.get('avatar_url')


def get_browser(user_agent_string):
def get_browser(user_agent_string=None):
'''Return browser name family and version.
It will pre-populate the bug reporting form.
'''
ua_dict = user_agent_parser.Parse(user_agent_string)
ua = ua_dict.get('user_agent')
name = ua.get('family')
version = ua.get('major', u'Unknown')
# Add on the minor and patch version numbers if they exist
if version != u'Unknown' and ua.get('minor'):
version = version + "." + ua.get('minor')
if ua.get('patch'):
version = version + "." + ua.get('patch')
else:
version = ''
# Check for tablet devices
if ua_dict.get('device').get('model') == 'Tablet':
model = ' (Tablet)'
else:
model = ''
return '{0} {1}{2}'.format(name, version, model)
if user_agent_string:
ua_dict = user_agent_parser.Parse(user_agent_string)
ua = ua_dict.get('user_agent')
name = ua.get('family')
version = ua.get('major', u'Unknown')
# Add on the minor and patch version numbers if they exist
if version != u'Unknown' and ua.get('minor'):
version = version + "." + ua.get('minor')
if ua.get('patch'):
version = version + "." + ua.get('patch')
else:
version = ''
# Check for tablet devices
if ua_dict.get('device').get('model') == 'Tablet':
model = '(Tablet) '
else:
model = ''
rv = '{0} {1}{2}'.format(name, model, version)
# bizarre UA strings can be parsed like so:
# {'major': None, 'minor': None, 'family': 'Other', 'patch': None}
# but we want to return "Unknown", rather than "Other"
print(rv)
if rv.strip().lower() == "other":
return "Unknown"
return rv
return "Unknown"


def get_browser_name(user_agent_string=None):
Expand Down

0 comments on commit 5eb5505

Please sign in to comment.