Skip to content

Commit

Permalink
fix: add "description" & "alias" fields into InstrumentName model
Browse files Browse the repository at this point in the history
- add "description" & "alias" fields into InstrumentName model;
- fix "Non classifiés" in facet search;
- support only English and French in facet search;
- fix a bug: replace `active_language.en_label == "french"` with `... "French"`;

Refs: #160 #161
  • Loading branch information
kunfang98927 committed Oct 4, 2024
1 parent e85db70 commit 6c04418
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Command(BaseCommand):
NOTE: For now, this script only imports instrument names in English and French. It
also only imports a set of previously-curated instruments that have images available.
This list of instruments is stored in startup_data/vim_instruments_with_images-15sept.csv
This list of instruments is stored in startup_data/all_instruments_with_16aug_2024.csv
"""

help = "Imports instrument objects"
Expand Down Expand Up @@ -48,6 +48,20 @@ def parse_instrument_data(
ins_names: dict[str, str] = {
value["language"]: value["value"] for key, value in ins_labels.items()
}

# Get available instrument descriptions
ins_descriptions: dict = instrument_data["descriptions"]
ins_descs: dict[str, str] = {
value["language"]: value["value"] for key, value in ins_descriptions.items()
}

# Get available instrument aliases
ins_aliases: dict = instrument_data["aliases"]
ins_alias: dict[str, list[str]] = {
key: [value["value"] for value in values]
for key, values in ins_aliases.items()
}

# Get Hornbostel-Sachs and MIMO classifications, if available
ins_hbs: Optional[list[dict]] = instrument_data["claims"].get("P1762")
ins_mimo: Optional[list[dict]] = instrument_data["claims"].get("P3763")
Expand All @@ -62,6 +76,8 @@ def parse_instrument_data(
parsed_data: dict[str, str | dict[str, str]] = {
"wikidata_id": instrument_id,
"ins_names": ins_names,
"ins_descs": ins_descs,
"ins_alias": ins_alias,
"hornbostel_sachs_class": hbs_class,
"mimo_class": mimo_class,
}
Expand All @@ -80,7 +96,7 @@ def get_instrument_data(self, instrument_ids: list[str]) -> list[dict]:
ins_ids_str: str = "|".join(instrument_ids)
url = (
"https://www.wikidata.org/w/api.php?action=wbgetentities&"
f"ids={ins_ids_str}&format=json&props=labels|descriptions|"
f"ids={ins_ids_str}&format=json&props=labels|descriptions|aliases|"
"claims&languages=en|fr"
)
response = requests.get(url, timeout=10)
Expand All @@ -104,11 +120,17 @@ def create_database_objects(
thumbnail_img_path [str]: Path to the thumbnail of the instrument image
"""
ins_names = instrument_attrs.pop("ins_names")
ins_descs = instrument_attrs.pop("ins_descs")
ins_alias = instrument_attrs.pop("ins_alias")
instrument = Instrument.objects.create(**instrument_attrs)
for lang, name in ins_names.items():
description = ins_descs.get(lang, "")
alias = ins_alias.get(lang, [])
InstrumentName.objects.create(
instrument=instrument,
language=self.language_map[lang],
description=description,
alias=", ".join(alias),
name=name,
source_name="Wikidata",
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.5 on 2024-10-04 15:30

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("instruments", "0005_remove_language_wikidata_id"),
]

operations = [
migrations.AddField(
model_name="instrumentname",
name="alias",
field=models.CharField(
blank=True, help_text="Alternative name for the instrument"
),
),
migrations.AddField(
model_name="instrumentname",
name="description",
field=models.CharField(
blank=True, help_text="Description of the instrument name"
),
),
]
6 changes: 6 additions & 0 deletions web-app/django/VIM/apps/instruments/models/instrument_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ class InstrumentName(models.Model):
source_name = models.CharField(
max_length=50, blank=False, help_text="Who or what called the instrument this?"
) # Stand-in for source data; format TBD
description = models.CharField(
blank=True, help_text="Description of the instrument name"
) # Stand-in for description
alias = models.CharField(
blank=True, help_text="Alternative name for the instrument"
) # Stand-in for alias
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<div class="col-xl-3">
<div class="px-3 pt-3 me-1 mb-3 sidebar-container">
<div class="info-block p-2 mb-3 notranslate">
{% if active_language.en_label == "french" %}
{% if active_language.en_label == "French" %}
<h4>Système Hornbostel-Sachs</h4>
{% else %}
<h4>Hornbostel-Sachs Classification</h4>
Expand All @@ -30,7 +30,11 @@ <h4>Hornbostel-Sachs Classification</h4>
<li class="list-group-item d-flex justify-content-between p-0"
current-value="{{ hbs_facet_item.value }}">
{% if hbs_facet_item.name == '' %}
<span class="text-start">Unclassified</span>
{% if active_language.en_label == "French" %}
<span class="text-start">Non classifiés</span>
{% else %}
<span class="text-start">Unclassified</span>
{% endif %}
{% else %}
<span class="text-start">{{ hbs_facet_item.value }} - {{ hbs_facet_item.name }} ({{ hbs_facet_item.count }})</span>
{% endif %}
Expand Down Expand Up @@ -94,10 +98,13 @@ <h4 class="ms-3 me-2 my-auto">
</button>
<ul class="dropdown-menu umil-dropdown-menu">
{% for language in languages %}
<li>
<a class="dropdown-item umil-dropdown-item"
href="?language={{ language.en_label }}">{{ language.autonym|title }}</a>
</li>
{% comment %} Here only English and French are shown {% endcomment %}
{% if language.en_label == "English" or language.en_label == "French" %}
<li>
<a class="dropdown-item umil-dropdown-item"
href="?language={{ language.en_label }}">{{ language.autonym|title }}</a>
</li>
{% endif %}
{% endfor %}
</ul>
</div>
Expand Down
4 changes: 2 additions & 2 deletions web-app/django/VIM/apps/main/templates/main/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ <h1>
</div>
<div class="row">
<div class="col-6">
<a href="{% url "instrument-list" %}">
<a href="{% url "instrument-list" %}?language=English">
<button class="btn btn-warning rounded-pill start-btn">Get Started</button>
</a>
</div>
<div class="col-6 notranslate">
<a href="{% url "instrument-list" %}?language=french">
<a href="{% url "instrument-list" %}?language=French">
<button class="btn btn-warning rounded-pill start-btn">Commencer</button>
</a>
</div>
Expand Down

0 comments on commit 6c04418

Please sign in to comment.