Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions web-app/django/VIM/apps/instruments/views/instrument_detail.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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()
Expand All @@ -39,23 +65,6 @@ def get_context_data(self, **kwargs):
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"] = (
Language.objects.get(en_label=active_language_en)
if active_language_en
else Language.objects.get(en_label="English") # default in English
)

# Get the instrument label in the active language
# 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"])
umil_label = active_labels.filter(umil_label=True)
if umil_label.exists():
context["active_instrument_label"] = umil_label.first()
else:
context["active_instrument_label"] = active_labels.first()

# Get all languages for the dropdown
context["languages"] = Language.objects.all()

Expand Down