def post(self, id, resource_id): u'''Data dictionary view: edit field labels and descriptions''' data_dict = self._prepare(id, resource_id) fields = data_dict[u'fields'] data = dict_fns.unflatten(tuplize_dict(parse_params(request.form))) info = data.get(u'info') if not isinstance(info, list): info = [] info = info[:len(fields)] get_action(u'datastore_create')( None, { u'resource_id': resource_id, u'force': True, u'fields': [{ u'id': f[u'id'], u'type': f[u'type'], u'info': fi if isinstance(fi, dict) else {} } for f, fi in zip_longest(fields, info)] } ) h.flash_success( _( u'Data Dictionary saved. Any type overrides will ' u'take effect when the resource is next uploaded ' u'to DataStore' ) ) return h.redirect_to( u'datastore.dictionary', id=id, resource_id=resource_id )
def edit_file(self, num): if not re.match(r'^\d+$', num): abort(404, _('Not found')) num = int(num) f = editable_files[num] errors = {} if request.POST: contents = request.POST['contents'] err = file_edit_update(num, contents) if not err: h.flash_success(_("File Updated")) redirect_to( controller='ckanext.fileedit.controller:FileEditController', action='edit_file', num='0') errors['contents'] = [err] else: contents = file_edit_show(num) return render('fileedit/edit.html', extra_vars={ 'data': { 'contents': contents }, 'errors': errors, 'editable_files': editable_files, 'label': f['label'], 'num': num, })
def dictionary(self, id, resource_id): u'''data dictionary view: show/edit field labels and descriptions''' try: # resource_edit_base template uses these c.pkg_dict = get_action('package_show')(None, {'id': id}) c.resource = get_action('resource_show')(None, {'id': resource_id}) rec = get_action('datastore_search')(None, { 'resource_id': resource_id, 'limit': 0 }) except (ObjectNotFound, NotAuthorized): abort(404, _('Resource not found')) fields = [f for f in rec['fields'] if not f['id'].startswith('_')] if request.method == 'POST': data = dict_fns.unflatten( tuplize_dict(parse_params(request.params))) info = data.get(u'info') if not isinstance(info, list): info = [] info = info[:len(fields)] get_action('datastore_create')(None, { 'resource_id': resource_id, 'force': True, 'fields': [{ 'id': f['id'], 'type': f['type'], 'info': fi if isinstance(fi, dict) else {} } for f, fi in izip_longest(fields, info)] }) h.flash_success( _('Data Dictionary saved. Any type overrides will ' 'take effect when the resource is next uploaded ' 'to DataStore')) h.redirect_to( controller='ckanext.datastore.controller:DatastoreController', action='dictionary', id=id, resource_id=resource_id) return render('datastore/dictionary.html', extra_vars={ 'fields': fields, 'pkg_dict': c.pkg_dict, 'resource': c.resource, })
def dictionary(self, id, resource_id): u'''data dictionary view: show/edit field labels and descriptions''' try: # resource_edit_base template uses these c.pkg_dict = get_action('package_show')( None, {'id': id}) c.resource = get_action('resource_show')( None, {'id': resource_id}) rec = get_action('datastore_search')(None, { 'resource_id': resource_id, 'limit': 0}) except (ObjectNotFound, NotAuthorized): abort(404, _('Resource not found')) fields = [f for f in rec['fields'] if not f['id'].startswith('_')] if request.method == 'POST': data = dict_fns.unflatten(tuplize_dict(parse_params( request.params))) info = data.get(u'info') if not isinstance(info, list): info = [] info = info[:len(fields)] get_action('datastore_create')(None, { 'resource_id': resource_id, 'force': True, 'fields': [{ 'id': f['id'], 'type': f['type'], 'info': fi if isinstance(fi, dict) else {} } for f, fi in izip_longest(fields, info)]}) h.flash_success(_('Data Dictionary saved. Any type overrides will ' 'take effect when the resource is next uploaded ' 'to DataStore')) h.redirect_to( controller='ckanext.datastore.controller:DatastoreController', action='dictionary', id=id, resource_id=resource_id) return render( 'datastore/dictionary.html', extra_vars={ 'fields': fields, 'pkg_dict': c.pkg_dict, 'resource': c.resource, })
def unflag(content_type, content_item_id, comment_id): """ Remove the 'flagged' attribute on a comment :param content_type: string 'dataset' or 'datarequest' :param content_item_id: string :param comment_id: string ID of the comment to unflag :return: """ context = {'model': model, 'user': c.user} # Using the comment model rather than the update action because update action updates modified timestamp comment = comment_model.Comment.get(comment_id) if not comment \ or not comment.flagged \ or not authz.auth_is_loggedin_user() \ or not helpers.user_can_manage_comments(content_type, content_item_id): abort(403) comment.flagged = False model.Session.add(comment) model.Session.commit() h.flash_success(_('Comment un-flagged')) data_dict = {'id': content_item_id} if content_type == 'datarequest': c.datarequest = get_action('show_datarequest')(context, data_dict) return h.redirect_to( str('/datarequest/comment/%s#comment_%s' % (content_item_id, comment_id))) else: c.pkg_dict = get_action('package_show')(context, data_dict) c.pkg = context['package'] return h.redirect_to( str('/dataset/%s#comment_%s' % (content_item_id, comment_id))) return helpers.render_content_template(content_type)
def visualize_data(): data_dict = clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.form)))) data_dict.update( clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.files))))) if request.method == 'POST' and 'save' in data_dict: del data_dict['save'] colors = [] for key in natsorted(data_dict.keys()): if key.startswith('color_'): color = {} color[key] = data_dict.get(key) colors.append(color) bar_image_url = _upload_chart_icon('bar', data_dict) line_image_url = _upload_chart_icon('line', data_dict) point_image_url = _upload_chart_icon('point', data_dict) if bar_image_url is None or bar_image_url == '': bar_image_url = config.get( 'bar_chart_icon') or '/base/images/Bar-symbol.png' if line_image_url is None or line_image_url == '': line_image_url = config.get( 'line_chart_icon') or '/base/images/Line-symbol.png' if point_image_url is None or point_image_url == '': point_image_url = config.get( 'point_chart_icon') or '/base/images/Point-symbol.png' data_dict = { 'visualize_colors': json.dumps(colors), 'bar_chart_icon': bar_image_url, 'line_chart_icon': line_image_url, 'point_chart_icon': point_image_url, } h.flash_success(_('Successfully updated.')) return redirect_to('admin_visualize.visualize_data') elif request.method == 'POST' and dict( request.params).get('reset') == 'true': visualize_colors = [] for i, default_color in enumerate(DEFAULT_COLORS): color = {} key_name = 'color_{0}'.format(i + 1) color[key_name] = default_color visualize_colors.append(color) data_dict = { 'visualize_colors': json.dumps(visualize_colors), 'bar_chart_icon': '/base/images/Bar-symbol.png', 'line_chart_icon': '/base/images/Line-symbol.png', 'point_chart_icon': '/base/images/Point-symbol.png', } get_action('config_option_update')({}, data_dict) h.flash_success(_('Successfully updated.')) return redirect_to('admin_visualize.visualize_data') # Initially set the colors from the default color palette. if not config.get('visualize_colors'): visualize_colors = [] for i, default_color in enumerate(DEFAULT_COLORS): color = {} key_name = 'color_{0}'.format(i + 1) color[key_name] = default_color visualize_colors.append(color) else: visualize_colors = json.loads(config.get('visualize_colors')) vars = { 'data': { 'visualize_colors': visualize_colors, 'bar_chart_icon': config.get('bar_chart_icon'), 'line_chart_icon': config.get('line_chart_icon'), 'point_chart_icon': config.get('point_chart_icon'), }, 'errors': {} } return render('admin/visualize_data.html', extra_vars=vars)