Skip to content

Commit

Permalink
Merge pull request #8110 from ckan/8108-data-dictionary-preview
Browse files Browse the repository at this point in the history
definition list for data dictionary view
  • Loading branch information
smotornyuk committed Mar 20, 2024
2 parents b0ed536 + c7543b7 commit fae6879
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 36 deletions.
3 changes: 3 additions & 0 deletions changes/8110.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Use a definition list for the Data Dictionary view on resource pages to allow
extra information for each field. Update example_idatadictionaryform plugin to
display extra information.
4 changes: 4 additions & 0 deletions ckanext/datastore/assets/css/datastore.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.accordion-button.no-after::after {
background-image: none;
}

input#lang-curl ~ div .example-curl {
display: none;
}
Expand Down
9 changes: 8 additions & 1 deletion ckanext/datastore/backend/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -2315,7 +2315,12 @@ def resource_fields(self, id: str) -> dict[str, Any]:
info['meta']['aliases'] = aliases

# get the data dictionary for the resource
data_dictionary = datastore_helpers.datastore_dictionary(id)
with engine.connect() as conn:
data_dictionary = _result_fields(
_get_fields_types(conn, id),
_get_field_info(conn, id),
None
)

schema_sql = sa.text(f'''
SELECT
Expand Down Expand Up @@ -2361,6 +2366,8 @@ def resource_fields(self, id: str) -> dict[str, Any]:
schemainfo[colname] = colinfo

for field in data_dictionary:
if field['id'].startswith('_'):
continue
field.update({'schema': schemainfo[field['id']]})
info['fields'].append(field)

Expand Down
7 changes: 2 additions & 5 deletions ckanext/datastore/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,8 @@ def datastore_dictionary(
"""
try:
return [
f for f in tk.get_action('datastore_search')(
{}, {
u'resource_id': resource_id,
u'limit': 0,
u'include_total': False})['fields']
f for f in tk.get_action('datastore_info')(
{}, {'id': resource_id})['fields']
if not f['id'].startswith(u'_') and (
include_columns is None or f['id'] in include_columns)
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{% block dictionary_view %}
<div class="module-content">
<h2>{{ _('Data Dictionary') }}</h2>

{% macro dictionary_field(label) %}
{% set content = caller().strip() %}
{% if content %}
<dt class="col-sm-3 text-secondary">{{ label }}</dt>
<dd class="col-sm-9">{{ content }}</dd>
{% endif %}
{% endmacro %}

{% for field in ddict %}

{% set field_type %}{% block dictionary_field_type scoped
%}{{ field.type }}{% endblock %}{% endset %}
{% set extra_info -%}
{% call dictionary_field(_('Label')) %}
{{ h.get_translated(field.get('info', {}), 'label') }}
{% endcall %}
{% call dictionary_field(_('Description')) %}
{{ h.render_markdown(h.get_translated(
field.get('info', {}), 'notes')) }}
{% endcall %}
{% block dictionary_field_extras scoped %}
{% endblock %}
{%- endset %}
{# render_markdown to strip snippet comments and whitespace #}
{% set has_extra=h.render_markdown(extra_info) %}

<div class="accordion">
<div class="accordion-item">
<h3 class="accordion-header" id="field{{ prefix }}-{{ loop.index }}">
{%- if has_extra -%}
<button class="accordion-button collapsed" type="button"
data-bs-toggle="collapse" aria-expanded="false"
data-bs-target="#collapse{{ prefix }}-{{ loop.index }}"
aria-controls="collapse{{ prefix }}-{{ loop.index }}">
{%- else -%}
<button class="accordion-button no-after" disabled>
{%- endif -%}
<div class="col-1">{{ loop.index }}.</div><div class="col-7">
{{- h.get_translated(field.get('info', {}), 'label')
or field.id -}}
</div>
<div class="col-4">{{ field_type }}</div>
</button>
</h3>
{% if has_extra %}
<div id="collapse{{ prefix }}-{{ loop.index }}"
class="accordion-collapse collapse"
aria-labelledby="field{{ prefix }}-{{ loop.index }}">
<dl class="row accordion-body">
{% block resource_data_dictionary_field scoped %}
{% call dictionary_field(_('ID')) %}{{ field.id }}{% endcall %}
{% call dictionary_field(_('Type')) %}{{ field_type }}{% endcall %}
{{ extra_info }}
{% endblock %}
</dl>
</div>
{% endif %}
</div>
</div>

{% endfor %}
</div>
{% endblock %}
27 changes: 4 additions & 23 deletions ckanext/datastore/templates/package/resource_read.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,10 @@

{% block resource_additional_information_inner %}
{% if res.datastore_active %}
{% block resource_data_dictionary %}
<div class="module-content">
<h2>{{ _('Data Dictionary') }}</h2>
<table class="table table-striped table-bordered table-condensed" data-module="table-toggle-more">
<thead>
{% block resouce_data_dictionary_headers %}
<tr>
<th scope="col">{{ _('Column') }}</th>
<th scope="col">{{ _('Type') }}</th>
<th scope="col">{{ _('Label') }}</th>
<th scope="col">{{ _('Description') }}</th>
</tr>
{% endblock %}
</thead>
{% block resource_data_dictionary_data %}
{% set dict=h.datastore_dictionary(res.id) %}
{% for field in dict %}
{% snippet "package/snippets/dictionary_table.html", field=field %}
{% endfor %}
{% endblock %}
</table>
</div>
{% endblock %}
{% block resource_data_dictionary %}
{% snippet 'datastore/snippets/dictionary_view.html',
ddict=h.datastore_dictionary(res.id) %}
{% endblock %}
{% endif %}
{{ super() }}
{% endblock %}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% ckan_extends %}

{% block dictionary_field_extras %}
{{ super() }}
{% call dictionary_field(_('An integer')) %}{{ field.an_int }}{% endcall %}
{% call dictionary_field(_('JSON object')) %}{{ field.json_obj }}{% endcall %}
{% call dictionary_field(_('Always increasing')) %}{{ field.only_up }}{% endcall %}
{% call dictionary_field(_('Sticky input')) %}{{ field.sticky }}{% endcall %}
{% endblock %}

0 comments on commit fae6879

Please sign in to comment.