def querytool_public_read(self, name): ''' :return: base template ''' querytool = _get_action('querytool_public_read', {'name': name}) if not querytool or not querytool['charts']: abort(404, _('Querytool not fully set.')) if querytool.get('charts'): querytool['charts'] = json.loads(querytool['charts']) params = toolkit.request.params new_filters = querytool.get('filters') new_filters = json.loads(new_filters) for k, v in params.items(): if k.startswith('data_filter_name_'): id = k.split('_')[-1] for filter in new_filters: if v == filter.get('name'): filter['value'] =\ params.get('data_filter_value_{}'.format(id)) if k.startswith('y_axis_column'): querytool['y_axis_column'] = v sql_string = helpers.create_query_str(querytool.get('chart_resource'), new_filters) querytool['public_filters'] = new_filters querytool['public_filters'].sort(key=itemgetter('order')) querytool['sql_string'] = sql_string return render('querytool/public/read.html', extra_vars={'querytool': querytool})
def querytool_public_read(self, name): ''' :return: base template ''' querytool = _get_action('querytool_public_read', {'name': name}) if not querytool: abort(404, _('Report not found.')) # only sysadmins or organization members can access private querytool if querytool['private'] is True: context = _get_context() try: check_access('querytool_show', context, {'name': name}) except NotAuthorized: abort(403, _('Not authorized to see this page')) # Check if the data for this querytool still exists if querytool['dataset_name']: try: _get_action('package_show', {'id': querytool['dataset_name']}) except NotFound: abort(404, _('The data used for creating this ' 'report has been removed ' 'by the administrator.')) if not querytool['visualizations']: abort(404, _('Report not fully set.')) params = toolkit.request.params querytools = [] items = [] items.append({u'order': 0, u'name': querytool['name']}) if querytool['related_querytools']: items.extend(json.loads(querytool['related_querytools'])) items.sort(key=itemgetter('order')) for item in items: q_item = _get_action( 'querytool_public_read', {'name': item['name']} ) if q_item: q_item['visualizations'] = json.loads( q_item['visualizations'] ) q_item['visualizations'].sort(key=itemgetter('order')) q_name = q_item['name'] new_filters = json.loads(q_item['filters']) for k, v in params.items(): # Update query filters if k.startswith('{}_data_filter_name_'.format(q_name)): id = k.split('_')[-1] for filter in new_filters: # Apply changes only on public filters # to protect changing private # filters by changing the url query params if filter['visibility'] == 'public': if v == filter.get('name'): filter['value'] = \ params.get('{}_data_filter_value_{}' .format(q_name, id)) # Replace & with %26 to fix the error for graphs # not being generated for values with & in them filter['value'] = filter['value'].replace('&','%26') # Update charts y_axis value if k.startswith('{}_y_axis_column'.format(q_name)): q_item['y_axis_column'] = v # Update visualizations filters if k.startswith('{}_viz_filter_name'.format(q_name)): id = k.split('_')[-1] for visualization in q_item['visualizations']: if visualization['order'] == int(id): visualization['filter_name'] = \ params.get('{}_viz_filter_name_{}'. format(q_name, id)) visualization['filter_value'] = \ params.get('{}_viz_filter_value_{}'. format(q_name, id)) for image in q_item['visualizations']: if image['type'] == 'image': is_upload = image['url'] and not image[ 'url'].startswith('http') if is_upload: image['url'] = '{0}/uploads/vs/{1}'.format( toolkit.request.host_url, image['url']) related_sql_string = helpers.create_query_str( q_item.get('chart_resource'), new_filters ) q_item['public_filters'] = new_filters q_item['public_filters'].sort(key=itemgetter('order')) q_item['sql_string'] = related_sql_string # Add slug to filters for filter in new_filters: filter['slug'] = helpers.slugify(filter.get('alias', '')) # Need this hack for chart filter q_item['public_main_filters'] = json.dumps(new_filters) querytools.append(q_item) return render('querytool/public/read.html', extra_vars={'querytools': querytools})
def querytool_edit(self, querytool=None, data=None, errors=None, error_summary=None): ''' Create/edit query tool :return: query create/edit template page ''' if querytool: querytool = querytool[1:] data_dict = { 'name': querytool } context = _get_context() try: check_access('querytool_update', context, data_dict) except NotAuthorized: abort(403, _('Not authorized to see this page')) _querytool = _get_action('querytool_get', data_dict) if _querytool is None and len(querytool) > 0: abort(404, _('Report not found.')) if _querytool is None: _querytool = {} # Check if the data for this querytool still exists if 'dataset_name' in _querytool.keys(): try: _get_action('package_show', {'id': _querytool['dataset_name']}) except NotFound: abort(404, _('The data used for creating this ' 'report has been removed ' 'by the administrator.')) if toolkit.request.method == 'POST' and not data: data = dict(toolkit.request.POST) group = ast.literal_eval(data['group']) data.pop('group') data['group'] = group['name'] filters = [] y_axis_columns = [] related_querytools = [] for k, v in data.items(): if k.startswith('data_filter_name_'): filter = {} id = k.split('_')[-1] filter['order'] = int(id) filter['name'] = data['data_filter_name_{}'.format(id)] filter['value'] = data['data_filter_value_{}'.format(id)] # Replace & with %26 to fix the error for graphs # not being generated for values with & in them filter['value'] = filter['value'].replace('&','%26') filter['alias'] = data['data_filter_alias_{}'.format(id)] filter['visibility'] = \ data['data_filter_visibility_{}'.format(id)] filters.append(filter) elif k.startswith('y_axis_name_'): id = k.split('_')[-1] alias = data.get('y_axis_alias_%s' % id, '') y_axis_columns.append({'name': v, 'alias': alias}) elif k.startswith('related_querytool_'): related_querytool = {} id = k.split('_')[-1] related_querytool['order'] = int(id) related_querytool['name'] = \ data['related_querytool_{}'.format(id)] related_querytools.append(related_querytool) if any(filters): _querytool['filters'] = json.dumps(filters) sql_string = helpers.create_query_str(data['chart_resource'], filters) else: _querytool['filters'] = '' sql_string = '' if 'private' not in data.keys(): _querytool['private'] = True if any(related_querytools): _querytool['related_querytools'] = json.\ dumps(related_querytools) else: _querytool['related_querytools'] = '' _querytool.update(data) _querytool['querytool'] = querytool _querytool['sql_string'] = sql_string _querytool['y_axis_columns'] = ( json.dumps(y_axis_columns) if y_axis_columns else '') _querytool['owner_org'] = data['owner_org'] _querytool['icon'] = data['icon'] _querytool['selection_label'] = data.get('selection_label') _querytool['report_caption'] = data.get('report_caption') try: junk = _get_action('querytool_update', _querytool) h.flash_success(_('Data Successfully updated.')) except ValidationError, e: errors = e.error_dict error_summary = e.error_summary return self.querytool_edit('/' + querytool, _querytool, errors, error_summary) if 'save_data' in data.keys(): # redirect to querytools group toolkit.redirect_to('/'+h.lang()+'/group/'+_querytool['group']+'/reports') else: # redirect to manage visualisations url = h.url_for('querytool_edit_visualizations', querytool='/' + _querytool['name']) h.redirect_to(url)
def querytool_edit(self, querytool=None, data=None, errors=None, error_summary=None): ''' Create/edit query tool :return: query create/edit template page ''' context = _get_context() try: check_access('querytool_update', context) except NotAuthorized: abort(403, _('Not authorized to see this page')) if querytool: querytool = querytool[1:] data_dict = {'name': querytool} _querytool = _get_action('querytool_get', data_dict) if _querytool is None and len(querytool) > 0: abort(404, _('Querytool not found.')) if _querytool is None: _querytool = {} if toolkit.request.method == 'POST' and not data: data = dict(toolkit.request.POST) filters = [] y_axis_columns = [] for k, v in data.items(): if k.startswith('data_filter_name_'): filter = {} id = k.split('_')[-1] filter['order'] = int(id) filter['name'] = data['data_filter_name_{}'.format(id)] filter['value'] = data['data_filter_value_{}'.format(id)] filter['alias'] = data['data_filter_alias_{}'.format(id)] filter['visibility'] = \ data['data_filter_visibility_{}'.format(id)] filters.append(filter) elif k.startswith('y_axis_column_'): y_axis_columns.append(v) if any(filters): _querytool['filters'] = json.dumps(filters) sql_string = helpers.create_query_str(data['resource_id_1'], filters) else: _querytool['filters'] = '' sql_string = '' _querytool.update(data) _querytool['querytool'] = querytool _querytool['sql_string'] = sql_string _querytool['y_axis_columns'] = ','.join(y_axis_columns) try: junk = _get_action('querytool_update', _querytool) h.flash_success(_('Data Successfully updated.')) except ValidationError, e: errors = e.error_dict error_summary = e.error_summary return self.querytool_edit('/' + querytool, _querytool, errors, error_summary) # redirect to manage visualisations url = h.url_for('querytool_edit_visualizations', querytool='/' + _querytool['name']) h.redirect_to(url)