From cdc731f9aa2e31514787a0dd5a257eb8d1173d66 Mon Sep 17 00:00:00 2001 From: PouyaMohseni Date: Mon, 24 Nov 2025 14:46:57 -0500 Subject: [PATCH 1/4] feat: prioritize labels in the active language for instrument detail page - The active language's label is moved to the top of the label list so users see their expected language first. refs: #410 --- .../VIM/apps/instruments/views/instrument_detail.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/web-app/django/VIM/apps/instruments/views/instrument_detail.py b/web-app/django/VIM/apps/instruments/views/instrument_detail.py index 942b2429..0875cc33 100644 --- a/web-app/django/VIM/apps/instruments/views/instrument_detail.py +++ b/web-app/django/VIM/apps/instruments/views/instrument_detail.py @@ -1,5 +1,6 @@ from django.views.generic import DetailView from VIM.apps.instruments.models import Instrument, Language +from collections import OrderedDict class InstrumentDetail(DetailView): @@ -47,12 +48,17 @@ def get_context_data(self, **kwargs): else Language.objects.get(en_label="English") # default in English ) - # Get the instrument label in the active language + # Get the instrument label in the active language and moving it to the top of label_aliases_dict # Set label to the first instrument name added in the language if there is no "umil_label" set - active_labels = instrument_names.filter(language=context["active_language"]) + active_lang = context["active_language"] + active_labels = instrument_names.filter(language=active_lang) umil_label = active_labels.filter(umil_label=True) if umil_label.exists(): context["active_instrument_label"] = umil_label.first() + + value = label_aliases_dict.pop(active_lang) + label_aliases_dict = {active_lang: value, **label_aliases_dict} + context["label_aliases_dict"] = label_aliases_dict else: context["active_instrument_label"] = active_labels.first() From b71730d4289f7f40b52c6f6cddea1011ec7d328e Mon Sep 17 00:00:00 2001 From: PouyaMohseni Date: Mon, 24 Nov 2025 14:51:06 -0500 Subject: [PATCH 2/4] chore: remove unused library import - Cleaned up unused import that was accidentally left in the previous commit --- web-app/django/VIM/apps/instruments/views/instrument_detail.py | 1 - 1 file changed, 1 deletion(-) diff --git a/web-app/django/VIM/apps/instruments/views/instrument_detail.py b/web-app/django/VIM/apps/instruments/views/instrument_detail.py index 0875cc33..19786738 100644 --- a/web-app/django/VIM/apps/instruments/views/instrument_detail.py +++ b/web-app/django/VIM/apps/instruments/views/instrument_detail.py @@ -1,6 +1,5 @@ from django.views.generic import DetailView from VIM.apps.instruments.models import Instrument, Language -from collections import OrderedDict class InstrumentDetail(DetailView): From f2bea0c889d5b870c074bd2272a93af65ddd3468 Mon Sep 17 00:00:00 2001 From: PouyaMohseni Date: Mon, 24 Nov 2025 16:46:58 -0500 Subject: [PATCH 3/4] refactor: refined interaction with context as recommended by the reviewer --- .../django/VIM/apps/instruments/views/instrument_detail.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/web-app/django/VIM/apps/instruments/views/instrument_detail.py b/web-app/django/VIM/apps/instruments/views/instrument_detail.py index 19786738..c36b7a39 100644 --- a/web-app/django/VIM/apps/instruments/views/instrument_detail.py +++ b/web-app/django/VIM/apps/instruments/views/instrument_detail.py @@ -37,19 +37,18 @@ def get_context_data(self, **kwargs): label_aliases_dict[language]["label"] = instrumentname else: label_aliases_dict[language]["aliases"].append(instrumentname) - context["label_aliases_dict"] = label_aliases_dict # Get the active language active_language_en = self.request.session.get("active_language_en", None) - context["active_language"] = ( + active_lang = ( Language.objects.get(en_label=active_language_en) if active_language_en else Language.objects.get(en_label="English") # default in English ) + context["active_language"] = active_lang # Get the instrument label in the active language and moving it to the top of label_aliases_dict # Set label to the first instrument name added in the language if there is no "umil_label" set - active_lang = context["active_language"] active_labels = instrument_names.filter(language=active_lang) umil_label = active_labels.filter(umil_label=True) if umil_label.exists(): @@ -61,6 +60,8 @@ def get_context_data(self, **kwargs): else: context["active_instrument_label"] = active_labels.first() + context["label_aliases_dict"] = label_aliases_dict + # Get all languages for the dropdown context["languages"] = Language.objects.all() From b15576203b2eb14acb7880b78e5816be2e939291 Mon Sep 17 00:00:00 2001 From: PouyaMohseni Date: Thu, 11 Dec 2025 13:55:27 -0500 Subject: [PATCH 4/4] refactor: order query directly instead of post-processing --- .../instruments/views/instrument_detail.py | 55 ++++++++++--------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/web-app/django/VIM/apps/instruments/views/instrument_detail.py b/web-app/django/VIM/apps/instruments/views/instrument_detail.py index c36b7a39..6df20897 100644 --- a/web-app/django/VIM/apps/instruments/views/instrument_detail.py +++ b/web-app/django/VIM/apps/instruments/views/instrument_detail.py @@ -1,5 +1,6 @@ from django.views.generic import DetailView from VIM.apps.instruments.models import Instrument, Language +from django.db.models import Case, When, Value class InstrumentDetail(DetailView): @@ -14,10 +15,35 @@ class InstrumentDetail(DetailView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - # Query the instrument names in all languages + # Get the active language + active_language_en = self.request.session.get("active_language_en", "English") + active_lang = Language.objects.get(en_label=active_language_en) + context["active_language"] = active_lang + + # Query the instrument names in all languages, sorted instrument_names = ( - context["instrument"].instrumentname_set.all().select_related("language") + context["instrument"] + .instrumentname_set.all() + .select_related("language") + # put 1 in labels in language=active_lang in the active_lang_first field + .annotate( + active_lang_first=Case( + When(language=active_lang, then=Value(1)), + default=Value(0), + ) + ) + .order_by( + "-active_lang_first", # active_lang matched names come first + "-umil_label", # the first label is umil_label + "pk", # first added names come first + ) ) + + if instrument_names.filter(language=active_lang).exists(): + context["active_instrument_label"] = instrument_names.first() + else: + context["active_instrument_label"] = "" + if self.request.user.is_authenticated: # Show all names for authenticated users context["instrument_names"] = instrument_names.all() @@ -37,30 +63,7 @@ def get_context_data(self, **kwargs): label_aliases_dict[language]["label"] = instrumentname else: label_aliases_dict[language]["aliases"].append(instrumentname) - - # Get the active language - active_language_en = self.request.session.get("active_language_en", None) - active_lang = ( - Language.objects.get(en_label=active_language_en) - if active_language_en - else Language.objects.get(en_label="English") # default in English - ) - context["active_language"] = active_lang - - # Get the instrument label in the active language and moving it to the top of label_aliases_dict - # Set label to the first instrument name added in the language if there is no "umil_label" set - active_labels = instrument_names.filter(language=active_lang) - umil_label = active_labels.filter(umil_label=True) - if umil_label.exists(): - context["active_instrument_label"] = umil_label.first() - - value = label_aliases_dict.pop(active_lang) - label_aliases_dict = {active_lang: value, **label_aliases_dict} - context["label_aliases_dict"] = label_aliases_dict - else: - context["active_instrument_label"] = active_labels.first() - - context["label_aliases_dict"] = label_aliases_dict + context["label_aliases_dict"] = label_aliases_dict # Get all languages for the dropdown context["languages"] = Language.objects.all()