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 00c4f77e1..2c0edadfe 100644 --- a/dataedit/templates/dataedit/dataview.html +++ b/dataedit/templates/dataedit/dataview.html @@ -191,7 +191,7 @@

Metadata specification - + Download JSON Edit @@ -507,29 +507,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 c1d99cf26..a9232d0f1 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 0eb9976a7..7315a7744 100644 --- a/dataedit/views.py +++ b/dataedit/views.py @@ -52,6 +52,7 @@ from login import models as login_models from oeplatform.settings import DOCUMENTATION_LINKS, EXTERNAL_URLS +from .helper import order_metadata from .models import TableRevision from .models import View as DataViewModel @@ -935,28 +936,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: @@ -1008,7 +998,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 } @@ -1046,6 +1036,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, @@ -1878,6 +1869,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 74d0dcef2..4d64359e0 100644 --- a/versions/changelogs/current.md +++ b/versions/changelogs/current.md @@ -2,6 +2,8 @@ ## Changes +- fixed metadata order when downloading metadata from data view [(#1748)](https://github.com/OpenEnergyPlatform/oeplatform/pull/1748) + - Updated oeo in docker image to version 2.5 [(#1878)](https://github.com/OpenEnergyPlatform/oeplatform/pull/1878) - Fix typo and font-size after tag assignment update [(#1880)](https://github.com/OpenEnergyPlatform/oeplatform/pull/1880)