Skip to content

Commit

Permalink
Fix hdx bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
bozarevskaandrea committed Apr 8, 2020
1 parent e471bda commit 96406e2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@
function removeResourceHDX(tr) {
document.getElementById("hdx-loader").style.visibility = "visible";
var id = $('#package-name').val();
var res_id = $('#resource-id').val();
var res_name = $('#resource-name').val();
var btn = $(this);
var flash_messages = $('.flash-messages');
btn.attr('disabled', true);
api.post('delete_resource_from_hdx', {
id: id,
resource_name: res_name
resource_name: res_name,
resource_id : res_id
})
.done(function (data) {
if (data.success) {
Expand Down
13 changes: 10 additions & 3 deletions ckanext/knowledgehub/logic/action/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,11 @@ def upsert_resource_to_hdx(context, data_dict):
rsc_hdx.create_datastore()
rsc['hdx_name_resource'] = rsc['name']
try:
toolkit.get_action('resource_update')(context, rsc)
dict_res = {
'id': rsc['id'],
'hdx_name_resource': rsc['name']
}
resource = toolkit.get_action('resource_update')({ 'ignore_auth': True }, dict_res)
except ValidationError as e:
try:
raise ValidationError(e.error_dict)
Expand Down Expand Up @@ -1517,14 +1521,17 @@ def upsert_dataset_to_hdx(context, data_dict):

resources = data['resources']
dataset = Dataset.read_from_hdx(data['name'])
for resource in resources:

for i in range(0, len(resources)):
resource = resources[i]
# for resource in resources:
data_dict = {
'resource_id': resource.get('id'),
'dataset_name': dataset.get('name')
}
if resource['name'] in hdx_resources:
data_dict.update({'hdx_rsc_name': resource['name']})
upsert_resource_to_hdx(context, data_dict)
resources[i] = upsert_resource_to_hdx(context, data_dict)

kwh_resources = list(map(
lambda r: r.get('name'), resources
Expand Down
17 changes: 14 additions & 3 deletions ckanext/knowledgehub/logic/action/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,25 +503,36 @@ def package_delete(context, data_dict):
def delete_resource_from_hdx(context, data_dict):

check_access('package_update', context)

id = data_dict.get('id')
if not id:
raise ValidationError('Dataset id is missing!')
res_id = data_dict.get('resource_id')
resource = toolkit.get_action('resource_show')(context,
{ 'id': res_id })

try:

data = logic.get_action('package_show')(
{'ignore_auth': True},
{'id': id})
{'ignore_auth': True },
{'id': id })

hdx_dataset = Dataset.read_from_hdx(data['name'])

for hdx_resource in hdx_dataset.get_resources():
if hdx_resource['name'] == data_dict['resource_name']:
hdx_resource.delete_from_hdx()
resource['hdx_name_resource']=""
try:
resource = toolkit.get_action('resource_update')(context, resource)
except ValidationError as e:
try:
raise ValidationError(e.error_dict)
except (KeyError, IndexError):
raise ValidationError(e.error_dict)
return
return []
except Exception as e:
log.exception(e)
log.debug(e)
return "Please try again!"

Expand Down
3 changes: 2 additions & 1 deletion ckanext/knowledgehub/templates/package/resource_read.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@

</ul>
<input id="package-name" value="{{ pkg.name }}" type="hidden" />
<input id="resource-name" value="{{ res.name }}" type="hidden"/>
<input id="resource-id" value="{{ res.id }}" type="hidden"/>
<input id="resource-name" value="{{ res.name }}" type="hidden" />
{% if h.check_access('package_update', {'id':pkg.id }) %}
<ul class="resource-item" style="text-align: end">
<div id="hdx-loader"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ <h3>{{ _('Data and Resources') }}</h3>
{% if h.check_access('package_update', {'id':pkg.id }) %}
<li class="resource-item" style="text-align: end">
<div id="hdx-loader"></div>
{% if pkg.get('hdx_name') %}
<button class="btn btn-success" id="removeHDX" type="submit">
{% if pkg.get('hdx_name') %}
<i class="fa fa-download"></i>{{ _(' Remove dataset from HDX') }}
{% endif %}
</button>
{% endif %}

</li>
{% endif %}

Expand Down

0 comments on commit 96406e2

Please sign in to comment.