From 4db4280e7b2be9ccc5003bbc402e4e4e53e6fd48 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sun, 19 Dec 2021 14:29:21 +0100
Subject: [PATCH] backoffice: use a JOIN clause for the user label column
 (#59893)

---
 wcs/backoffice/management.py | 46 ++++++++++++++++++++++++++++++------
 wcs/sql.py                   |  4 +---
 2 files changed, 40 insertions(+), 10 deletions(-)

diff --git a/wcs/backoffice/management.py b/wcs/backoffice/management.py
index 38dcf650e..a80d9ba32 100644
--- a/wcs/backoffice/management.py
+++ b/wcs/backoffice/management.py
@@ -1320,15 +1320,15 @@ class FormPage(Directory):
                     continue
                 classnames = ''
                 attrs = ''
-                if isinstance(field, RelatedField):
+                if getattr(field, 'has_relations', False):
+                    classnames = 'has-relations-field'
+                    attrs = 'data-field-id="%s"' % field.id
+                    seen_parents.add(field.id)
+                elif isinstance(field, RelatedField):
                     classnames = 'related-field'
                     if field.parent_field_id in seen_parents:
                         classnames += ' collapsed'
                     attrs = 'data-relation-attr="%s"' % field.parent_field_id
-                elif getattr(field, 'has_relations', False):
-                    classnames = 'has-relations-field'
-                    attrs = 'data-field-id="%s"' % field.id
-                    seen_parents.add(field.id)
                 r += htmltext('<li class="%s" %s><span class="handle">⣿</span>' % (classnames, attrs))
                 r += htmltext('<label><input type="checkbox" name="%s"') % field.id
                 if field.id in field_ids:
@@ -1519,8 +1519,13 @@ class FormPage(Directory):
         yield FakeField('last_update_time', 'last_update_time', _('Last Modified'))
 
         # user fields
-        yield FakeField('user-label', 'user-label', _('User Label'))
-        if get_publisher().is_using_postgresql():
+        if not get_publisher().is_using_postgresql():
+            # full name of user, this will load individual user objects to get it
+            yield FakeField('user-label', 'user-label', _('User Label'))
+        else:
+            # user-label field but as a custom field, to get full name of user
+            # using a sql join clause.
+            yield UserLabelRelatedField()
             for field in get_publisher().user_class.get_fields():
                 if not hasattr(field, 'get_view_value'):
                     continue
@@ -3442,6 +3447,14 @@ class RelatedField:
     def get_csv_value(self, value, **kwargs):
         return [self.get_view_value(value)]
 
+    def get_column_field_id(self):
+        from wcs.sql import get_field_id
+
+        column_field_id = get_field_id(self.related_field)
+        if self.related_field.store_display_value:
+            column_field_id += '_display'
+        return column_field_id
+
 
 class UserRelatedField(RelatedField):
     # it is named 'user-label' and not 'user' for compatibility with existing
@@ -3456,6 +3469,25 @@ class UserRelatedField(RelatedField):
         return _('%s of User') % self.related_field.label
 
 
+class UserLabelRelatedField(UserRelatedField):
+    # custom user-label column, targetting the "name" (= full name) column
+    # of the users table
+    id = 'user-label'
+    type = 'user-label'
+    varname = 'user_label'
+    has_relations = True
+
+    def __init__(self):
+        pass
+
+    def get_column_field_id(self):
+        return 'name'
+
+    @property
+    def label(self):
+        return _('User Label')
+
+
 def do_graphs_section(period_start=None, period_end=None, criterias=None):
     from wcs import sql
 
diff --git a/wcs/sql.py b/wcs/sql.py
index 6fc49214a..b3211859e 100644
--- a/wcs/sql.py
+++ b/wcs/sql.py
@@ -1589,9 +1589,7 @@ class SqlMixin:
                 if carddef_table_decl not in tables:
                     tables.append(carddef_table_decl)
 
-                column_field_id = get_field_id(field.related_field)
-                if field.related_field.store_display_value:
-                    column_field_id += '_display'
+                column_field_id = field.get_column_field_id()
                 columns.append('%s.%s' % (carddef_table_alias, column_field_id))
                 extra_fields.append(field.id)
 
-- 
2.34.1

