Skip to content

Commit

Permalink
feat: sort and search by language/name on edit page
Browse files Browse the repository at this point in the history
- Support sort by language
- Support sort by name
- Support search by language

Refs: #137
  • Loading branch information
zhannaklimanova committed Sep 18, 2024
1 parent 355d5fb commit 5b96da5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ th[scope='row'] {
text-align: center;
text-decoration: none;
outline: none;
color: #fff;
border: none;
border-radius: 3px;
margin-right: 5px;
Expand All @@ -97,3 +96,6 @@ th[scope='row'] {
.btn.publish {
background-color: #28a745;
}
.btn.search {
background-color: #9eb384
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{% extends "base.html" %}

{% load static %}

{% block title %}
Expand All @@ -23,6 +22,18 @@ <h2>
</h2>
<hr />
</div>

<div class="sort-search-form">
<form method="get" action="">
<input type="text" name="search" placeholder="Search by language..." value="{{ request.GET.search }}" />
<select name="sort">
<option value="language__en_label" {% if request.GET.sort == 'language__en_label' %}selected{% endif %}>Sort by Language</option>
<option value="name" {% if request.GET.sort == 'name' %}selected{% endif %}>Sort by Name</option>
</select>
<button class="btn search">Search/Sort</button>
</form>
</div>

<div class="detail-body">
<div class="instrument-forms">
<table class="table table-sm table-striped table-bordered">
Expand Down
15 changes: 10 additions & 5 deletions web-app/django/VIM/apps/instruments/views/instrument_detail.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.views.generic import DetailView
from VIM.apps.instruments.models import Instrument, Language


class InstrumentDetail(DetailView):
"""
Displays details of a specific instrument.
Expand All @@ -14,10 +13,16 @@ class InstrumentDetail(DetailView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)

# Query the instrument names in all languages
context["instrument_names"] = (
context["instrument"].instrumentname_set.all().select_related("language")
)
search_query = self.request.GET.get('search', '')
sort_by = self.request.GET.get('sort', 'language__en_label')

instrument_names = context["instrument"].instrumentname_set.all().select_related("language")

if search_query:
instrument_names = instrument_names.filter(language__en_label__icontains=search_query)

instrument_names = instrument_names.order_by(sort_by)
context["instrument_names"] = instrument_names

# Get the active language
active_language_en = self.request.session.get("active_language_en", None)
Expand Down

0 comments on commit 5b96da5

Please sign in to comment.