コード例 #1
0
    def new_resource(self, id, data=None, errors=None, error_summary=None):
        ''' FIXME: This is a temporary action to allow styling of the
        forms. '''
        if request.method == 'POST' and not data:
            save_action = request.params.get('save')
            data = data or clean_dict(dictization_functions.unflatten(
                tuplize_dict(parse_params(request.POST))))
            # we don't want to include save as it is part of the form
            del data['save']
            resource_id = data['id']
            del data['id']

            context = {'model': model, 'session': model.Session,
                       'user': c.user or c.author, 'auth_user_obj': c.userobj}

            # see if we have any data that we are trying to save
            data_provided = False
            for key, value in data.iteritems():
                if ((value or isinstance(value, cgi.FieldStorage))
                    and key != 'resource_type'):
                    data_provided = True
                    break

            if not data_provided and save_action != "go-dataset-complete":
                if save_action == 'go-dataset':
                    # go to final stage of adddataset
                    redirect(h.url_for(controller='package',
                                       action='edit', id=id))
                # see if we have added any resources
                try:
                    data_dict = get_action('package_show')(context, {'id': id})
                except NotAuthorized:
                    abort(401, _('Unauthorized to update dataset'))
                except NotFound:
                    abort(404,
                      _('The dataset {id} could not be found.').format(id=id))
                if not len(data_dict['resources']):
                    # no data so keep on page
                    msg = _('You must add at least one data resource')
                    # On new templates do not use flash message
                    if g.legacy_templates:
                        h.flash_error(msg)
                        redirect(h.url_for(controller='package',
                                           action='new_resource', id=id))
                    else:
                        errors = {}
                        error_summary = {_('Error'): msg}
                        return self.new_resource(id, data, errors, error_summary)
                # we have a resource so let them add metadata
                redirect(h.url_for(controller='package',
                                   action='new_metadata', id=id))

            data['package_id'] = id
            try:
                if resource_id:
                    data['id'] = resource_id
                    get_action('resource_update')(context, data)
                else:
                    get_action('resource_create')(context, data)
            except ValidationError, e:
                errors = e.error_dict
                error_summary = e.error_summary
                return self.new_resource(id, data, errors, error_summary)
            except NotAuthorized:
                abort(401, _('Unauthorized to create a resource'))
