Hi Ross,

I produced a patch that adds the is_mobile boolean flag to a dist if
the useragent belongs to a mobile device.
Ptch is made on the last dev revision pulled from mercurial.
It's actually a simple patch that modifies no more than ten lines of
code, hope this helps!

Now you could make:

from gluon.contrib import user_agent_parser
ua = user_agent_parser.detect(request.env.http_user_agent)
if ua.dist.is_mobile:
   ...
else:
   ...

2011/8/23 Anthony <abasta...@gmail.com>:
> Also, note that you can access user_agent_parser.py by calling
> request.user_agent(). The nice thing about the latter is that it will store
> the result in the session (as session._user_agent), so it only needs to
> parse the user agent once per session.
>
> Anthony
> On Tuesday, August 23, 2011 1:30:36 PM UTC-4, AngeloC wrote:
>>
>> Hi Ross,
>>
>> I'm looking into your user_agent_parser.py but I cannot find something
>> like the mobile detector.
>>
>> I'll explain better. With user_agent_parser.py you can look for
>> browser version and os version, but there isn't an easy way to dectect
>> if a browser is mobile or not, or I'm missing something?
>>
>> I think that a decorator for mobile versions or a global variable that
>> tells me a mobile browser wants mobile view is really necessary.
>>
>> I think it could be a really great and appreciated addition to the
>> framework!
>>
>> If you have somthing in mind, please share with us, I can contribute
>> to code and help testing.
>>
>> Thank you!
>>
>> 2011/8/18 Ross Peoples <ross.p...@gmail.com>:
>>
>> > We already have a user_agent_parser.py in contrib. Isn't that the same
>> > thing?
>> > Usage:
>> > import contrib.user_agent_parser
>> > user_agent_parser.simple_detect(request.env.http_user_agent)
>> > By the way, didn't you add something to the Google Issues page for the
>> > proposal saying you were getting an error from that? If so can you give
>> > me
>> > the user agent string that caused it? I asked for it on the issue, but I
>> > figured you didn't have notifications turned on and never got the
>> > message.
>
# HG changeset patch
# User Angelo Compagnucci <angelo.compagnu...@gmail.com>
# Date 1314136920 -7200
# Node ID eb6774c91adac9a064e8d361f2cc3f1ea0dfe955
# Parent  2c1f4c7f4f8e7e286d561688a16b08d139d5ea62
Added is_mobile flag in each dist that is actually a mobile device.
If a device is a mobile one, the resulting Storage has the attribute "is_mobile" in dist set to True.

diff -r 2c1f4c7f4f8e -r eb6774c91ada gluon/contrib/user_agent_parser.py
--- a/gluon/contrib/user_agent_parser.py	Mon Aug 22 22:58:57 2011 -0500
+++ b/gluon/contrib/user_agent_parser.py	Wed Aug 24 00:02:00 2011 +0200
@@ -72,8 +72,11 @@
         if agent and self.checkWords(agent):
             result[self.info_type] = Storage(name=self.name)
             version = self.getVersion(agent)
+            is_mobile = getattr(self, 'is_mobile', False)
             if version:
                 result[self.info_type].version = version
+            if is_mobile:
+                result[self.info_type].is_mobile = is_mobile
             return True
         return False
 
@@ -81,7 +84,7 @@
         for w in self.skip_if_found:
             if w in agent:
                 return False
-        if self.look_for:
+        if self.look_for in agent:
             return True
         return False
 
@@ -234,6 +237,7 @@
 
 class Android(Dist):
     look_for = 'Android'
+    is_mobile = True
 
     def getVersion(self, agent):
         return agent.split('Android')[-1].split(';')[0].strip()
@@ -241,6 +245,7 @@
 
 class iPhone(Dist):
     look_for = 'iPhone'
+    is_mobile = True
 
     def getVersion(self, agent):
         version_end_chars = ['like', ';', ')']
@@ -253,6 +258,7 @@
 
 class iPad(Dist):
     look_for = 'iPad'
+    is_mobile = True
 
     def getVersion(self, agent):
         version_end_chars = ['like', ';', ')']

Reply via email to