Coverage for tasks/esspri.py : 65%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
3"""
4camcops_server/tasks/esspri.py
6===============================================================================
8 Copyright (C) 2012-2020 Rudolf Cardinal (rudolf@pobox.com).
10 This file is part of CamCOPS.
12 CamCOPS is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
17 CamCOPS is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.
25===============================================================================
27**EULAR Sjögren’s Syndrome Patient Reported Index (ESSPRI) task.**
29"""
31from camcops_server.cc_modules.cc_constants import CssClass
32from camcops_server.cc_modules.cc_html import tr_qa, tr, answer
33from camcops_server.cc_modules.cc_request import CamcopsRequest
34from camcops_server.cc_modules.cc_sqla_coltypes import (
35 CamcopsColumn,
36 ZERO_TO_10_CHECKER,
37)
39from camcops_server.cc_modules.cc_summaryelement import SummaryElement
40from camcops_server.cc_modules.cc_task import TaskHasPatientMixin, Task
41import cardinal_pythonlib.rnc_web as ws
42from cardinal_pythonlib.stringfunc import strseq
43from sqlalchemy import Float, Integer
44from sqlalchemy.ext.declarative import DeclarativeMeta
45from typing import List, Type, Tuple, Dict, Any
48class EsspriMetaclass(DeclarativeMeta):
49 # noinspection PyInitNewSignature
50 def __init__(cls: Type['Esspri'],
51 name: str,
52 bases: Tuple[Type, ...],
53 classdict: Dict[str, Any]) -> None:
55 comment_strings = [
56 "dryness",
57 "fatigue",
58 "pain",
59 ]
61 for q_index in range(0, cls.N_QUESTIONS):
62 q_num = q_index + 1
63 q_field = "q{}".format(q_num)
65 score_comment = "(0 none - 10 maximum imaginable)"
67 setattr(cls, q_field, CamcopsColumn(
68 q_field, Integer,
69 permitted_value_checker=ZERO_TO_10_CHECKER,
70 comment="Q{} ({}) {}".format(
71 q_num, comment_strings[q_index], score_comment)
72 ))
74 super().__init__(name, bases, classdict)
77class Esspri(TaskHasPatientMixin,
78 Task,
79 metaclass=EsspriMetaclass):
80 __tablename__ = "esspri"
81 shortname = "ESSPRI"
83 N_QUESTIONS = 3
84 MAX_SCORE = 10 # Mean of 3 scores of 10
85 ALL_QUESTIONS = strseq("q", 1, N_QUESTIONS)
87 @staticmethod
88 def longname(req: "CamcopsRequest") -> str:
89 _ = req.gettext
90 return _("EULAR Sjögren’s Syndrome Patient Reported Index")
92 def get_summaries(self, req: CamcopsRequest) -> List[SummaryElement]:
93 return self.standard_task_summary_fields() + [
94 SummaryElement(
95 name="overall_score", coltype=Float(),
96 value=self.overall_score(),
97 comment=f"Overall score (/{self.MAX_SCORE})"),
98 ]
100 def is_complete(self) -> bool:
101 if self.any_fields_none(self.ALL_QUESTIONS):
102 return False
103 if not self.field_contents_valid():
104 return False
105 return True
107 def overall_score(self) -> float:
108 return self.mean_fields(self.ALL_QUESTIONS)
110 def get_task_html(self, req: CamcopsRequest) -> str:
111 rows = ""
112 for q_num in range(1, self.N_QUESTIONS + 1):
113 q_field = "q" + str(q_num)
114 question_cell = "{}. {}".format(q_num, self.wxstring(req, q_field))
116 score = getattr(self, q_field)
118 rows += tr_qa(question_cell, score)
120 formatted_score = ws.number_to_dp(self.overall_score(), 3, default="?")
122 html = """
123 <div class="{CssClass.SUMMARY}">
124 <table class="{CssClass.SUMMARY}">
125 {tr_is_complete}
126 {overall_score}
127 </table>
128 </div>
129 <table class="{CssClass.TASKDETAIL}">
130 <tr>
131 <th width="60%">Question</th>
132 <th width="40%">Answer</th>
133 </tr>
134 {rows}
135 </table>
136 <div class="{CssClass.FOOTNOTES}">
137 [1] Mean of three numerical rating scales, each rated 0-10.
138 </div>
139 """.format(
140 CssClass=CssClass,
141 tr_is_complete=self.get_is_complete_tr(req),
142 overall_score=tr(
143 self.wxstring(req, "overall_score") + " <sup>[1]</sup>",
144 "{} / {}".format(
145 answer(formatted_score),
146 self.MAX_SCORE
147 )
148 ),
149 rows=rows,
150 )
151 return html