From 6ae10dd87220bf8343a7e8e1bf051cf18d7ae291 Mon Sep 17 00:00:00 2001
From: mpeltriaux <michel.peltriaux@sgdnord.rlp.de>
Date: Fri, 7 Jan 2022 13:50:37 +0100
Subject: [PATCH] #54 Grouped autocomplete

* refactors default autocomplete into grouped autocompletes, if parents exist for grouping
* updates requirement for django-autocomplete-light due to an issue with an attribute in pip's default version 3.8.2. More info here: https://github.com/yourlabs/django-autocomplete-light/issues/1278
---
 codelist/models.py      |  4 +--
 konova/autocompletes.py | 76 ++++++++++++++++++++++++++++++++++++++---
 requirements.txt        |  2 +-
 3 files changed, 75 insertions(+), 7 deletions(-)

diff --git a/codelist/models.py b/codelist/models.py
index 7537c8f7..d5be5b10 100644
--- a/codelist/models.py
+++ b/codelist/models.py
@@ -48,9 +48,9 @@ class KonovaCode(models.Model):
         help_text="Whether this code is archived or not"
     )
 
-    def __str__(self):
+    def __str__(self, with_parent: bool = True):
         ret_val = ""
-        if self.parent:
+        if self.parent and with_parent:
             ret_val += self.parent.long_name + " > "
         ret_val += self.long_name
         if self.short_name and self.short_name != self.long_name:
diff --git a/konova/autocompletes.py b/konova/autocompletes.py
index 241143c0..f47117cc 100644
--- a/konova/autocompletes.py
+++ b/konova/autocompletes.py
@@ -5,7 +5,7 @@ Contact: michel.peltriaux@sgdnord.rlp.de
 Created on: 07.12.20
 
 """
-from dal_select2.views import Select2QuerySetView
+from dal_select2.views import Select2QuerySetView, Select2GroupQuerySetView
 from django.contrib.auth.models import User
 from django.db.models import Q
 
@@ -85,7 +85,7 @@ class ShareUserAutocomplete(Select2QuerySetView):
         return qs
 
 
-class KonovaCodeAutocomplete(Select2QuerySetView):
+class KonovaCodeAutocomplete(Select2GroupQuerySetView):
     """
     Provides simple autocomplete functionality for codes
 
@@ -94,6 +94,22 @@ class KonovaCodeAutocomplete(Select2QuerySetView):
     * c: Search inside a special codelist
 
     """
+
+    def order_by(self, qs):
+        """ Orders by a predefined value
+
+        Wrapped in a function to provide inheritance-based different orders
+
+        Args:
+            qs (QuerySet): The queryset to be ordered
+
+        Returns:
+            qs (QuerySet): The ordered queryset
+        """
+        return qs.order_by(
+            "long_name"
+        )
+
     def get_queryset(self):
         if self.request.user.is_anonymous:
             return KonovaCode.objects.none()
@@ -101,9 +117,8 @@ class KonovaCodeAutocomplete(Select2QuerySetView):
             is_archived=False,
             is_selectable=True,
             is_leaf=True,
-        ).order_by(
-            "long_name"
         )
+        qs = self.order_by(qs)
         if self.c:
             qs = qs.filter(
                 code_lists__in=[self.c]
@@ -123,38 +138,82 @@ class KonovaCodeAutocomplete(Select2QuerySetView):
             qs = qs.filter(_filter).distinct()
         return qs
 
+    def get_result_label(self, result):
+        return f"{result.long_name}"
+
+    def get_selected_result_label(self, result):
+        return f"{result.__str__()}"
+
 
 class CompensationActionCodeAutocomplete(KonovaCodeAutocomplete):
     """
     Due to limitations of the django dal package, we need to subclass for each code list
     """
+    group_by_related = "parent"
+    related_field_name = "long_name"
+
     def __init__(self, *args, **kwargs):
         self.c = CODELIST_COMPENSATION_ACTION_ID
         super().__init__(*args, **kwargs)
 
+    def order_by(self, qs):
+        return qs.order_by(
+            "parent__long_name"
+        )
+
 
 class BiotopeCodeAutocomplete(KonovaCodeAutocomplete):
     """
     Due to limitations of the django dal package, we need to subclass for each code list
     """
+    group_by_related = "parent"
+    related_field_name = "long_name"
+
     def __init__(self, *args, **kwargs):
         self.c = CODELIST_BIOTOPES_ID
         super().__init__(*args, **kwargs)
 
+    def order_by(self, qs):
+        """ Orders by a predefined value
+
+        Wrapped in a function to provide inheritance-based different orders
+
+        Args:
+            qs (QuerySet): The queryset to be ordered
+
+        Returns:
+            qs (QuerySet): The ordered queryset
+        """
+        return qs.order_by(
+            "short_name",
+        )
+
+    def get_result_label(self, result):
+        return f"{result.long_name} ({result.short_name})"
+
 
 class LawCodeAutocomplete(KonovaCodeAutocomplete):
     """
     Due to limitations of the django dal package, we need to subclass for each code list
     """
+    group_by_related = "parent"
+    related_field_name = "long_name"
+
     def __init__(self, *args, **kwargs):
         self.c = CODELIST_LAW_ID
         super().__init__(*args, **kwargs)
 
+    def get_result_label(self, result):
+        return f"{result.long_name} ({result.short_name})"
+
 
 class ProcessTypeCodeAutocomplete(KonovaCodeAutocomplete):
     """
     Due to limitations of the django dal package, we need to subclass for each code list
     """
+    group_by_related = "parent"
+    related_field_name = "long_name"
+
     def __init__(self, *args, **kwargs):
         self.c = CODELIST_PROCESS_TYPE_ID
         super().__init__(*args, **kwargs)
@@ -164,6 +223,9 @@ class RegistrationOfficeCodeAutocomplete(KonovaCodeAutocomplete):
     """
     Due to limitations of the django dal package, we need to subclass for each code list
     """
+    group_by_related = "parent"
+    related_field_name = "long_name"
+
     def __init__(self, *args, **kwargs):
         self.c = CODELIST_REGISTRATION_OFFICE_ID
         super().__init__(*args, **kwargs)
@@ -173,6 +235,12 @@ class ConservationOfficeCodeAutocomplete(KonovaCodeAutocomplete):
     """
     Due to limitations of the django dal package, we need to subclass for each code list
     """
+    group_by_related = "parent"
+    related_field_name = "long_name"
+
     def __init__(self, *args, **kwargs):
         self.c = CODELIST_CONSERVATION_OFFICE_ID
         super().__init__(*args, **kwargs)
+
+    def get_result_label(self, result):
+        return f"{result.long_name} ({result.short_name})"
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
index c7a6fa46..763913c4 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -12,7 +12,7 @@ click-plugins==1.1.1
 click-repl==0.2.0
 Deprecated==1.2.13
 Django==3.1.3
-django-autocomplete-light==3.8.2
+django-autocomplete-light==3.9.0rc5
 django-bootstrap-modal-forms==2.2.0
 django-bootstrap4==3.0.1
 django-debug-toolbar==3.1.1