-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathuser_agents.py
45 lines (34 loc) · 1.63 KB
/
user_agents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Copyright 2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
# Regular expression to detect Japanese Tier-2 mobile phones.
JP_TIER2_MOBILE_USER_AGENT_RE = re.compile(
r'^(KDDI|DoCoMo|SoftBank|J-PHONE|Vodafone)')
# Regular expression to detect phones which prefer Shift_JIS charset.
# Some KDDI phones support UTF-8 but they have a bug encoding UTF-8 query
# parameters.
SJIS_PREFERRED_USER_AGENT_RE = re.compile(r'^KDDI')
def is_jp_tier2_mobile_phone(request):
"""Returns True if the user agent is a Japanese Tier-2 mobile phone.
TODO(nworden): clean this up once we're fully on Django.
"""
user_agent = request.headers.get('User-Agent')
return user_agent and JP_TIER2_MOBILE_USER_AGENT_RE.match(user_agent)
def prefer_lite_ui(request):
user_agent = request.META.get('HTTP_USER_AGENT', None)
return user_agent and JP_TIER2_MOBILE_USER_AGENT_RE.match(user_agent)
def prefer_sjis_charset(request):
"""Returns True if Shift_JIS charset should be used for the user agent."""
user_agent = request.headers.get('User-Agent')
return user_agent and SJIS_PREFERRED_USER_AGENT_RE.match(user_agent)