コード例 #2
0
			    messages['result'].append({
                                'resource': result.get('resourceName', ''),
                                'valid': valid,
                                'messages': message,
                                'link': link,
                                'resourceId': resourceId,
				'fileName': fileName,
                            })

		    #if at least one resource file validation is failed, redirect user to new_resource page with error message
		    if not validationProcess:
			html = "The file(s) provided might have changes to be applied or might have failed the validation. For more details, please click <a href='#' class='btn btn-info btn-small openUSGINModelValidationMessage' style='color:white !important'>here</a>"
			# </div><div> HACK template (close alert div) and open div
			html = html + "</div><div>" + render('usginmodels/modalValidationMessages.html', messages)
			h.flash_error(html, True)

		        redirect(h.url_for(controller='package',
                                           action='new_resource', id=id))
		### END USGINModels File Validation ###

	    if save_action == 'go-metadata':
		data_dict = get_action('package_show')(context, {'id': id})
                get_action('package_update')(
                    dict(context, allow_state_change=True),
                    dict(data_dict, state='active'))
                redirect(h.url_for(controller='package',
                                   action='read', id=id))
                """
                # this is the original route
                # go to final stage of add dataset
コード例 #3
0
def is_usgin_valid_data(key, data, errors, context):
    resource_id = data.get(('resources', 0, 'id'), None)

    if resource_id is None:
        return

    resource_name = data.get(('resources', 0, 'name'), None)

    md_resource = None
    for k, v in data.iteritems():
        if k[0] == 'resources' and k[-1] == 'md_resource':
            query_key = k
            md_resource = json.loads(data.get(query_key, None))

    md_package = None
    for k, v in data.iteritems():
        if k[0] == 'extras' and v == 'md_package':
            query_key = (k[0], k[1], 'value')
            md_package = json.loads(data.get(query_key, None))

    resourceDescription = md_package.get('resourceDescription', {})
    uri = resourceDescription.get('usginContentModel', None)
    version = resourceDescription.get('usginContentModelVersion', None)
    layer = resourceDescription.get('usginContentModelLayer', None)

    if None in [uri, version, layer]:
        return

    def get_file_path(res_id):
        dir_1 = res_id[0:3]
        dir_2 = res_id[3:6]
        file = res_id[6:]
        storage_base = config.get('ckan.storage_path', 'default')
        return path.join(storage_base, 'resources', dir_1, dir_2, file)

    validation_msg = []
    csv_file = get_file_path(resource_id)

    if csv_file:
        log.info("Filename full path: %s " % csv_file)
    else:
        msg = base._("Cannot find the full path of the resources from %s"\
            % resource_name)
        validation_msg.append({
            'row': 0,
            'col': 0,
            'errorType': 'systemError',
            'message': msg
        })

    if 'none' in [uri.lower(), version.lower(), layer.lower()]:
        log.debug("Start USGIN content model validation")
        log.debug("USGIN tier 2 data model/version/layer are none")
        return {'valid': True}
    else:
	try:
	    # Valid intialization to resove this issue:
            # Error - <type 'exceptions.UnboundLocalError'>: local variable 'valid' referenced before assignment
            valid = False

            csv = open(csv_file, 'rbU')
	    valid, messages, dataCorrected, long_fields, srs = usginmodels.validate_file(csv, version, layer)
        except:
            log.info("The given format's file is not a CSV")
	    os.remove(csv_file)
	    get_action('resource_delete')(context, {'id': resource_id})
	    errors[key].append(base._("The given format's file is not a CSV"))
	    #raise ValidationError(errors)
	    #raise StopOnError

        log.debug("Finished USGIN content model validation")

	if valid and messages:
            log.debug('With changes the USGIN document will be valid')
            h.flash_error(base._('With changes the USGIN document will be valid'))
        elif valid and not messages:
            log.debug("USGIN document is valid")
        else:
            log.debug('USGIN document is not valid')
            h.flash_error(base._('The USGIN document is not valid'))
コード例 #4
0
    def new_resource(self, id, data=None, errors=None, error_summary=None):
        ''' FIXME: This is a temporary action to allow styling of the
        forms. '''
        if request.method == 'POST' and not data:
            save_action = request.params.get('save')
            data = data or clean_dict(
                dictization_functions.unflatten(
                    tuplize_dict(parse_params(request.POST))))
            # we don't want to include save as it is part of the form
            del data['save']
            resource_id = data['id']
            del data['id']

            context = {
                'model': model,
                'session': model.Session,
                'user': c.user or c.author,
                'auth_user_obj': c.userobj
            }

            # see if we have any data that we are trying to save
            data_provided = False
            for key, value in data.iteritems():
                if ((value or isinstance(value, cgi.FieldStorage))
                        and key != 'resource_type'):
                    data_provided = True
                    break

            if not data_provided and save_action != "go-dataset-complete":
                if save_action == 'go-dataset':
                    # go to final stage of adddataset
                    redirect(
                        h.url_for(controller='package', action='edit', id=id))
                # see if we have added any resources
                try:
                    data_dict = get_action('package_show')(context, {'id': id})
                except NotAuthorized:
                    abort(401, _('Unauthorized to update dataset'))
                except NotFound:
                    abort(
                        404,
                        _('The dataset {id} could not be found.').format(
                            id=id))
                if not len(data_dict['resources']):
                    # no data so keep on page
                    msg = _('You must add at least one data resource')
                    # On new templates do not use flash message
                    if g.legacy_templates:
                        h.flash_error(msg)
                        redirect(
                            h.url_for(controller='package',
                                      action='new_resource',
                                      id=id))
                    else:
                        errors = {}
                        error_summary = {_('Error'): msg}
                        return self.new_resource(id, data, errors,
                                                 error_summary)
                # we have a resource so let them add metadata
                redirect(
                    h.url_for(controller='package',
                              action='new_metadata',
                              id=id))

            data['package_id'] = id
            try:
                if resource_id:
                    data['id'] = resource_id
                    get_action('resource_update')(context, data)
                else:
                    get_action('resource_create')(context, data)
            except ValidationError, e:
                errors = e.error_dict
                error_summary = e.error_summary
                return self.new_resource(id, data, errors, error_summary)
            except NotAuthorized:
                abort(401, _('Unauthorized to create a resource'))
コード例 #5
0
                                'link':
                                link,
                                'resourceId':
                                resourceId,
                                'fileName':
                                fileName,
                            })

#if at least one resource file validation is failed, redirect user to new_resource page with error message
                    if not validationProcess:
                        html = "The file(s) provided might have changes to be applied or might have failed the validation. For more details, please click <a href='#' class='btn btn-info btn-small openUSGINModelValidationMessage' style='color:white !important'>here</a>"
                        # </div><div> HACK template (close alert div) and open div
                        html = html + "</div><div>" + render(
                            'usginmodels/modalValidationMessages.html',
                            messages)
                        h.flash_error(html, True)

                        redirect(
                            h.url_for(controller='package',
                                      action='new_resource',
                                      id=id))
### END USGINModels File Validation ###

            if save_action == 'go-metadata':
                data_dict = get_action('package_show')(context, {'id': id})
                get_action('package_update')(dict(context,
                                                  allow_state_change=True),
                                             dict(data_dict, state='active'))
                redirect(h.url_for(controller='package', action='read', id=id))
                """
                # this is the original route
