Exemplo n.º 1
0
def package_update_validate(context, data_dict):
    model = context['model']
    user = context['user']

    id = data_dict["id"]
    model.Session.remove()
    model.Session()._context = context

    pkg = model.Package.get(id)
    context["package"] = pkg

    if pkg is None:
        raise NotFound(_('Package was not found.'))
    data_dict["id"] = pkg.id

    # get the schema
    package_plugin = lib_plugins.lookup_package_plugin(pkg.type)
    try:
        schema = package_plugin.form_to_db_schema_options({'type':'update',
                                               'api':'api_version' in context})
    except AttributeError:
        schema = package_plugin.form_to_db_schema()

    check_access('package_update', context, data_dict)

    data, errors = validate(data_dict, schema, context)


    if errors:
        model.Session.rollback()
        raise ValidationError(errors, error_summary(errors))
    return data
Exemplo n.º 2
0
def package_update_validate(context, data_dict):
    model = context['model']
    user = context['user']

    id = data_dict["id"]
    model.Session.remove()
    model.Session()._context = context

    pkg = model.Package.get(id)
    context["package"] = pkg

    if pkg is None:
        raise NotFound(_('Package was not found.'))
    data_dict["id"] = pkg.id

    # get the schema
    package_plugin = lib_plugins.lookup_package_plugin(pkg.type)
    try:
        schema = package_plugin.form_to_db_schema_options({
            'type':
            'update',
            'api':
            'api_version' in context
        })
    except AttributeError:
        schema = package_plugin.form_to_db_schema()

    check_access('package_update', context, data_dict)

    data, errors = validate(data_dict, schema, context)

    if errors:
        model.Session.rollback()
        raise ValidationError(errors, error_summary(errors))
    return data
Exemplo n.º 3
0
def package_create(context, data_dict):

    model = context['model']
    user = context['user']
    model.Session.remove()
    model.Session()._context = context

    package_type = data_dict.get('type')
    package_plugin = lib_plugins.lookup_package_plugin(package_type)
    try:
        schema = package_plugin.form_to_db_schema_options({'type':'create',
                                               'api':'api_version' in context})
    except AttributeError:
        schema = package_plugin.form_to_db_schema()

    check_access('package_create', context, data_dict)

    if 'api_version' not in context:
        # old plugins do not support passing the schema so we need
        # to ensure they still work
        try:
            package_plugin.check_data_dict(data_dict, schema)
        except TypeError:
            package_plugin.check_data_dict(data_dict)

    data, errors = validate(data_dict, schema, context)

    if errors:
        model.Session.rollback()
        raise ValidationError(errors, error_summary(errors))

    rev = model.repo.new_revision()
    rev.author = user
    if 'message' in context:
        rev.message = context['message']
    else:
        rev.message = _(u'REST API: Create object %s') % data.get("name")

    pkg = model_save.package_dict_save(data, context)
    admins = []
    if user:
        admins = [model.User.by_name(user.decode('utf8'))]

    model.setup_default_user_roles(pkg, admins)
    # Needed to let extensions know the package id
    model.Session.flush()

    for item in plugins.PluginImplementations(plugins.IPackageController):
        item.create(pkg)

    if not context.get('defer_commit'):
        model.repo.commit()

    ## need to let rest api create
    context["package"] = pkg
    ## this is added so that the rest controller can make a new location
    context["id"] = pkg.id
    log.debug('Created object %s' % str(pkg.name))
    return get_action('package_show')(context, {'id':context['id']})
Exemplo n.º 4
0
def package_update(context, data_dict):

    model = context['model']
    user = context['user']
    name_or_id = data_dict.get("id") or data_dict['name_or_id']
    model.Session.remove()
    model.Session()._context = context

    pkg = model.Package.get(name_or_id)
    if pkg is None:
        raise NotFound(_('Package was not found.'))
    context["package"] = pkg
    data_dict["id"] = pkg.id

    check_access('package_update', context, data_dict)

    # get the schema
    package_plugin = lib_plugins.lookup_package_plugin(pkg.type)
    try:
        schema = package_plugin.form_to_db_schema_options({
            'type':
            'update',
            'api':
            'api_version' in context
        })
    except AttributeError:
        schema = package_plugin.form_to_db_schema()

    if 'api_version' not in context:
        # old plugins do not support passing the schema so we need
        # to ensure they still work
        try:
            package_plugin.check_data_dict(data_dict, schema)
        except TypeError:
            package_plugin.check_data_dict(data_dict)

    data, errors = validate(data_dict, schema, context)

    if errors:
        model.Session.rollback()
        raise ValidationError(errors, error_summary(errors))

    rev = model.repo.new_revision()
    rev.author = user
    if 'message' in context:
        rev.message = context['message']
    else:
        rev.message = _(u'REST API: Update object %s') % data.get("name")

    pkg = model_save.package_dict_save(data, context)

    for item in plugins.PluginImplementations(plugins.IPackageController):
        item.edit(pkg)
    if not context.get('defer_commit'):
        model.repo.commit()
    return get_action('package_show')(context, data_dict)
