def get_cstruct(self, request: Request, params: dict=None) -> dict: """Return cstruct data. Bind `request` and `self.context` to colander schema (self.schema). Get sheet appstruct data and serialize. :param request: Bind to schema and get principals to filter elements by 'view' permission. :param params: Parameters to update the search query to find `elements` For available values in `params` read the `get` method docstring. Automatically set params are: `only_visible` and `allows` read permission. Additional params are: serialization_form (str): serialization for references resources: `omit` returns an empty list, `url` the resource urls, `content` all resource data. Defaults to `path`. show_count (bool): add 'count` field, defaults to False. show_frequency (bool): add 'aggregateby` field. defaults to False. """ params = params or {} filter_view_permission = self.registry.settings.get( 'adhocracy.filter_by_view_permission', True) if filter_view_permission: params['allows'] = (request.effective_principals, 'view') filter_visible = self.registry.settings.get( 'adhocracy.filter_by_visible', True) if filter_visible: params['only_visible'] = True params_query = remove_keys_from_dict(params, self._additional_params) appstruct = self.get(params=params_query) if params.get('serialization_form', False) == 'omit': appstruct['elements'] = [] if params.get('show_frequency', False): index_name = params.get('frequency_of', '') frequency = appstruct['frequency_of'] appstruct['aggregateby'] = {index_name: frequency} # TODO: rename aggregateby in frequency_of schema = self._get_schema_for_cstruct(request, params) cstruct = schema.serialize(appstruct) return cstruct
def serialize(self, params: dict=None) -> dict: """Get sheet appstruct data and serialize. Extends :func:`adhocracy_core.interfaces.IResourceSheet.serialize` with the following parameters: serialization_form (str): serialization for references resources: `omit` returns an empty list, `url` the resource urls, `content` all resource data. Defaults to `path`. show_count (bool): add 'count` field, defaults to True. show_frequency (bool): add 'aggregateby` field. defaults to False. """ params = params or {} has_custom_filters = params != {} filter_view_permission = asbool(self.registry.settings.get( 'adhocracy.filter_by_view_permission', True)) if filter_view_permission: params['allows'] = (self.request.effective_principals, 'view') filter_visible = asbool(self.registry.settings.get( 'adhocracy.filter_by_visible', True)) if filter_visible: params['only_visible'] = True params_query = remove_keys_from_dict(params, self._additional_params) appstruct = self.get(params=params_query, omit_readonly=True) if not has_custom_filters and self.meta.isheet is IPool: # workaround to reduce needless but expensive listing of elements params['serialization_form'] = 'omit' params['show_count'] = True if params.get('serialization_form', False) == 'omit': appstruct['elements'] = [] if params.get('show_frequency', False): index_name = params.get('frequency_of', '') frequency = appstruct['frequency_of'] appstruct['aggregateby'] = {index_name: frequency} # TODO: rename aggregateby in frequency_of schema = self.get_schema_with_bindings() schema = self._add_additional_nodes(schema, params) cstruct = schema.serialize(appstruct) return cstruct
def get_cstruct(self, request: Request, params: dict = None) -> dict: """Return cstruct data. Bind `request` and `self.context` to colander schema (self.schema). Get sheet appstruct data and serialize. :param request: Bind to schema and get principals to filter elements by 'view' permission. :param params: Parameters to update the search query to find `elements` For available values in `params` read the `get` method docstring. Automatically set params are: `only_visible` and `allows` read permission. Additional params are: serialization_form (str): serialization for references resources: `omit` returns an empty list, `url` the resource urls, `content` all resource data. Defaults to `path`. show_count (bool): add 'count` field, defaults to False. show_frequency (bool): add 'aggregateby` field. defaults to False. """ params = params or {} filter_view_permission = self.registry.settings.get( 'adhocracy.filter_by_view_permission', True) if filter_view_permission: params['allows'] = (request.effective_principals, 'view') filter_visible = self.registry.settings.get( 'adhocracy.filter_by_visible', True) if filter_visible: params['only_visible'] = True params_query = remove_keys_from_dict(params, self._additional_params) appstruct = self.get(params=params_query) if params.get('serialization_form', False) == 'omit': appstruct['elements'] = [] if params.get('show_frequency', False): index_name = params.get('frequency_of', '') frequency = appstruct['frequency_of'] appstruct['aggregateby'] = {index_name: frequency} # TODO: rename aggregateby in frequency_of schema = self._get_schema_for_cstruct(request, params) cstruct = schema.serialize(appstruct) return cstruct
def serialize(self, params: dict = None) -> dict: """Get sheet appstruct data and serialize. Extends :func:`adhocracy_core.interfaces.IResourceSheet.serialize` with the following parameters: serialization_form (str): serialization for references resources: `omit` returns an empty list, `url` the resource urls, `content` all resource data. Defaults to `path`. show_count (bool): add 'count` field, defaults to True. show_frequency (bool): add 'aggregateby` field. defaults to False. """ params = params or {} has_custom_filters = params != {} settings = self.registry['config'] if settings.adhocracy.filter_by_view_permission: params['allows'] = (self.request.effective_principals, 'view') if settings.adhocracy.filter_by_visible: params['only_visible'] = True params_query = remove_keys_from_dict(params, self._additional_params) appstruct = self.get(params=params_query, omit_readonly=True) if not has_custom_filters and self.meta.isheet is IPool: # workaround to reduce needless but expensive listing of elements params['serialization_form'] = 'omit' params['show_count'] = True if params.get('serialization_form', False) == 'omit': appstruct['elements'] = [] if params.get('show_frequency', False): index_name = params.get('frequency_of', '') frequency = appstruct['frequency_of'] appstruct['aggregateby'] = {index_name: frequency} # TODO: rename aggregateby in frequency_of schema = self.get_schema_with_bindings() schema = self._add_additional_nodes(schema, params) cstruct = schema.serialize(appstruct) return cstruct
def _omit_omit_keys(self, appstruct: dict, omit): omit_keys = normalize_to_tuple(omit) return remove_keys_from_dict(appstruct, keys_to_remove=omit_keys)
def _omit_readonly_keys(self, appstruct: dict): omit_keys = tuple(self._fields['readonly']) return remove_keys_from_dict(appstruct, keys_to_remove=omit_keys)
def test_remove_keys_from_dict_with_single_key_to_remove(): from adhocracy_core.utils import remove_keys_from_dict dictionary = {'key': 'value', 'other_key': 'value'} assert remove_keys_from_dict(dictionary, keys_to_remove='key')\ == {'other_key': 'value'}