From 797df2b73e4d66b1f3cad2e7fe06a038ab1c3296 Mon Sep 17 00:00:00 2001 From: Michael An <2331806369@qq.com> Date: Mon, 6 May 2024 12:05:44 +0800 Subject: [PATCH] change user scalable in different devices (#6051) --- seahub/base/middleware.py | 79 +++++++++++++++++++ seahub/settings.py | 1 + seahub/templates/base.html | 4 + seahub/templates/base_for_react.html | 4 + .../templates/markdown_file_view_react.html | 4 + .../plain_markdown_file_view_react.html | 4 + .../templates/two_factor/_base.html | 6 +- 7 files changed, 101 insertions(+), 1 deletion(-) diff --git a/seahub/base/middleware.py b/seahub/base/middleware.py index 92e89a4dd38..708e7b53f7e 100644 --- a/seahub/base/middleware.py +++ b/seahub/base/middleware.py @@ -98,3 +98,82 @@ def process_request(self, request): if request.session.get('force_passwd_change', False): if self._request_in_black_list(request): return HttpResponseRedirect(reverse('auth_password_change')) + +class UserAgentMiddleWare(MiddlewareMixin): + user_agents_test_match = ( + "w3c ", "acs-", "alav", "alca", "amoi", "audi", + "avan", "benq", "bird", "blac", "blaz", "brew", + "cell", "cldc", "cmd-", "dang", "doco", "eric", + "hipt", "inno", "ipaq", "java", "jigs", "kddi", + "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-", + "maui", "maxo", "midp", "mits", "mmef", "mobi", + "mot-", "moto", "mwbp", "nec-", "newt", "noki", + "xda", "palm", "pana", "pant", "phil", "play", + "port", "prox", "qwap", "sage", "sams", "sany", + "sch-", "sec-", "send", "seri", "sgh-", "shar", + "sie-", "siem", "smal", "smar", "sony", "sph-", + "symb", "t-mo", "teli", "tim-", "tosh", "tsm-", + "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", + "wapi", "wapp", "wapr", "webc", "winw", "xda-",) + user_agents_test_search = u"(?:%s)" % u'|'.join(( + 'up.browser', 'up.link', 'mmp', 'symbian', 'smartphone', 'midp', + 'wap', 'phone', 'windows ce', 'pda', 'mobile', 'mini', 'palm', + 'netfront', 'opera mobi', + )) + user_agents_exception_search = u"(?:%s)" % u'|'.join(( + 'ipad', + )) + http_accept_regex = re.compile("application/vnd\.wap\.xhtml\+xml", re.IGNORECASE) + user_agents_android_search = u"(?:android)" + user_agents_mobile_search = u"(?:mobile)" + user_agents_tablets_search = u"(?:%s)" % u'|'.join(('ipad', 'tablet', )) + + def __init__(self, get_response=None): + self.get_response = get_response + + # these for detect mobile + user_agents_test_match = r'^(?:%s)' % '|'.join(self.user_agents_test_match) + self.user_agents_test_match_regex = re.compile(user_agents_test_match, re.IGNORECASE) + self.user_agents_test_search_regex = re.compile(self.user_agents_test_search, re.IGNORECASE) + self.user_agents_exception_search_regex = re.compile(self.user_agents_exception_search, re.IGNORECASE) + + # these three used to detect tablet + self.user_agents_android_search_regex = re.compile(self.user_agents_android_search, re.IGNORECASE) + self.user_agents_mobile_search_regex = re.compile(self.user_agents_mobile_search, re.IGNORECASE) + self.user_agents_tablets_search_regex = re.compile(self.user_agents_tablets_search, re.IGNORECASE) + + def process_request(self, request): + is_mobile = False + is_tablet = False + + if 'HTTP_USER_AGENT' in request.META: + user_agent = request.META['HTTP_USER_AGENT'] + + # Test common mobile values. + if self.user_agents_test_search_regex.search(user_agent) and \ + not self.user_agents_exception_search_regex.search(user_agent): + is_mobile = True + else: + # Nokia like test for WAP browsers. + # http://www.developershome.com/wap/xhtmlmp/xhtml_mp_tutorial.asp?page=mimeTypesFileExtension + + if 'HTTP_ACCEPT' in request.META: + http_accept = request.META['HTTP_ACCEPT'] + if self.http_accept_regex.search(http_accept): + is_mobile = True + + if not is_mobile: + # Now we test the user_agent from a big list. + if self.user_agents_test_match_regex.match(user_agent): + is_mobile = True + + # Ipad or Blackberry + if self.user_agents_tablets_search_regex.search(user_agent): + is_tablet = True + # Android-device. If User-Agent doesn't contain Mobile, then it's a tablet + elif (self.user_agents_android_search_regex.search(user_agent) and + not self.user_agents_mobile_search_regex.search(user_agent)): + is_tablet = True + + request.is_mobile = is_mobile + request.is_tablet = is_tablet diff --git a/seahub/settings.py b/seahub/settings.py index 80efb67f974..7d8f05535df 100644 --- a/seahub/settings.py +++ b/seahub/settings.py @@ -139,6 +139,7 @@ 'seahub.two_factor.middleware.ForceTwoFactorAuthMiddleware', 'seahub.trusted_ip.middleware.LimitIpMiddleware', 'seahub.organizations.middleware.RedirectMiddleware', + 'seahub.base.middleware.UserAgentMiddleWare', ] SITE_ROOT_URLCONF = 'seahub.urls' diff --git a/seahub/templates/base.html b/seahub/templates/base.html index e2e056380f8..d889c46a533 100644 --- a/seahub/templates/base.html +++ b/seahub/templates/base.html @@ -8,7 +8,11 @@ {% if site_description != '' %}{% endif %} {% block viewport %} +{% if request.is_mobile or request.is_tablet %} +{% else %} + +{% endif %} {% endblock %} diff --git a/seahub/templates/base_for_react.html b/seahub/templates/base_for_react.html index c79d6b088da..daadec85d74 100644 --- a/seahub/templates/base_for_react.html +++ b/seahub/templates/base_for_react.html @@ -9,7 +9,11 @@ {% if site_description != '' %}{% endif %} {% block viewport %} +{% if request.is_mobile or request.is_tablet %} +{% else %} + +{% endif %} {% endblock %} {% block extra_ogp_tags %}{% endblock %} diff --git a/seahub/templates/markdown_file_view_react.html b/seahub/templates/markdown_file_view_react.html index 0c03da87927..2a8d09b9fb0 100644 --- a/seahub/templates/markdown_file_view_react.html +++ b/seahub/templates/markdown_file_view_react.html @@ -4,7 +4,11 @@ + {% if request.is_mobile or request.is_tablet %} + {% else %} + + {% endif %} {{ filename }} diff --git a/seahub/templates/plain_markdown_file_view_react.html b/seahub/templates/plain_markdown_file_view_react.html index 26a7e330e58..9aa1456e6c8 100644 --- a/seahub/templates/plain_markdown_file_view_react.html +++ b/seahub/templates/plain_markdown_file_view_react.html @@ -4,7 +4,11 @@ + {% if request.is_mobile or request.is_tablet %} + {% else %} + + {% endif %} {{ filename }} diff --git a/seahub/two_factor/templates/two_factor/_base.html b/seahub/two_factor/templates/two_factor/_base.html index 2f76b33681f..358d1e30412 100644 --- a/seahub/two_factor/templates/two_factor/_base.html +++ b/seahub/two_factor/templates/two_factor/_base.html @@ -2,7 +2,11 @@ {% block title %}{% endblock %} - + {% if request.is_mobile or request.is_tablet %} + + {% else %} + + {% endif %}