def group_show(context, data_dict): '''Shows group details''' model = context['model'] id = data_dict['id'] group = model.Group.get(id) context['group'] = group if group is None: raise NotFound check_access('group_show',context, data_dict) group_dict = model_dictize.group_dictize(group, context) for item in plugins.PluginImplementations(plugins.IGroupController): item.read(group) group_plugin = lib_plugins.lookup_group_plugin(group_dict['type']) try: schema = group_plugin.db_to_form_schema_options({ 'type':'show', 'api': 'api_version' in context, 'context': context }) except AttributeError: schema = group_plugin.db_to_form_schema() if schema: package_dict, errors = validate(group_dict, schema, context=context) return group_dict
def dge_organization_publisher(context, data_dict=None): try: model = context['model'] id = logic.get_or_bust(data_dict, 'id') group = model.Group.get(id) context['group'] = group if group is None: raise NotFound if not group.is_organization: raise NotFound group_dict = model_dictize.group_dictize( group, context, packages_field='dataset_count', include_tags=False, include_extras=True, include_groups=False, include_users=False, ) group_plugin = lib_plugins.lookup_group_plugin(group_dict['type']) schema = logic.schema.default_show_group_schema() group_dict, errors = lib_plugins.plugin_validate( group_plugin, context, group_dict, schema, 'organization_show') return group_dict except: return {}
def validate_publisher_create(data_dict, context): session = context['session'] group_plugin = lib_plugins.lookup_group_plugin('organization') try: _schema = group_plugin.form_to_db_schema_options({ 'type': 'create', 'api': 'api_version' in context, 'context': context }) except AttributeError: _schema = group_plugin.form_to_db_schema() data, errors = lib_plugins.plugin_validate(group_plugin, context, data_dict, _schema, 'organization_create') session.rollback() if errors: return errors return dict()
def group_show(context, data_dict): '''Shows group details''' model = context['model'] id = data_dict['id'] group = model.Group.get(id) context['group'] = group if group is None: raise NotFound check_access('group_show', context, data_dict) group_dict = model_dictize.group_dictize(group, context) for item in plugins.PluginImplementations(plugins.IGroupController): item.read(group) schema = lib_plugins.lookup_group_plugin( group_dict['type']).db_to_form_schema() if schema: package_dict, errors = validate(group_dict, schema, context=context) return group_dict
def group_or_org_plugin_dictize(context, group_dict, include_followers, is_org): plugins = p if is_org: plugin_type = plugins.IOrganizationController else: plugin_type = plugins.IGroupController for item in plugins.PluginImplementations(plugin_type): item.read(group) group_plugin = lib_plugins.lookup_group_plugin(group_dict['type']) try: schema = group_plugin.db_to_form_schema_options({ 'type': 'show', 'api': 'api_version' in context, 'context': context }) except AttributeError: schema = group_plugin.db_to_form_schema() if include_followers: model = context['model'] group_dict['num_followers'] = get_action('group_follower_count')( { 'model': model, 'session': model.Session }, { 'id': group_dict['id'] }) else: group_dict['num_followers'] = 0 if not group_dict.get('display_name'): group_dict['display_name'] = None if not group_dict.get('package_count'): group_dict['package_count'] = None schema = default_show_group_schema() group_dict, errors = lib_plugins.plugin_validate( group_plugin, context, group_dict, schema, 'organization_show' if is_org else 'group_show') return group_dict
def group_show(context, data_dict): '''Return the details of a group. :param id: the id or name of the group :type id: string :rtype: dictionary ''' model = context['model'] id = data_dict['id'] group = model.Group.get(id) context['group'] = group if group is None or group.state == u'deleted': raise logic.NotFound logic.check_access('group_show', context, data_dict) group_dict = group_dictize(group, context) for item in plugins.PluginImplementations(plugins.IGroupController): item.read(group) group_plugin = lib_plugins.lookup_group_plugin(group_dict['type']) try: group_schema = group_plugin.db_to_form_schema_options({ 'type': 'show', 'api': 'api_version' in context, 'context': context }) except AttributeError: group_schema = group_plugin.db_to_form_schema() if group_schema: package_dict, errors = _validate(group_dict, group_schema, context=context) return group_dict
def group_show(context, data_dict): '''Shows group details''' model = context['model'] id = data_dict['id'] group = model.Group.get(id) context['group'] = group if group is None: raise NotFound check_access('group_show',context, data_dict) group_dict = model_dictize.group_dictize(group, context) for item in plugins.PluginImplementations(plugins.IGroupController): item.read(group) schema = lib_plugins.lookup_group_plugin(group_dict['type']).db_to_form_schema() if schema: package_dict, errors = validate(group_dict, schema, context=context) return group_dict
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).db_to_form_schema()
def _group_form(self, group_type=None): return lookup_group_plugin(group_type).group_form()
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, 'context': 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)
def _form_to_db_schema(self, group_type=None): return lookup_group_plugin(group_type).form_to_db_schema()
def _group_or_org_update(context, data_dict, is_org=False): model = context["model"] user = context["user"] session = context["session"] id = _get_or_bust(data_dict, "id") group = model.Group.get(id) context["group"] = group if group is None: raise NotFound("Group was not found.") data_dict["type"] = group.type # 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, "context": context} ) except AttributeError: schema = group_plugin.form_to_db_schema() upload = uploader.get_uploader("group", group.image_url) upload.update_data_dict(data_dict, "image_url", "image_upload", "clear_upload") if is_org: _check_access("organization_update", context, data_dict) else: _check_access("group_update", 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: group_plugin.check_data_dict(data_dict, schema) except TypeError: group_plugin.check_data_dict(data_dict) data, errors = lib_plugins.plugin_validate( group_plugin, context, data_dict, schema, "organization_update" if is_org else "group_update" ) log.debug( "group_update validate_errs=%r user=%s group=%s data_dict=%r", errors, context.get("user"), context.get("group").name if context.get("group") else "", data_dict, ) if errors: session.rollback() raise ValidationError(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, prevent_packages_update=is_org) if is_org: plugin_type = plugins.IOrganizationController else: plugin_type = plugins.IGroupController for item in plugins.PluginImplementations(plugin_type): item.edit(group) if is_org: activity_type = "changed organization" else: activity_type = "changed group" activity_dict = { "user_id": model.User.by_name(user.decode("utf8")).id, "object_id": group.id, "activity_type": activity_type, } # 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": dictization.table_dictize(group, context)} activity_create_context = { "model": model, "user": user, "defer_commit": True, "ignore_auth": True, "session": session, } _get_action("activity_create")(activity_create_context, activity_dict) # TODO: Also create an activity detail recording what exactly changed # in the group. upload.upload(uploader.get_max_image_size()) if not context.get("defer_commit"): model.repo.commit() return model_dictize.group_dictize(group, context)
def _group_or_org_create(context, data_dict, is_org=False): model = context['model'] user = context['user'] session = context['session'] parent = context.get('parent', None) data_dict['is_organization'] = is_org # get the schema group_plugin = lib_plugins.lookup_group_plugin( group_type=data_dict.get('type')) try: schema = group_plugin.form_to_db_schema_options({ 'type': 'create', 'api': 'api_version' in context, 'context': context }) except AttributeError: schema = group_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: group_plugin.check_data_dict(data_dict, schema) except TypeError: group_plugin.check_data_dict(data_dict) data, errors = _validate(data_dict, schema, context) log.debug('group_create validate_errs=%r user=%s group=%s data_dict=%r', errors, context.get('user'), data_dict.get('name'), data_dict) if errors: session.rollback() raise ValidationError(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) log.debug('Group %s is made child of group %s', group.name, parent_group.name) 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() if is_org: plugin_type = plugins.IOrganizationController else: plugin_type = plugins.IGroupController for item in plugins.PluginImplementations(plugin_type): item.create(group) if is_org: activity_type = 'new organization' else: activity_type = 'new group' user_id = model.User.by_name(user.decode('utf8')).id activity_dict = { 'user_id': user_id, 'object_id': group.id, 'activity_type': activity_type, } activity_dict['data'] = { 'group': ckan.lib.dictization.table_dictize(group, context) } activity_create_context = { 'model': model, 'user': user, 'defer_commit': True, 'ignore_auth': True, 'session': session } logic.get_action('activity_create')(activity_create_context, activity_dict) if not context.get('defer_commit'): model.repo.commit() context["group"] = group context["id"] = group.id # creator of group/org becomes an admin # this needs to be after the repo.commit or else revisions break member_dict = { 'id': group.id, 'object': user_id, 'object_type': 'user', 'capacity': 'admin', } member_create_context = { 'model': model, 'user': user, 'ignore_auth': True, # we are not a member of the group at this point 'session': session } logic.get_action('member_create')(member_create_context, member_dict) log.debug('Created object %s' % group.name) return model_dictize.group_dictize(group, context)
def _edit_template(self, group_type): return lookup_group_plugin(group_type).edit_template()
def _group_or_org_create(context, data_dict, is_org=False): model = context['model'] user = context['user'] session = context['session'] data_dict['is_organization'] = is_org upload = uploader.Upload('group') upload.update_data_dict(data_dict, 'image_url', 'image_upload', 'clear_upload') # get the schema group_plugin = lib_plugins.lookup_group_plugin( group_type=data_dict.get('type')) try: schema = group_plugin.form_to_db_schema_options({'type':'create', 'api':'api_version' in context, 'context': context}) except AttributeError: schema = group_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: group_plugin.check_data_dict(data_dict, schema) except TypeError: group_plugin.check_data_dict(data_dict) data, errors = lib_plugins.plugin_validate( group_plugin, context, data_dict, schema, 'organization_create' if is_org else 'group_create') log.debug('group_create validate_errs=%r user=%s group=%s data_dict=%r', errors, context.get('user'), data_dict.get('name'), data_dict) if errors: session.rollback() raise ValidationError(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 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() if is_org: plugin_type = plugins.IOrganizationController else: plugin_type = plugins.IGroupController for item in plugins.PluginImplementations(plugin_type): item.create(group) if is_org: activity_type = 'new organization' else: activity_type = 'new group' user_id = model.User.by_name(user.decode('utf8')).id activity_dict = { 'user_id': user_id, 'object_id': group.id, 'activity_type': activity_type, } activity_dict['data'] = { 'group': ckan.lib.dictization.table_dictize(group, context) } activity_create_context = { 'model': model, 'user': user, 'defer_commit': True, 'ignore_auth': True, 'session': session } # DGU checks if activity streams are enabled first, to avoid Auth Audit # issue #1421 if paste.deploy.converters.asbool( config.get('ckan.activity_streams_enabled', 'true')): logic.get_action('activity_create')(activity_create_context, activity_dict) upload.upload(uploader.get_max_image_size()) if not context.get('defer_commit'): model.repo.commit() context["group"] = group context["id"] = group.id # creator of group/org becomes an admin # this needs to be after the repo.commit or else revisions break member_dict = { 'id': group.id, 'object': user_id, 'object_type': 'user', 'capacity': 'admin', } member_create_context = { 'model': model, 'user': user, 'ignore_auth': True, # we are not a member of the group at this point 'session': session } logic.get_action('member_create')(member_create_context, member_dict) log.debug('Created object %s' % group.name) return model_dictize.group_dictize(group, context)
def hdx_group_or_org_update(context, data_dict, is_org=False): # Overriding default so that orgs can have multiple images model = context['model'] user = context['user'] session = context['session'] id = _get_or_bust(data_dict, 'id') group = model.Group.get(id) context["group"] = group if group is None: raise NotFound('Group was not found.') if is_org: check_access('organization_update', context, data_dict) else: check_access('group_update', context, data_dict) # 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, 'context': context}) except AttributeError: schema = group_plugin.form_to_db_schema() try: customization = json.loads(group.extras['customization']) except: customization = {'image_sq': '', 'image_rect': ''} try: data_dict['customization'] = json.loads(data_dict['customization']) except: data_dict['customization'] = {} # If we're removing the image if 'clear_image_sq' in data_dict and data_dict['clear_image_sq']: remove_image(customization['image_sq']) data_dict['customization']['image_sq'] = '' customization['image_rect'] = '' if 'clear_image_rect' in data_dict and data_dict['clear_image_rect']: remove_image(customization['image_rect']) data_dict['customization']['image_rect'] = '' customization['image_rect'] = '' if 'image_sq_upload' in data_dict and data_dict['image_sq_upload'] != '' and data_dict['image_sq_upload'] != None: # Although weird, the FieldStorage instance has a boolean value of False so we need to compare to None # If old image was uploaded remove it if customization['image_sq']: remove_image(customization['image_sq']) upload1 = uploader.Upload('group', customization['image_sq']) upload1.update_data_dict(data_dict, 'image_sq', 'image_sq_upload', 'clear_upload') if 'image_rect_upload' in data_dict and data_dict['image_rect_upload'] != '' \ and data_dict['image_rect_upload'] != None: # Although weird, the FieldStorage instance has a boolean value of False so we need to compare to None if customization['image_rect']: remove_image(customization['image_rect']) upload2 = uploader.Upload('group', customization['image_rect']) upload2.update_data_dict(data_dict, 'image_rect', 'image_rect_upload', 'clear_upload') storage_path = uploader.get_storage_path() ##Rearrange things the way we need them try: if data_dict['image_sq'] != '' and data_dict['image_sq'] != None: data_dict['customization']['image_sq'] = data_dict['image_sq'] else: data_dict['customization']['image_sq'] = customization['image_sq'] except KeyError: data_dict['customization']['image_sq'] = '' try: if data_dict['image_rect'] != '' and data_dict['image_rect'] != None: data_dict['customization']['image_rect'] = data_dict['image_rect'] else: data_dict['customization']['image_rect'] = customization['image_rect'] except KeyError: data_dict['customization']['image_rect'] = '' data_dict['customization'] = json.dumps(data_dict['customization']) if 'api_version' not in context: # old plugins do not support passing the schema so we need # to ensure they still work try: group_plugin.check_data_dict(data_dict, schema) except TypeError: group_plugin.check_data_dict(data_dict) data, errors = _validate(data_dict, schema, context) log.debug('group_update validate_errs=%r user=%s group=%s data_dict=%r', errors, context.get('user'), context.get('group').name if context.get('group') else '', data_dict) if errors: session.rollback() raise ValidationError(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") # when editing an org we do not want to update the packages if using the # new templates. if ((not is_org) and not converters.asbool( config.get('ckan.legacy_templates', False)) and 'api_version' not in context): context['prevent_packages_update'] = True group = model_save.group_dict_save(data, context) if is_org: plugin_type = plugins.IOrganizationController else: plugin_type = plugins.IGroupController for item in plugins.PluginImplementations(plugin_type): item.edit(group) if is_org: activity_type = 'changed organization' else: activity_type = 'changed group' activity_dict = { 'user_id': model.User.by_name(user.decode('utf8')).id, 'object_id': group.id, 'activity_type': activity_type, } # 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': dictization.table_dictize(group, context) } activity_create_context = { 'model': model, 'user': user, 'defer_commit': True, 'ignore_auth': True, 'session': session } get_action('activity_create')(activity_create_context, activity_dict) # TODO: Also create an activity detail recording what exactly changed # in the group. try: upload1.upload(uploader.get_max_image_size()) except: pass try: upload2.upload(uploader.get_max_image_size()) except: pass if not context.get('defer_commit'): model.repo.commit() return model_dictize.group_dictize(group, context)
def hdx_group_or_org_create(context, data_dict, is_org=False): # Overriding default so that orgs can have multiple images model = context['model'] user = context['user'] session = context['session'] data_dict['is_organization'] = is_org if is_org: check_access('organization_create', context, data_dict) else: check_access('group_create', context, data_dict) # get the schema group_type = data_dict.get('type') group_plugin = lib_plugins.lookup_group_plugin(group_type) try: schema = group_plugin.form_to_db_schema_options({ 'type': 'create', 'api': 'api_version' in context, 'context': context}) except AttributeError: schema = group_plugin.form_to_db_schema() # try: # customization = json.loads(group.extras['customization']) # except: customization = {'image_sq': '', 'image_rect': ''} try: data_dict['customization'] = json.loads(data_dict['customization']) except: data_dict['customization'] = {} if 'image_sq_upload' in data_dict and data_dict['image_sq_upload'] != '' and data_dict['image_sq_upload'] != None: # If old image was uploaded remove it if customization['image_sq']: remove_image(customization['image_sq']) upload1 = uploader.Upload('group', customization['image_sq']) upload1.update_data_dict(data_dict, 'image_sq', 'image_sq_upload', 'clear_upload') if 'image_rect_upload' in data_dict and data_dict['image_rect_upload'] != '' and data_dict[ 'image_rect_upload'] != None: if customization['image_rect']: remove_image(customization['image_rect']) upload2 = uploader.Upload('group', customization['image_rect']) upload2.update_data_dict(data_dict, 'image_rect', 'image_rect_upload', 'clear_upload') storage_path = uploader.get_storage_path() ##Rearrange things the way we need them try: if data_dict['image_sq'] != '' and data_dict['image_sq'] != None: data_dict['customization']['image_sq'] = data_dict['image_sq'] else: data_dict['customization']['image_sq'] = customization['image_sq'] except KeyError: data_dict['customization']['image_sq'] = '' try: if data_dict['image_rect'] != '' and data_dict['image_rect'] != None: data_dict['customization']['image_rect'] = data_dict['image_rect'] else: data_dict['customization']['image_rect'] = customization['image_rect'] except KeyError: data_dict['customization']['image_rect'] = '' data_dict['customization'] = json.dumps(data_dict['customization']) if 'api_version' not in context: # old plugins do not support passing the schema so we need # to ensure they still work try: group_plugin.check_data_dict(data_dict, schema) except TypeError: group_plugin.check_data_dict(data_dict) data, errors = lib_plugins.plugin_validate( group_plugin, context, data_dict, schema, 'organization_create' if is_org else 'group_create') log.debug('group_create validate_errs=%r user=%s group=%s data_dict=%r', errors, context.get('user'), data_dict.get('name'), data_dict) if errors: session.rollback() raise ValidationError(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) # Needed to let extensions know the group id session.flush() if is_org: plugin_type = plugins.IOrganizationController else: plugin_type = plugins.IGroupController for item in plugins.PluginImplementations(plugin_type): item.create(group) if is_org: activity_type = 'new organization' else: activity_type = 'new group' user_id = model.User.by_name(user.decode('utf8')).id activity_dict = { 'user_id': user_id, 'object_id': group.id, 'activity_type': activity_type, } activity_dict['data'] = { 'group': ckan.lib.dictization.table_dictize(group, context) } activity_create_context = { 'model': model, 'user': user, 'defer_commit': True, 'ignore_auth': True, 'session': session } logic.get_action('activity_create')(activity_create_context, activity_dict) try: upload1.upload(uploader.get_max_image_size()) except: pass try: upload2.upload(uploader.get_max_image_size()) except: pass if not context.get('defer_commit'): model.repo.commit() context["group"] = group context["id"] = group.id # creator of group/org becomes an admin # this needs to be after the repo.commit or else revisions break member_dict = { 'id': group.id, 'object': user_id, 'object_type': 'user', 'capacity': 'admin', } member_create_context = { 'model': model, 'user': user, 'ignore_auth': True, # we are not a member of the group at this point 'session': session } logic.get_action('member_create')(member_create_context, member_dict) log.debug('Created object %s' % group.name) return model_dictize.group_dictize(group, context)
def _group_or_org_create(context, data_dict, is_org=False): model = context["model"] user = context["user"] session = context["session"] data_dict["is_organization"] = is_org upload = uploader.Upload("group") upload.update_data_dict(data_dict, "image_url", "image_upload", "clear_upload") # get the schema group_type = data_dict.get("type") group_plugin = lib_plugins.lookup_group_plugin(group_type) try: schema = group_plugin.form_to_db_schema_options( {"type": "create", "api": "api_version" in context, "context": context} ) except AttributeError: schema = group_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: group_plugin.check_data_dict(data_dict, schema) except TypeError: group_plugin.check_data_dict(data_dict) data, errors = lib_plugins.plugin_validate( group_plugin, context, data_dict, schema, "organization_create" if is_org else "group_create" ) log.debug( "group_create validate_errs=%r user=%s group=%s data_dict=%r", errors, context.get("user"), data_dict.get("name"), data_dict, ) if errors: session.rollback() raise ValidationError(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 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() if is_org: plugin_type = plugins.IOrganizationController else: plugin_type = plugins.IGroupController for item in plugins.PluginImplementations(plugin_type): item.create(group) if is_org: activity_type = "new organization" else: activity_type = "new group" user_id = model.User.by_name(user.decode("utf8")).id activity_dict = {"user_id": user_id, "object_id": group.id, "activity_type": activity_type} activity_dict["data"] = {"group": ckan.lib.dictization.table_dictize(group, context)} activity_create_context = { "model": model, "user": user, "defer_commit": True, "ignore_auth": True, "session": session, } logic.get_action("activity_create")(activity_create_context, activity_dict) upload.upload(uploader.get_max_image_size()) if not context.get("defer_commit"): model.repo.commit() context["group"] = group context["id"] = group.id # creator of group/org becomes an admin # this needs to be after the repo.commit or else revisions break member_dict = {"id": group.id, "object": user_id, "object_type": "user", "capacity": "admin"} member_create_context = { "model": model, "user": user, "ignore_auth": True, # we are not a member of the group at this point "session": session, } logic.get_action("member_create")(member_create_context, member_dict) log.debug("Created object %s" % group.name) return model_dictize.group_dictize(group, context)
def _admins_template(self, group_type): return lookup_group_plugin(group_type).admins_template()
def _about_template(self, group_type): return lookup_group_plugin(group_type).about_template()
def _new_template(self, group_type): return lookup_group_plugin(group_type).new_template()
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, "context": 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)
def _read_template(self, group_type): return lookup_group_plugin(group_type).read_template()
def _setup_template_variables(self, context, data_dict, group_type=None): return lookup_group_plugin(group_type).\ setup_template_variables(context, data_dict)
def _group_or_org_update(context, data_dict, is_org=False): model = context['model'] user = context['user'] session = context['session'] id = _get_or_bust(data_dict, 'id') 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, 'context': context}) except AttributeError: schema = group_plugin.form_to_db_schema() upload = uploader.Upload('group', group.image_url) upload.update_data_dict(data_dict, 'image_url', 'image_upload', 'clear_upload') if is_org: _check_access('organization_update', context, data_dict) else: _check_access('group_update', 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: group_plugin.check_data_dict(data_dict, schema) except TypeError: group_plugin.check_data_dict(data_dict) data, errors = lib_plugins.plugin_validate( group_plugin, context, data_dict, schema, 'organization_update' if is_org else 'group_update') log.debug('group_update validate_errs=%r user=%s group=%s data_dict=%r', errors, context.get('user'), context.get('group').name if context.get('group') else '', data_dict) if errors: session.rollback() raise ValidationError(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") # when editing an org we do not want to update the packages if using the # new templates. if ((not is_org) and not converters.asbool( config.get('ckan.legacy_templates', False)) and 'api_version' not in context): context['prevent_packages_update'] = True group = model_save.group_dict_save(data, context) if is_org: plugin_type = plugins.IOrganizationController else: plugin_type = plugins.IGroupController for item in plugins.PluginImplementations(plugin_type): item.edit(group) if is_org: activity_type = 'changed organization' else: activity_type = 'changed group' activity_dict = { 'user_id': model.User.by_name(user.decode('utf8')).id, 'object_id': group.id, 'activity_type': activity_type, } # 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': dictization.table_dictize(group, context) } activity_create_context = { 'model': model, 'user': user, 'defer_commit': True, 'ignore_auth': True, 'session': session } # DGU checks if activity streams are enabled first, to avoid Auth Audit # issue #1421 if converters.asbool( config.get('ckan.activity_streams_enabled', 'true')): _get_action('activity_create')(activity_create_context, activity_dict) # TODO: Also create an activity detail recording what exactly changed # in the group. upload.upload(uploader.get_max_image_size()) if not context.get('defer_commit'): model.repo.commit() return model_dictize.group_dictize(group, context)
def _index_template(self, group_type): return lookup_group_plugin(group_type).index_template()
def _group_or_org_update(context, data_dict, is_org=False): model = context['model'] user = context['user'] session = context['session'] id = _get_or_bust(data_dict, 'id') group = model.Group.get(id) context["group"] = group if group is None: raise NotFound('Group was not found.') data_dict['type'] = group.type # 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, 'context': context }) except AttributeError: schema = group_plugin.form_to_db_schema() upload = uploader.get_uploader('group', group.image_url) upload.update_data_dict(data_dict, 'image_url', 'image_upload', 'clear_upload') if is_org: _check_access('organization_update', context, data_dict) else: _check_access('group_update', 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: group_plugin.check_data_dict(data_dict, schema) except TypeError: group_plugin.check_data_dict(data_dict) data, errors = lib_plugins.plugin_validate( group_plugin, context, data_dict, schema, 'organization_update' if is_org else 'group_update') log.debug('group_update validate_errs=%r user=%s group=%s data_dict=%r', errors, context.get('user'), context.get('group').name if context.get('group') else '', data_dict) if errors: session.rollback() raise ValidationError(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, prevent_packages_update=is_org) if is_org: plugin_type = plugins.IOrganizationController else: plugin_type = plugins.IGroupController for item in plugins.PluginImplementations(plugin_type): item.edit(group) if is_org: activity_type = 'changed organization' else: activity_type = 'changed group' activity_dict = { 'user_id': model.User.by_name(user.decode('utf8')).id, 'object_id': group.id, 'activity_type': activity_type, } # 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': dictization.table_dictize(group, context) } activity_create_context = { 'model': model, 'user': user, 'defer_commit': True, 'ignore_auth': True, 'session': session } _get_action('activity_create')(activity_create_context, activity_dict) # TODO: Also create an activity detail recording what exactly changed # in the group. upload.upload(uploader.get_max_image_size()) if not context.get('defer_commit'): model.repo.commit() return model_dictize.group_dictize(group, context)
def _history_template(self, group_type): return lookup_group_plugin(group_type).history_template()
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, 'context': 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)
def _group_or_org_create(context, data_dict, is_org=False): model = context['model'] user = context['user'] session = context['session'] data_dict['is_organization'] = is_org upload = uploader.Upload('group') upload.update_data_dict(data_dict, 'image_url', 'image_upload', 'clear_upload') # get the schema group_plugin = lib_plugins.lookup_group_plugin( group_type=data_dict.get('type')) try: schema = group_plugin.form_to_db_schema_options({ 'type': 'create', 'api': 'api_version' in context, 'context': context }) except AttributeError: schema = group_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: group_plugin.check_data_dict(data_dict, schema) except TypeError: group_plugin.check_data_dict(data_dict) data, errors = lib_plugins.plugin_validate( group_plugin, context, data_dict, schema, 'organization_create' if is_org else 'group_create') log.debug('group_create validate_errs=%r user=%s group=%s data_dict=%r', errors, context.get('user'), data_dict.get('name'), data_dict) if errors: session.rollback() raise ValidationError(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 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() if is_org: plugin_type = plugins.IOrganizationController else: plugin_type = plugins.IGroupController for item in plugins.PluginImplementations(plugin_type): item.create(group) if is_org: activity_type = 'new organization' else: activity_type = 'new group' user_id = model.User.by_name(user.decode('utf8')).id activity_dict = { 'user_id': user_id, 'object_id': group.id, 'activity_type': activity_type, } activity_dict['data'] = { 'group': ckan.lib.dictization.table_dictize(group, context) } activity_create_context = { 'model': model, 'user': user, 'defer_commit': True, 'ignore_auth': True, 'session': session } # DGU checks if activity streams are enabled first, to avoid Auth Audit # issue #1421 if paste.deploy.converters.asbool( config.get('ckan.activity_streams_enabled', 'true')): logic.get_action('activity_create')(activity_create_context, activity_dict) upload.upload(uploader.get_max_image_size()) if not context.get('defer_commit'): model.repo.commit() context["group"] = group context["id"] = group.id # creator of group/org becomes an admin # this needs to be after the repo.commit or else revisions break member_dict = { 'id': group.id, 'object': user_id, 'object_type': 'user', 'capacity': 'admin', } member_create_context = { 'model': model, 'user': user, 'ignore_auth': True, # we are not a member of the group at this point 'session': session } logic.get_action('member_create')(member_create_context, member_dict) log.debug('Created object %s' % group.name) return model_dictize.group_dictize(group, context)
def _activity_template(self, group_type): return lookup_group_plugin(group_type).activity_template()
def _group_or_org_update(context, data_dict, is_org=False): model = context['model'] user = context['user'] session = context['session'] id = _get_or_bust(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, 'context': context}) except AttributeError: schema = group_plugin.form_to_db_schema() if is_org: _check_access('organization_update', context, data_dict) else: _check_access('group_update', 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: group_plugin.check_data_dict(data_dict, schema) except TypeError: group_plugin.check_data_dict(data_dict) data, errors = _validate(data_dict, schema, context) log.debug('group_update validate_errs=%r user=%s group=%s data_dict=%r', errors, context.get('user'), context.get('group').name if context.get('group') else '', data_dict) if errors: session.rollback() raise ValidationError(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") # when editing an org we do not want to update the packages if using the # new templates. if ((not is_org) and not paste.deploy.converters.asbool( config.get('ckan.legacy_templates', False)) and 'api_version' not in context): context['prevent_packages_update'] = True 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() if current: log.debug('Parents of group %s deleted: %r', group.name, [membership.group.name for membership in current]) for c in current: session.delete(c) member = model.Member(group=parent_group, table_id=group.id, table_name='group') session.add(member) log.debug('Group %s is made child of group %s', group.name, parent_group.name) if is_org: plugin_type = plugins.IOrganizationController else: plugin_type = plugins.IGroupController for item in plugins.PluginImplementations(plugin_type): item.edit(group) if is_org: activity_type = 'changed organization' else: activity_type = 'changed group' activity_dict = { 'user_id': model.User.by_name(user.decode('utf8')).id, 'object_id': group.id, 'activity_type': activity_type, } # 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)
def _bulk_process_template(self, group_type): return lookup_group_plugin(group_type).bulk_process_template()
def group_create(context, data_dict): '''Create a new group. You must be authorized to create groups. Plugins may change the parameters of this function depending on the value of the ``type`` parameter, see the ``IGroupForm`` plugin interface. :param name: the name of the group, a string between 2 and 100 characters long, containing only lowercase alphanumeric characters, ``-`` and ``_`` :type name: string :param id: the id of the group (optional) :type id: string :param title: the title of the group (optional) :type title: string :param description: the description of the group (optional) :type description: string :param image_url: the URL to an image to be displayed on the group's page (optional) :type image_url: string :param type: the type of the group (optional), ``IGroupForm`` plugins associate themselves with different group types and provide custom group handling behaviour for these types :type type: string :param state: the current state of the group, e.g. ``'active'`` or ``'deleted'``, only active groups show up in search results and other lists of groups, this parameter will be ignored if you are not authorized to change the state of the group (optional, default: ``'active'``) :type state: string :param approval_status: (optional) :type approval_status: string :param extras: the group's extras (optional), extras are arbitrary (key: value) metadata items that can be added to groups, each extra dictionary should have keys ``'key'`` (a string), ``'value'`` (a string), and optionally ``'deleted'`` :type extras: list of dataset extra dictionaries :param packages: the datasets (packages) that belong to the group, a list of dictionaries each with keys ``'name'`` (string, the id or name of the dataset) and optionally ``'title'`` (string, the title of the dataset) :type packages: list of dictionaries :param groups: the groups that belong to the group, a list of dictionaries each with key ``'name'`` (string, the id or name of the group) and optionally ``'capacity'`` (string, the capacity in which the group is a member of the group) :type groups: list of dictionaries :param users: the users that belong to the group, a list of dictionaries each with key ``'name'`` (string, the id or name of the user) and optionally ``'capacity'`` (string, the capacity in which the user is a member of the group) :type users: list of dictionaries :returns: the newly created group :rtype: dictionary ''' 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, 'context': 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)
def group_update(context, data_dict): '''Update a group. You must be authorized to edit the group. Plugins may change the parameters of this function depending on the value of the group's ``type`` attribute, see the ``IGroupForm`` plugin interface. For further parameters see ``group_create()``. :param id: the name or id of the group to update :type id: string :returns: the updated group :rtype: dictionary ''' model = context['model'] user = context['user'] session = context['session'] id = _get_or_bust(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, 'context': 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)
def _index_template(self,group_type): from ckan.lib.helpers import default_group_type return lookup_group_plugin(group_type).index_template()
def _group_or_org_update( context: Context, data_dict: DataDict, is_org: bool = False): model = context['model'] session = context['session'] id = _get_or_bust(data_dict, 'id') group = model.Group.get(id) if group is None: raise NotFound('Group was not found.') context["group"] = group data_dict['type'] = group.type # 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, 'context': context}) except AttributeError: schema = group_plugin.form_to_db_schema() upload = uploader.get_uploader('group') upload.update_data_dict(data_dict, 'image_url', 'image_upload', 'clear_upload') if is_org: _check_access('organization_update', context, data_dict) else: _check_access('group_update', 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: group_plugin.check_data_dict(data_dict, schema) except TypeError: group_plugin.check_data_dict(data_dict) data, errors = lib_plugins.plugin_validate( group_plugin, context, data_dict, schema, 'organization_update' if is_org else 'group_update') group = context.get('group') log.debug('group_update validate_errs=%r user=%s group=%s data_dict=%r', errors, context.get('user'), group.name if group else '', data_dict) if errors: session.rollback() raise ValidationError(errors) contains_packages = 'packages' in data_dict group = model_save.group_dict_save( data, context, prevent_packages_update=is_org or not contains_packages ) if is_org: plugin_type = plugins.IOrganizationController else: plugin_type = plugins.IGroupController for item in plugins.PluginImplementations(plugin_type): item.edit(group) upload.upload(uploader.get_max_image_size()) if not context.get('defer_commit'): model.repo.commit() return model_dictize.group_dictize(group, context)
def _group_or_org_create(context, data_dict, is_org=False): model = context['model'] user = context['user'] session = context['session'] parent = context.get('parent', None) data_dict['is_organization'] = is_org # get the schema group_plugin = lib_plugins.lookup_group_plugin( group_type=data_dict.get('type')) try: schema = group_plugin.form_to_db_schema_options({'type':'create', 'api':'api_version' in context, 'context': context}) except AttributeError: schema = group_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: group_plugin.check_data_dict(data_dict, schema) except TypeError: group_plugin.check_data_dict(data_dict) data, errors = _validate(data_dict, schema, context) log.debug('group_create validate_errs=%r user=%s group=%s data_dict=%r', errors, context.get('user'), data_dict.get('name'), data_dict) if errors: session.rollback() raise ValidationError(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) log.debug('Group %s is made child of group %s', group.name, parent_group.name) 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() if is_org: plugin_type = plugins.IOrganizationController else: plugin_type = plugins.IGroupController for item in plugins.PluginImplementations(plugin_type): item.create(group) if is_org: activity_type = 'new organization' else: activity_type = 'new group' user_id = model.User.by_name(user.decode('utf8')).id activity_dict = { 'user_id': user_id, 'object_id': group.id, 'activity_type': activity_type, } activity_dict['data'] = { 'group': ckan.lib.dictization.table_dictize(group, context) } activity_create_context = { 'model': model, 'user': user, 'defer_commit':True, 'session': session } logic.get_action('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 # creator of group/org becomes an admin # this needs to be after the repo.commit or else revisions break member_dict = { 'id': group.id, 'object': user_id, 'object_type': 'user', 'capacity': 'admin', } member_create_context = { 'model': model, 'user': user, 'ignore_auth': True, # we are not a member of the group at this point 'session': session } logic.get_action('member_create')(member_create_context, member_dict) log.debug('Created object %s' % str(group.name)) return model_dictize.group_dictize(group, context)
def db_to_form_schema(group_type=None): from ckan.lib.plugins import lookup_group_plugin return lookup_group_plugin(group_type).db_to_form_schema()
def _group_or_org_update(context, data_dict, is_org=False): model = context["model"] user = context["user"] session = context["session"] id = _get_or_bust(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, "context": context} ) except AttributeError: schema = group_plugin.form_to_db_schema() if is_org: _check_access("organization_update", context, data_dict) else: _check_access("group_update", 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: group_plugin.check_data_dict(data_dict, schema) except TypeError: group_plugin.check_data_dict(data_dict) data, errors = _validate(data_dict, schema, context) log.debug( "group_update validate_errs=%r user=%s group=%s data_dict=%r", errors, context.get("user"), context.get("group").name if context.get("group") else "", data_dict, ) if errors: session.rollback() raise ValidationError(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") # when editing an org we do not want to update the packages if using the # new templates. if ( (not is_org) and not paste.deploy.converters.asbool(config.get("ckan.legacy_templates", False)) and "api_version" not in context ): context["prevent_packages_update"] = True 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() ) if current: log.debug( "Parents of group %s deleted: %r", group.name, [membership.group.name for membership in current] ) for c in current: session.delete(c) member = model.Member(group=parent_group, table_id=group.id, table_name="group") session.add(member) log.debug("Group %s is made child of group %s", group.name, parent_group.name) if is_org: plugin_type = plugins.IOrganizationController else: plugin_type = plugins.IGroupController for item in plugins.PluginImplementations(plugin_type): item.edit(group) if is_org: activity_type = "changed organization" else: activity_type = "changed group" activity_dict = { "user_id": model.User.by_name(user.decode("utf8")).id, "object_id": group.id, "activity_type": activity_type, } # 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)
def group_create(context, data_dict): '''Create a new group. You must be authorized to create groups. Plugins may change the parameters of this function depending on the value of the ``type`` parameter, see the ``IGroupForm`` plugin interface. :param name: the name of the group, a string between 2 and 100 characters long, containing only lowercase alphanumeric characters, ``-`` and ``_`` :type name: string :param id: the id of the group (optional) :type id: string :param title: the title of the group (optional) :type title: string :param description: the description of the group (optional) :type description: string :param image_url: the URL to an image to be displayed on the group's page (optional) :type image_url: string :param type: the type of the group (optional), ``IGroupForm`` plugins associate themselves with different group types and provide custom group handling behaviour for these types :type type: string :param state: the current state of the group, e.g. ``'active'`` or ``'deleted'``, only active groups show up in search results and other lists of groups, this parameter will be ignored if you are not authorized to change the state of the group (optional, default: ``'active'``) :type state: string :param approval_status: (optional) :type approval_status: string :param extras: the group's extras (optional), extras are arbitrary (key: value) metadata items that can be added to groups, each extra dictionary should have keys ``'key'`` (a string), ``'value'`` (a string), and optionally ``'deleted'`` :type extras: list of dataset extra dictionaries :param packages: the datasets (packages) that belong to the group, a list of dictionaries each with keys ``'name'`` (string, the id or name of the dataset) and optionally ``'title'`` (string, the title of the dataset) :type packages: list of dictionaries :param groups: the groups that belong to the group, a list of dictionaries each with key ``'name'`` (string, the id or name of the group) and optionally ``'capacity'`` (string, the capacity in which the group is a member of the group) :type groups: list of dictionaries :param users: the users that belong to the group, a list of dictionaries each with key ``'name'`` (string, the id or name of the user) and optionally ``'capacity'`` (string, the capacity in which the user is a member of the group) :type users: list of dictionaries :returns: the newly created group :rtype: dictionary ''' 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( group_type=data_dict.get('type')) try: schema = group_plugin.form_to_db_schema_options({'type':'create', 'api':'api_version' in context, 'context': context}) except AttributeError: schema = group_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: group_plugin.check_data_dict(data_dict, schema) except TypeError: group_plugin.check_data_dict(data_dict) data, errors = _validate(data_dict, schema, context) log.debug('group_create validate_errs=%r user=%s group=%s data_dict=%r', errors, context.get('user'), data_dict.get('name'), data_dict) if errors: session.rollback() raise ValidationError(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) log.debug('Group %s is made child of group %s', group.name, parent_group.name) 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 } logic.get_action('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)
def _group_or_org_update(context, data_dict, is_org=False): model = context['model'] user = context['user'] session = context['session'] id = _get_or_bust(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, 'context': context}) except AttributeError: schema = group_plugin.form_to_db_schema() if is_org: _check_access('organization_update', context, data_dict) else: _check_access('group_update', 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: group_plugin.check_data_dict(data_dict, schema) except TypeError: group_plugin.check_data_dict(data_dict) data, errors = _validate(data_dict, schema, context) log.debug('group_update validate_errs=%r user=%s group=%s data_dict=%r', errors, context.get('user'), context.get('group').name if context.get('group') else '', data_dict) if errors: session.rollback() raise ValidationError(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") # when editing an org we do not want to update the packages if using the # new templates. if ((not is_org) and not converters.asbool( config.get('ckan.legacy_templates', False)) and 'api_version' not in context): context['prevent_packages_update'] = True 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() if current: log.debug('Parents of group %s deleted: %r', group.name, [membership.group.name for membership in current]) for c in current: session.delete(c) member = model.Member(group=parent_group, table_id=group.id, table_name='group') session.add(member) log.debug('Group %s is made child of group %s', group.name, parent_group.name) if is_org: plugin_type = plugins.IOrganizationController else: plugin_type = plugins.IGroupController for item in plugins.PluginImplementations(plugin_type): item.edit(group) if is_org: activity_type = 'changed organization' else: activity_type = 'changed group' activity_dict = { 'user_id': model.User.by_name(user.decode('utf8')).id, 'object_id': group.id, 'activity_type': activity_type, } # 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': dictization.table_dictize(group, context) } activity_create_context = { 'model': model, 'user': user, 'defer_commit': True, 'ignore_auth': True, 'session': session } _get_action('activity_create')(activity_create_context, activity_dict) # 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)
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, 'context': 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)
def _index_template(self, group_type): from ckan.lib.helpers import default_group_type return lookup_group_plugin(group_type).index_template()