Coverage for an_website / troet / troet.py: 100.000%

28 statements  

« 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/>. 

13 

14"""Toot cool stuff to the world.""" 

15 

16from urllib.parse import urlencode, urlsplit 

17 

18from ..utils.request_handler import HTMLRequestHandler 

19from ..utils.utils import ModuleInfo 

20 

21 

22def get_module_info() -> ModuleInfo: 

23 """Create and return the ModuleInfo for this module.""" 

24 return ModuleInfo( 

25 handlers=((r"/troet", Troeter),), 

26 name="Tröter", 

27 description="Tröte witzige Sachen in die Welt", 

28 path="/troet", 

29 aliases=("/toot",), 

30 keywords=("Tröt", "Mastodon", "Toot"), 

31 hidden=True, # TODO: remove this line 

32 ) 

33 

34 

35class Troeter(HTMLRequestHandler): 

36 """The request handler that makes tooting easier.""" 

37 

38 async def get(self, *, head: bool = False) -> None: 

39 """Handle GET requests to the page.""" 

40 text: str = self.get_argument("text", "") 

41 instance: str = self.get_argument("instance", "") 

42 save: bool = self.get_bool_argument("save", False) 

43 

44 if not instance: 

45 instance = self.saved_mastodon_instance() 

46 

47 if instance: 

48 if "://" not in instance: 

49 instance = f"https://{instance}" 

50 scheme, netloc = urlsplit(instance)[:2] 

51 if netloc: 

52 instance = f"{scheme}://{netloc}".rstrip("/") 

53 if save: 

54 self.set_cookie( 

55 "mastodon-instance", 

56 instance, 

57 path="/troet", 

58 expires_days=90, 

59 ) 

60 if text: 

61 self.redirect( 

62 f"{instance}/share?{urlencode({'text': text})}" 

63 ) 

64 return 

65 

66 if head: 

67 return 

68 

69 await self.render( 

70 "pages/troet.html", 

71 text=text, 

72 instance=instance, 

73 save=save, 

74 ) 

75 

76 def saved_mastodon_instance(self) -> str: 

77 """Get the mastodon instance saved in a cookie.""" 

78 return self.get_cookie("mastodon-instance", "")