Compare commits

...

4 Commits

Author SHA1 Message Date
Peter Pfeufer
23dd987892 Merge branch 'hide-menu' into 'master'
Draft: [ADD] User setting to keep the sidebar menu minimized

See merge request allianceauth/allianceauth!1769
2025-10-15 08:41:02 +00:00
Peter Pfeufer
67081ab465
[ADD] Device detection and always minimize the sidebar on mobile 2025-10-15 10:40:44 +02:00
Peter Pfeufer
bce90344f8
[CHANGE] Always minimize when not authenticated
We have no user settings here, so keep it minimized on public pages.
2025-10-15 09:45:15 +02:00
Peter Pfeufer
29c6fa292a
[REMOVE] JS local storage usage
To make it an actual choice through the setting
2025-10-15 09:42:07 +02:00
6 changed files with 70 additions and 27 deletions

View File

@ -3,7 +3,7 @@
{% load menu_menu_items %}
<div class="col-auto px-0">
<div class="collapse collapse-horizontal" 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 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 %}

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.contrib.sessions.middleware.SessionMiddleware",
"allianceauth.authentication.middleware.UserSettingsMiddleware",
"allianceauth.middleware.DeviceDetectionMiddleware",
"django.middleware.locale.LocaleMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",

View File

@ -1,27 +1,6 @@
/* global sidebarSettings */
$(document).ready(() => {
'use strict';
const sidebar = document.getElementById('sidebar');
const sidebarKey = `sidebar_${sidebar.id}`;
sidebar.addEventListener('shown.bs.collapse', (event) => {
if (event.target.id === sidebar.id) {
localStorage.removeItem(sidebarKey);
}
});
sidebar.addEventListener('hidden.bs.collapse', (event) => {
if (event.target.id === sidebar.id) {
localStorage.setItem(sidebarKey, 'closed');
}
});
if (!sidebarSettings.minimizeSidebar) {
sidebar.classList.toggle('show', localStorage.getItem(sidebarKey) !== 'closed');
}
const activeChildMenuItem = document.querySelector('ul#sidebar-menu ul.collapse a.active');
if (activeChildMenuItem) {

View File

@ -102,11 +102,6 @@
</main>
<!-- End Body -->
<script>
const sidebarSettings = {
minimizeSidebar: {% if request.session.MINIMIZE_SIDEBAR %}true{% else %}false{% endif %}
};
</script>
{% include "bundles/auth-sidebar-collapse-js.html" %}
{% theme_js %}

View File

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