diff --git a/ckanext/cfpb_extrafields/plugin.py b/ckanext/cfpb_extrafields/plugin.py index 16a761d..aeee0c9 100644 --- a/ckanext/cfpb_extrafields/plugin.py +++ b/ckanext/cfpb_extrafields/plugin.py @@ -280,6 +280,7 @@ def _modify_package_schema(self, schema): schema['resources'].update({ 'approximate_total_size' : [tk.get_validator('ignore_missing'),], 'intake_date' : [v.reasonable_date_validator, tk.get_validator('ignore_missing'),], + 'update_date' : [v.reasonable_date_validator, tk.get_validator('ignore_missing'),], 'resource_type' : [tk.get_validator('ignore_missing'),], 'storage_location' : [tk.get_validator('ignore_missing'),], 'storage_location_path' : [tk.get_validator('ignore_missing'),], @@ -395,6 +396,7 @@ def show_package_schema(self): schema['resources'].update({ 'approximate_total_size' : [ tk.get_validator('ignore_missing'),], 'intake_date' : [tk.get_validator('ignore_missing'),], + 'update_date' : [tk.get_validator('ignore_missing'),], 'resource_type' : [ tk.get_validator('ignore_missing'),], 'storage_location' : [ tk.get_validator('ignore_missing'),], 'storage_location_path' : [ tk.get_validator('ignore_missing'),], diff --git a/ckanext/cfpb_extrafields/scripts/update_resource.py b/ckanext/cfpb_extrafields/scripts/update_resource.py new file mode 100644 index 0000000..4591de6 --- /dev/null +++ b/ckanext/cfpb_extrafields/scripts/update_resource.py @@ -0,0 +1,60 @@ +"""Update CKAN Resources + +usage: CKAN_API_KEY= CKAN_URL= [CKAN_DATE_FIELD=] python update_resource.py [] + + CKAN_API_KEY: the API key on the bottom left of your ckan user settings page + CKAN_URL: url to the data catalog, i.e. http://catalog.data.cfpb.local + CKAN_DATE_FIELD: the field on the resource metadata to update. defaults to update_date + resource_id: the unique ID of a ckan resource + timestamp: YYYY-MM-DD timestamp to use as the new update time. Defaults to now. +""" +from __future__ import print_function +import datetime as dt +import json +import os +import sys + +import requests + +import logging +logging.basicConfig(level=logging.DEBUG) + +API_KEY = os.environ["CKAN_API_KEY"] +CKAN_URL = os.environ["CKAN_URL"] +DATE_FIELD = os.environ.get("CKAN_DATE_FIELD", "update_date") + +def do_action(action, data, ckan_url=CKAN_URL, api_key=API_KEY): + response = requests.post( + ckan_url + "/api/3/action/" + action, + json=data, + headers={"Authorization": api_key}, + ) + response.raise_for_status() + return response + +def get_resource(resource_id, ckan_url=CKAN_URL, api_key=API_KEY): + response = do_action("resource_show", {"id": resource_id}, ckan_url, api_key) + return response.json()["result"] + +def update_resource(resource, ckan_url=CKAN_URL, api_key=API_KEY): + do_action("resource_update", resource, ckan_url, api_key) + +def main(args): + if len(args) < 2 or args[1] in ["help", "-h", "--help"]: + print(__doc__) + else: + resource_id = args[1] + + if len(args) > 2: + timestamp = args[2] + else: + timestamp = dt.datetime.now().strftime("%Y-%m-%d") + + resource = get_resource(resource_id) + resource[DATE_FIELD] = timestamp + update_resource(resource) + + +if __name__ == "__main__": + main(sys.argv) + diff --git a/ckanext/cfpb_extrafields/templates/package/resource_read.html b/ckanext/cfpb_extrafields/templates/package/resource_read.html index d036df7..f1fd1a9 100644 --- a/ckanext/cfpb_extrafields/templates/package/resource_read.html +++ b/ckanext/cfpb_extrafields/templates/package/resource_read.html @@ -145,6 +145,12 @@

{{ h.resource_display_name(res) | truncate(50) }}

{{ res.intake_date }} {% endif %} + {% if res.update_date %} + + {{ _("update date") }} + {{ res.update_date }} + + {% endif %} {{ _('metadata resource ID') }} {{ res.id or _('unknown') }} diff --git a/ckanext/cfpb_extrafields/templates/package/snippets/resource_type-specific/cfpb_extrafields_database.html b/ckanext/cfpb_extrafields/templates/package/snippets/resource_type-specific/cfpb_extrafields_database.html index ef6db4d..f5cea63 100644 --- a/ckanext/cfpb_extrafields/templates/package/snippets/resource_type-specific/cfpb_extrafields_database.html +++ b/ckanext/cfpb_extrafields/templates/package/snippets/resource_type-specific/cfpb_extrafields_database.html @@ -7,6 +7,7 @@ X description X roles and descriptions (+ reveals more) X + intake_date +X + update_date X + hidden (required) url #} @@ -44,6 +45,9 @@ {{ form.input('intake_date', type='date', label=_("intake date"), placeholder="eg 2012-12-21",value=data.intake_date, error=errors.intake_date) }} + {{ form.input('update_date', type='date', label=_("update date"), + placeholder="eg 2012-12-21",value=data.update_date, error=errors.update_date) }} +

Roles and access descriptions:

diff --git a/ckanext/cfpb_extrafields/templates/package/snippets/resource_type-specific/cfpb_extrafields_datafile.html b/ckanext/cfpb_extrafields/templates/package/snippets/resource_type-specific/cfpb_extrafields_datafile.html index a7369e2..c117f43 100644 --- a/ckanext/cfpb_extrafields/templates/package/snippets/resource_type-specific/cfpb_extrafields_datafile.html +++ b/ckanext/cfpb_extrafields/templates/package/snippets/resource_type-specific/cfpb_extrafields_datafile.html @@ -8,6 +8,7 @@ X +storage location ; path (drop-down+free-text) X +approximate_size X +intake_date +X +update_date #} {% if location=="form" %} @@ -44,6 +45,9 @@ {{ form.input('intake_date', type='date', label=_("intake date"), placeholder="eg 2012-12-21",value=data.intake_date, error=errors.intake_date) }} + + {{ form.input('update_date', type='date', label=_("update date"), + placeholder="eg 2012-12-21",value=data.update_date, error=errors.update_date) }} {% elif location=="description" %} diff --git a/ckanext/cfpb_extrafields/templates/package/snippets/resource_type-specific/cfpb_extrafields_defaults.html b/ckanext/cfpb_extrafields/templates/package/snippets/resource_type-specific/cfpb_extrafields_defaults.html index b0de27e..484a1ef 100644 --- a/ckanext/cfpb_extrafields/templates/package/snippets/resource_type-specific/cfpb_extrafields_defaults.html +++ b/ckanext/cfpb_extrafields/templates/package/snippets/resource_type-specific/cfpb_extrafields_defaults.html @@ -42,4 +42,6 @@ {{ form.input('intake_date', type='date', label=_("intake date"), placeholder="eg 2012-12-21", value=data.intake_date, error=errors.intake_date) }} +{{ form.input('update_date', type='date', label=_("update date"), placeholder="eg 2012-12-21", value=data.update_date, error=errors.update_date) }} + {{ form.select('approximate_total_size', label=_('approximate total size'), id='field-approximate_total_size', options=h.options_approximate_total_size(), selected=data.approximate_total_size, error=errors.approximate_total_size, classes=['control-medium']) }} diff --git a/ckanext/cfpb_extrafields/templates/package/snippets/resource_type-specific/cfpb_extrafields_documentation.html b/ckanext/cfpb_extrafields/templates/package/snippets/resource_type-specific/cfpb_extrafields_documentation.html index 38e39ff..9e858e0 100644 --- a/ckanext/cfpb_extrafields/templates/package/snippets/resource_type-specific/cfpb_extrafields_documentation.html +++ b/ckanext/cfpb_extrafields/templates/package/snippets/resource_type-specific/cfpb_extrafields_documentation.html @@ -7,6 +7,7 @@ X +format X +storage location ; path (drop-down+free-text) X +intake_date +X +update_date #} {% if location=="form" %} @@ -43,6 +44,9 @@ {{ form.input('intake_date', type='date', label=_("intake date"), placeholder="eg 2012-12-21",value=data.intake_date, error=errors.intake_date) }} + + {{ form.input('update_date', type='date', label=_("update date"), + placeholder="eg 2012-12-21",value=data.update_date, error=errors.update_date) }} {% elif location=="description" %}