Skip to content

Commit

Permalink
https://github.com/DanePubliczneGovPl/ckanext-danepubliczne/issues/62
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofMadejski committed Mar 30, 2015
1 parent 45ecf64 commit c399f70
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions ckanext/qa/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,33 @@ def _update_task_status(context, data):
raise CkanError('ckan failed to update task_status, status_code (%s), error %s'
% (res.status_code, res.content))

def _update_resource(context, resource, log):
"""
Use CKAN API to update the given resource.
If cannot update, records this fact in the task_status table.
Returns the content of the response.
"""
api_url = urlparse.urljoin(context['site_url'], 'api/3/action') + '/resource_update'
resource['last_modified'] = datetime.datetime.now().isoformat()
post_data = json.dumps(resource)
res = requests.post(
api_url, post_data,
headers = {'Authorization': context['apikey'],
'Content-Type': 'application/json'}
)

if res.status_code == 200:
log.info('Resource updated OK: %s', resource['id'])
return res.content
else:
try:
content = res.content
except:
content = '<could not read request content to discover error>'
log.error('ckan failed to update resource, status_code (%s), error %s. Maybe the API key or site URL are wrong?.\ncontext: %r\nresource: %r\nres: %r\nres.error: %r\npost_data: %r\napi_url: %r'
% (res.status_code, content, context, resource, res, res.error, post_data, api_url))
raise CkanError('ckan failed to update resource, status_code (%s), error %s' % (res.status_code, content))

def _task_status_data(id, result):
return [
Expand Down Expand Up @@ -124,13 +151,17 @@ def update(context, data):

task_status_data = _task_status_data(data['id'], result)

data['openness_score'] = result['openness_score']
_update_resource(context, data, log)

api_url = urlparse.urljoin(context['site_url'], 'api/action')
response = requests.post(
api_url + '/task_status_update_many',
json.dumps({'data': task_status_data}),
headers={'Authorization': context['apikey'],
'Content-Type': 'application/json'}
)

if not response.ok:
err = 'ckan failed to update task_status, error %s' \
% response.error
Expand Down

0 comments on commit c399f70

Please sign in to comment.