Coverage for an_website/troet/troet.py: 100.000%
30 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-16 19:56 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-16 19:56 +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"""Toot cool stuff to the world."""
16from __future__ import annotations
18from typing import cast
19from urllib.parse import urlencode, urlsplit
21from ..utils.request_handler import HTMLRequestHandler
22from ..utils.utils import ModuleInfo
25def get_module_info() -> ModuleInfo:
26 """Create and return the ModuleInfo for this module."""
27 return ModuleInfo(
28 handlers=((r"/troet", Troeter),),
29 name="Tröter",
30 description="Tröte witzige Sachen in die Welt",
31 path="/troet",
32 aliases=("/toot",),
33 keywords=("Tröt", "Mastodon", "Toot"),
34 hidden=True, # TODO: remove this line
35 )
38class Troeter(HTMLRequestHandler):
39 """The request handler that makes tooting easier."""
41 async def get(self, *, head: bool = False) -> None:
42 """Handle GET requests to the page."""
43 text: str = self.get_argument("text", "")
44 instance: str = self.get_argument("instance", "")
45 save: bool = self.get_bool_argument("save", False)
47 if not instance:
48 instance = self.saved_mastodon_instance()
50 if instance:
51 if "://" not in instance:
52 instance = f"https://{instance}"
53 scheme, netloc = urlsplit(instance)[:2]
54 if netloc:
55 instance = f"{scheme}://{netloc}".rstrip("/")
56 if save:
57 self.set_cookie(
58 "mastodon-instance",
59 instance,
60 path="/troet",
61 expires_days=90,
62 )
63 if text:
64 self.redirect(
65 f"{instance}/share?{urlencode({'text': text})}"
66 )
67 return
69 if head:
70 return
72 await self.render(
73 "pages/troet.html",
74 text=text,
75 instance=instance,
76 save=save,
77 )
79 def saved_mastodon_instance(self) -> str:
80 """Get the mastodon instance saved in a cookie."""
81 return cast(str, self.get_cookie("mastodon-instance", ""))