Skip to content

Commit

Permalink
scheming_missing_required_fields helper and error icons
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Nov 25, 2024
1 parent d43207d commit e83cea9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ckanext/scheming/ckan_formpages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ dataset_fields:
label: Description
form_snippet: markdown.html
form_placeholder: eg. Some useful notes about the data
required: true
validators: scheming_required unicode_safe

- field_name: owner_org
label: Organization
Expand Down
18 changes: 18 additions & 0 deletions ckanext/scheming/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,21 @@ def scheming_flatten_subfield(subfield, data):
for k in record:
flat[prefix + k] = record[k]
return flat


@helper
def scheming_missing_required_fields(pages, data=None, package_id=None):
if package_id:
try:
data = LocalCKAN().action.package_show(id=package_id)
except (NotFound, NotAuthorized):
pass
if data is None:
data = {}
missing = []
for p in pages:
missing.append([
f['field_name'] for f in p['fields']
if f.get('required') and not data.get(f['field_name'])
])
return missing
6 changes: 6 additions & 0 deletions ckanext/scheming/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ def resource_form(self):
def package_types(self):
return list(self._schemas)

def resource_validation_dependencies(self, package_type):
# Compatibility with https://github.com/ckan/ckan/pull/8421
schema = self._schemas.get(package_type, {})
dfr = schema.get('draft_fields_required', True)
return [] if dfr else ['state']

def validate(self, context, data_dict, schema, action):
"""
Validate and convert for package_create, package_update and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{%- set pages = h.scheming_get_dataset_form_pages(dataset_type) -%}
{%- if pages -%}
{%- set active_page = data.get('_form_page', 1) | int -%}
{%- set draft_missing_required = h.scheming_missing_required_fields(pages, data) -%}
<ol class="stages stage-1">
{%- for p in pages -%}
<li class="{{
Expand All @@ -20,6 +21,16 @@
page=loop.index)
}}">{{ h.scheming_language_text(p.title) }}</a>{%
else %}{{ h.scheming_language_text(p.title) }}{% endif %}
{%- set mreq = draft_missing_required[loop.index0] -%}
{% if mreq %}
<abbr title="{{
ungettext("Missing required field", "Missing required fields", mreq | length)
}}: {% for m in mreq %}{{
h.scheming_language_text(h.scheming_field_by_name(p.fields, m).label)
}}{% if not loop.last %}, {% endif %}{% endfor %}">
<i class="fa fa-exclamation-circle text-danger fs-3" aria-hidden="true"></i>
</abbr>
{% endif %}
</span>
</li>
{%- endfor -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

{% block stages %}
{%- set pages = h.scheming_get_dataset_form_pages(dataset_type) -%}
{%- set draft_missing_required = h.scheming_missing_required_fields(pages, package_id=pkg_name) -%}
{%- if pages and stage -%}
<ol class="stages stage-1">
{%- for p in pages -%}
Expand All @@ -14,6 +15,16 @@
id=pkg_name,
page=loop.index)
}}">{{ h.scheming_language_text(p.title) }}</a>
{%- set mreq = draft_missing_required[loop.index0] -%}
{% if mreq %}
<abbr title="{{
ungettext("Missing required field", "Missing required fields", mreq | length)
}}: {% for m in mreq %}{{
h.scheming_language_text(h.scheming_field_by_name(p.fields, m).label)
}}{% if not loop.last %}, {% endif %}{% endfor %}">
<i class="fa fa-exclamation-circle text-danger fs-3" aria-hidden="true"></i>
</abbr>
{% endif %}
</span>
</li>
{%- endfor -%}
Expand Down

0 comments on commit e83cea9

Please sign in to comment.