Exemplo n.º 5
0
def package_update(context, data_dict):

    model = context['model']
    user = context['user']
    name_or_id = data_dict.get("id") or data_dict['name_or_id']
    model.Session.remove()
    model.Session()._context = context

    pkg = model.Package.get(name_or_id)
    if pkg is None:
        raise NotFound(_('Package was not found.'))
    context["package"] = pkg
    data_dict["id"] = pkg.id

    check_access('package_update', context, data_dict)

    # get the schema
    package_plugin = lib_plugins.lookup_package_plugin(pkg.type)
    try:
        schema = package_plugin.form_to_db_schema_options({'type':'update',
                                               'api':'api_version' in context})
    except AttributeError:
        schema = package_plugin.form_to_db_schema()

    if 'api_version' not in context:
        # old plugins do not support passing the schema so we need
        # to ensure they still work
        try:
            package_plugin.check_data_dict(data_dict, schema)
        except TypeError:
            package_plugin.check_data_dict(data_dict)

    data, errors = validate(data_dict, schema, context)

    if errors:
        model.Session.rollback()
        raise ValidationError(errors, error_summary(errors))

    rev = model.repo.new_revision()
    rev.author = user
    if 'message' in context:
        rev.message = context['message']
    else:
        rev.message = _(u'REST API: Update object %s') % data.get("name")

    pkg = model_save.package_dict_save(data, context)

    for item in plugins.PluginImplementations(plugins.IPackageController):
        item.edit(pkg)
    if not context.get('defer_commit'):
        model.repo.commit()
    return get_action('package_show')(context, data_dict)
Exemplo n.º 6
0
def package_create_validate(context, data_dict):
    model = context['model']
    schema = lib_plugins.lookup_package_plugin().form_to_db_schema()
    model.Session.remove()
    model.Session()._context = context

    check_access('package_create', context, data_dict)

    data, errors = validate(data_dict, schema, context)

    if errors:
        model.Session.rollback()
        raise ValidationError(errors, error_summary(errors))
    else:
        return data
Exemplo n.º 7
0
def package_create_validate(context, data_dict):
    model = context['model']
    schema = lib_plugins.lookup_package_plugin().form_to_db_schema()
    model.Session.remove()
    model.Session()._context = context

    check_access('package_create',context,data_dict)

    data, errors = validate(data_dict, schema, context)

    if errors:
        model.Session.rollback()
        raise ValidationError(errors, error_summary(errors))
    else:
        return data
Exemplo n.º 8
0
 def _setup_template_variables(self, context, data_dict, package_type=None):
     return lookup_package_plugin(package_type).setup_template_variables(context, data_dict)
Exemplo n.º 9
0
 def _check_data_dict(self, data_dict, package_type=None):
     '''Check if the return data is correct, mostly for checking out if
     spammers are submitting only part of the form'''
     return lookup_package_plugin(package_type).check_data_dict(data_dict)
Exemplo n.º 10
0
 def _db_to_form_schema(self, package_type=None):
     '''This is an interface to manipulate data from the database
     into a format suitable for the form (optional)'''
     return lookup_package_plugin(package_type).db_to_form_schema()
Exemplo n.º 11
0
 def _form_to_db_schema(self, package_type=None):
     return lookup_package_plugin(package_type).form_to_db_schema()
Exemplo n.º 12
0
 def _package_form(self, package_type=None):    
     return lookup_package_plugin(package_type).package_form()
Exemplo n.º 13
0
def package_create(context, data_dict):

    model = context['model']
    user = context['user']
    model.Session.remove()
    model.Session()._context = context

    package_type = data_dict.get('type')
    package_plugin = lib_plugins.lookup_package_plugin(package_type)
    try:
        schema = package_plugin.form_to_db_schema_options({
            'type':
            'create',
            'api':
            'api_version' in context
        })
    except AttributeError:
        schema = package_plugin.form_to_db_schema()

    check_access('package_create', context, data_dict)

    if 'api_version' not in context:
        # old plugins do not support passing the schema so we need
        # to ensure they still work
        try:
            package_plugin.check_data_dict(data_dict, schema)
        except TypeError:
            package_plugin.check_data_dict(data_dict)

    data, errors = validate(data_dict, schema, context)

    if errors:
        model.Session.rollback()
        raise ValidationError(errors, error_summary(errors))

    rev = model.repo.new_revision()
    rev.author = user
    if 'message' in context:
        rev.message = context['message']
    else:
        rev.message = _(u'REST API: Create object %s') % data.get("name")

    pkg = model_save.package_dict_save(data, context)
    admins = []
    if user:
        admins = [model.User.by_name(user.decode('utf8'))]

    model.setup_default_user_roles(pkg, admins)
    # Needed to let extensions know the package id
    model.Session.flush()

    for item in plugins.PluginImplementations(plugins.IPackageController):
        item.create(pkg)

    if not context.get('defer_commit'):
        model.repo.commit()

    ## need to let rest api create
    context["package"] = pkg
    ## this is added so that the rest controller can make a new location
    context["id"] = pkg.id
    log.debug('Created object %s' % str(pkg.name))
    return get_action('package_show')(context, {'id': context['id']})
Exemplo n.º 14
0
 def _package_form(self, package_type=None):
     return lookup_package_plugin(package_type).package_form()
Exemplo n.º 15
0
 def _form_to_db_schema(self, package_type=None):
     return lookup_package_plugin(package_type).form_to_db_schema()
Exemplo n.º 16
0
 def _db_to_form_schema(self, package_type=None):
     '''This is an interface to manipulate data from the database
     into a format suitable for the form (optional)'''
     return lookup_package_plugin(package_type).db_to_form_schema()
Exemplo n.º 17
0
 def _setup_template_variables(self, context, data_dict, package_type=None):
     return lookup_package_plugin(package_type).setup_template_variables(
         context, data_dict)
Exemplo n.º 18
0
 def _check_data_dict(self, data_dict, package_type=None):
     '''Check if the return data is correct, mostly for checking out if
     spammers are submitting only part of the form'''
     return lookup_package_plugin(package_type).check_data_dict(data_dict)