Coverage for cc_modules/cc_mako_helperfunc.py: 38%

13 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-08 23:14 +0000

1#!/usr/bin/env python 

2 

3""" 

4camcops_server/cc_modules/cc_mako_helperfunc.py 

5 

6=============================================================================== 

7 

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

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

10 

11 This file is part of CamCOPS. 

12 

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

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

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

16 (at your option) any later version. 

17 

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

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

20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

21 GNU General Public License for more details. 

22 

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

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

25 

26=============================================================================== 

27 

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

29 

30""" 

31 

32from typing import Any, Iterable, List, TYPE_CHECKING 

33 

34from camcops_server.cc_modules.cc_pyramid import ViewParam 

35 

36if TYPE_CHECKING: 

37 from camcops_server.cc_modules.cc_request import CamcopsRequest 

38 

39 

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

41# Helper functions 

42# ============================================================================= 

43 

44 

45def listview( 

46 req: "CamcopsRequest", 

47 objects: Iterable[Any], 

48 route_name: str, 

49 description: str, 

50 icon: str, 

51 sep: str = "<br>", 

52) -> str: 

53 """ 

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

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

56 

57 Args: 

58 req: 

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

60 objects: 

61 Objects to catalogue. 

62 route_name: 

63 Pyramid route name. 

64 description: 

65 Object type description. 

66 icon: 

67 Icon name for each object. 

68 sep: 

69 Separator string for HTML. 

70 

71 Returns: 

72 str: HTML 

73 """ 

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

75 for obj in objects: 

76 obj_id = obj.id 

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

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

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

80 return sep.join(parts)