Esempio n. 1
0
def group_create(context, data_dict):
    model = context['model']
    user = context['user']
    session = context['session']
    parent = context.get('parent', None)

    check_access('group_create', context, data_dict)

    # get the schema
    group_plugin = lib_plugins.lookup_group_plugin()
    try:
        schema = group_plugin.form_to_db_schema_options({
            'type':
            'create',
            'api':
            'api_version' in context
        })
    except AttributeError:
        schema = group_plugin.form_to_db_schema()

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

    if errors:
        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")

    group = model_save.group_dict_save(data, context)

    if parent:
        parent_group = model.Group.get(parent)
        if parent_group:
            member = model.Member(group=parent_group,
                                  table_id=group.id,
                                  table_name='group')
            session.add(member)

    if user:
        admins = [model.User.by_name(user.decode('utf8'))]
    else:
        admins = []
    model.setup_default_user_roles(group, admins)
    # Needed to let extensions know the group id
    session.flush()

    for item in plugins.PluginImplementations(plugins.IGroupController):
        item.create(group)

    activity_dict = {
        'user_id': model.User.by_name(user.decode('utf8')).id,
        'object_id': group.id,
        'activity_type': 'new group',
    }
    activity_dict['data'] = {
        'group': ckan.lib.dictization.table_dictize(group, context)
    }
    activity_create_context = {
        'model': model,
        'user': user,
        'defer_commit': True,
        'session': session
    }
    activity_create(activity_create_context, activity_dict, ignore_auth=True)

    if not context.get('defer_commit'):
        model.repo.commit()
    context["group"] = group
    context["id"] = group.id
    log.debug('Created object %s' % str(group.name))
    return model_dictize.group_dictize(group, context)
Esempio n. 2
0
def group_create(context, data_dict):
    model = context['model']
    user = context['user']
    session = context['session']
    parent = context.get('parent', None)

    check_access('group_create', context, data_dict)

    # get the schema
    group_plugin = lib_plugins.lookup_group_plugin()
    try:
        schema = group_plugin.form_to_db_schema_options({'type':'create',
                                               'api':'api_version' in context})
    except AttributeError:
        schema = group_plugin.form_to_db_schema()

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

    if errors:
        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")

    group = model_save.group_dict_save(data, context)

    if parent:
        parent_group = model.Group.get( parent )
        if parent_group:
            member = model.Member(group=parent_group, table_id=group.id, table_name='group')
            session.add(member)

    if user:
        admins = [model.User.by_name(user.decode('utf8'))]
    else:
        admins = []
    model.setup_default_user_roles(group, admins)
    # Needed to let extensions know the group id
    session.flush()

    for item in plugins.PluginImplementations(plugins.IGroupController):
        item.create(group)

    activity_dict = {
            'user_id': model.User.by_name(user.decode('utf8')).id,
            'object_id': group.id,
            'activity_type': 'new group',
            }
    activity_dict['data'] = {
            'group': ckan.lib.dictization.table_dictize(group, context)
            }
    activity_create_context = {
        'model': model,
        'user': user,
        'defer_commit':True,
        'session': session
    }
    activity_create(activity_create_context, activity_dict, ignore_auth=True)

    if not context.get('defer_commit'):
        model.repo.commit()
    context["group"] = group
    context["id"] = group.id
    log.debug('Created object %s' % str(group.name))
    return model_dictize.group_dictize(group, context)
Esempio n. 3
0
def group_update(context, data_dict):
    model = context['model']
    user = context['user']
    session = context['session']
    id = data_dict['id']
    parent = context.get('parent', None)

    group = model.Group.get(id)
    context["group"] = group
    if group is None:
        raise NotFound('Group was not found.')

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

    check_access('group_update', context, data_dict)

    data, errors = validate(data_dict, schema, context)
    if errors:
        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")

    group = model_save.group_dict_save(data, context)

    if parent:
        parent_group = model.Group.get(parent)
        if parent_group and not parent_group in group.get_groups(group.type):
            # Delete all of this groups memberships
            current = session.query(model.Member).\
               filter(model.Member.table_id == group.id).\
               filter(model.Member.table_name == "group").all()
            for c in current:
                session.delete(c)
            member = model.Member(group=parent_group,
                                  table_id=group.id,
                                  table_name='group')
            session.add(member)

    for item in plugins.PluginImplementations(plugins.IGroupController):
        item.edit(group)

    activity_dict = {
        'user_id': model.User.by_name(user.decode('utf8')).id,
        'object_id': group.id,
        'activity_type': 'changed group',
    }
    # Handle 'deleted' groups.
    # When the user marks a group as deleted this comes through here as
    # a 'changed' group activity. We detect this and change it to a 'deleted'
    # activity.
    if group.state == u'deleted':
        if session.query(ckan.model.Activity).filter_by(
                object_id=group.id, activity_type='deleted').all():
            # A 'deleted group' activity for this group has already been
            # emitted.
            # FIXME: What if the group was deleted and then activated again?
            activity_dict = None
        else:
            # We will emit a 'deleted group' activity.
            activity_dict['activity_type'] = 'deleted group'
    if activity_dict is not None:
        activity_dict['data'] = {
            'group': ckan.lib.dictization.table_dictize(group, context)
        }
        activity_create_context = {
            'model': model,
            'user': user,
            'defer_commit': True,
            'session': session
        }
        get_action('activity_create')(activity_create_context,
                                      activity_dict,
                                      ignore_auth=True)
        # TODO: Also create an activity detail recording what exactly changed
        # in the group.

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

    return model_dictize.group_dictize(group, context)
