Coverage for tasks/esspri.py: 65%
51 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-08 23:14 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-08 23:14 +0000
1#!/usr/bin/env python
3"""
4camcops_server/tasks/esspri.py
6===============================================================================
8 Copyright (C) 2012, University of Cambridge, Department of Psychiatry.
9 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
11 This file is part of CamCOPS.
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.
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.
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/>.
26===============================================================================
28**EULAR Sjögren’s Syndrome Patient Reported Index (ESSPRI) task.**
30"""
32from camcops_server.cc_modules.cc_constants import CssClass
33from camcops_server.cc_modules.cc_html import tr_qa, tr, answer
34from camcops_server.cc_modules.cc_request import CamcopsRequest
35from camcops_server.cc_modules.cc_sqla_coltypes import (
36 CamcopsColumn,
37 ZERO_TO_10_CHECKER,
38)
40from camcops_server.cc_modules.cc_summaryelement import SummaryElement
41from camcops_server.cc_modules.cc_task import TaskHasPatientMixin, Task
42import cardinal_pythonlib.rnc_web as ws
43from cardinal_pythonlib.stringfunc import strseq
44from sqlalchemy import Float, Integer
45from sqlalchemy.ext.declarative import DeclarativeMeta
46from typing import List, Type, Tuple, Dict, Any
49class EsspriMetaclass(DeclarativeMeta):
50 # noinspection PyInitNewSignature
51 def __init__(
52 cls: Type["Esspri"],
53 name: str,
54 bases: Tuple[Type, ...],
55 classdict: Dict[str, Any],
56 ) -> None:
58 comment_strings = ["dryness", "fatigue", "pain"]
60 for q_index in range(0, cls.N_QUESTIONS):
61 q_num = q_index + 1
62 q_field = "q{}".format(q_num)
64 score_comment = "(0 none - 10 maximum imaginable)"
66 setattr(
67 cls,
68 q_field,
69 CamcopsColumn(
70 q_field,
71 Integer,
72 permitted_value_checker=ZERO_TO_10_CHECKER,
73 comment="Q{} ({}) {}".format(
74 q_num, comment_strings[q_index], score_comment
75 ),
76 ),
77 )
79 super().__init__(name, bases, classdict)
82class Esspri(TaskHasPatientMixin, Task, metaclass=EsspriMetaclass):
83 __tablename__ = "esspri"
84 shortname = "ESSPRI"
86 N_QUESTIONS = 3
87 MAX_SCORE = 10 # Mean of 3 scores of 10
88 ALL_QUESTIONS = strseq("q", 1, N_QUESTIONS)
90 @staticmethod
91 def longname(req: "CamcopsRequest") -> str:
92 _ = req.gettext
93 return _("EULAR Sjögren’s Syndrome Patient Reported Index")
95 def get_summaries(self, req: CamcopsRequest) -> List[SummaryElement]:
96 return self.standard_task_summary_fields() + [
97 SummaryElement(
98 name="overall_score",
99 coltype=Float(),
100 value=self.overall_score(),
101 comment=f"Overall score (/{self.MAX_SCORE})",
102 )
103 ]
105 def is_complete(self) -> bool:
106 if self.any_fields_none(self.ALL_QUESTIONS):
107 return False
108 if not self.field_contents_valid():
109 return False
110 return True
112 def overall_score(self) -> float:
113 return self.mean_fields(self.ALL_QUESTIONS)
115 def get_task_html(self, req: CamcopsRequest) -> str:
116 rows = ""
117 for q_num in range(1, self.N_QUESTIONS + 1):
118 q_field = "q" + str(q_num)
119 question_cell = "{}. {}".format(q_num, self.wxstring(req, q_field))
121 score = getattr(self, q_field)
123 rows += tr_qa(question_cell, score)
125 formatted_score = ws.number_to_dp(self.overall_score(), 3, default="?")
127 html = """
128 <div class="{CssClass.SUMMARY}">
129 <table class="{CssClass.SUMMARY}">
130 {tr_is_complete}
131 {overall_score}
132 </table>
133 </div>
134 <table class="{CssClass.TASKDETAIL}">
135 <tr>
136 <th width="60%">Question</th>
137 <th width="40%">Answer</th>
138 </tr>
139 {rows}
140 </table>
141 <div class="{CssClass.FOOTNOTES}">
142 [1] Mean of three numerical rating scales, each rated 0-10.
143 </div>
144 """.format(
145 CssClass=CssClass,
146 tr_is_complete=self.get_is_complete_tr(req),
147 overall_score=tr(
148 self.wxstring(req, "overall_score") + " <sup>[1]</sup>",
149 "{} / {}".format(answer(formatted_score), self.MAX_SCORE),
150 ),
151 rows=rows,
152 )
153 return html