-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsubplot_charts.py
More file actions
213 lines (179 loc) · 7.92 KB
/
subplot_charts.py
File metadata and controls
213 lines (179 loc) · 7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from decimal import Decimal
from rest_framework import serializers
from rest_framework.request import Request
from metrics.api.serializers import help_texts
from metrics.domain.common.utils import (
DEFAULT_CHART_HEIGHT,
DEFAULT_CHART_WIDTH,
DEFAULT_Y_AXIS_MINIMUM_VAlUE,
)
from metrics.domain.models.charts.subplot_charts import SubplotChartRequestParameters
FILE_FORMAT_CHOICES: list[str] = ["svg", "png", "jpg", "jpeg", "json", "csv"]
class SubplotPlotSerializer(serializers.Serializer):
label = serializers.CharField(required=True)
line_colour = serializers.CharField(required=True)
age = serializers.CharField(required=False)
sex = serializers.CharField(required=False)
stratum = serializers.CharField(required=False)
geography_type = serializers.CharField(required=False)
geography = serializers.CharField(required=False)
class SubPlotPlotsSerializer(serializers.ListSerializer):
child = SubplotPlotSerializer()
class SubplotParametersSerializer(serializers.Serializer):
metric = serializers.CharField(required=True)
topic = serializers.CharField(required=True)
stratum = serializers.CharField(required=False, allow_null=True, allow_blank=True)
theme = serializers.CharField(required=False)
sub_theme = serializers.CharField(required=False)
def validate(self, data):
"""
Check that theme and sub_theme are present at either
chart_parameters or subplot_parameters level
"""
chart_params = self.parent.parent.parent.initial_data["chart_parameters"]
chart_theme = chart_params.get("theme")
chart_sub_theme = chart_params.get("sub_theme")
subplot_theme = data.get("theme")
subplot_sub_theme = data.get("sub_theme")
if not subplot_theme and not chart_theme:
msg = "'theme' must be specified at either subplot_parameters or chart_parameters level"
raise serializers.ValidationError(msg)
if not subplot_sub_theme and not chart_sub_theme:
msg = "'sub_theme' must be specified at either subplot_parameters or chart_parameters level"
raise serializers.ValidationError(msg)
return data
class SubplotSerializer(serializers.Serializer):
subplot_title = serializers.CharField(required=True)
subplot_parameters = SubplotParametersSerializer(required=True)
plots = SubPlotPlotsSerializer(required=True)
class SubplotsSerializer(serializers.ListSerializer):
child = SubplotSerializer()
class MetricRangeSerializer(serializers.Serializer):
start = serializers.DecimalField(max_digits=20, decimal_places=5)
end = serializers.DecimalField(max_digits=20, decimal_places=5)
class ChartParametersSerializer(serializers.Serializer):
x_axis = serializers.CharField(required=True)
y_axis = serializers.CharField(required=True)
theme = serializers.CharField(required=False)
sub_theme = serializers.CharField(required=False)
date_from = serializers.DateField(required=True)
date_to = serializers.DateField(required=True)
age = serializers.CharField(required=False, allow_null=True, allow_blank=True)
sex = serializers.CharField(required=False, allow_null=True, allow_blank=True)
stratum = serializers.CharField(required=False, allow_null=True, allow_blank=True)
geography_type = serializers.CharField(
required=False, allow_null=True, allow_blank=True
)
geography = serializers.CharField(required=False, allow_null=True, allow_blank=True)
metric_value_ranges = MetricRangeSerializer(
many=True, allow_null=True, required=False
)
SUBPLOT_CHART_PARAMETER_PAYLOAD_TYPE = dict[str, str]
SUBPLOT_PARAMETERS = dict[str, str]
class SubplotChartRequestSerializer(serializers.Serializer):
file_format = serializers.ChoiceField(
choices=FILE_FORMAT_CHOICES,
help_text=help_texts.CHART_FILE_FORMAT_FIELD,
default="svg",
)
chart_height = serializers.IntegerField(
help_text=help_texts.CHART_HEIGHT,
default=DEFAULT_CHART_HEIGHT,
allow_null=True,
)
chart_width = serializers.IntegerField(
help_text=help_texts.CHART_WIDTH,
default=DEFAULT_CHART_WIDTH,
allow_null=True,
)
x_axis_title = serializers.CharField(
required=False,
allow_blank=True,
allow_null=True,
default="",
help_text=help_texts.CHART_X_AXIS_TITLE,
)
y_axis_title = serializers.CharField(
required=False,
allow_blank=True,
allow_null=True,
default="",
help_text=help_texts.CHART_Y_AXIS_TITLE,
)
y_axis_minimum_value = serializers.DecimalField(
required=False,
allow_null=True,
help_text=help_texts.CHART_Y_AXIS_MINIMUM_VALUE,
default=None,
max_digits=10,
decimal_places=1,
)
y_axis_maximum_value = serializers.DecimalField(
required=False,
allow_null=True,
help_text=help_texts.CHART_Y_AXIS_MAXIMUM_VALUE,
default=None,
max_digits=10,
decimal_places=1,
)
target_threshold = serializers.DecimalField(
required=False,
allow_null=True,
max_digits=10,
decimal_places=2,
)
target_threshold_label = serializers.CharField(
required=False,
allow_blank=True,
allow_null=True,
)
chart_parameters = ChartParametersSerializer()
subplots = SubplotsSerializer()
def to_models(self, request: Request) -> SubplotChartRequestParameters:
"""
chart_parameters is used to reduce the duplication of data entry for content editors
for the chart request its really a part of the `subplot_parameters`. Its the combination
of these two objects that when added to the `plot` fields make the request.
"""
chart_parameters: SUBPLOT_CHART_PARAMETER_PAYLOAD_TYPE = (
self.validated_data.pop("chart_parameters")
)
if chart_parameters["date_from"]:
chart_parameters["date_from"] = chart_parameters["date_from"].isoformat()
if chart_parameters["date_to"]:
chart_parameters["date_to"] = chart_parameters["date_to"].isoformat()
provided_metric_value_ranges = chart_parameters.pop("metric_value_ranges", [])
metric_value_ranges: list[tuple[Decimal, Decimal]] = [
(metric_value_range.get("start"), metric_value_range.get("end"))
for metric_value_range in provided_metric_value_ranges
]
for subplot in self.validated_data["subplots"]:
subplot_parameters = subplot.pop("subplot_parameters")
subplot["x_axis"] = chart_parameters["x_axis"]
subplot["y_axis"] = chart_parameters["y_axis"]
for plot in subplot["plots"]:
plot.update(
{
**chart_parameters,
**subplot_parameters,
}
)
plot["metric_value_ranges"] = metric_value_ranges
return SubplotChartRequestParameters(
file_format=self.validated_data["file_format"],
chart_height=self.validated_data["chart_height"] or DEFAULT_CHART_HEIGHT,
chart_width=self.validated_data["chart_width"] or DEFAULT_CHART_WIDTH,
x_axis_title=self.validated_data["x_axis_title"],
y_axis_title=self.validated_data["y_axis_title"],
y_axis_minimum_value=self.validated_data["y_axis_minimum_value"]
or DEFAULT_Y_AXIS_MINIMUM_VAlUE,
y_axis_maximum_value=self.validated_data["y_axis_maximum_value"],
target_threshold=self.validated_data.get("target_threshold", None),
target_threshold_label=self.validated_data.get(
"target_threshold_label", None
),
subplots=self.validated_data["subplots"],
request=request,
)
class ChartPreviewQueryParamsSerializer(serializers.Serializer):
preview = serializers.BooleanField(required=False)