Hi Martin,

In trunk the correct syntax should be:

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

Or better:

ua = request.user_agent()
if ua.is_mobile: ...

The mothod user_agent() search in session if the user_agent was
already parsed, so in the end it parses the user agent only first
time.

2011/8/29 Martin Weissenboeck <mweis...@gmail.com>:
> I tried:
>
> def mobile():
>     from gluon.contrib import user_agent_parser
>     ua = user_agent_parser.detect(request.env.http_user_agent)
>     if ua.dist.is_mobile: ....
> and i got:
> AttributeError: 'NoneType' object has no attribute 'is_mobile'
> Browsers: Opera 11.50, Safari 3.1.2
> (1) What is wrong?
> (2) There have been a lot of proposals how to change every view from .html
> to .m.html  if is_mobile is true.
> Now I am a little bit confused: what would be the best code following   if
> ua.dist.is_mobile...?
> Regards, Martin
> 2011/8/28 Massimo Di Pierro <massimo.dipie...@gmail.com>
>>
>> I have no objection.What do others think?
>>
>> On Aug 28, 10:53 am, Angelo Compagnucci <angelo.compagnu...@gmail.com>
>> wrote:
>> > HI Ross, Massimo,
>> >
>> > I wrote a small decorator to use the newly added is_mobile flag:
>> >
>> > class mobilize(object):
>> >     def __init__(self, func):
>> >         self.func = func
>> >     def __call__(self):
>> >         from gluon import current
>> >         if current.session._user_agent:
>> >             if current.session._user_agent.is_mobile:
>> >                 current.response.view = \
>> >                     current.response.view.split(".")[0] + ".mobi"
>> >         return self.func()
>> >
>> > It should be included at the bottom of user_agent_parser.py.
>> >
>> > With this you can have automatically selected the view.html or the
>> > view.mobi depending on your browser is mobile or not, an example could
>> > be:
>> >
>> > @mobilize
>> > def index():
>> >     """
>> >     example action using the mobilizer decorator it is
>> >     rendered by views/default/index.html or
>> >     views/default/index.mobi depending if your browser
>> >     is mobile or not
>> >     """
>> >     return dict(message=response.view)
>> >
>> > Could this be added in trunk?
>> >
>> > 2011/8/27 Massimo Di Pierro <massimo.dipie...@gmail.com>:
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> > > This is in trunk as of last night.
>> >
>> > > On Aug 26, 12:13 pm, Ross Peoples <ross.peop...@gmail.com> wrote:
>> > >> I submitted this to Massimo for inclusion. Now we wait :)
>
>

Reply via email to