From 0c2eebed76845c1b2d6560e2f9857916b94bf131 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Fri, 24 Jan 2025 15:40:07 +0100
Subject: [PATCH] =?UTF-8?q?refactor(graphs):=20=F0=9F=92=84=20use=20group?=
 =?UTF-8?q?=20field=20display=20when=20possible?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 scipost_django/graphs/graphs/plotkind.py | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/scipost_django/graphs/graphs/plotkind.py b/scipost_django/graphs/graphs/plotkind.py
index d90c37b84..17d0bf4d1 100644
--- a/scipost_django/graphs/graphs/plotkind.py
+++ b/scipost_django/graphs/graphs/plotkind.py
@@ -466,7 +466,16 @@ class BarPlot(PlotKind):
 
         if qs.exists():
             groups, vals = zip(*qs.values_list(group_key, "agg"))
-            return [str(group) for group in groups], vals
+
+            # Attempt to convert the group values to display labels if possible
+            try:
+                field_choices = self.plotter.model._meta.get_field(group_key).choices
+                group_display_labels = dict(field_choices)
+                groups = [group_display_labels.get(group, group) for group in groups]
+            except Exception:
+                groups = [str(group) for group in groups]
+
+            return groups, vals
         else:
             return [], []
 
-- 
GitLab