Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add a chart_type override to measurement categories

Roland Geider2aaea90b48f9
diff --git a/wger/measurements/api/serializers.py b/wger/measurements/api/serializers.py
index 345f3f4a0..2a0a9f78f 100644
--- a/wger/measurements/api/serializers.py
+++ b/wger/measurements/api/serializers.py
@@ -39,7 +39,16 @@ class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
- fields = ('id', 'name', 'unit', 'metric_type', 'parent', 'order', 'is_official')
+ fields = (
+ 'id',
+ 'name',
+ 'unit',
+ 'metric_type',
+ 'chart_type',
+ 'parent',
+ 'order',
+ 'is_official',
+ )
read_only_fields = ('is_official',)
def validate_metric_type(self, metric_type):
diff --git a/wger/measurements/migrations/0006_category_externally_synced_category_metric_type_and_more.py b/wger/measurements/migrations/0006_category_externally_synced_category_metric_type_and_more.py
index 025fd443b..f38060310 100644
--- a/wger/measurements/migrations/0006_category_externally_synced_category_metric_type_and_more.py
+++ b/wger/measurements/migrations/0006_category_externally_synced_category_metric_type_and_more.py
@@ -61,6 +61,17 @@ class Migration(migrations.Migration):
verbose_name='Metric type',
),
),
+ migrations.AddField(
+ model_name='category',
+ name='chart_type',
+ field=models.CharField(
+ blank=True,
+ choices=[('line', 'Line'), ('bar', 'Bar'), ('heatmap', 'Heatmap')],
+ max_length=20,
+ null=True,
+ verbose_name='Chart type',
+ ),
+ ),
migrations.AddConstraint(
model_name='category',
constraint=models.UniqueConstraint(
diff --git a/wger/measurements/models/category.py b/wger/measurements/models/category.py
index 6a693f4e6..f16bdbf9d 100644
--- a/wger/measurements/models/category.py
+++ b/wger/measurements/models/category.py
@@ -86,6 +86,16 @@ class MetricType(models.TextChoices):
return GROUP_COMPONENTS.get(metric_type, [])
+class ChartType(models.TextChoices):
+ """
+ Chart a category is drawn as, when the user picked one
+ """
+
+ LINE = 'line'
+ BAR = 'bar'
+ HEATMAP = 'heatmap'
+
+
GROUP_COMPONENTS: dict[str, list[tuple[str, str]]] = {
MetricType.BLOOD_PRESSURE: [
(MetricType.BLOOD_PRESSURE_SYSTOLIC, 'Systolic'),
@@ -173,6 +183,20 @@ class Category(models.Model):
default=MetricType.CUSTOM,
)
+ # Null means "derive the chart from the metric type", which is what every
+ # category does unless the user picked something else. Which of the values
+ # are offered is a client decision (a step count is a bar or a heatmap, not
+ # a line), and so is what happens to a value that does not fit the category:
+ # the clients fall back to the derived default instead of showing nothing,
+ # which is also what keeps an older client working when a type is added here
+ chart_type = models.CharField(
+ verbose_name='Chart type',
+ max_length=20,
+ choices=ChartType.choices,
+ blank=True,
+ null=True,
+ )
+
# Multi-value measurements (e.g. blood pressure) are modelled as a parent
# category holding one child category per component. Only leaf categories
# (no children) carry measurements; nesting is limited to one level.