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

[WIP] Resource update script #35

Open
wants to merge 4 commits into
base: master
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
2 changes: 2 additions & 0 deletions ckanext/cfpb_extrafields/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),],
Expand Down Expand Up @@ -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'),],
Expand Down
60 changes: 60 additions & 0 deletions ckanext/cfpb_extrafields/scripts/update_resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""Update CKAN Resources

usage: CKAN_API_KEY=<ckan_api_key> CKAN_URL=<ckan_url> [CKAN_DATE_FIELD=<date_field>] python update_resource.py <resource_id> [<timestamp>]

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)

6 changes: 6 additions & 0 deletions ckanext/cfpb_extrafields/templates/package/resource_read.html
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ <h1 class="page-heading">{{ h.resource_display_name(res) | truncate(50) }}</h1>
<td class="dataset-details">{{ res.intake_date }}</td>
</tr>
{% endif %}
{% if res.update_date %}
<tr>
<th scope="row" class="dataset-label">{{ _("update date") }}</th>
<td class="dataset-details">{{ res.update_date }}</td>
</tr>
{% endif %}
<tr>
<th scope="row">{{ _('metadata resource ID') }}</th>
<td>{{ res.id or _('unknown') }}</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
X description
X roles and descriptions (+ reveals more)
X + intake_date
X + update_date
X + hidden (required) url
#}

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

<div>
<p>Roles and access descriptions:</p>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
X +storage location ; path (drop-down+free-text)
X +approximate_size
X +intake_date
X +update_date
#}

{% if location=="form" %}
Expand Down Expand Up @@ -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) }}
<!-- end cfpb_extrafields_datafile.html snippet -->

{% elif location=="description" %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']) }}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
X +format
X +storage location ; path (drop-down+free-text)
X +intake_date
X +update_date
#}

{% if location=="form" %}
Expand Down Expand Up @@ -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) }}
<!-- end cfpb_extrafields_documentation.html snippet -->

{% elif location=="description" %}
Expand Down