Skip to content

Commit

Permalink
Merge pull request #309 from keitaroinc/feature/tags-ui
Browse files Browse the repository at this point in the history
Feature/tags ui
  • Loading branch information
nushkovg committed Feb 4, 2020
2 parents 629d809 + 1cc4b75 commit 9ffcf10
Show file tree
Hide file tree
Showing 14 changed files with 429 additions and 14 deletions.
2 changes: 1 addition & 1 deletion ckanext/knowledgehub/fanstatic/css/main.css

Large diffs are not rendered by default.

108 changes: 108 additions & 0 deletions ckanext/knowledgehub/fanstatic/javascript/user_profile_tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
(function (_, jQuery) {
'use strict';

var api = {
get: function (action, params) {
var api_ver = 3;
var base_url = ckan.sandbox().client.endpoint;
params = $.param(params);
var url = base_url + '/api/' + api_ver + '/action/' + action + '?' + params;
return $.getJSON(url);
},
post: function (action, data) {
var api_ver = 3;
var base_url = ckan.sandbox().client.endpoint;
var url = base_url + '/api/' + api_ver + '/action/' + action;
return $.post(url, data, 'json');
}
};

function removeTag(tr) {
var tagID = tr.get(0).cells[0].innerHTML;

api.post('tag_delete', {
id: tagID
})
.done(function (data) {
if (data.success) {
tr.detach();
}
})
.fail(function (error) {
console.log("Tag delete: " + error.statusText);
});
};

function updateTag(tagID, vocabularyID, loader) {
loader.css("visibility", "visible");
api.get('tag_show', {
id: tagID
})
.done(function (data) {
if (data.success) {
var name = data.result.name
api.post('tag_update', {
id: tagID,
name: name,
vocabulary_id: vocabularyID
})
.fail(function (error) {
console.log("Tag update: " + error.statusText);
})
}
})
.fail(function (error) {
console.log("Tag show: " + error.statusText);
});
setTimeout(function () {
loader.css("visibility", "hidden");
}, 500);
}

$(document).ready(function () {
var $table = $('#tags');
$table.DataTable({
"paging": true,
"iDisplayLength": 25
});

$('.dataTables_length').addClass('bs-select');
$table.removeClass('dataTable');

var modalConfirm = function (callback) {
var tr;
var modalDelete = $('#modalDelete');

$table.on('click', '.table-remove', function () {
tr = $(this).parents('tr');
modalDelete.modal('show');
});

$('#btnYes').on('click', function () {
callback(true, tr);
modalDelete.modal('hide');
});

$('#btnNo').on('click', function () {
callback(false, tr);
modalDelete.modal('hide');
})
}

modalConfirm(function (confirm, tr) {
if (confirm) {
removeTag(tr);
}
});

document.querySelectorAll('#keyword-select').forEach(item => {
item.addEventListener('change', event => {
var vocabularyID = event.target.value;
var parentRow = event.target.parentElement.parentElement;
var tagID = parentRow.cells[0].innerHTML;
var loader = $(event.target).closest('td').find('#loader');
updateTag(tagID, vocabularyID, loader);
})
})
});
})(ckan.i18n.ngettext, $);
86 changes: 85 additions & 1 deletion ckanext/knowledgehub/fanstatic/less/_visualizations.less
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
text-align: center;
}

#tags {
text-align: center
}

#intents-header-id {
padding-bottom: 20px
}
Expand All @@ -91,6 +95,10 @@
text-align: center;
}

#tags-header-id {
padding-bottom: 20px;
}

.remove-th {
text-align: end !important;
padding-right: 20px !important;
Expand All @@ -108,14 +116,26 @@
text-align: center;
}

#tags-header th {
text-align: center;
}

#intents-footer th {
text-align: center;
}

#tags-footer th {
text-align: center;
}

#intents tfoot {
background-color: #bfdcee;
}

#tags tfoot {
background-color: #bfdcee;
}

#intents thead {
background-color: #bfdcee;
}
Expand All @@ -124,6 +144,10 @@
background-color: #bfdcee;
}

#tags thead {
background-color: #bfdcee;
}

#intents_filter {
width: 33rem;
margin-bottom: 5px;
Expand All @@ -139,10 +163,35 @@
border-radius: 4px;
}

.custom-select {
background-color: transparent;
border-radius: 4px;
margin-left: 25px;
}

#intents_length label {
cursor: default;
}

#tags_filter {
width: 33rem;
margin-bottom: 5px;
}

#tags_filter input {
border-radius: 4px;
width: 25rem;
}

#tags_length select {
background-color: transparent;
border-radius: 4px;
}

#tags_length label {
cursor: default;
}

.no-items {
margin-bottom: @grid-gutter-width * 2;
}
Expand Down Expand Up @@ -210,6 +259,41 @@
margin: 5px;
}

#loader {
border: 5px solid #c5cdd9;
border-radius: 50%;
border-top: 5px solid #3498db;
width: 20px;
height: 20px;
-webkit-animation: spin 2s linear infinite;
/* Safari */
animation: spin 2s linear infinite;
margin-bottom: -5px;
visibility: hidden;
display: inline-block;

}

