Coverage for app/venv/lib/python3.14/site-packages/weblate/accounts/strategy.py: 35%
45 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
1# Copyright © Michal Čihař <michal@weblate.org>
2#
3# SPDX-License-Identifier: GPL-3.0-or-later
5from importlib import import_module
6from urllib.parse import urlparse
8from django.conf import settings
9from django.urls import reverse
10from django.utils.functional import cached_property
11from django.utils.http import url_has_allowed_host_and_scheme
12from social_django.strategy import DjangoStrategy
14from weblate.utils.site import get_site_url
17def create_session(*args):
18 engine = import_module(settings.SESSION_ENGINE)
19 return engine.SessionStore(*args)
22class WeblateStrategy(DjangoStrategy):
23 def __init__(self, storage, request=None, tpl=None) -> None:
24 """Restore session data based on passed ID."""
25 super().__init__(storage, request, tpl)
26 if request and "verification_code" in request.GET and "id" in request.GET:
27 self.session = create_session(request.GET["id"])
29 @cached_property
30 def _site_url(self):
31 return urlparse(get_site_url())
33 def request_data(self, merge=True):
34 if not self.request:
35 return {}
36 if merge:
37 data = self.request.GET.copy()
38 data.update(self.request.POST)
39 elif self.request.method == "POST":
40 data = self.request.POST.copy()
41 else:
42 data = self.request.GET.copy()
43 # This is mostly fix for lack of next validation in Python Social Auth
44 # - https://github.com/python-social-auth/social-core/pull/92
45 # - https://github.com/python-social-auth/social-core/issues/62
46 if "next" in data and not url_has_allowed_host_and_scheme(
47 data["next"], allowed_hosts=None
48 ):
49 data["next"] = "{}#account".format(reverse("profile"))
50 return data
52 def build_absolute_uri(self, path=None):
53 if self.request:
54 self.request._current_scheme_host = get_site_url() # noqa: SLF001
55 return super().build_absolute_uri(path)
57 def clean_partial_pipeline(self, token) -> None:
58 # The cleanup somehow breaks our partial pipelines, simply skip
59 # it for now
60 # See https://github.com/python-social-auth/social-core/issues/287
61 return
63 def really_clean_partial_pipeline(self, token) -> None:
64 super().clean_partial_pipeline(token)
66 def request_is_secure(self):
67 return settings.ENABLE_HTTPS
69 def request_port(self):
70 return self._site_url.port
72 def request_host(self):
73 return self._site_url.hostname