Coverage for cc_modules/cc_mako_helperfunc.py: 33%

1"""

2 camcops_server/cc_modules/cc_mako_helperfunc.py

3

4===============================================================================

5

6 Copyright (C) 2012, University of Cambridge, Department of Psychiatry.

7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).

8

9 This file is part of CamCOPS.

10

11 CamCOPS is free software: you can redistribute it and/or modify

12 it under the terms of the GNU General Public License as published by

13 the Free Software Foundation, either version 3 of the License, or

14 (at your option) any later version.

15

16 CamCOPS is distributed in the hope that it will be useful,

17 but WITHOUT ANY WARRANTY; without even the implied warranty of

18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

19 GNU General Public License for more details.

20

21 You should have received a copy of the GNU General Public License

22 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.

23

24===============================================================================

25

26**Helper functions used by Mako templates.**

27

28"""

29

30 from typing import Any, Iterable, List, TYPE_CHECKING

31

32 from camcops_server.cc_modules.cc_pyramid import ViewParam

33

34 if TYPE_CHECKING:

35 from camcops_server.cc_modules.cc_request import CamcopsRequest

36

37

38# =============================================================================

39# Helper functions

40# =============================================================================

41

42

43 def listview(

44 req: "CamcopsRequest",

45 objects: Iterable[Any],

46 route_name: str,

47 description: str,

48 icon: str,

49 sep: str = "<br>",

50) -> str:

51 """

52 Provides an autolinked catalogue of objects, in HTML, via those objects'

53 ``id`` fields and a standard URL format

54

55 Args:

56 req:

57 A :class:`camcops_server.cc_modules.cc_request.CamcopsRequest`.

58 objects:

59 Objects to catalogue.

60 route_name:

61 Pyramid route name.

62 description:

63 Object type description.

64 icon:

65 Icon name for each object.

66 sep:

67 Separator string for HTML.

68

69 Returns:

70 str: HTML

71 """

72 parts = [] # type: List[str]

73 for obj in objects:

74 obj_id = obj.id

75 url = req.route_url(route_name, _query={ViewParam.ID: obj_id})

76 text = f"{description} {obj_id}"

77 parts.append(req.icon_text(icon=icon, url=url, text=text))

78 return sep.join(parts)