Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sort and search by language/name on edit page #159

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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
Comment on lines +20 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a solr set-up for UMIL... let's use that for search!

Lemme know if you want some examples for how to set this up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, some example would be great!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, a few things would be helpful:

  • The solr admin panel is location at port 8983 (so in development at localhost:8983). One of the more useful areas of this for debugging search applications is the Query section. In the drop-down on the left, you'll see the virtual-instrument-museum core. Select that one, and then in the panel that appears you can go to Query to get a UI for making solr queries. Useful for debugging queries.
  • An example from Cantus Ultimus: we use this view to query solr to provide search suggestions: https://github.com/DDMAL/cantus/blob/main/app/public/cantusdata/views/suggestion.py. The view is pretty simple: building a request to the solr server and then making it with the requests package.
  • I don't have an example of this, but if you wanted to perform the search without routing through a Django view, you could just make the request via Javascript to the solr server (eg. have the browser request to an endpoint that your route to the solr server using the nginx configurations and then unpack the json response in the client).


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