Coverage for tasks/cpft_covid_medical.py: 62%

37 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/cpft_covid_medical.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**CPFT Post-Covid Clinic Medical Questionnaire task.** 

29 

30""" 

31 

32from typing import Any, Dict, Optional, Tuple, Type 

33 

34from sqlalchemy.ext.declarative import DeclarativeMeta 

35from sqlalchemy.sql.sqltypes import Integer 

36 

37from camcops_server.cc_modules.cc_constants import CssClass 

38from camcops_server.cc_modules.cc_html import tr_qa 

39from camcops_server.cc_modules.cc_request import CamcopsRequest 

40from camcops_server.cc_modules.cc_sqla_coltypes import ( 

41 CamcopsColumn, 

42 ZERO_TO_THREE_CHECKER, 

43) 

44from camcops_server.cc_modules.cc_task import Task, TaskHasPatientMixin 

45 

46 

47class CpftCovidMedicalMetaclass(DeclarativeMeta): 

48 # noinspection PyInitNewSignature 

49 def __init__( 

50 cls: Type["CpftCovidMedical"], 

51 name: str, 

52 bases: Tuple[Type, ...], 

53 classdict: Dict[str, Any], 

54 ) -> None: 

55 setattr( 

56 cls, 

57 cls.FN_HOW_AND_WHEN_SYMPTOMS, 

58 CamcopsColumn( 

59 cls.FN_HOW_AND_WHEN_SYMPTOMS, 

60 Integer, 

61 permitted_value_checker=ZERO_TO_THREE_CHECKER, 

62 comment=( 

63 "0 Present before C-19, " 

64 "1 Within 6 weeks of catching C-19, " 

65 "2 Between 6 weeks and 6 months of catching C-19, " 

66 "3 Following immunisation for C-19" 

67 ), 

68 ), 

69 ) 

70 

71 super().__init__(name, bases, classdict) 

72 

73 

74class CpftCovidMedical( 

75 TaskHasPatientMixin, Task, metaclass=CpftCovidMedicalMetaclass 

76): 

77 """ 

78 Server implementation of the CPFT_Covid_Medical task 

79 """ 

80 

81 __tablename__ = "cpft_covid_medical" 

82 shortname = "CPFT_Covid_Medical" 

83 provides_trackers = False 

84 

85 FN_HOW_AND_WHEN_SYMPTOMS = "how_and_when_symptoms" 

86 

87 @staticmethod 

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

89 _ = req.gettext 

90 return _("CPFT Post-COVID-19 Clinic Medical Questionnaire") 

91 

92 def is_complete(self) -> bool: 

93 if not self.field_contents_valid(): 

94 return False 

95 

96 if getattr(self, self.FN_HOW_AND_WHEN_SYMPTOMS) is None: 

97 return False 

98 

99 return True 

100 

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

102 rows = [ 

103 tr_qa( 

104 self.wxstring(req, f"q_{self.FN_HOW_AND_WHEN_SYMPTOMS}"), 

105 self.get_how_and_when_symptoms_answer(req), 

106 ) 

107 ] 

108 

109 html = f""" 

110 <div class="{CssClass.SUMMARY}"> 

111 <table class="{CssClass.SUMMARY}"> 

112 {self.get_is_complete_tr(req)} 

113 </table> 

114 </div> 

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

116 <tr> 

117 <th width="60%">Question</th> 

118 <th width="40%">Answer</th> 

119 </tr> 

120 {''.join(rows)} 

121 </table> 

122 """ 

123 

124 return html 

125 

126 def get_how_and_when_symptoms_answer( 

127 self, req: CamcopsRequest 

128 ) -> Optional[str]: 

129 

130 answer = getattr(self, self.FN_HOW_AND_WHEN_SYMPTOMS) 

131 if answer is None: 

132 return None 

133 

134 return self.xstring( 

135 req, f"{self.FN_HOW_AND_WHEN_SYMPTOMS}_option{answer}" 

136 )