Coverage for an_website / example / example.py: 92.857%

28 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-21 19:24 +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"""EXAMPLE.""" 

15 

16 

17import logging 

18from dataclasses import dataclass 

19from typing import Final 

20 

21from tornado.web import MissingArgumentError 

22 

23from ..utils.data_parsing import parse_args 

24from ..utils.request_handler import APIRequestHandler, HTMLRequestHandler 

25from ..utils.utils import ModuleInfo 

26 

27LOGGER: Final = logging.getLogger(__name__) 

28 

29 

30def get_module_info() -> ModuleInfo: 

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

32 return ModuleInfo( 

33 handlers=( 

34 (r"/beispiel", Example), 

35 (r"/api/beispiel", ExampleAPI), 

36 ), 

37 name="EXAMPLE", 

38 short_name="EXAMPLE", 

39 description="EXAMPLE", 

40 path="/beispiel", 

41 aliases=("/example",), 

42 sub_pages=(), 

43 keywords=(), 

44 ) 

45 

46 

47@dataclass(slots=True) 

48class ExampleArguments: 

49 """The arguments for the example page.""" 

50 

51 name: str = "Welt" 

52 

53 def validate(self) -> None: 

54 """Validate this.""" 

55 self.name = self.name.strip() 

56 if not self.name: 

57 raise MissingArgumentError("name") 

58 

59 

60class Example(HTMLRequestHandler): 

61 """The request handler for the example page.""" 

62 

63 @parse_args(type_=ExampleArguments, validation_method="validate") 

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

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

66 self.set_header("X-Name", args.name) 

67 if head: 

68 # only after all headers have been set and the status code is clear 

69 return 

70 await self.render("pages/EXAMPLE.html", name=args.name) 

71 

72 

73class ExampleAPI(APIRequestHandler): 

74 """The request handler for the example API.""" 

75 

76 @parse_args(type_=ExampleArguments, validation_method="validate") 

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

78 """Handle GET requests to the API.""" 

79 # pylint: disable=unused-argument 

80 await self.finish({"text": f"Hallo, {args.name}!", "name": args.name})