Skip to content

Commit

Permalink
Merge pull request #288 from keitaroinc/link_rq_visuals
Browse files Browse the repository at this point in the history
 Link rq visuals
  • Loading branch information
Pavle Jonoski committed Jan 22, 2020
2 parents 1b96085 + b7e5f67 commit c1b75d0
Show file tree
Hide file tree
Showing 21 changed files with 401 additions and 103 deletions.
2 changes: 1 addition & 1 deletion ckanext/knowledgehub/fanstatic/css/main.css

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions ckanext/knowledgehub/fanstatic/javascript/rqs_selection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";

$(document).ready(function() {
$('#table_research_questions').select2();
$('#chart_research_questions').select2();
$('#map_research_questions').select2();

});
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

.accordion-inner {
padding: @grid-gutter-width / 2 0 @grid-gutter-width 0;

input,
select,
textarea {
Expand Down Expand Up @@ -45,6 +46,10 @@
}
}

.rs-questions {
width: 45rem;
}

.data-transformation-module {
padding-top: 40px;
margin-left: -1rem;
Expand Down Expand Up @@ -122,10 +127,11 @@
.select2-container {
width: 100% !important;
}

.dataTables_filter {
&>label {
&::after {
content: ""
}
}
}
}
37 changes: 22 additions & 15 deletions ckanext/knowledgehub/fanstatic/less/_responsive-md.less
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
@media only screen and (max-width: @screen-md) {
.accordion {
display: block;
}
.accordion-group {
display: block;
}

.accordion-group {
width: 100%;
padding: 0;
}

.module-content,
.visualization-wrapper {
> .resource-view {
.flex-column;
&>div {
width: 100%;
.module-content,
.visualization-wrapper {
>.resource-view {
.flex-column;

&>div {
width: 100%;
}
}
}
}
.visualization-logo{
max-width: 200px;
}

}

.visualization-logo {
max-width: 200px;
}

.rs-questions {
width: 100%;
padding: 0;
}
}
180 changes: 180 additions & 0 deletions ckanext/knowledgehub/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,186 @@ def dashboard_research_questions(dashboard):

return questions

def add_rqs_to_dataset(res_view):

context = _get_context()
pkg_dict = toolkit.get_action('package_show')(
dict({'ignore_auth': True}, return_type='dict'),
{'id': res_view['package_id']})


rq_options = get_rq_options()

all_rqs = []
if not pkg_dict.get('research_question'):
pkg_dict['research_question'] = []
else:
if isinstance(pkg_dict['research_question'], unicode):
old_rqs = pkg_dict.get('research_question')
old_list = old_rqs.split(',')

for old in old_list:
all_rqs.append(old)
if res_view.get('research_questions'):
res_rq = json.loads(res_view.get('research_questions'))
if isinstance(res_rq, unicode):
all_rqs.append(res_rq)
else:
for new in res_rq:
all_rqs.append(new)

eliminate_duplicates = set(all_rqs)
all_rqs = list(eliminate_duplicates)
pkg_dict['research_question'] = ",".join(all_rqs)
try:
context['defer_commit'] = True
context['use_cache'] = False
toolkit.get_action('package_update')(context, pkg_dict)
context.pop('defer_commit')
except logic.ValidationError as e:
try:
raise logic.ValidationError(e.error_dict['research_question'][-1])
except (KeyError, IndexError):
raise logic.ValidationError(e.error_dict)


def remove_rqs_from_dataset(res_view):

context = _get_context()
pkg_id = res_view.get('package_id')
if res_view.get('__extras'):
ext = res_view.get('__extras')
if ext.get('research_questions'):
list_rqs = res_view['__extras']['research_questions']
data_dict = {}
should_stay = {}
for rq in list_rqs:
data_dict['text']= rq
data_dict['fq'] = "khe_package_id:" + pkg_id
should_stay[rq] = False
results_search = toolkit.get_action('search_visualizations')(context, data_dict)
for res in results_search['results']:
if res.get('research_questions') and res.get('id') != res_view.get('id') \
and res.get('khe_package_id') == pkg_id:
questions = json.loads(res.get('research_questions'))
for q in questions:
if q == rq:
should_stay[rq] = True
new_rqs_package_dict = {}
package_sh = toolkit.get_action('package_show')(
dict({'ignore_auth': True}, return_type='dict'), {'id': pkg_id})
if package_sh.get('research_question'):
questions_package = package_sh.get('research_question').split(",")
for q in questions_package:
if q in should_stay:
if should_stay[q] == False:
questions_package.remove(q)
package_sh['research_question'] = ",".join(questions_package)


try:
context['defer_commit'] = True
context['use_cache'] = False
toolkit.get_action('package_update')(context, package_sh)
context.pop('defer_commit')
except ValidationError as e:
try:
raise ValidationError(e.error_dict['research_question'][-1])
except (KeyError, IndexError):
raise ValidationError(e.error_dict)


def update_rqs_in_dataset(old_data, res_view):

context = _get_context()
pkg_dict = toolkit.get_action('package_show')(
dict({'ignore_auth': True }, return_type='dict'),
{'id': res_view['package_id']})

rq_options = get_rq_options()
all_rqs = []
if not pkg_dict.get('research_question'): # dataset has no rqs
pkg_dict['research_question'] = []
else:
if isinstance(pkg_dict['research_question'], unicode): # expected format
old_rqs = pkg_dict.get('research_question')
old_list = old_rqs.split(',')

for old in old_list:
all_rqs.append(old)
if res_view.get('research_questions'):
res_rq = json.loads(res_view.get('research_questions'))
if isinstance(res_rq, list):
for new in res_rq:
all_rqs.append(new)
else:
all_rqs.append(res_rq)

eliminate_duplicates = set(all_rqs)
all_rqs = list(eliminate_duplicates)

pkg_id = res_view.get('package_id')

if old_data.get('__extras') and res_view.get('__extras'):
new_ext = res_view.get('__extras')
old_ext = old_data.get('__extras')
list_rqs = [] # list of rqs that were removed in update
if old_ext.get('research_questions'): # alrdy had rqs
if new_ext.get('research_questions'): # and we have new rqs
if isinstance(new_ext.get('research_questions'), list):
set_new = set(new_ext.get('research_questions'))
else: # only one new
li = []
li.append(new_ext.get('research_questions'))
set_new = set(li)
if isinstance(old_ext.get('research_questions'), list):
set_old = set(old_ext.get('research_questions'))
else: # only one old
li = []
li.append(old_ext.get('research_questions'))
set_old = set(li)
list_rqs = list(set_old-set_new)
else: # all were removed
if isinstance(old_ext.get('research_questions'), list): # if they are more than 1
list_rqs = old_ext.get('research_questions')
else: # if it is only 1
list_rqs.append(old_ext.get('research_questions'))
data_dict = {}
should_stay = {}
for rq in list_rqs:
data_dict['text']= rq
data_dict['fq'] = "khe_package_id:" + pkg_id
should_stay[rq] = False
results_search = toolkit.get_action('search_visualizations')(context, data_dict)
for res in results_search['results']:
if res.get('research_questions'):
questions = json.loads(res.get('research_questions'))
if isinstance(questions, list):
for q in questions:
if q == rq:
should_stay[rq] = True
else:
if questions == rq:
should_stay[rq] = True
new_rqs_package_dict = {}

questions_package = all_rqs
for q in questions_package:
if q in should_stay:
if should_stay[q] == False:
questions_package.remove(q)
pkg_dict['research_question'] = ",".join(questions_package)

try:
context['defer_commit'] = True
context['use_cache'] = False
toolkit.get_action('package_update')(context, pkg_dict)
context.pop('defer_commit')
except ValidationError as e:
try:
raise ValidationError(e.error_dict['research_question'][-1])
except (KeyError, IndexError):
raise ValidationError(e.error_dict)

def get_single_dash(data_dict):
single_dash = toolkit.get_action('dashboard_show')(_get_context(),
Expand Down
27 changes: 6 additions & 21 deletions ckanext/knowledgehub/logic/action/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from ckanext.knowledgehub.lib.writer import WriterService
from ckanext.knowledgehub import helpers as plugin_helpers


log = logging.getLogger(__name__)

_df = lib.navl.dictization_functions
Expand Down Expand Up @@ -303,8 +302,13 @@ def resource_view_create(context, data_dict):
rv_data = model_dictize.resource_view_dictize(resource_view, context)

# Add to index
Visualization.add_to_index(rv_data)
Visualization.add_to_index(rv_data)

# this check is because of the unit tests
if rv_data.get('__extras'):
ext = rv_data['__extras']
if ext.get('research_questions'):
plugin_helpers.add_rqs_to_dataset(rv_data)
return rv_data


Expand Down Expand Up @@ -365,25 +369,6 @@ def dashboard_create(context, data_dict):


def package_create(context, data_dict):

research_questions = data_dict.get('research_question')
rq_options = plugin_helpers.get_rq_options()
rq_ids = []

if research_questions:
if isinstance(research_questions, list):
for rq in research_questions:
for rq_opt in rq_options:
if rq == rq_opt.get('text'):
rq_ids.append(rq_opt.get('id'))
break
data_dict['research_question'] = rq_ids
elif isinstance(research_questions, unicode):
for rq in rq_options:
if rq.get('text') == research_questions:
data_dict['research_question'] = [rq.get('id')]
break

return ckan_package_create(context, data_dict)


Expand Down
3 changes: 3 additions & 0 deletions ckanext/knowledgehub/logic/action/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from ckan.logic.action.delete import resource_view_delete as _resource_view_delete
from ckan.plugins import toolkit
from ckan.common import _
from ckanext.knowledgehub import helpers as plugin_helpers


from ckanext.knowledgehub.model import Dashboard
from ckanext.knowledgehub.model import Theme
Expand Down Expand Up @@ -122,6 +124,7 @@ def dashboard_delete(context, data_dict):

def resource_view_delete(context, data_dict):
resource_view = logic.get_action('resource_view_show')(context, data_dict)
plugin_helpers.remove_rqs_from_dataset(resource_view)
_resource_view_delete(context, data_dict)
Visualization.delete_from_index({'id': resource_view['id']})

Expand Down
32 changes: 14 additions & 18 deletions ckanext/knowledgehub/logic/action/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ def resource_view_update(context, data_dict):
# TODO need to implement custom authorization actions
# check_access('resource_view_update', context, data_dict)

old_resource_view_data = model_dictize.resource_view_dictize(resource_view,
context)

# before update

resource_view = model_save.resource_view_dict_save(data, context)
if not context.get('defer_commit'):
model.repo.commit()
Expand All @@ -287,6 +292,14 @@ def resource_view_update(context, data_dict):
# Update index
Visualization.update_index_doc(resource_view_data)

# this check is done for the unit tests
if resource_view_data.get('__extras'):
ext = resource_view_data['__extras']
ext_old = resource_view_data['__extras']
if ext_old.get('research_questions') or ext.get('research_questions'):
plugin_helpers.update_rqs_in_dataset(old_resource_view_data, resource_view_data)


return resource_view_data


Expand Down Expand Up @@ -346,24 +359,7 @@ def dashboard_update(context, data_dict):


def package_update(context, data_dict):
research_questions = data_dict.get('research_question')
rq_options = plugin_helpers.get_rq_options()
rq_ids = []

if research_questions:
if isinstance(research_questions, list):
for rq in research_questions:
for rq_opt in rq_options:
if rq == rq_opt.get('text'):
rq_ids.append(rq_opt.get('id'))
break
data_dict['research_question'] = rq_ids
elif isinstance(research_questions, unicode):
for rq in rq_options:
if rq.get('text') == research_questions:
data_dict['research_question'] = [rq.get('id')]
break


return ckan_package_update(context, data_dict)


Expand Down
Loading

0 comments on commit c1b75d0

Please sign in to comment.