This commit is contained in:
Paul Konstantin Gerke 2023-07-05 00:18:52 +02:00 committed by Christopher Charbonneau Wells
parent c716334f02
commit c1eb9da7f3
2 changed files with 10 additions and 9 deletions

View File

@ -10,6 +10,7 @@ from django.utils import timezone, translation
from django.contrib.auth.middleware import RemoteUserMiddleware from django.contrib.auth.middleware import RemoteUserMiddleware
from django.http import HttpRequest from django.http import HttpRequest
class UserLanguageMiddleware: class UserLanguageMiddleware:
""" """
Customizes settings based on user language setting. Customizes settings based on user language setting.
@ -97,17 +98,17 @@ class HomeAssistant:
Django middleware that adds HomeAssistant specific properties and checks Django middleware that adds HomeAssistant specific properties and checks
to the request-object. to the request-object.
The middleware is only active if the settings variable The middleware is only active if the settings variable
`ENABLE_HOME_ASSISTANT_SUPPORT` is set to True. Note that some features `ENABLE_HOME_ASSISTANT_SUPPORT` is set to True. Note that some features
remain enabled even if the middleware is set to inactive through the remain enabled even if the middleware is set to inactive through the
settings. settings.
Features: Features:
- request.is_homeassistant_ingress_request (bool) - request.is_homeassistant_ingress_request (bool)
Indicates if a request was rerouted through the home assistant ingress Indicates if a request was rerouted through the home assistant ingress
service. This parameters is always present regardless of the service. This parameters is always present regardless of the
ENABLE_HOME_ASSISTANT_SUPPORT settings option. It defaults to false ENABLE_HOME_ASSISTANT_SUPPORT settings option. It defaults to false
if the middleware is disabled. if the middleware is disabled.
@ -139,11 +140,10 @@ class HomeAssistant:
url_parts._replace(path=x_ingress_path + url_parts.path) url_parts._replace(path=x_ingress_path + url_parts.path)
) )
return url return url
return wrapper return wrapper
request.build_absolute_uri = wrap_x_ingress_path( request.build_absolute_uri = wrap_x_ingress_path(request.build_absolute_uri)
request.build_absolute_uri
)
def __call__(self, request: HttpRequest): def __call__(self, request: HttpRequest):
if self.home_assistant_support_enabled: if self.home_assistant_support_enabled:
@ -156,4 +156,3 @@ class HomeAssistant:
self.__wrap_build_absolute_uri(request) self.__wrap_build_absolute_uri(request)
return self.get_response(request) return self.get_response(request)

View File

@ -67,4 +67,6 @@ class HomeAssistantMiddlewareTestCase(TestCase):
) )
json_response = json.loads(response.content) json_response = json.loads(response.content)
self.assertEqual(json_response["profile"], "http://testserver/magic/sub/url/api/profile") self.assertEqual(
json_response["profile"], "http://testserver/magic/sub/url/api/profile"
)