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

Fix metadata order when downloading from data view #1748

Open
wants to merge 7 commits 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
10 changes: 10 additions & 0 deletions dataedit/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

from dataedit.models import Table
from dataedit.metadata import TEMPLATE_V1_5

##############################################
# Table view related #
Expand Down Expand Up @@ -54,6 +55,15 @@ def get_readable_table_name(table_obj: Table) -> str:
return label


##############################################
# Metadata related #
##############################################

def order_metadata(metadata: dict) -> dict:
Copy link
Contributor

@jh-RLI jh-RLI Jul 1, 2024

Choose a reason for hiding this comment

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

👍 If you agree that this is good enough. There is still a slight difference (1. download while using the oemetabuilde 2. download while viewing the table detail page) because this only sorts the "top" level key´s. I think it's fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

could adapt this also, so that key order is preserved recursively

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, that could be a quick solution. Then I would vote for implementing it : in favour of everything being as similar as possible.

"""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 #
##############################################
Expand Down
25 changes: 1 addition & 24 deletions dataedit/templates/dataedit/dataview.html
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ <h1 class="main-header__title">
Metadata specification
</a>
</span>
<button onclick="downloadMetadata();" data-bs-toggle="tooltip" title="Download metadata in JSON format">Download JSON</button>
<a href="{% url 'dataedit:metadata' schema=schema table=table %}" class="btn btn-primary btn-sm" data-bs-toggle="tooltip" title="Download metadata in JSON format">Download JSON</a>
<span {% if not can_add or opr.opr_id and not opr.is_finished %} data-bs-toggle="tooltip" title="You need write permissions on this table to edit metadata or your review is in progress, data can't be changed" {% endif %}>
<a href="{{table}}/meta_edit" type="button" class="btn btn-primary btn-sm {% if not can_add or opr.opr_id and not opr.is_finished %} disabled {% endif %}">
Edit
Expand Down Expand Up @@ -507,29 +507,6 @@ <h2 class="table-sidebar__heading">API Usage</h2>
};
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 () {
Expand Down
7 changes: 7 additions & 0 deletions dataedit/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
views.RevisionView.as_view(),
name="input",
),
url(
r"^view/(?P<schema>{qual})/(?P<table>{qual})/download_metadata$".format(
qual=pgsql_qualifier
),
views.get_metadata,
name="metadata",
),
url(
r"^view/(?P<schema>{qual})/(?P<table>{qual})/permissions$".format(
qual=pgsql_qualifier
Expand Down
36 changes: 19 additions & 17 deletions dataedit/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions versions/changelogs/current.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down