Coverage for an_website / redirect / redirect.py: 89.474%
19 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-24 17:35 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-24 17:35 +0000
1# This program is free software: you can redistribute it and/or modify
2# it under the terms of the GNU Affero General Public License as
3# published by the Free Software Foundation, either version 3 of the
4# License, or (at your option) any later version.
5#
6# This program is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9# GNU Affero General Public License for more details.
10#
11# You should have received a copy of the GNU Affero General Public License
12# along with this program. If not, see <https://www.gnu.org/licenses/>.
14"""
15The redirect page of the website.
17This page is used to redirect users to third party websites.
18This page will ask users if they want to leave this website.
19"""
21from urllib.parse import urlsplit
23from tornado.web import Application
25from .. import pytest_is_running
26from ..utils.request_handler import HTMLRequestHandler
27from ..utils.utils import ModuleInfo
30def get_module_info() -> ModuleInfo:
31 """Create and return the ModuleInfo for this module."""
32 return ModuleInfo(
33 handlers=((r"/redirect", RedirectPage),),
34 name="Weiterleitungsseite",
35 description=(
36 "Seite, die User davon abhält versehentlich eine fremde "
37 "Webseite zu besuchen"
38 ),
39 path="/redirect",
40 short_name="Weiterleitung",
41 hidden=True,
42 required_background_tasks=(_confirm_loading,),
43 )
46async def _confirm_loading(app: Application, worker: int | None) -> None:
47 """Save in settings that the redirect module has been loaded."""
48 app.settings["REDIRECT_MODULE_LOADED"] = worker or True # noqa: SIM222
51class RedirectPage(HTMLRequestHandler):
52 """The redirect page that redirects you to another page."""
54 async def get(self, *, head: bool = False) -> None:
55 """Handle GET requests to the redirect page."""
56 # pylint: disable=unused-argument
58 redirect_url = self.get_argument("to", None) or "/"
59 referrer = self.request.headers.get_list("Referer")
60 from_url = referrer[0] if referrer else None
62 if redirect_url.startswith("/"):
63 # it is a local URL, so just redirect
64 # use fix_url to maybe add no_3rd_party
65 return self.redirect(self.fix_url(redirect_url))
67 if (
68 (domain := urlsplit(redirect_url).hostname)
69 and domain.endswith(".asozial.org")
70 and not pytest_is_running()
71 ):
72 return self.redirect(redirect_url)
74 await self.render(
75 "pages/redirect.html",
76 redirect_url=redirect_url,
77 send_referrer=False,
78 from_url=from_url,
79 discord=False,
80 )