Esempio n. 4
0
 def _db_to_form_schema(self, group_type=None):
     '''This is an interface to manipulate data from the database
     into a format suitable for the form (optional)'''
     return lookup_group_plugin(group_type).form_to_db_schema()
Esempio n. 5
0
 def _setup_template_variables(self, context, data_dict, group_type=None):
     return lookup_group_plugin(group_type).setup_template_variables(context,data_dict)
Esempio n. 6
0
 def _group_form(self, group_type=None):
     return lookup_group_plugin(group_type).group_form()
Esempio n. 7
0
 def _form_to_db_schema(self, group_type=None):
     return lookup_group_plugin(group_type).form_to_db_schema()
Esempio n. 8
0
def group_update(context, data_dict):
    model = context['model']
    user = context['user']
    session = context['session']
    id = data_dict['id']
    parent = context.get('parent', None)

    group = model.Group.get(id)
    context["group"] = group
    if group is None:
        raise NotFound('Group was not found.')

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

    check_access('group_update', context, data_dict)

    data, errors = validate(data_dict, schema, context)
    if errors:
        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")

    group = model_save.group_dict_save(data, context)

    if parent:
        parent_group = model.Group.get( parent )
        if parent_group and not parent_group in group.get_groups(group.type):
            # Delete all of this groups memberships
            current = session.query(model.Member).\
               filter(model.Member.table_id == group.id).\
               filter(model.Member.table_name == "group").all()
            for c in current:
                session.delete(c)
            member = model.Member(group=parent_group, table_id=group.id, table_name='group')
            session.add(member)


    for item in plugins.PluginImplementations(plugins.IGroupController):
        item.edit(group)

    activity_dict = {
            'user_id': model.User.by_name(user.decode('utf8')).id,
            'object_id': group.id,
            'activity_type': 'changed group',
            }
    # Handle 'deleted' groups.
    # When the user marks a group as deleted this comes through here as
    # a 'changed' group activity. We detect this and change it to a 'deleted'
    # activity.
    if group.state == u'deleted':
        if session.query(ckan.model.Activity).filter_by(
                object_id=group.id, activity_type='deleted').all():
            # A 'deleted group' activity for this group has already been
            # emitted.
            # FIXME: What if the group was deleted and then activated again?
            activity_dict = None
        else:
            # We will emit a 'deleted group' activity.
            activity_dict['activity_type'] = 'deleted group'
    if activity_dict is not None:
        activity_dict['data'] = {
                'group': ckan.lib.dictization.table_dictize(group, context)
                }
        activity_create_context = {
            'model': model,
            'user': user,
            'defer_commit':True,
            'session': session
        }
        get_action('activity_create')(activity_create_context, activity_dict,
                ignore_auth=True)
        # TODO: Also create an activity detail recording what exactly changed
        # in the group.

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

    return model_dictize.group_dictize(group, context)
Esempio n. 9
0
 def _setup_template_variables(self, context, data_dict, group_type=None):
     return lookup_group_plugin(group_type).setup_template_variables(context,data_dict)
Esempio n. 10
0
 def _db_to_form_schema(self, group_type=None):
     '''This is an interface to manipulate data from the database
     into a format suitable for the form (optional)'''
     return lookup_group_plugin(group_type).form_to_db_schema()
Esempio n. 11
0
 def _form_to_db_schema(self, group_type=None):
     return lookup_group_plugin(group_type).form_to_db_schema()
Esempio n. 12
0
 def _group_form(self, group_type=None):
     return lookup_group_plugin(group_type).group_form()