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

29 statements  

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

13 

14"""EXAMPLE.""" 

15 

16from __future__ import annotations 

17 

18import logging 

19from dataclasses import dataclass 

20from typing import Final 

21 

22from tornado.web import MissingArgumentError 

23 

24from ..utils.data_parsing import parse_args 

25from ..utils.request_handler import APIRequestHandler, HTMLRequestHandler 

26from ..utils.utils import ModuleInfo 

27 

28LOGGER: Final = logging.getLogger(__name__) 

29 

30 

31def get_module_info() -> ModuleInfo: 

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

33 return ModuleInfo( 

34 handlers=( 

35 (r"/beispiel", Example), 

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

37 ), 

38 name="EXAMPLE", 

39 short_name="EXAMPLE", 

40 description="EXAMPLE", 

41 path="/beispiel", 

42 aliases=("/example",), 

43 sub_pages=(), 

44 keywords=(), 

45 ) 

46 

47 

48@dataclass(slots=True) 

49class ExampleArguments: 

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

51 

52 name: str = "Welt" 

53 

54 def validate(self) -> None: 

55 """Validate this.""" 

56 self.name = self.name.strip() 

57 if not self.name: 

58 raise MissingArgumentError("name") 

59 

60 

61class Example(HTMLRequestHandler): 

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

63 

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

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

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

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

68 if head: 

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

70 return 

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

72 

73 

74class ExampleAPI(APIRequestHandler): 

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

76 

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

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

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

80 # pylint: disable=unused-argument 

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