Anonymous committed d5ad678

pep8 and pyflakes fixes

  • Participants
  • Parent commits f510443

Comments (0)

Files changed (4)

 Fork of:
 Fork Description: I reorganized the code, added caching, and made a few tweaks here and there.
 Description: Django middleware and view decorator to detect phones and small-screen devices
-Version: 0.1.6
-Last Update: 9/30/2011
+Version: 0.1.7
+Last Update: 11/17/2013


-from mobi.useragents import search_strings
 from mobi.middleware import MobileDetectionMiddleware
 from functools import wraps
     def detected(request, *args, **kwargs):
         return view(request, *args, **kwargs)
-    detected.__doc__ = "%s\n[Wrapped by detect_mobile which detects if the request is from a phone]" % view.__doc__
+    detected.__doc__ = "%s\n[Wrapped by detect_mobile which detects if the " \
+        "request is from a phone]" % view.__doc__
     return detected


 from mobi.useragents import search_strings, load_tablet_strings
 MOBI_USER_AGENT_IGNORE_LIST = getattr(settings,
-                                        'MOBI_USER_AGENT_IGNORE_LIST', list())
+                                      'MOBI_USER_AGENT_IGNORE_LIST', list())
 MOBI_DETECT_TABLET = getattr(settings, 'MOBI_DETECT_TABLET', False)
                 return True
     return False
 class MobileDetectionMiddleware(object):
     def process_request(request):
            depending on whether the request should be considered to come from a
            small-screen device such as a phone or a PDA"""
-        if request.META.has_key("HTTP_X_OPERAMINI_FEATURES"):
+        if "HTTP_X_OPERAMINI_FEATURES" in request.META:
             #Then it's running opera mini. 'Nuff said.
             #Reference from:
    = True
             return None
-        if request.META.has_key("HTTP_ACCEPT"):
+        if "HTTP_ACCEPT" in request.META:
             s = request.META["HTTP_ACCEPT"].lower()
             if 'application/vnd.wap.xhtml+xml' in s:
                 # Then it's a wap browser
        = True
                 return None
-        if request.META.has_key("HTTP_USER_AGENT"):
+        if "HTTP_USER_AGENT" in request.META:
             # This takes the most processing. Surprisingly enough, when I
             # Experimented on my own machine, this was the most efficient
             # algorithm. Certainly more so than regexes.
     return is_tablet
 class MobileRedirectMiddleware(object):
-    # Add MOBI_REDIRECT_URL to your file with a fully qualified 
+    # Add MOBI_REDIRECT_URL to your file with a fully qualified
     # url that you want to redirect mobile clients too.
     # i.e.
     MOBI_REDIRECT_URL = getattr(settings, 'MOBI_REDIRECT_URL', None)
-    #---------------------------------------------------------------------------
     def process_request(self, request):
         do_redirect = False
             if is_mobile:
                 do_redirect = True
-        if do_redirect and MOBI_REDIRECT_URL:
+        if do_redirect and self.MOBI_REDIRECT_URL:
              # tell adaptation services (transcoders and proxies) to not
              # alter the content based on user agent as it's already being
              # managed by this script
-             response = HttpResponseRedirect(MOBI_REDIRECT_URL)
-             response['Cache-Control'] = 'no-transform'
-             response['Vary'] = 'User-Agent, Accept'
-             return response
+            response = HttpResponseRedirect(self.MOBI_REDIRECT_URL)
+            response['Cache-Control'] = 'no-transform'
+            response['Vary'] = 'User-Agent, Accept'
+            return response
             return None


 from django.core.cache import cache
-# 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)
     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
     with open(os.path.join(os.path.dirname(__file__), file_name)) as f:
         ss =
     return agents
-search_strings = load_strings_from_file('MOBI_USER_AGENT', 'search_strings.txt')
+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')
+    return load_strings_from_file('MOBI_TABLE_USER_AGENT',
+                                  'tablet_strings.txt')