misc: add request.is_from_mobile variable (#19942) #1347
|
@ -0,0 +1,49 @@
|
|||
import pytest
|
||||
|
||||
from wcs.qommon.http_request import HTTPRequest
|
||||
from wcs.variables import LazyRequest
|
||||
|
||||
from .utilities import clean_temporary_pub, create_temporary_pub
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def pub():
|
||||
return create_temporary_pub()
|
||||
|
||||
|
||||
def teardown_module(module):
|
||||
clean_temporary_pub()
|
||||
|
||||
|
||||
def test_is_in_backoffice(pub):
|
||||
req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
|
||||
assert not req.is_in_backoffice()
|
||||
assert not LazyRequest(req).is_in_backoffice
|
||||
req = HTTPRequest(None, {'SCRIPT_NAME': '/backoffice/test', 'SERVER_NAME': 'example.net'})
|
||||
assert req.is_in_backoffice()
|
||||
assert LazyRequest(req).is_in_backoffice
|
||||
|
||||
|
||||
def test_is_from_mobile(pub):
|
||||
req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
|
||||
assert not req.is_from_mobile()
|
||||
assert not LazyRequest(req).is_from_mobile
|
||||
req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net', 'HTTP_USER_AGENT': 'bot/1.0'})
|
||||
assert not req.is_from_mobile()
|
||||
assert not LazyRequest(req).is_from_mobile
|
||||
req = HTTPRequest(
|
||||
None,
|
||||
{'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net', 'HTTP_USER_AGENT': 'Mozilla/5.0 (Mobile) plop'},
|
||||
)
|
||||
assert req.is_from_mobile()
|
||||
assert LazyRequest(req).is_from_mobile
|
||||
req = HTTPRequest(
|
||||
None,
|
||||
{
|
||||
'SCRIPT_NAME': '/',
|
||||
'SERVER_NAME': 'example.net',
|
||||
'HTTP_USER_AGENT': 'Mozilla/5.0 (Chrome) Mobile Safari',
|
||||
},
|
||||
)
|
||||
assert req.is_from_mobile()
|
||||
assert LazyRequest(req).is_from_mobile
|
|
@ -28,6 +28,8 @@ from quixote.errors import RequestError
|
|||
|
||||
from .http_response import HTTPResponse
|
||||
|
||||
user_agent_regex = re.compile(r'(?P<product>.*?)(?P<comment>\(.*?\))(?P<rest>.*)')
|
||||
|
||||
|
||||
class HTTPRequest(quixote.http_request.HTTPRequest):
|
||||
signed = False
|
||||
|
@ -222,6 +224,18 @@ class HTTPRequest(quixote.http_request.HTTPRequest):
|
|||
or user_agent.startswith('Wget')
|
||||
)
|
||||
|
||||
def is_from_mobile(self):
|
||||
user_agent = self.get_environ('HTTP_USER_AGENT', '')
|
||||
try:
|
||||
dummy, comment, rest = user_agent_regex.match(user_agent).groups()
|
||||
except AttributeError:
|
||||
return False
|
||||
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent#mobile_tablet_or_desktop
|
||||
# Mozilla (Gecko, Firefox) / Mobile or Tablet inside the comment
|
||||
# WebKit-based (Android, Safari) / Mobile Safari token outside the comment
|
||||
# Blink-based (Chromium, etc.) / Mobile Safari token outside the comment
|
||||
return bool('Mobile' in comment or 'Tablet' in comment or 'Mobile Safari' in rest)
|
||||
|
||||
def has_anonymised_data_api_restriction(self):
|
||||
from wcs.api_access import ApiAccess
|
||||
|
||||
|
|
|
@ -1994,6 +1994,10 @@ class LazyRequest:
|
|||
def is_in_backoffice(self):
|
||||
return self._request.is_in_backoffice()
|
||||
|
||||
@property
|
||||
def is_from_mobile(self):
|
||||
return self._request.is_from_mobile()
|
||||
|
||||
@property
|
||||
def method(self):
|
||||
return self._request.method
|
||||
|
|
Loading…
Reference in New Issue