Coverage for tasks/irac.py: 69%

1"""

2 camcops_server/tasks/irac.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"""

27

28 from typing import Optional

29 import cardinal_pythonlib.rnc_web as ws

30

31 from sqlalchemy.orm import Mapped, mapped_column

32 from sqlalchemy.sql.sqltypes import UnicodeText

33

34 from camcops_server.cc_modules.cc_constants import CssClass

35 from camcops_server.cc_modules.cc_html import tr_qa

36 from camcops_server.cc_modules.cc_request import CamcopsRequest

37 from camcops_server.cc_modules.cc_sqla_coltypes import (

38 mapped_camcops_column,

39 ZERO_TO_TWO_CHECKER,

40)

41 from camcops_server.cc_modules.cc_task import (

42 get_from_dict,

43 Task,

44 TaskHasPatientMixin,

45)

46

47

48# =============================================================================

49# IRAC

50# =============================================================================

51

52

53 class Irac(TaskHasPatientMixin, Task): # type: ignore[misc]

54 """

55 Server implementation of the IRAC task.

56 """

57

58 __tablename__ = "irac"

59 shortname = "IRAC"

60

61 aim: Mapped[Optional[str]] = mapped_column(

62 UnicodeText, comment="Main aim of the contact"

63 )

64 achieved: Mapped[Optional[int]] = mapped_camcops_column(

65 permitted_value_checker=ZERO_TO_TWO_CHECKER,

66 comment="Was the aim achieved? (0 not, 1 partially, 2 fully)",

67 )

68

69 TASK_FIELDS = ["aim", "achieved"]

70

71 @staticmethod

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

73 _ = req.gettext

74 return _("Identify and Rate the Aim of the Contact")

75

76 def is_complete(self) -> bool:

77 return (

78 self.all_fields_not_none(self.TASK_FIELDS)

79 and self.field_contents_valid()

80 )

81

82 def get_achieved_text(self, req: CamcopsRequest) -> str:

83 achieveddict = {

84 None: None,

85 0: self.wxstring(req, "achieved_0"),

86 1: self.wxstring(req, "achieved_1"),

87 2: self.wxstring(req, "achieved_2"),

88 }

89 return get_from_dict(achieveddict, self.achieved)

90

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

92 if self.achieved is not None:

93 achieved = f"{self.achieved}. {self.get_achieved_text(req)}"

94 else:

95 achieved = None

96 return f"""

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

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

99 {self.get_is_complete_tr(req)}

100 </table>

101 </div>

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

103 <tr>

104 <th width="50%">Question</th>

105 <th width="50%">Answer</th>

106 </tr>

107 {tr_qa(self.wxstring(req, "q_aim"), ws.webify(self.aim))}

108 {tr_qa(self.wxstring(req, "q_achieved"), achieved)}

109 </table>

110 """