[ADD] Device detection and always minimize the sidebar on mobile

This commit is contained in:
Peter Pfeufer 2025-10-15 10:38:34 +02:00
parent bce90344f8
commit 67081ab465
No known key found for this signature in database
4 changed files with 70 additions and 1 deletions

View File

@ -3,7 +3,7 @@
{% load menu_menu_items %} {% load menu_menu_items %}
<div class="col-auto px-0"> <div class="col-auto px-0">
<div class="collapse collapse-horizontal {% if not request.session.MINIMIZE_SIDEBAR and user.is_authenticated %}show{% endif %}" tabindex="-1" id="sidebar"> <div class="collapse collapse-horizontal {% if user.is_authenticated and not request.is_mobile_device and not request.session.MINIMIZE_SIDEBAR %}show{% endif %}" tabindex="-1" id="sidebar">
<div> <div>
<div class="nav-padding navbar-dark text-bg-dark px-0 d-flex flex-column overflow-hidden vh-100 {% if not user.is_authenticated %}position-relative{% endif %}"> <div class="nav-padding navbar-dark text-bg-dark px-0 d-flex flex-column overflow-hidden vh-100 {% if not user.is_authenticated %}position-relative{% endif %}">
{% if user.is_authenticated %} {% if user.is_authenticated %}

View File

@ -0,0 +1,65 @@
"""
Alliance Auth Middleware
"""
from user_agents import parse
class DeviceDetectionMiddleware:
"""
Middleware to detect the type of device making the request.
Sets flags on the request object for easy access in views and templates.
Flags include:
- is_mobile: True if the device is a mobile phone.
- is_tablet: True if the device is a tablet.
- is_mobile_device: True if the device is either a mobile phone or a tablet.
- is_touch_capable: True if the device has touch capabilities.
- is_pc: True if the device is a desktop or laptop computer.
- is_bot: True if the device is identified as a bot or crawler.
"""
def __init__(self, get_response):
"""
Initialize the middleware with the get_response callable.
:param get_response:
:type get_response:
"""
self.get_response = get_response
def __call__(self, request):
"""
Process the incoming request to determine if it's from a mobile device.
This method is called when the middleware is invoked. It inspects the
`user-agent` header of the incoming HTTP request to determine the type
of client making the request (e.g., mobile, tablet, PC, bot, etc.).
Flags are set on the `request` object to indicate the client type.
:param request: The HTTP request object.
:type request: HttpRequest
:return: The HTTP response object after processing the request.
:rtype: HttpResponse
"""
# Retrieve the user-agent string from the request headers
user_agent_string = request.headers.get("user-agent", "")
# Parse the user-agent string to extract client information
user_agent = parse(user_agent_string)
# Set flags on the request object based on the client type
request.is_mobile = user_agent.is_mobile # True if the client is a mobile phone
request.is_tablet = user_agent.is_tablet # True if the client is a tablet
request.is_mobile_device = user_agent.is_mobile or user_agent.is_tablet # True if mobile phone or tablet
request.is_touch_capable = user_agent.is_touch_capable # True if the client supports touch input
request.is_pc = user_agent.is_pc # True if the client is a PC
request.is_bot = user_agent.is_bot # True if the client is a bot
# Pass the request to the next middleware or view and get the response
response = self.get_response(request)
# Return the processed response
return response

View File

@ -88,6 +88,7 @@ MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware", "django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware", "django.contrib.sessions.middleware.SessionMiddleware",
"allianceauth.authentication.middleware.UserSettingsMiddleware", "allianceauth.authentication.middleware.UserSettingsMiddleware",
"allianceauth.middleware.DeviceDetectionMiddleware",
"django.middleware.locale.LocaleMiddleware", "django.middleware.locale.LocaleMiddleware",
"django.middleware.common.CommonMiddleware", "django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware", "django.middleware.csrf.CsrfViewMiddleware",

View File

@ -61,11 +61,14 @@ dependencies = [
"passlib", "passlib",
"pydiscourse", "pydiscourse",
"python-slugify>=1.2", "python-slugify>=1.2",
"pyyaml",
"redis>=4", "redis>=4",
"requests>=2.9.1", "requests>=2.9.1",
"requests-oauthlib", "requests-oauthlib",
"semantic-version", "semantic-version",
"slixmpp<1.9", "slixmpp<1.9",
"ua-parser",
"user-agents",
] ]
optional-dependencies.docs = [ optional-dependencies.docs = [
"myst-parser", "myst-parser",