def post(self) -> dict: """Create new resource and get response data.""" metric = self._get_post_metric_name() with statsd_timer(metric, rate=1, registry=self.registry): resource = self._create() cstruct = self.build_post_response(resource) return cstruct
def post(self): """Create new resource and get response data. For :class:`adhocracy_core.interfaces.IItemVersion`: If a `new version` is already created in this transaction we don't want to create a new one. Instead we modify the existing one. This is needed to make :class:`adhocray_core.rest.batchview.BatchView` work. """ metric = self._get_post_metric_name() with statsd_timer(metric, rate=1, registry=self.registry): if is_batchmode(self.request) and self._creating_new_version(): last = self.registry.content.get_sheet_field(self.context, ITags, 'LAST') if is_created_in_current_transaction(last, self.registry): self._update_version(last) resource = last else: resource = self._create() else: resource = self._create() cstruct = self.build_post_response(resource) return cstruct
def groups_and_roles_finder(userid: str, request: Request) -> list: """A Pyramid authentication policy groupfinder callback.""" with statsd_timer('authentication.groups', rate=.1): userlocator = request.registry.getMultiAdapter( (request.context, request), IRolesUserLocator) groupids = userlocator.get_groupids(userid) or [] roleids = userlocator.get_role_and_group_roleids(userid) or [] return groupids + roleids
def get(self) -> dict: """Get resource data (unless deleted or hidden).""" metric = self._get_get_metric_name() with statsd_timer(metric, rate=.1, registry=self.registry): schema = GETResourceResponseSchema().bind(request=self.request, context=self.context) cstruct = schema.serialize() cstruct['data'] = self._get_sheets_data_cstruct() return cstruct
def groups_and_roles_finder(userid: str, request: Request) -> list: """A Pyramid authentication policy groupfinder callback.""" with statsd_timer('authentication.groups', rate=.1): userlocator = request.registry.getMultiAdapter((request.context, request), IRolesUserLocator) groupids = userlocator.get_groupids(userid) or [] roleids = userlocator.get_role_and_group_roleids(userid) or [] return groupids + roleids
def permits(self, context: IResource, principals: list, permission: str) -> ACLPermitsResult: """Check `permission` for `context`. Read interface docstring.""" with statsd_timer('authorization', rate=.1): if _is_creator(context, principals): principals += [CREATOR_ROLEID] allow = super().permits(context, principals, permission) return allow
def permits(self, context: IResource, principals: list, permission: str) -> ACLPermitsResult: """Check `permission` for `context`. Read interface docstring.""" with statsd_timer('authorization', rate=.1): local_roles = get_local_roles_all(context) principals_with_roles = set(principals) for principal, roles in local_roles.items(): if principal in principals: principals_with_roles.update(roles) return super().permits(context, principals_with_roles, permission)
def get(self) -> dict: """Get resource data.""" with statsd_timer('process.get', rate=.1, registry=self.registry): schema = GETItemResponseSchema().bind(request=self.request, context=self.context) appstruct = {} first_version = self._get_first_version(self.context) if first_version is not None: appstruct['first_version_path'] = first_version cstruct = schema.serialize(appstruct) cstruct['data'] = self._get_sheets_data_cstruct() return cstruct
def _get_authenticated_user_id(self, request: Request, tokenmanager: ITokenManger) -> str: userid, token = _get_x_user_headers(request) settings = request.registry.settings if not asbool(settings.get('adhocracy.validate_user_token', True)): return userid if token is None: raise KeyError with statsd_timer('authentication.user', rate=.1): authenticated_userid = \ tokenmanager.get_user_id(token, timeout=self.timeout) if authenticated_userid != userid: raise KeyError return authenticated_userid
def post(self) -> dict: """Create new resource and get response data.""" iresource = self.request.validated['content_type'] resource_type = iresource.__identifier__ appstructs = self.request.validated.get('data', {}) creator = get_user(self.request) metric = self._get_post_metric_name(iresource) with statsd_timer(metric, rate=1, registry=self.registry): resource = self.content.create(resource_type, self.context, creator=creator, appstructs=appstructs, request=self.request, ) return self.build_post_response(resource)
def get(self) -> dict: """Get resource data.""" with statsd_timer('process.get', rate=.1, registry=self.registry): first_version = self.registry.content.get_sheet_field(self.context, ITags, 'FIRST') appstruct = {} if first_version is not None: appstruct['first_version_path'] = first_version schema = create_schema(GETItemResponseSchema, self.context, self.request) cstruct = schema.serialize(appstruct) cstruct['data'] = self._get_sheets_data_cstruct() return cstruct
def post(self) -> dict: """Create new resource and get response data.""" iresource = self.request.validated['content_type'] resource_type = iresource.__identifier__ appstructs = self.request.validated.get('data', {}) creator = get_user(self.request) metric = self._get_post_metric_name(iresource) with statsd_timer(metric, rate=1, registry=self.registry): resource = self.content.create( resource_type, self.context, creator=creator, appstructs=appstructs, request=self.request, ) return self.build_post_response(resource)
def __init__(self, context, request): """Initialize self.""" self.context = context """Context Resource.""" self.request = request """:class:`pyramid.request.Request`.""" self.registry = request.registry """:class:`pyramid.registry.Registry`.""" with statsd_timer('validate', rate=.1, registry=self.registry): respond_if_blocked(context, request) set_cache_header(context, request) schema_class, validators = _get_schema_and_validators(self, request) validate_request_data(context, request, schema=schema_class(), extra_validators=validators)
def __init__(self, context, request): """Initialize self.""" self.context = context """Context Resource.""" self.request = request """:class:`pyramid.request.Request`.""" self.registry = request.registry """:class:`pyramid.registry.Registry`.""" with statsd_timer('validate', rate=.1, registry=self.registry): respond_if_blocked(context, request) set_cache_header(context, request) schema_class, validators = _get_schema_and_validators( self, request) validate_request_data(context, request, schema=schema_class(), extra_validators=validators)
def put(self) -> dict: """Edit resource and get response data.""" with statsd_timer('process.put', rate=.1, registry=self.registry): sheets = self.content.get_sheets_edit(self.context, self.request) appstructs = self.request.validated.get('data', {}) for sheet in sheets: name = sheet.meta.isheet.__identifier__ if name in appstructs: sheet.set(appstructs[name]) appstruct = {} if not is_batchmode(self.request): # pragma: no branch updated = _build_updated_resources_dict(self.registry) appstruct['updated_resources'] = updated schema = create_schema(ResourceResponseSchema, self.context, self.request) cstruct = schema.serialize(appstruct) return cstruct
def post(self): """Create new resource and get response data. For :class:`adhocracy_core.interfaces.IItemVersion`: If a `new version` is already created in this transaction we don't want to create a new one. Instead we modify the existing one. This is needed to make :class:`adhocray_core.rest.batchview.BatchView` work. """ batchmode = is_batchmode(self.request) validated = self.request.validated iresource = validated['content_type'] resource_type = iresource.__identifier__ appstructs = validated.get('data', {}) creator = get_user(self.request) root_versions = validated.get('root_versions', []) last_new_version = validated.get('_last_new_version_in_transaction', None) metric = self._get_post_metric_name(iresource) with statsd_timer(metric, rate=1, registry=self.registry): if last_new_version is not None: # only happens in batch request sheets = self.content.get_sheets_create( last_new_version, self.request) appstructs = self.request.validated.get('data', {}) for sheet in sheets: name = sheet.meta.isheet.__identifier__ if name in appstructs: # pragma: no branch sheet.set(appstructs[name], request=self.request) resource = last_new_version else: resource = self.content.create( resource_type, self.context, appstructs=appstructs, creator=creator, root_versions=root_versions, request=self.request, is_batchmode=batchmode, ) return self.build_post_response(resource)
def post(self): """Create new resource and get response data. For :class:`adhocracy_core.interfaces.IItemVersion`: If a `new version` is already created in this transaction we don't want to create a new one. Instead we modify the existing one. This is needed to make :class:`adhocray_core.rest.batchview.BatchView` work. """ batchmode = is_batchmode(self.request) validated = self.request.validated iresource = validated['content_type'] resource_type = iresource.__identifier__ appstructs = validated.get('data', {}) creator = get_user(self.request) root_versions = validated.get('root_versions', []) last_new_version = validated.get('_last_new_version_in_transaction', None) metric = self._get_post_metric_name(iresource) with statsd_timer(metric, rate=1, registry=self.registry): if last_new_version is not None: # only happens in batch request sheets = self.content.get_sheets_create(last_new_version, self.request) appstructs = self.request.validated.get('data', {}) for sheet in sheets: name = sheet.meta.isheet.__identifier__ if name in appstructs: # pragma: no branch sheet.set(appstructs[name], request=self.request) resource = last_new_version else: resource = self.content.create(resource_type, self.context, appstructs=appstructs, creator=creator, root_versions=root_versions, request=self.request, is_batchmode=batchmode, ) return self.build_post_response(resource)
def get(self) -> dict: """Get the API specification of this installation as JSON.""" # Collect info about all resources with statsd_timer('process.get.metaapi', rate=.1, registry=self.registry): resources_meta = self.request.registry.content.resources_meta resource_map = self._describe_resources(resources_meta) # Collect info about all sheets referenced by any of the resources sheet_metadata = self.request.registry.content.sheets_meta sheet_map = self._describe_sheets(sheet_metadata) workflows_meta = self.request.registry.content.workflows_meta workflows_map = self._describe_workflows(workflows_meta) struct = {'resources': resource_map, 'sheets': sheet_map, 'workflows': workflows_map, } return struct
def put(self) -> dict: """Edit resource and get response data.""" with statsd_timer('process.put', rate=.1, registry=self.registry): sheets = self.content.get_sheets_edit(self.context, self.request) appstructs = self.request.validated.get('data', {}) for sheet in sheets: name = sheet.meta.isheet.__identifier__ if name in appstructs: sheet.set(appstructs[name], request=self.request) appstruct = {} if not is_batchmode(self.request): # pragma: no branch appstruct[ 'updated_resources'] = self._build_updated_resources_dict( ) schema = ResourceResponseSchema().bind(request=self.request, context=self.context) cstruct = schema.serialize(appstruct) return cstruct
def get(self) -> dict: """Get the API specification of this installation as JSON.""" # Collect info about all resources with statsd_timer('process.get.metaapi', rate=.1, registry=self.registry): resources_meta = self.request.registry.content.resources_meta resource_map = self._describe_resources(resources_meta) # Collect info about all sheets referenced by any of the resources sheet_metadata = self.request.registry.content.sheets_meta sheet_map = self._describe_sheets(sheet_metadata) workflows_meta = self.request.registry.content.workflows_meta workflows_map = self._describe_workflows(workflows_meta) struct = { 'resources': resource_map, 'sheets': sheet_map, 'workflows': workflows_map, } return struct
def options(self) -> dict: """Get possible request/response data structures and http methods.""" with statsd_timer('process.options', rate=.1, registry=self.registry): cstruct = self._options(self.context, self.request) return cstruct