Skip to content

Commit e1aed66

Browse files
authored
Merge pull request #1686 from open-data/feature/user-opt-ins
User Opt-in Feature w/ PD DataTable as First One
2 parents 224a50a + fc80071 commit e1aed66

14 files changed

Lines changed: 712 additions & 401 deletions

File tree

changes/1686.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Users can now opt-in to the new PD DataTable on the Registry. Adds a general `canada-user-opt-in-feature` JS module, along with User validation for the opt-in features.

ckanext/canada/assets/datatables/pd_datatables.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ function load_pd_datatable(CKAN_MODULE){
6060
const chromoFields = CKAN_MODULE.options.chromo_fields;
6161
const isEditable = CKAN_MODULE.options.is_editable;
6262
const tableStyles = CKAN_MODULE.options.table_styles;
63-
const EDITOR = pd_datatables__EDITOR;
63+
64+
// TODO: Disable Editor - enable Table Editor when ready...
65+
// const EDITOR = pd_datatables__EDITOR;
66+
const EDITOR = false;
6467

6568
const selectAllLabel = _('Select All');
6669
const colSearchLabel = _('Search:');
@@ -167,6 +170,10 @@ function load_pd_datatable(CKAN_MODULE){
167170
let isCompactView = typeof tableState != 'undefined' && typeof tableState.compact_view != 'undefined' ? tableState.compact_view : true;
168171
let isFullScreen = is_page_fullscreen();
169172
let isEditMode = typeof tableState != 'undefined' && typeof tableState.edit_view != 'undefined' ? tableState.edit_view : false;
173+
174+
// TODO: Disable Editor - enable Table Editor when ready...
175+
isEditMode = false;
176+
170177
if( isEditMode ){
171178
$('.pd-datable-instructions').css({'display': 'none'});
172179
}
@@ -2002,22 +2009,33 @@ function load_pd_datatable(CKAN_MODULE){
20022009
_data.selected = this.api().rows({selected: true})[0];
20032010
_data.compact_view = isCompactView;
20042011
_data.edit_view = isEditMode;
2012+
2013+
// TODO: Disable Editor - enable Table Editor when ready...
2014+
_data.edit_view = false;
2015+
20052016
_data.editing_rows = editingRows;
20062017
// TODO: save filledRows, erroredRows, requiredRows??? need to save the field values too???
20072018
let localInstanceSelected = typeof tableState != 'undefined' ? tableState.selected : _data.selected;
20082019
tableState = _data;
20092020
tableState.selected = localInstanceSelected;
20102021
tableState.edit_view = isEditMode;
2022+
2023+
// TODO: Disable Editor - enable Table Editor when ready...
2024+
tableState.edit_view = false;
2025+
20112026
tableState.editing_rows = editingRows;
20122027
},
20132028
stateLoadParams: function(_settings, _data){
20142029
let localInstanceSelected = typeof tableState != 'undefined' ? tableState.selected : _data.selected;
20152030
tableState = _data;
20162031
tableState.selected = localInstanceSelected;
2032+
2033+
// TODO: Disable Editor - enable Table Editor when ready...
2034+
tableState.edit_view = false;
20172035
},
20182036
buttons: get_available_buttons(),
20192037
});
20202038
}
20212039
window.onbeforeunload = warn_unsaved_records;
20222040
initialize_datatable();
2023-
}
2041+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
this.ckan.module('canada-user-opt-in-feature', function($){
2+
return {
3+
/* options object can be extended using data-module-* attributes */
4+
options: {
5+
csrf_name: null,
6+
csrf_value: null,
7+
page_redirect: null,
8+
label: null,
9+
button_label: null,
10+
user_id: null,
11+
feature_key: null,
12+
feature_value: true,
13+
},
14+
initialize: function (){
15+
_load_user_opt_in(this);
16+
}
17+
};
18+
});
19+
20+
function _load_user_opt_in(CKAN_MODULE){
21+
const _ = CKAN_MODULE._;
22+
const csrfTokenName = CKAN_MODULE.options.csrf_name;
23+
const csrfTokenValue = CKAN_MODULE.options.csrf_value;
24+
const pageRedirect = CKAN_MODULE.options.page_redirect;
25+
const label = CKAN_MODULE.options.label;
26+
const buttonLabel = CKAN_MODULE.options.button_label;
27+
const userID = CKAN_MODULE.options.user_id;
28+
const featureKey = CKAN_MODULE.options.feature_key;
29+
const featureValue = CKAN_MODULE.options.feature_value;
30+
31+
let postData = {'id': userID};
32+
postData[featureKey] = featureValue;
33+
postData[csrfTokenName] = csrfTokenValue;
34+
35+
let container = $('[data-module="canada-user-opt-in-feature"][data-module-feature_key="' + featureKey + '"]');
36+
let htmlContent = '<div class="alert alert-warning"><p>' + label + '</p><p><a role="button" class="btn btn-primary btn-small" href="javascript:void(0);">' + buttonLabel + '</a></p></div>';
37+
38+
function _render_failure(_consoleMessage, _message, _type){
39+
console.warn(_consoleMessage);
40+
$(container).find('#uoif_failure_message').remove();
41+
$(container).append('<div id="uoif_failure_message" class="alert alert-dismissible show alert-' + _type + '"><p>' + _message + '</p></div>');
42+
}
43+
44+
if( $(container).children('.alert').length == 0 ){
45+
$(container).append(htmlContent);
46+
let button = $(container).find('a.btn');
47+
if( button.length > 0 ){
48+
$(button).off('click.optInFeature');
49+
$(button).on('click.optInFeature', function(_event){
50+
$(button).addClass('disabled');
51+
$(button).attr('disabled', 'disabled');
52+
$.ajax({
53+
'url': '/api/action/user_patch',
54+
'type': 'POST',
55+
'dataType': 'JSON',
56+
'data': postData,
57+
'complete': function(_data){
58+
if( _data.responseJSON ){ // we have response JSON
59+
if( _data.responseJSON.success ){ // successful patch
60+
if(
61+
_data.responseJSON.result[featureKey] == featureValue
62+
){
63+
if( pageRedirect && pageRedirect.startsWith('#') ){
64+
window.location.hash = pageRedirect.replace('#', '');
65+
window.location.reload();
66+
return;
67+
}else if( pageRedirect ){
68+
window.location.href = pageRedirect;
69+
return;
70+
}
71+
window.location.reload();
72+
return;
73+
}else{ // unexpected return dict
74+
_render_failure('Opt-in Feature — Unexpected response data.', _('Failed to change user settings'), 'danger');
75+
console.warn(_data.responseJSON);
76+
}
77+
}else{ // validation error
78+
_render_failure('Opt-in Feature — ValidationError exception was raised.', _('Failed to change user settings'), 'danger');
79+
console.warn(_data.responseJSON);
80+
}
81+
}else{ // fully flopped ajax request
82+
_render_failure('Opt-in Feature — Failed to gather a JSON response.', _('Failed to change user settings'), 'danger');
83+
}
84+
}
85+
});
86+
});
87+
}
88+
}
89+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
main_js:
2+
contents:
3+
- user_opt_in.js
4+
extra:
5+
preload:
6+
- base/main # need ckan JS and jQuery
7+
output: canada_modules/%(version)s_user_opt_in.js

ckanext/canada/helpers.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,13 @@ def get_pd_datatable(resource_name: str,
531531
for fk_ci, fk_cc in enumerate(fk['child_columns']))
532532

533533
# TODO: DEPRECATED: REMOVE AFTER FULL PD DATATABLES QA
534-
snippet = 'pd_datatable.html' if config.get(
535-
'ckanext.canada.enable_pd_datatable_editor') else 'pd_datatable_depr.html'
534+
try:
535+
user_dict = get_action('user_show')({'ignore_auth': True}, {'id': g.user})
536+
except (ObjectNotFound, RuntimeError):
537+
user_dict = {}
538+
enable_new_template = user_dict.get('opt_in_features__pd_datatables', config.get(
539+
'ckanext.canada.enable_pd_datatable_editor'))
540+
snippet = 'pd_datatable.html' if enable_new_template else 'pd_datatable_depr.html'
536541

537542
return h.snippet('snippets/%s' % snippet,
538543
resource_name=resource_name,
@@ -1175,5 +1180,6 @@ def get_inline_script_nonce() -> str:
11751180
return str(request.environ.get('CSP_NONCE', ''))
11761181

11771182

1178-
def get_allowed_frame_hosts() -> Optional[list]:
1183+
# type_ignore_reason: incomplete typing
1184+
def get_allowed_frame_hosts() -> Optional[list]: # type: ignore
11791185
return config.get('ckanext.canada.allowed_frame_hosts')

0 commit comments

Comments
 (0)