Coverage for tasks/cpft_research_preferences.py: 58%

1"""

2 camcops_server/tasks/cpft_research_preferences.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**CPFT Research Preferences task.**

27

28"""

29

30 from typing import Any, Optional, Type

31

32 from camcops_server.cc_modules.cc_constants import CssClass, PV

33 from camcops_server.cc_modules.cc_html import tr_qa, get_yes_no_unknown

34 from camcops_server.cc_modules.cc_request import CamcopsRequest

35 from camcops_server.cc_modules.cc_sqla_coltypes import (

36 bool_column,

37 camcops_column,

38 CharColType,

39 PermittedValueChecker,

40)

41 from camcops_server.cc_modules.cc_task import Task, TaskHasPatientMixin

42

43

44 class CpftResearchPreferences( # type: ignore[misc]

45 TaskHasPatientMixin,

46 Task,

47):

48 """

49 Server implementation of the CPFT_Research_Preferences task

50 """

51

52 __tablename__ = "cpft_research_preferences"

53 shortname = "CPFT_Research_Preferences"

54 provides_trackers = False

55

56 FN_CONTACT_PREFERENCE = "contact_preference"

57 FN_CONTACT_BY_EMAIL = "contact_by_email"

58 FN_RESEARCH_OPT_OUT = "research_opt_out"

59

60 MANDATORY_FIELD_NAMES = [

61 FN_CONTACT_PREFERENCE,

62 FN_CONTACT_BY_EMAIL,

63 FN_RESEARCH_OPT_OUT,

64 ]

65

66 @classmethod

67 def extend_columns(

68 cls: Type["CpftResearchPreferences"], **kwargs: Any

69 ) -> None:

70 setattr(

71 cls,

72 cls.FN_CONTACT_PREFERENCE,

73 camcops_column(

74 cls.FN_CONTACT_PREFERENCE,

75 CharColType,

76 permitted_value_checker=PermittedValueChecker(

77 permitted_values=PV.RYG

78 ),

79 ),

80 )

81 setattr(

82 cls, cls.FN_CONTACT_BY_EMAIL, bool_column(cls.FN_CONTACT_BY_EMAIL)

83 )

84 setattr(

85 cls, cls.FN_RESEARCH_OPT_OUT, bool_column(cls.FN_RESEARCH_OPT_OUT)

86 )

87

88 @staticmethod

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

90 _ = req.gettext

91 return _("CPFT Research Preferences")

92

93 def is_complete(self) -> bool:

94 if not self.field_contents_valid():

95 return False

96

97 contact_preference = getattr(self, self.FN_CONTACT_PREFERENCE)

98 if contact_preference is None:

99 return False

100

101 if contact_preference != "R":

102 return getattr(self, self.FN_CONTACT_BY_EMAIL) is not None

103

104 return True

105

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

107 rows = [

108 tr_qa(

109 self.wxstring(req, f"q_{self.FN_CONTACT_PREFERENCE}_short"),

110 self.get_contact_preference_answer(req),

111 ),

112 tr_qa(

113 self.wxstring(req, f"q_{self.FN_CONTACT_BY_EMAIL}_short"),

114 self.get_contact_by_email_answer(req),

115 ),

116 tr_qa(

117 self.wxstring(req, f"q_{self.FN_RESEARCH_OPT_OUT}_short"),

118 self.get_research_opt_out_answer(req),

119 ),

120 ]

121

122 html = f"""

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

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

125 {self.get_is_complete_tr(req)}

126 </table>

127 </div>

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

129 <tr>

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

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

132 </tr>

133 {''.join(rows)}

134 </table>

135 """

136

137 return html

138

139 def get_contact_preference_answer(

140 self, req: CamcopsRequest

141 ) -> Optional[str]:

142

143 answer = getattr(self, self.FN_CONTACT_PREFERENCE)

144 if answer is None:

145 return None

146

147 return self.xstring(req, answer)

148

149 def get_contact_by_email_answer(

150 self, req: CamcopsRequest

151 ) -> Optional[str]:

152 return get_yes_no_unknown(req, getattr(self, self.FN_CONTACT_BY_EMAIL))

153

154 def get_research_opt_out_answer(

155 self, req: CamcopsRequest

156 ) -> Optional[str]:

157 return get_yes_no_unknown(req, getattr(self, self.FN_RESEARCH_OPT_OUT))