Coverage for cc_modules/cc_mako_helperfunc.py: 33%
12 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-15 14:23 +0100
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-15 14:23 +0100
1"""
2camcops_server/cc_modules/cc_mako_helperfunc.py
4===============================================================================
6 Copyright (C) 2012, University of Cambridge, Department of Psychiatry.
7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
9 This file is part of CamCOPS.
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.
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.
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/>.
24===============================================================================
26**Helper functions used by Mako templates.**
28"""
30from typing import Any, Iterable, List, TYPE_CHECKING
32from camcops_server.cc_modules.cc_pyramid import ViewParam
34if TYPE_CHECKING:
35 from camcops_server.cc_modules.cc_request import CamcopsRequest
38# =============================================================================
39# Helper functions
40# =============================================================================
43def 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
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.
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)