Roman Lyashov avatar Roman Lyashov committed ac9d95f

added support for tablets, request.tablet is set when enabled with MOBI_DETECT_TABLET = True

Comments (0)

Files changed (3)


 from django.conf import settings
 from django.http import HttpResponseRedirect
-from mobi.useragents import search_strings
+from mobi.useragents import search_strings, load_tablet_strings
 MOBI_USER_AGENT_IGNORE_LIST = getattr(settings,
                                         'MOBI_USER_AGENT_IGNORE_LIST', list())
+MOBI_DETECT_TABLET = getattr(settings, 'MOBI_DETECT_TABLET', False)
 def ignore_user_agent(user_agent):
     """ compare the useragent from the broswer to the ignore list
         This is popular if you want a mobile device to not trigger
             for ua in search_strings:
                 if ua in s:
                     # check if we are ignoring this user agent: (IPad)
                     if not ignore_user_agent(s):
                = True
+                        if MOBI_DETECT_TABLET:
+                            request.tablet = _is_tablet(s)
                         return None
         #Otherwise it's not a mobile = False
         return None
+def _is_tablet(s):
+    is_tablet = False
+    print s
+    tablet_strings = load_tablet_strings()
+    for ta in tablet_strings:
+        if ta == '__android__not_mobile__':
+            if 'android' in s and not 'mobile' in s:
+                is_tablet = True
+                break
+        if ta in s:
+            is_tablet = True
+            #print tablet_strings
+            break
+    return is_tablet
 class MobileRedirectMiddleware(object):


+# list based on
+# __android__not__mobile__, is a special key, means with 'android' but without 'mobile', which is what tablets have
+android 3
+dell streak


 from django.core.cache import cache
-def load_from_search_strings_file():
+# def load_from_search_strings_file():
+#     return load_strings_from_file('MOBI_USER_AGENT', 'search_strings.txt')
+def load_strings_from_file(cache_key, file_name):
     CACHE_TIMEOUT = 86400
-    agents = cache.get(CACHE_KEY)
+    agents = cache.get(cache_key)
     if agents:
         # we got something, we are done, send it back.
         return agents
     # it wasn't in the cache, get it from the file, then store in the cache
-    f = None
-    try:
-        f = open(os.path.join(os.path.dirname(__file__), 'search_strings.txt'))
+    with open(os.path.join(os.path.dirname(__file__), file_name)) as f:
         ss = f.readlines()
-    finally:
-        if f:
-            f.close()
-    agents = [s.strip() for s in ss if not s.startswith('#')]
+    agents = [s.strip() for s in ss if s and not s.startswith('#')]
     # store to the cache
-    cache.set(CACHE_KEY, agents, CACHE_TIMEOUT)
+    cache.set(cache_key, agents, CACHE_TIMEOUT)
     return agents
-search_strings = load_from_search_strings_file()
+search_strings = load_strings_from_file('MOBI_USER_AGENT', 'search_strings.txt')
+def load_tablet_strings():
+    return load_strings_from_file('MOBI_TABLE_USER_AGENT', 'tablet_strings.txt')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.