diff --git a/web-app/django/VIM/apps/instruments/management/commands/import_instruments.py b/web-app/django/VIM/apps/instruments/management/commands/import_instruments.py index f838f92..0328ded 100644 --- a/web-app/django/VIM/apps/instruments/management/commands/import_instruments.py +++ b/web-app/django/VIM/apps/instruments/management/commands/import_instruments.py @@ -125,15 +125,23 @@ def create_database_objects( instrument = Instrument.objects.create(**instrument_attrs) for lang, name in ins_names.items(): description = ins_descs.get(lang, "") - alias = ins_alias.get(lang, []) + # Create InstrumentName object for "name" in "lang" with "description" InstrumentName.objects.create( instrument=instrument, language=self.language_map[lang], description=description, - alias=", ".join(alias), name=name, source_name="Wikidata", ) + alias = ins_alias.get(lang, []) + # Create InstrumentName object for "alias" in language "lang" + for alias_name in alias: + InstrumentName.objects.create( + instrument=instrument, + language=self.language_map[lang], + name=alias_name, + source_name="Wikidata", + ) img_obj = AVResource.objects.create( instrument=instrument, type="image", diff --git a/web-app/django/VIM/apps/instruments/migrations/0006_instrumentname_alias_instrumentname_description.py b/web-app/django/VIM/apps/instruments/migrations/0006_instrumentname_description.py similarity index 60% rename from web-app/django/VIM/apps/instruments/migrations/0006_instrumentname_alias_instrumentname_description.py rename to web-app/django/VIM/apps/instruments/migrations/0006_instrumentname_description.py index 1238c97..fb05de8 100644 --- a/web-app/django/VIM/apps/instruments/migrations/0006_instrumentname_alias_instrumentname_description.py +++ b/web-app/django/VIM/apps/instruments/migrations/0006_instrumentname_description.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.5 on 2024-10-04 15:30 +# Generated by Django 4.2.5 on 2024-10-21 15:05 from django.db import migrations, models @@ -9,13 +9,6 @@ class Migration(migrations.Migration): ] 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", diff --git a/web-app/django/VIM/apps/instruments/models/instrument_name.py b/web-app/django/VIM/apps/instruments/models/instrument_name.py index 5232bb6..a8af53a 100644 --- a/web-app/django/VIM/apps/instruments/models/instrument_name.py +++ b/web-app/django/VIM/apps/instruments/models/instrument_name.py @@ -11,6 +11,3 @@ class InstrumentName(models.Model): 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 diff --git a/web-app/django/VIM/apps/instruments/templates/instruments/includes/instrumentContainer.html b/web-app/django/VIM/apps/instruments/templates/instruments/includes/instrumentContainer.html new file mode 100644 index 0000000..4909f6d --- /dev/null +++ b/web-app/django/VIM/apps/instruments/templates/instruments/includes/instrumentContainer.html @@ -0,0 +1,28 @@ +{% load static %} + +
+ instrument thumbnail +
+ {% if user.is_authenticated %} + + + {% endif %} + View on Wikidata +
+
diff --git a/web-app/django/VIM/apps/instruments/templates/instruments/includes/masonryView.html b/web-app/django/VIM/apps/instruments/templates/instruments/includes/masonryView.html index 28041e2..f0e93c6 100644 --- a/web-app/django/VIM/apps/instruments/templates/instruments/includes/masonryView.html +++ b/web-app/django/VIM/apps/instruments/templates/instruments/includes/masonryView.html @@ -6,37 +6,11 @@
-
- instrument thumbnail -
- {% if user.is_authenticated %} - - - {% endif %} - View on Wikidata -
-
+ {% include "instruments/includes/instrumentContainer.html" %} +

- {% for instrumentname in instrument.instrumentname_set.all %} - {{ instrumentname.name|title }} - {% endfor %} + {{ instrument.instrumentname_set.all.first.name|title }}

diff --git a/web-app/django/VIM/apps/instruments/templates/instruments/includes/stdView.html b/web-app/django/VIM/apps/instruments/templates/instruments/includes/stdView.html index a528eb7..c9c4825 100644 --- a/web-app/django/VIM/apps/instruments/templates/instruments/includes/stdView.html +++ b/web-app/django/VIM/apps/instruments/templates/instruments/includes/stdView.html @@ -8,38 +8,12 @@
-
- instrument thumbnail -
- {% if user.is_authenticated %} - - - {% endif %} - View on Wikidata -
-
+ {% include "instruments/includes/instrumentContainer.html" %} +

- {% for instrumentname in instrument.instrumentname_set.all %} - {{ instrumentname.name|title }} - {% endfor %} + {{ instrument.instrumentname_set.all.first.name|title }}

diff --git a/web-app/django/VIM/apps/instruments/views/instrument_list.py b/web-app/django/VIM/apps/instruments/views/instrument_list.py index 9ffd947..9507fa5 100644 --- a/web-app/django/VIM/apps/instruments/views/instrument_list.py +++ b/web-app/django/VIM/apps/instruments/views/instrument_list.py @@ -1,17 +1,10 @@ """ Django views for the instrument list page. """ -import json from django.db.models import Prefetch from django.db.models.query import QuerySet -from django.views.decorators.csrf import csrf_exempt from django.views.generic import ListView -from django.http import JsonResponse import requests from VIM.apps.instruments.models import Instrument, Language, InstrumentName -from VIM.apps.instruments.views.wiki_apis import ( - get_csrf_token, - add_info_to_wikidata_entity, -) class InstrumentList(ListView): @@ -111,149 +104,3 @@ def get_queryset(self) -> QuerySet[Instrument]: return Instrument.objects.select_related("thumbnail").prefetch_related( instrumentname_prefetch_manager ) - - -# Django view to handle publishing to Wikidata -@csrf_exempt -def publish_name(request): - """ - View to publish new instrument names to Wikidata. - - This view expects a POST request with the following JSON body like this: - { - "wikidata_id": "Q12345", - "entries": [ - { - "language": "en", - "name": "English label", - "source": "Source name", - "description": "Description", - "alias": "Alias" - }, - { - "language": "fr", - "name": "French label", - ... - } - ], - "publish_to_wikidata": true - } - - The view will publish the provided entries to the Wikidata entity with the given ID. - - Returns: - JsonResponse: JSON response with status and message - """ - - if request.method == "POST": - # Parse the JSON request body - data = json.loads(request.body) - username = "YOUR_USERNAME" # Replace with actual username - password = "YOUR_PASSWORD" # Replace with actual credentials - wikidata_id = data.get("wikidata_id") - entries = data.get("entries") - publish_to_wikidata = data.get("publish_to_wikidata", False) - - if not wikidata_id or not entries: - return JsonResponse({"status": "error", "message": "Missing required data"}) - - try: - # Fetch the instrument from the database - instrument = Instrument.objects.get(wikidata_id=wikidata_id) - - # Process each entry: save locally, and conditionally publish to Wikidata - if publish_to_wikidata: - csrf_token, lgusername, error_message = get_csrf_token( - username, password - ) - - # Check if there was an error - if error_message: - return JsonResponse( - { - "status": "error", - "message": f"Failed to get CSRF token: {error_message}", - } - ) - for entry in entries: - language_code = entry["language"] - name = entry["name"] - source = entry["source"] - description = entry["description"] - alias = entry["alias"] - - # Get the language object - language = Language.objects.get(wikidata_code=language_code) - - # Optionally publish to Wikidata - if publish_to_wikidata: - # Publish the new label to Wikidata - response_label = add_info_to_wikidata_entity( - "wbsetlabel", - csrf_token, - wikidata_id, - name, - language_code, - lgusername, - ) - if "error" in response_label: - return JsonResponse( - { - "status": "error", - "message": f"Failed to publish label: {response_label}", - } - ) - - # Publish the new description to Wikidata - response_desc = add_info_to_wikidata_entity( - "wbsetdescription", - csrf_token, - wikidata_id, - description, - language_code, - lgusername, - ) - if "error" in response_desc: - return JsonResponse( - { - "status": "error", - "message": f"Failed to publish description: {response_desc}", - } - ) - - # Publish the new alias to Wikidata - response_alias = add_info_to_wikidata_entity( - "wbsetaliases", - csrf_token, - wikidata_id, - alias, - language_code, - lgusername, - ) - if "error" in response_alias: - return JsonResponse( - { - "status": "error", - "message": f"Failed to publish alias: {response_alias}", - } - ) - - # Save to the local database - InstrumentName.objects.create( - instrument=instrument, - language=language, - name=name, - source_name=source, - description=description, - alias=alias, - ) - - return JsonResponse( - { - "status": "success", - "message": "Data saved successfully!", - } - ) - except Instrument.DoesNotExist: - return JsonResponse({"status": "error", "message": "Instrument not found"}) - return JsonResponse({"status": "error", "message": "Invalid request method"}) diff --git a/web-app/django/VIM/apps/instruments/views/publish_name.py b/web-app/django/VIM/apps/instruments/views/publish_name.py new file mode 100644 index 0000000..0994fde --- /dev/null +++ b/web-app/django/VIM/apps/instruments/views/publish_name.py @@ -0,0 +1,169 @@ +""" Django view to handle publishing to Wikidata. """ + +import json +from django.views.decorators.csrf import csrf_protect +from django.http import JsonResponse +from VIM.apps.instruments.models import Instrument, Language, InstrumentName +from VIM.apps.instruments.views.wiki_apis import ( + get_csrf_token, + add_info_to_wikidata_entity, +) + + +@csrf_protect +def publish_name(request): + """ + View to publish new instrument names to Wikidata. + + This view expects a POST request with the following JSON body like this: + { + "wikidata_id": "Q12345", + "entries": [ + { + "language": "en", + "name": "English label", + "source": "Source name", + "description": "Description", + "alias": "Alias" + }, + { + "language": "fr", + "name": "French label", + ... + } + ], + "publish_to_wikidata": true + } + + The view will publish the provided entries to the Wikidata entity with the given ID. + + Returns: + JsonResponse: JSON response with status and message + """ + + if request.method == "POST": + # Parse the JSON request body + data = json.loads(request.body) + username = "YOUR_USERNAME" # Replace with actual username + password = "YOUR_PASSWORD" # Replace with actual credentials + wikidata_id = data.get("wikidata_id") + entries = data.get("entries") + publish_to_wikidata = data.get("publish_to_wikidata", False) + + if not wikidata_id or not entries: + return JsonResponse( + { + "status": "error", + "message": "Missing required data", + } + ) + + try: + # Fetch the instrument from the database + instrument = Instrument.objects.get(wikidata_id=wikidata_id) + + # Process each entry: save locally, and conditionally publish to Wikidata + if publish_to_wikidata: + csrf_token, lgusername, error_message = get_csrf_token( + username, password + ) + + # Check if there was an error + if error_message: + return JsonResponse( + { + "status": "error", + "message": f"Failed to get CSRF token: {error_message}", + } + ) + for entry in entries: + language_code = entry["language"] + name = entry["name"] + source = entry["source"] + description = entry["description"] + alias = entry["alias"] + + # Get the language object + language = Language.objects.get(wikidata_code=language_code) + + # Optionally publish to Wikidata + if publish_to_wikidata: + # Publish the new label to Wikidata + response_label = add_info_to_wikidata_entity( + "wbsetlabel", + csrf_token, + wikidata_id, + name, + language_code, + lgusername, + ) + if "error" in response_label: + return JsonResponse( + { + "status": "error", + "message": f"Failed to publish label: {response_label}", + } + ) + + # Publish the new description to Wikidata + response_desc = add_info_to_wikidata_entity( + "wbsetdescription", + csrf_token, + wikidata_id, + description, + language_code, + lgusername, + ) + if "error" in response_desc: + return JsonResponse( + { + "status": "error", + "message": f"Failed to publish description: {response_desc}", + } + ) + + # Publish the new alias to Wikidata + response_alias = add_info_to_wikidata_entity( + "wbsetaliases", + csrf_token, + wikidata_id, + alias, + language_code, + lgusername, + ) + if "error" in response_alias: + return JsonResponse( + { + "status": "error", + "message": f"Failed to publish alias: {response_alias}", + } + ) + + # Save to the local database + # for "name" in "lang" with "description" + InstrumentName.objects.create( + instrument=instrument, + language=language, + name=name, + source_name=source, + description=description, + ) + # if alias is provided, save to the local database + if alias: + # for "alias" in language "lang" + InstrumentName.objects.create( + instrument=instrument, + language=language, + name=alias, + source_name=source, + ) + + return JsonResponse( + { + "status": "success", + "message": "Data saved successfully!", + } + ) + except Instrument.DoesNotExist: + return JsonResponse({"status": "error", "message": "Instrument not found"}) + return JsonResponse({"status": "error", "message": "Invalid request method"}) diff --git a/web-app/django/VIM/urls.py b/web-app/django/VIM/urls.py index ed3b98e..88d36f7 100644 --- a/web-app/django/VIM/urls.py +++ b/web-app/django/VIM/urls.py @@ -19,10 +19,8 @@ from django.urls import path, include from django.conf import settings from django.conf.urls.i18n import i18n_patterns -from VIM.apps.instruments.views.instrument_list import ( - InstrumentList, - publish_name, -) +from VIM.apps.instruments.views.instrument_list import InstrumentList +from VIM.apps.instruments.views.publish_name import publish_name urlpatterns = i18n_patterns( path("admin/", admin.site.urls),