Detecting user devices

264 views
Skip to first unread message

Neda Jafarian

unread,
Jun 17, 2021, 5:13:40 AM6/17/21
to oTree help & discussion
Hi,

I have a task in my experiment that can't be done on mobile phones. I am trying to detect the user devices by this library:
https://pypi.org/project/user-agents/
But I can't figure out how to get the user agent string in the oTree.
Would you please help me with this?

Best regards,
Neda

Chris @ oTree

unread,
Jun 17, 2021, 5:16:01 AM6/17/21
to oTree help & discussion
See the detect_mobile example here: https://www.otreehub.com/projects/otree-snippets/

Pipoca

unread,
Jun 25, 2021, 2:34:37 AM6/25/21
to oTree help & discussion
Hi! I've noticed that the detect_mobile app doesn't allow you to continue even if you try to access it again with a desktop pc or laptop. How should we edit that code to allow access in that scenario?

Pipoca

unread,
Jun 25, 2021, 2:39:18 AM6/25/21
to oTree help & discussion
In oTree <5, you could just have a single page without a next button that is displayed if the user is not using a mobile device. If you did the switch to valid device, that page wasn't displayed. The backend was like this:

class TestMobile(Page):
    def is_displayed(self):
        # Here, we'll obtain the type of user we've
        user_agent = self.request.META['HTTP_USER_AGENT']
        # Default value
        self.participant.vars['is_mobile'] = False
        # The default is changed if the player is mobile
        for substring in ['Mobi', 'Android']:
            if substring in user_agent:
                self.participant.vars['is_mobile'] = True
        return self.participant.vars['is_mobile']


Is there a way to do the same with oTree >= 5?

Chris @ oTree

unread,
Jun 25, 2021, 2:49:14 AM6/25/21
to oTree help & discussion
Good point, I just fixed it!

Pipoca

unread,
Jun 25, 2021, 3:19:04 AM6/25/21
to oTree help & discussion
Do you mean you fixed otree 5 or the example? haha, I've just updated my otree installation, but I keep getting this error:
 user_agent = self.request.META['HTTP_USER_AGENT']
AttributeError: 'Request' object has no attribute 'META'

Chris @ oTree

unread,
Jun 25, 2021, 3:21:50 AM6/25/21
to oTree help & discussion
I mean that i fixed the example in otree-snippets. The request.META stuff is not going to work in oTree Lite.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages