|
| 1 | +import contextlib |
| 2 | + |
| 3 | +from django.db.utils import OperationalError |
| 4 | +from rest_framework import serializers |
| 5 | +from rest_framework.request import Request |
| 6 | + |
| 7 | +from metrics.api.serializers import help_texts |
| 8 | +from metrics.api.serializers.plots import PlotSerializer |
| 9 | +from metrics.domain.common.utils import ( |
| 10 | + DEFAULT_CHART_HEIGHT, |
| 11 | + DEFAULT_CHART_WIDTH, |
| 12 | + DEFAULT_X_AXIS, |
| 13 | + DEFAULT_Y_AXIS, |
| 14 | + ChartAxisFields, |
| 15 | +) |
| 16 | +from metrics.domain.models import ChartRequestParams |
| 17 | + |
| 18 | + |
| 19 | +class DualCategoryTableSegmentSerializer(serializers.Serializer): |
| 20 | + colour = serializers.CharField( |
| 21 | + required=False, |
| 22 | + allow_blank=True, |
| 23 | + allow_null=True, |
| 24 | + default="", |
| 25 | + help_text=help_texts.LABEL_FIELD, |
| 26 | + ) |
| 27 | + |
| 28 | + secondary_field_value = serializers.CharField( |
| 29 | + required=False, |
| 30 | + allow_blank=True, |
| 31 | + allow_null=True, |
| 32 | + default="", |
| 33 | + help_text=help_texts.LABEL_FIELD, |
| 34 | + ) |
| 35 | + |
| 36 | + label = serializers.CharField( |
| 37 | + required=False, |
| 38 | + allow_blank=True, |
| 39 | + allow_null=True, |
| 40 | + default="", |
| 41 | + help_text=help_texts.LABEL_FIELD, |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +class DualCategoryTableSegmentListSerializer(serializers.ListSerializer): |
| 46 | + child = DualCategoryTableSegmentSerializer() |
| 47 | + |
| 48 | + |
| 49 | +class DualCategoryTablesSerializer(serializers.Serializer): |
| 50 | + |
| 51 | + segments = DualCategoryTableSegmentListSerializer() |
| 52 | + |
| 53 | + static_fields = PlotSerializer() |
| 54 | + |
| 55 | + x_axis = serializers.ChoiceField( |
| 56 | + choices=ChartAxisFields.choices(), |
| 57 | + required=False, |
| 58 | + allow_blank=True, |
| 59 | + allow_null=True, |
| 60 | + help_text=help_texts.CHART_X_AXIS, |
| 61 | + default=DEFAULT_X_AXIS, |
| 62 | + ) |
| 63 | + |
| 64 | + y_axis = serializers.ChoiceField( |
| 65 | + choices=ChartAxisFields.choices(), |
| 66 | + required=False, |
| 67 | + allow_blank=True, |
| 68 | + allow_null=True, |
| 69 | + help_text=help_texts.CHART_Y_AXIS, |
| 70 | + default=DEFAULT_Y_AXIS, |
| 71 | + ) |
| 72 | + |
| 73 | + primary_field_values = serializers.ListField( |
| 74 | + child=serializers.CharField(), |
| 75 | + help_text="List of primary field values for this segment", |
| 76 | + required=True, |
| 77 | + allow_empty=False, |
| 78 | + ) |
| 79 | + |
| 80 | + secondary_category = serializers.CharField( |
| 81 | + help_text="Secondary category field for the chart", |
| 82 | + required=True, |
| 83 | + ) |
| 84 | + |
| 85 | + def __init__(self, *args, **kwargs): |
| 86 | + with contextlib.suppress(OperationalError): |
| 87 | + super().__init__(*args, **kwargs) |
| 88 | + |
| 89 | + def to_models(self, request: Request) -> ChartRequestParams: |
| 90 | + |
| 91 | + groups_plots = [] |
| 92 | + primary_field_values = self.data.get("primary_field_values") |
| 93 | + x_axis = self.data.get("x_axis") or DEFAULT_X_AXIS |
| 94 | + y_axis = self.data.get("y_axis") or DEFAULT_Y_AXIS |
| 95 | + static_fields = self.data.get("static_fields") |
| 96 | + print(f"AIDAN static_fields {static_fields}") |
| 97 | + topic = static_fields.get("topic") |
| 98 | + |
| 99 | + for primary_field_value in primary_field_values: |
| 100 | + for segment in self.data["segments"]: |
| 101 | + plot = { |
| 102 | + "y_axis": y_axis, |
| 103 | + "x_axis": primary_field_value, |
| 104 | + self.data.get("secondary_category"): segment[ |
| 105 | + "secondary_field_value" |
| 106 | + ], |
| 107 | + **static_fields, |
| 108 | + } |
| 109 | + groups_plots.append(plot) |
| 110 | + print(f"AIDAN: plots {groups_plots}") |
| 111 | + return ChartRequestParams( |
| 112 | + chart_height=DEFAULT_CHART_HEIGHT, |
| 113 | + chart_width=DEFAULT_CHART_WIDTH, |
| 114 | + file_format="svg", |
| 115 | + plots=groups_plots, |
| 116 | + request=request, |
| 117 | + x_axis=x_axis, |
| 118 | + y_axis=y_axis, |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +class DualCategoryTablesResponseValueSerializer(serializers.Serializer): |
| 123 | + label = serializers.CharField() |
| 124 | + value = serializers.CharField() |
| 125 | + in_reporting_delay_period = serializers.BooleanField() |
| 126 | + # Confidence intervals aren't implemented for dual category charts |
| 127 | + |
| 128 | + |
| 129 | +class DualCategoryTablesResponseValuesListSerializer(serializers.ListSerializer): |
| 130 | + child = DualCategoryTablesResponseValueSerializer() |
| 131 | + |
| 132 | + |
| 133 | +class DualCategoryTablesResponsePlotsListSerializer(serializers.Serializer): |
| 134 | + reference = serializers.CharField() |
| 135 | + values = DualCategoryTablesResponseValuesListSerializer() |
| 136 | + |
| 137 | + |
| 138 | +class DualCategoryTablesResponseSerializer(serializers.ListSerializer): |
| 139 | + child = DualCategoryTablesResponsePlotsListSerializer() |
0 commit comments