From d771c4b4594fb7dc3044863d6bff4eaa58fd81b6 Mon Sep 17 00:00:00 2001 From: Hendrik Huyskens Date: Tue, 25 Jun 2024 13:58:44 +0200 Subject: [PATCH 1/2] Fix metadata order when downloading from data view --- dataedit/helper.py | 10 +++++++ dataedit/templates/dataedit/dataview.html | 25 +--------------- dataedit/urls.py | 7 +++++ dataedit/views.py | 36 ++++++++++++----------- versions/changelogs/current.md | 1 + 5 files changed, 38 insertions(+), 41 deletions(-) diff --git a/dataedit/helper.py b/dataedit/helper.py index 1e883ff18..beb913f53 100644 --- a/dataedit/helper.py +++ b/dataedit/helper.py @@ -4,6 +4,7 @@ """ from dataedit.models import Table +from dataedit.metadata import TEMPLATE_V1_5 ############################################## # Table view related # @@ -54,6 +55,15 @@ def get_readable_table_name(table_obj: Table) -> str: return label +############################################## +# Metadata related # +############################################## + +def order_metadata(metadata: dict) -> dict: + """Brings metadata into consistent order based on TEMPLATE_V1_5""" + ordered_metadata = {key: metadata.get(key) for key in TEMPLATE_V1_5} + return ordered_metadata + ############################################## # Open Peer Review related # ############################################## diff --git a/dataedit/templates/dataedit/dataview.html b/dataedit/templates/dataedit/dataview.html index c89cf788f..197cdd34a 100644 --- a/dataedit/templates/dataedit/dataview.html +++ b/dataedit/templates/dataedit/dataview.html @@ -191,7 +191,7 @@

Metadata specification - + Download JSON Edit @@ -505,29 +505,6 @@

API Usage

}; load_view(schema, table, csrftoken, view); - var downloadMetadata = function () { - var metaUrl = "/api/v0/schema/{{ schema }}/tables/{{ table }}/meta"; - $.get(metaUrl).then(function (json) { - console.log(json); - // create data url - var json = JSON.stringify(json, null, 1); - console.log(json); - blob = new Blob([json], { type: "application/json" }), - dataUrl = URL.createObjectURL(blob); - // create link - var a = document.createElement("a"); - document.body.appendChild(a); - // assign url and click - a.style = "display: none"; - a.href = dataUrl; - a.download = '{{ table }}.metadata.json'; - a.click(); - // cleanup - URL.revokeObjectURL(dataUrl); - a.parentNode.removeChild(a); - }) - }; - DataEdit(table=table, schema=schema); $(function () { diff --git a/dataedit/urls.py b/dataedit/urls.py index 098deb902..2b38431c3 100644 --- a/dataedit/urls.py +++ b/dataedit/urls.py @@ -33,6 +33,13 @@ views.RevisionView.as_view(), name="input", ), + url( + r"^view/(?P{qual})/(?P{qual})/download_metadata$".format( + qual=pgsql_qualifier + ), + views.get_metadata, + name="metadata", + ), url( r"^view/(?P{qual})/(?P
{qual})/permissions$".format( qual=pgsql_qualifier diff --git a/dataedit/views.py b/dataedit/views.py index 5638e1403..170b9f9a7 100644 --- a/dataedit/views.py +++ b/dataedit/views.py @@ -51,6 +51,7 @@ from dataedit.structures import TableTags, Tag from login import models as login_models +from .helper import order_metadata from .models import TableRevision from .models import View as DataViewModel @@ -928,28 +929,17 @@ def get(self, request, schema, table): actions.create_meta(schema, table) metadata = load_metadata_from_db(schema, table) - table_obj = Table.load(schema, table) - if table_obj is None: - raise Http404("Table object could not be loaded") - - oemetadata = table_obj.oemetadata - - from dataedit.metadata import TEMPLATE_V1_5 - - def iter_oem_key_order(metadata: dict): - oem_151_key_order = [key for key in TEMPLATE_V1_5.keys()] - for key in oem_151_key_order: - yield key, metadata.get(key) - - ordered_oem_151 = {key: value for key, value in iter_oem_key_order(metadata)} - meta_widget = MetaDataWidget(ordered_oem_151) - revisions = [] + ordered_metadata = order_metadata(metadata) + meta_widget = MetaDataWidget(ordered_metadata) api_changes = change_requests(schema, table) data = api_changes.get("data") display_message = api_changes.get("display_message") display_items = api_changes.get("display_items") + table_obj = Table.load(schema, table) + if table_obj is None: + raise Http404("Table object could not be loaded") is_admin = False can_add = False if request.user and not request.user.is_anonymous: @@ -1001,7 +991,7 @@ def iter_oem_key_order(metadata: dict): schema=schema, table=table ), "reviewer": PeerReviewManager.load_reviewer(schema=schema, table=table), - "opr_enabled": oemetadata + "opr_enabled": metadata is not None, # check if the table has the metadata } @@ -1039,6 +1029,7 @@ def iter_oem_key_order(metadata: dict): # Construct the context object for the template # ######################################################### + revisions = [] context_dict = { "meta_widget": meta_widget.render(), "revisions": revisions, @@ -1860,6 +1851,17 @@ def get(self, request): ) +def get_metadata(request, schema, table): + """Return metadata as JSON in order consistent with MetadataBuilder""" + metadata = load_metadata_from_db(schema, table) + ordered_metadata = order_metadata(metadata) + + json_data = json.dumps(ordered_metadata, indent=1) + response = HttpResponse(json_data, content_type='application/json') + response['Content-Disposition'] = f'attachment; filename="{table}.metadata.json"' + return response + + class PeerReviewView(LoginRequiredMixin, View): """ A view handling the peer review of metadata. This view supports loading, diff --git a/versions/changelogs/current.md b/versions/changelogs/current.md index 339c31ecd..e62a660ac 100644 --- a/versions/changelogs/current.md +++ b/versions/changelogs/current.md @@ -1,6 +1,7 @@ # Changes to the oeplatform code ## Changes +- fixed metadata order when downloading metadata from data view ## Features From c1e7fde427302dde5e8a5a4e949ce44b7961674e Mon Sep 17 00:00:00 2001 From: Jonas Huber <38939526+jh-RLI@users.noreply.github.com> Date: Tue, 2 Jul 2024 13:53:27 +0200 Subject: [PATCH 2/2] Update versions/changelogs/current.md --- versions/changelogs/current.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/versions/changelogs/current.md b/versions/changelogs/current.md index 9b7ec6500..74ba37f68 100644 --- a/versions/changelogs/current.md +++ b/versions/changelogs/current.md @@ -2,7 +2,7 @@ ## Changes -- fixed metadata order when downloading metadata from data view +- fixed metadata order when downloading metadata from data view [(#1748)](https://github.com/OpenEnergyPlatform/oeplatform/pull/1748) - All visitors can use metadata builder in standalone version [(#1746)](https://github.com/OpenEnergyPlatform/oeplatform/pull/1746)