Skip to content

Commit

Permalink
Merge pull request #2 from salsa-nathan/feature/DDCI-5_temporal_conte…
Browse files Browse the repository at this point in the history
…nt_ac4

[DDCI-115] - added temporal date fields
  • Loading branch information
salsa-nathan authored Aug 26, 2020
2 parents 586803a + 2d8f743 commit 3c47dec
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 1 deletion.
9 changes: 8 additions & 1 deletion ckanext/qdes_schema/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
import ckan.plugins.toolkit as toolkit
import json

from ckanext.qdes_schema import helpers
from ckanext.qdes_schema import helpers, validators


class QDESSchemaPlugin(plugins.SingletonPlugin):
plugins.implements(plugins.IConfigurer)
plugins.implements(plugins.ITemplateHelpers)
plugins.implements(plugins.IValidators)

# IConfigurer

# IValidators
def get_validators(self):
return {
'qdes_temporal_start_end_date': validators.qdes_temporal_start_end_date
}

def update_config(self, config_):
toolkit.add_template_directory(config_, 'templates')
toolkit.add_public_directory(config_, 'public')
Expand Down
18 changes: 18 additions & 0 deletions ckanext/qdes_schema/qdes_ckan_dataset.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@
"display_property": "dcat:theme",
"display_group": "description"
},
{
"field_name": "temporal_start",
"label": "Temporal coverage start",
"display_property": "dcat:startDate",
"preset": "date",
"validators": "qdes_temporal_start_end_date",
"display_group": "temporal content",
"sub_heading": "temporal coverage"
},
{
"field_name": "temporal_end",
"label": "Temporal coverage end",
"display_property": "dcat:endDate",
"preset": "date",
"validators": "qdes_temporal_start_end_date",
"display_group": "temporal content",
"sub_heading": "temporal coverage"
},
{
"field_name": "tag_string",
"label": "Tags",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{% extends "package/snippets/additional_info.html" %}

{%- set exclude_fields = [
'id',
'title',
'name',
'notes',
'tag_string',
'license_id',
'owner_org',
] -%}

{%- set temporal_coverage = [
'temporal_start',
'temporal_end',
] -%}

{% block package_additional_info %}
{%- for field in schema.dataset_fields -%}
{%- if field.field_name not in exclude_fields
and field.display_snippet is not none -%}
<tr>
<th scope="row" class="dataset-label">
{% if field.field_name in temporal_coverage %}
{{ "Temporal coverage start" if field.field_name == 'temporal_start' else "Temporal coverage end" }}
{%- else -%}
{{ h.scheming_language_text(field.label) }}
{%- endif -%}
</th>
<td class="dataset-details"{%
if field.display_property %} property="{{ field.display_property
}}"{% endif %}>{%- snippet 'scheming/snippets/display_field.html',
field=field, data=pkg_dict, schema=schema -%}</td>
</tr>
{%- endif -%}
{%- endfor -%}
{% if h.check_access('package_update',{'id':pkg_dict.id}) %}
<tr>
<th scope="row" class="dataset-label">{{ _("State") }}</th>
<td class="dataset-details">{{ _(pkg_dict.state) }}</td>
</tr>
{% endif %}
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<h2>{{ field.display_group|capitalize }}</h2>
{%- endif -%}
{%- endif -%}

{%- if field.sub_heading is not none and field.sub_heading != ns.sub_heading -%}
{%- set ns.sub_heading = field.sub_heading -%}
<h3>{{ field.sub_heading|capitalize }}</h3>
{%- endif -%}

{%- if field.form_snippet is not none -%}
{%- snippet 'scheming/snippets/form_field.html',
field=field, data=data, errors=errors, licenses=c.licenses,
Expand Down
23 changes: 23 additions & 0 deletions ckanext/qdes_schema/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import ckan.plugins.toolkit as toolkit
from datetime import datetime as dt

def qdes_temporal_start_end_date(key, flattened_data, errors, context):
"""
Validate the start and date.
It will raise an error, when:
- If the either start or end date is not empty.
- If the start date < end date
"""
temporal_start_value = flattened_data[('temporal_start',)]
temporal_end_value = flattened_data[('temporal_end',)]

if ((len(temporal_start_value) > 0) != (len(temporal_end_value) > 0)) and (len(flattened_data.get(key)) == 0):
raise toolkit.Invalid("This field should not be empty")

if (len(temporal_start_value) > 0) and (len(temporal_end_value) > 0):
if dt.strptime(temporal_start_value, '%Y-%m-%d') > dt.strptime(temporal_end_value, '%Y-%m-%d'):
if key == ('temporal_start',):
raise toolkit.Invalid("Must be earlier than end date.")
elif key == ('temporal_end',):
raise toolkit.Invalid("Must be later than start date.")

0 comments on commit 3c47dec

Please sign in to comment.