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

28 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-10 18: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/>. 

13 

14"""EXAMPLE.""" 

15 

16import logging 

17from dataclasses import dataclass 

18from typing import Final 

19 

20from tornado.web import MissingArgumentError 

21 

22from ..utils.data_parsing import parse_args 

23from ..utils.request_handler import APIRequestHandler, HTMLRequestHandler 

24from ..utils.utils import ModuleInfo 

25 

26LOGGER: Final = logging.getLogger(__name__) 

27 

28 

29def get_module_info() -> ModuleInfo: 

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

31 return ModuleInfo( 

32 handlers=( 

33 (r"/beispiel", Example), 

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

35 ), 

36 name="EXAMPLE", 

37 short_name="EXAMPLE", 

38 description="EXAMPLE", 

39 path="/beispiel", 

40 aliases=("/example",), 

41 sub_pages=(), 

42 keywords=(), 

43 ) 

44 

45 

46@dataclass(slots=True) 

47class ExampleArguments: 

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

49 

50 name: str = "Welt" 

51 

52 def validate(self) -> None: 

53 """Validate this.""" 

54 self.name = self.name.strip() 

55 if not self.name: 

56 raise MissingArgumentError("name") 

57 

58 

59class Example(HTMLRequestHandler): 

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

61 

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

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

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

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

66 if head: 

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

68 return 

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

70 

71 

72class ExampleAPI(APIRequestHandler): 

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

74 

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

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

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

78 # pylint: disable=unused-argument 

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