mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-12-06 21:01:42 +01:00
Compare commits
4 Commits
b321ebabf2
...
23dd987892
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
23dd987892 | ||
|
|
67081ab465 | ||
|
|
bce90344f8 | ||
|
|
29c6fa292a |
@ -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 %}
|
||||
|
||||
65
allianceauth/middleware.py
Normal file
65
allianceauth/middleware.py
Normal 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
|
||||
@ -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",
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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 %}
|
||||
|
||||
@ -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",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user