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

29 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-06-01 08:32 +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 __future__ import annotations 

17 

18from urllib.parse import urlencode, urlsplit 

19 

20from ..utils.request_handler import HTMLRequestHandler 

21from ..utils.utils import ModuleInfo 

22 

23 

24def get_module_info() -> ModuleInfo: 

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

26 return ModuleInfo( 

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

28 name="Tröter", 

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

30 path="/troet", 

31 aliases=("/toot",), 

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

33 hidden=True, # TODO: remove this line 

34 ) 

35 

36 

37class Troeter(HTMLRequestHandler): 

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

39 

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

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

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

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

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

45 

46 if not instance: 

47 instance = self.saved_mastodon_instance() 

48 

49 if instance: 

50 if "://" not in instance: 

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

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

53 if netloc: 

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

55 if save: 

56 self.set_cookie( 

57 "mastodon-instance", 

58 instance, 

59 path="/troet", 

60 expires_days=90, 

61 ) 

62 if text: 

63 self.redirect( 

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

65 ) 

66 return 

67 

68 if head: 

69 return 

70 

71 await self.render( 

72 "pages/troet.html", 

73 text=text, 

74 instance=instance, 

75 save=save, 

76 ) 

77 

78 def saved_mastodon_instance(self) -> str: 

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

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