Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 58 additions & 16 deletions ckanext/canada/assets/datatables/pd_datatables.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ this.ckan.module('pd-datatables', function($){
table_styles: null,
primary_keys: null,
foreign_keys: null,
edit_keys: null,
foreign_links: null,
chromo_fields: null,
is_editable: false,
Expand All @@ -36,6 +37,10 @@ this.ckan.module('pd-datatables', function($){

function load_pd_datatable(CKAN_MODULE){
const _ = CKAN_MODULE._;

// TODO: Disable Editor - enable Table Editor when ready...
const DISABLE_EDITOR = true;

const searchParams = new URLSearchParams(document.location.search);
const currentDate = new Date().toISOString().split('T')[0];
const currentYear = new Date().getFullYear();
Expand All @@ -56,14 +61,14 @@ function load_pd_datatable(CKAN_MODULE){
const ajaxURI = CKAN_MODULE.options.ajax_uri;
const primaryKeys = CKAN_MODULE.options.primary_keys;
const foreignKeys = CKAN_MODULE.options.foreign_keys;
const editKeys = CKAN_MODULE.options.edit_keys;
const foreignLinks = CKAN_MODULE.options.foreign_links;
const chromoFields = CKAN_MODULE.options.chromo_fields;
const isEditable = CKAN_MODULE.options.is_editable;
const tableStyles = CKAN_MODULE.options.table_styles;

// TODO: Disable Editor - enable Table Editor when ready...
// const EDITOR = pd_datatables__EDITOR;
const EDITOR = false;
const EDITOR = DISABLE_EDITOR ? false : pd_datatables__EDITOR; // force editor disablement
const hasOpenCanadaID = (primaryKeys != editKeys && editKeys.length == 1 && editKeys[0] == 0);

const selectAllLabel = _('Select All');
const colSearchLabel = _('Search:');
Expand Down Expand Up @@ -171,8 +176,9 @@ function load_pd_datatable(CKAN_MODULE){
let isFullScreen = is_page_fullscreen();
let isEditMode = typeof tableState != 'undefined' && typeof tableState.edit_view != 'undefined' ? tableState.edit_view : false;

// TODO: Disable Editor - enable Table Editor when ready...
isEditMode = false;
if( DISABLE_EDITOR ){
isEditMode = false; // force editor disablement
}

if( isEditMode ){
$('.pd-datable-instructions').css({'display': 'none'});
Expand Down Expand Up @@ -205,6 +211,9 @@ function load_pd_datatable(CKAN_MODULE){
"targets": 0,
"render": function(_data, _type, _row, _meta){
if( isEditMode ){
if( editingRows.length > 0 && hasOpenCanadaID ){
return _data;
}
return _meta.row + 1;
}
return _data;
Expand Down Expand Up @@ -408,7 +417,8 @@ function load_pd_datatable(CKAN_MODULE){
let readOnlyClass = '';
let tabIndex = 0;
let isPrimaryKey = primaryKeys.includes(_colIndex);
if( editingRows.length > 0 && primaryKeys.includes(_colIndex) ){
let isEditingKey = editKeys.includes(_colIndex);
if( editingRows.length > 0 && primaryKeys.includes(_colIndex) && ! hasOpenCanadaID ){
readOnly = 'readonly';
readOnlyClass = 'editor-input-readonly';
tabIndex = -1;
Expand Down Expand Up @@ -574,13 +584,33 @@ function load_pd_datatable(CKAN_MODULE){

// Compile available columns
let _sortOrderIndex = 1;
if( hasOpenCanadaID ){
// render the open_canada_id (_id) field, and treat as primary key field
let _openCanadaIdField = {
'type': 'int',
'name': '_id',
'id': '_id',
'datastore_id': '_id',
'label': 'open_canada_id',
'priority': 0,
}
availableColumns.push({
"name": '_id',
"className": ' pd-datatables-non-editable-col pd-datatables-primary-key-fixed col-sm ',
"searchable": true,
"render": function(_data, _type, _row, _meta){
return cell_renderer(_data, _type, _row, _meta, _openCanadaIdField);
}
});
_sortOrderIndex += 1;
}
for( let i = 0; i < chromoFields.length; i++ ){
if( typeof chromoFields[i].published_resource_computed_field == 'undefined' || ! chromoFields[i].published_resource_computed_field ){
let previewClass = '';
if( typeof chromoFields[i].preview_class != 'undefined' ){
previewClass = chromoFields[i].preview_class;
}
if( primaryKeys.includes(i) ){
if( !hasOpenCanadaID && primaryKeys.includes(i) ){
previewClass += ' pd-datatables-primary-key-fixed ';
}
if( typeof chromoFields[i].import_template_include != 'undefined' && ! chromoFields[i].import_template_include ){
Expand Down Expand Up @@ -1270,14 +1300,14 @@ function load_pd_datatable(CKAN_MODULE){
enabled: ! isExportingExcel,
className: "pd-datatable-btn pd-datatable-excel-btn btn-primary",
action: function(e, dt, button, config){
let pk_cols = primaryKeys.map(function(value){
let pk_cols = editKeys.map(function(value){
return value + colOffset;
});
let rows = dt.rows({ selected: true });
let params = new Object();
params['resource_name'] = resourceName;
params[csrfTokenName] = csrfTokenValue;
params['key_indices'] = primaryKeys;
params['key_indices'] = editKeys;
params['bulk-template'] = [];
rows.eq(0).each(function(index){
params['bulk-template'].push(dt.cells(index, pk_cols).data().toArray());
Expand Down Expand Up @@ -1322,7 +1352,7 @@ function load_pd_datatable(CKAN_MODULE){
text: '<i aria-hidden="true" class="fas fa-edit"></i>&nbsp;' + editSingleButtonLabel,
className: "pd-datatable-btn pd-datatable-btn-single btn-success",
action: function(e, dt, button, config){
let pk_cols = primaryKeys.map(function(value){
let pk_cols = editKeys.map(function(value){
return value + colOffset;
});
let rows = dt.rows({ selected: true });
Expand Down Expand Up @@ -1837,6 +1867,15 @@ function load_pd_datatable(CKAN_MODULE){
$(_field).css({'height': 'auto'});
$(_field).css({'height': this.scrollHeight + 'px'});
});
}else{
// do not submit form fields on enter
// textarea can have newlines, select2 opens on enter
$(_field).off('keydown.PreventSubmit');
$(_field).on('keydown.PreventSubmit', function(_event){
if( _event.keyCode == 13 ){
_event.preventDefault();
}
});
}
$(_field).off('change.EDITOR');
$(_field).on('change.EDITOR', function(_event){
Expand Down Expand Up @@ -2010,8 +2049,9 @@ function load_pd_datatable(CKAN_MODULE){
_data.compact_view = isCompactView;
_data.edit_view = isEditMode;

// TODO: Disable Editor - enable Table Editor when ready...
_data.edit_view = false;
if( DISABLE_EDITOR ){
_data.edit_view = false; // force editor disablement
}

_data.editing_rows = editingRows;
// TODO: save filledRows, erroredRows, requiredRows??? need to save the field values too???
Expand All @@ -2020,8 +2060,9 @@ function load_pd_datatable(CKAN_MODULE){
tableState.selected = localInstanceSelected;
tableState.edit_view = isEditMode;

// TODO: Disable Editor - enable Table Editor when ready...
tableState.edit_view = false;
if( DISABLE_EDITOR ){
tableState.edit_view = false; // force editor disablement
}

tableState.editing_rows = editingRows;
},
Expand All @@ -2030,8 +2071,9 @@ function load_pd_datatable(CKAN_MODULE){
tableState = _data;
tableState.selected = localInstanceSelected;

// TODO: Disable Editor - enable Table Editor when ready...
tableState.edit_view = false;
if( DISABLE_EDITOR ){
tableState.edit_view = false; // force editor disablement
}
},
buttons: get_available_buttons(),
});
Expand Down
9 changes: 9 additions & 0 deletions ckanext/canada/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ def get_pd_datatable(resource_name: str,
activity_priority = 1
fields = []
fids = []
if chromo.get('edit_using__id'):
fields.append({'type': 'int', 'id': '_id',
'label': 'open_canada_id', 'priority': 0})
fids.append('_id')
for f in chromo['fields']:
if f.get('published_resource_computed_field'):
continue
Expand All @@ -502,6 +506,10 @@ def get_pd_datatable(resource_name: str,
fids.append(f['datastore_id'])

pkids = [fids.index(k) for k in aslist(chromo['datastore_primary_key'])]
if chromo.get('edit_using__id'):
ekids = [0]
else:
ekids = pkids

fkids = {}
ds_info = get_action('datastore_info')(
Expand Down Expand Up @@ -544,6 +552,7 @@ def get_pd_datatable(resource_name: str,
resource_id=resource_id,
owner_org=owner_org,
primary_keys=pkids,
edit_keys=ekids,
foreign_keys=fkids,
dataset_type=dataset_type,
ds_fields=fields)
Expand Down
8 changes: 5 additions & 3 deletions ckanext/canada/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,17 @@ def datastore_create_temp_user_table(context: Context,
context['connection'].execute('''
CREATE TEMP TABLE IF NOT EXISTS datastore_user (
username text NOT NULL,
sysadmin boolean NOT NULL
sysadmin boolean NOT NULL,
importing boolean NOT NULL
){drop_statement};
INSERT INTO datastore_user VALUES (
{username}, {sysadmin}
{username}, {sysadmin}, {importing}
);
'''.format(
drop_statement=' ON COMMIT DROP' if drop_on_commit else '',
username=literal_string(username),
sysadmin='TRUE' if is_sysadmin(username) else 'FALSE'))
sysadmin='TRUE' if is_sysadmin(username) else 'FALSE',
importing='TRUE' if context.get('datastore_import') else 'FALSE'))
yield

# __exit__ of context manager
Expand Down
12 changes: 11 additions & 1 deletion ckanext/canada/templates/recombinant/update_pd_record.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@

{%- set chromo = h.recombinant_get_chromo(resource_name) -%}
<div class='container-fluid mrgn-bttm-lg'>
{%- if chromo.edit_using__id -%}
{% snippet 'scheming/form_snippets/text.html',
field={
'field_name': '_id',
'label': 'open_canada_id',
'form_attrs': {'readonly':''},
'required': true,
'help_text': _('open.canada.ca unique record identifier'),
}, data=data, errors={} %}
{%- endif -%}
{%- for f in chromo.fields %}
{%- if f.get('import_template_include', True) and not f.get('published_resource_computed_field', false) %}
{%- set required = f.get('form_required', False) %}
Expand All @@ -50,7 +60,7 @@
{%- set form_attrs = f.get('form_attrs', {}) %}
{%- set form_select_attrs = f.get('form_select_attrs', {}) %}
{%- set form_snippet = f.get('form_snippet', 'scheming/form_snippets/text.html') %}
{%- if f.datastore_id in pk_fields %}
{%- if not chromo.edit_using__id and f.datastore_id in pk_fields %}
{%- set form_attrs = dict(form_attrs, readonly='') %}
{%- endif %}
{%- if f.datastore_id in choice_fields %}
Expand Down
11 changes: 11 additions & 0 deletions ckanext/canada/templates/snippets/pd_datatable.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ <h2 id="prtable">{{_("Interactive Table (beta version)")}}</h2>
<script type="text/javascript" nonce="{{- h.get_inline_script_nonce() -}}">
{% if can_edit %}
const pd_datatables__EDITOR = {
{% if chromo.edit_using__id %}
'_id': {
type: 'int',
label: 'open_canada_id',
form_attrs: null,
select_choices: null,
is_required: function(VALUE, ROW_INDEX){ return false; },
is_invalid: function(VALUE, ROW_INDEX){ return false; },
},
{% endif %}
{% for field in chromo.fields %}
{{- field.datastore_id -}}: {
type: '{{- field.datastore_type -}}',
Expand Down Expand Up @@ -242,6 +252,7 @@ <h2 id="prtable">{{_("Interactive Table (beta version)")}}</h2>
data-module-table_styles='{{ tableStyles|tojson }}'
data-module-primary_keys='{{ primary_keys|tojson }}'
data-module-foreign_keys='{{ foreign_keys|tojson }}'
data-module-edit_keys='{{ edit_keys|tojson }}'
data-module-foreign_links='{{ fk_links|tojson }}'
data-module-chromo_fields='{{ chromo.fields|tojson }}'
data-module-is_editable='{{ (chromo.edit_form and can_edit)|tojson }}'>
Expand Down
5 changes: 3 additions & 2 deletions ckanext/canada/templates/snippets/pd_datatable_depr.html
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ <h2 id="prtable">{{_("Preview")}}</h2>
{% if chromo.edit_form and can_edit %}
{"className": "bg-info", "orderable": false, "targets": "no-sort"},
{% endif %}
{% if chromo.edit_using__id %}null,{% endif %}
{% for f in chromo.fields %}
{% if not f.get('published_resource_computed_field', false) %}
{{- "null" if 'preview_class' not in f else '{"className":"' | safe ~ f['preview_class'] ~ '"}' | safe }}
Expand Down Expand Up @@ -199,10 +200,10 @@ <h2 id="prtable">{{_("Preview")}}</h2>
}));
form.append($('<input>').attr({
type: 'hidden',
value: {{primary_keys}},
value: {{edit_keys}},
name: 'key_indices'
}));
var pk_cols = {{primary_keys}}.map(function(value){
var pk_cols = {{edit_keys}}.map(function(value){
return value + {{offset}};
});
let rows = dt.rows( { selected: true } );
Expand Down
Loading
Loading