コード例 #6
0
def is_usgin_valid_data(key, data, errors, context):
    resource_id = data.get(('resources', 0, 'id'), None)

    if resource_id is None:
        return

    resource_name = data.get(('resources', 0, 'name'), None)

    md_resource = None
    for k, v in data.iteritems():
        if k[0] == 'resources' and k[-1] == 'md_resource':
            query_key = k
            md_resource = json.loads(data.get(query_key, None))

    md_package = None
    for k, v in data.iteritems():
        if k[0] == 'extras' and v == 'md_package':
            query_key = (k[0], k[1], 'value')
            md_package = json.loads(data.get(query_key, None))

    uri = md_package.get('usginContentModel', None)
    version = md_package.get('usginContentModelVersion', None)
    layer = md_resource.get('usginContentModelLayer', None)

    if None in [uri, version, layer]:
        return

    def get_file_path(res_id):
        dir_1 = res_id[0:3]
        dir_2 = res_id[3:6]
        file = res_id[6:]
        storage_base = config.get('ckan.storage_path', 'default')
        return path.join(storage_base, 'resources', dir_1, dir_2, file)

    validation_msg = []
    csv_file = get_file_path(resource_id)

    if csv_file:
        log.info("Filename full path: %s " % csv_file)
    else:
        msg = base._("Cannot find the full path of the resources from %s"\
            % resource_name)
        validation_msg.append({
            'row': 0,
            'col': 0,
            'errorType': 'systemError',
            'message': msg
        })

    if 'none' in [uri.lower(), version.lower(), layer.lower()]:
        log.debug("Start USGIN content model validation")
        log.debug("USGIN tier 2 data model/version/layer are none")
        return {'valid': True}
    else:
        csv = open(csv_file, 'rbU')
        valid, errors, dataCorrected, long_fields, srs = \
            usginmodels.validate_file(csv, version, layer)

        log.debug("Finished USGIN content model validation")

        if valid and not errors:
            log.debug("USGIN document is valid")
        if valid and errors:
            log.debug('With changes the USGIN document will be valid')
            h.flash_error(base._('With changes the USGIN document will be valid'))
        else:
            log.debug('USGIN document is not valid')
            h.flash_error(base._('The USGIN document is not valid'))