@-webkit-keyframes spin {
0% {
-webkit-transform: rotate(0deg);
}

100% {
-webkit-transform: rotate(360deg);
}
}

@keyframes spin {
0% {
transform: rotate(0deg);
}

100% {
transform: rotate(360deg);
}
}

#keywords-header-id {
padding-bottom: 20px
}
}
10 changes: 10 additions & 0 deletions ckanext/knowledgehub/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1335,3 +1335,13 @@ def views_dashboards_groups_update(package_id):
{'ignore_auth': True},
data_dict
)


def vocabulary_list():
'''Return a list of all vocabularies
:returns: the list of all vocabularies
:rtype: string
'''

return toolkit.get_action('vocabulary_list')({'ignore_auth': True}, {})
3 changes: 2 additions & 1 deletion ckanext/knowledgehub/logic/action/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ckan.common import _, config, json
from ckan import lib
from ckan import model
from ckan.model.meta import Session

from ckanext.knowledgehub.model import Theme
from ckanext.knowledgehub.model import SubThemes
Expand Down Expand Up @@ -1231,7 +1232,7 @@ def tag_list(context, data_dict):
elif vocab_id_or_name:
tags = model.Tag.all(vocab_id_or_name)
else:
tags = model.Tag.all()
tags = Session.query(model.Tag).autoflush(False).all()

if tags:
if all_fields:
Expand Down
17 changes: 11 additions & 6 deletions ckanext/knowledgehub/logic/action/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,9 +747,9 @@ def tag_update(context, data_dict):
:param id: id or name of the tag
:type id: string
:param name: the name of the tag (optional)
:param name: the name of the tag
:type name: string
:param vocabulary_id: the id of the vocabulary
:param vocabulary_id: the id of the vocabulary (optional)
:type vocabulary_id: string
:returns: the updated tag
Expand All @@ -764,16 +764,21 @@ def tag_update(context, data_dict):
raise NotAuthorized(_(u'Need to be system '
u'administrator to administer'))

id = _get_or_bust(data_dict, 'id')
schema = knowledgehub_schema.tag_update_schema()
data, errors = _df.validate(data_dict, schema, context)
if errors:
raise ValidationError(errors)

tag = model.Tag.get(id)
tag = model.Tag.get(data.get('id'))
if not tag:
raise NotFound(_('Tag was not found'))

items = ['name', 'vocabulary_id']
for item in items:
if data_dict.get(item):
setattr(tag, item, data_dict.get(item))
if data.get(item):
setattr(tag, item, data.get(item))
else:
setattr(tag, item, None)

session = context['session']
tag.save()
Expand Down
5 changes: 2 additions & 3 deletions ckanext/knowledgehub/logic/auth/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,5 @@ def resource_validate_show(context, data_dict):


def tag_list(context, data_dict):
''' CKAN tag_list auth function must be override
because it enters in infinite recursion'''
return ckan_tag_list(context, data_dict)
# sysadmins only
return {'success': False}
8 changes: 8 additions & 0 deletions ckanext/knowledgehub/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,11 @@ def tag_create_schema():
# You're not allowed to specify your own ID when creating a tag.
schema['id'] = [empty]
return schema


def tag_update_schema():
return {
'id': [not_empty, unicode],
'name': [not_empty, unicode],
'vocabulary_id': [ignore_missing, unicode]
}
4 changes: 3 additions & 1 deletion ckanext/knowledgehub/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ def get_helpers(self):
'get_resource_validation_data': h.get_resource_validation_data,
'get_resource_validation_options': h.get_resource_validation_options,
'check_resource_status': h.check_resource_status,
'check_validation_admin': h.check_validation_admin
'check_validation_admin': h.check_validation_admin,
'vocabulary_list': h.vocabulary_list
}

# IDatasetForm
Expand All @@ -114,6 +115,7 @@ def _modify_package_schema(self, schema):
toolkit.get_converter('convert_to_extras')]
mandatory_defaults = [toolkit.get_validator('not_empty')]


schema.update({
'unit_supported': package_defaults,
'im_product_name': package_defaults,
Expand Down
3 changes: 2 additions & 1 deletion ckanext/knowledgehub/templates/user/read_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
{% if user_is_sysadmin %}
{{ h.build_nav_icon('kwh_user.intents', _('Intents'), id=user.name) }}
{{ h.build_nav_icon('kwh_user.keywords', _('Keywords')) }}
{{ h.build_nav_icon('kwh_user.tags', _('Tags'), id=user.name) }}
{% endif %}

{{ h.build_nav_icon('user.activity', _('Activity Stream'), id=user.name, offset=0) }}
{{ h.build_nav_icon('user_datarequests', _('Data Requests'), id=user.name) }}
{% endblock %}
{% endblock %}
Loading

0 comments on commit 9ffcf10

Please sign in to comment.