Coverage for tasks/contactlog.py: 78%

36 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/tasks/contactlog.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""" 

29 

30from typing import List 

31 

32from cardinal_pythonlib.datetimefunc import format_datetime, get_duration_h_m 

33import cardinal_pythonlib.rnc_web as ws 

34from sqlalchemy.sql.schema import Column 

35from sqlalchemy.sql.sqltypes import Integer, UnicodeText 

36 

37from camcops_server.cc_modules.cc_constants import CssClass, DateFormat 

38from camcops_server.cc_modules.cc_ctvinfo import CTV_INCOMPLETE, CtvInfo 

39from camcops_server.cc_modules.cc_html import ( 

40 italic, 

41 get_yes_no_none, 

42 tr, 

43 tr_qa, 

44) 

45from camcops_server.cc_modules.cc_request import CamcopsRequest 

46from camcops_server.cc_modules.cc_sqla_coltypes import ( 

47 CamcopsColumn, 

48 BIT_CHECKER, 

49 PendulumDateTimeAsIsoTextColType, 

50) 

51from camcops_server.cc_modules.cc_task import ( 

52 Task, 

53 TaskHasClinicianMixin, 

54 TaskHasPatientMixin, 

55) 

56 

57 

58# ============================================================================= 

59# ContactLog 

60# ============================================================================= 

61 

62 

63class ContactLog(TaskHasClinicianMixin, TaskHasPatientMixin, Task): 

64 """ 

65 Server implementation of the ContactLog task. 

66 """ 

67 

68 __tablename__ = "contactlog" 

69 shortname = "ContactLog" 

70 info_filename_stem = "clinical" 

71 

72 location = Column("location", UnicodeText, comment="Location") 

73 start = Column( 

74 "start", 

75 PendulumDateTimeAsIsoTextColType, 

76 comment="Date/time that contact started", 

77 ) 

78 end = Column( 

79 "end", 

80 PendulumDateTimeAsIsoTextColType, 

81 comment="Date/time that contact ended", 

82 ) 

83 patient_contact = CamcopsColumn( 

84 "patient_contact", 

85 Integer, 

86 permitted_value_checker=BIT_CHECKER, 

87 comment="Patient contact involved (0 no, 1 yes)?", 

88 ) 

89 staff_liaison = CamcopsColumn( 

90 "staff_liaison", 

91 Integer, 

92 permitted_value_checker=BIT_CHECKER, 

93 comment="Liaison with staff involved (0 no, 1 yes)?", 

94 ) 

95 other_liaison = CamcopsColumn( 

96 "other_liaison", 

97 Integer, 

98 permitted_value_checker=BIT_CHECKER, 

99 comment="Liaison with others (e.g. family) involved (0 no, 1 yes)?", 

100 ) 

101 comment = Column("comment", UnicodeText, comment="Comment") 

102 

103 @staticmethod 

104 def longname(req: "CamcopsRequest") -> str: 

105 _ = req.gettext 

106 return _("Clinical contact log") 

107 

108 def get_clinical_text(self, req: CamcopsRequest) -> List[CtvInfo]: 

109 if not self.is_complete(): 

110 return CTV_INCOMPLETE 

111 contact_type = "Patient" if self.patient_contact else "Non-patient" 

112 return [ 

113 CtvInfo( 

114 content=( 

115 f"{contact_type} contact. Duration (hours:minutes) " 

116 f"{get_duration_h_m(self.start, self.end)}." 

117 ) 

118 ) 

119 ] 

120 

121 def is_complete(self) -> bool: 

122 return ( 

123 self.start is not None 

124 and self.end is not None 

125 and self.field_contents_valid() 

126 ) 

127 

128 def get_task_html(self, req: CamcopsRequest) -> str: 

129 return f""" 

130 <table class="{CssClass.TASKDETAIL}"> 

131 <tr> 

132 <td width="33%">Location:</td> 

133 <td width="67%"><b>{ws.webify(self.location)}</b></td> 

134 </tr> 

135 {tr_qa("Start:", format_datetime(self.start, 

136 DateFormat.SHORT_DATETIME, 

137 None))} 

138 {tr_qa("End:", format_datetime(self.end, 

139 DateFormat.SHORT_DATETIME, 

140 None))} 

141 {tr(italic("Calculated duration (hours:minutes)"), 

142 italic(get_duration_h_m(self.start, self.end)))} 

143 {tr_qa("Patient contact?", 

144 get_yes_no_none(req, self.patient_contact))} 

145 {tr_qa("Staff liaison?", 

146 get_yes_no_none(req, self.staff_liaison))} 

147 {tr_qa("Other liaison?", 

148 get_yes_no_none(req, self.other_liaison))} 

149 {tr_qa("Comment:", self.comment)} 

150 </table> 

151 """