Example #1
0
async def get_all_types(context, request):
    result = []
    base_url = IAbsoluteURL(context, request)()
    constrains = ICMSConstrainTypes(context, None)

    policy = get_security_policy()

    for id, factory in FACTORY_CACHE.items():
        add = True
        if constrains is not None:
            if not constrains.is_type_allowed(id):
                add = False

        if factory.add_permission:
            if factory.add_permission in PERMISSIONS_CACHE:
                permission = PERMISSIONS_CACHE[factory.add_permission]
            else:
                permission = query_utility(IPermission, name=factory.add_permission)
                PERMISSIONS_CACHE[factory.add_permission] = permission

            if permission is not None and not policy.check_permission(
                permission.id, context
            ):
                add = False
        if add:
            result.append(
                {"@id": base_url + "/@types/" + id, "addable": True, "title": id}
            )
    return result
Example #2
0
async def get_block_schema(context, request):
    key = request.matchdict["key"]
    vocabulary_registry = getVocabularyRegistry()
    try:
        vocab = vocabulary_registry.get(context, key)
    except VocabularyRegistryError:
        return HTTPNotFound()

    title_filter = request.query.get("title")
    if title_filter:
        title_filter = title_filter.lower()
    token_filter = request.query.get("token")
    if token_filter:
        token_filter = token_filter.lower()

    result = {}
    result["@id"] = join(IAbsoluteURL(context)(), "@vocabularies", key)
    result["items"] = []
    for term in vocab.keys():
        if token_filter and token_filter not in str(term).lower():
            continue
        new_title = vocab.getTerm(term)
        if title_filter and title_filter not in str(new_title).lower():
            continue
        result["items"].append({"title": new_title, "token": term})
    result["items_total"] = len(result["items"])
    return result
Example #3
0
    async def clone_request(self, method, endpoint, payload, headers):
        container = task_vars.container.get()
        container_url = IAbsoluteURL(container, self.request)()
        url = posixpath.join(container_url, endpoint)
        parsed = urlparse(url)
        raw_headers = tuple(
            (k.encode("utf-8"), v.encode("utf-8")) for k, v in headers.items())
        request = self.request.__class__(
            self.request.scheme,
            method,
            parsed.path,
            parsed.query.encode("utf-8"),
            raw_headers,
            client_max_size=self.request._client_max_size,
            send=self.request.send,
            receive=self.request.receive,
            scope=self.request.scope,
        )
        request._state = self.request._state.copy()
        request._read_bytes = payload

        registry = await get_registry(container)
        layers = registry.get(ACTIVE_LAYERS_KEY, [])
        for layer in layers:
            try:
                alsoProvides(request, import_class(layer))
            except ModuleNotFoundError:
                pass
        return request
 def _get_items_from_result(self, container, request, result):
     items = []
     container_url = IAbsoluteURL(container, request)()
     for item in result['hits']['hits']:
         data = item.pop('_source', {})
         for key, val in item.get('fields', {}).items():
             container_data = data
             if isinstance(val, list):
                 if len(val) == 1:
                     val = val[0]
                 else:
                     val = None
             if '.' in key:
                 name, key = key.split('.', 1)
                 if name not in container_data:
                     container_data[name] = {}
                 container_data = container_data[name]
             container_data[key] = val
         data.update({
             '@absolute_url': container_url + data.get('path', ''),
             '@type': data.get('type_name'),
             '@uid': item['_id'],
             '@name': data.get('id', data.get('path', '').split('/')[-1])
         })
         sort_value = item.get('sort')
         if sort_value:
             data.update({'sort': sort_value})
         items.append(data)
     return items
Example #5
0
    async def __call__(self):

        summary = json_compatible({
            '@id': IAbsoluteURL(self.context)(),
            '@type': self.context.type_name
        })
        return summary
Example #6
0
    async def __call__(self):
        parent = self.context.__parent__
        if parent is not None:
            # We render the summary of the parent
            try:
                parent_summary = await getMultiAdapter(
                    (parent, self.request), IResourceSerializeToJsonSummary)()
            except ComponentLookupError:
                parent_summary = {}
        else:
            parent_summary = {}

        result = {
            '@id': IAbsoluteURL(self.context, self.request)(),
            '@type': self.context.type_name,
            'parent': parent_summary,
            'creation_date': json_compatible(self.context.creation_date),
            'modification_date':
            json_compatible(self.context.modification_date),
            'UID': self.context.uuid,
        }

        factory = get_cached_factory(self.context.type_name)

        main_schema = factory.schema
        await self.get_schema(main_schema, self.context, result, False)

        for behavior_schema, behavior in await get_all_behaviors(self.context):
            await self.get_schema(behavior_schema, behavior, result, True)

        return result
Example #7
0
    async def __call__(self):
        obj_url = IAbsoluteURL(self.context, self.request)()

        workflow = {
            "@id": obj_url + "/@workflow",
            "history": [],
            "transitions": []
        }

        workflow_obj = IWorkflow(self.context)
        if workflow_obj is None:
            return workflow
        async for action_name, action in workflow_obj.available_actions(
                self.request):
            workflow["transitions"].append({
                "@id": obj_url + "/@workflow/" + action_name,
                "title": action["title"]
            })

        workflow_obj = query_adapter(self.context, IWorkflowBehavior)
        if workflow_obj is not None:
            await workflow_obj.load()
            workflow["history"] = workflow_obj.history

        return workflow
Example #8
0
async def get_all_types(context, request):
    result = []
    base_url = IAbsoluteURL(context, request)()
    constrains = IConstrainTypes(context, None)

    for id, factory in FACTORY_CACHE.items():
        add = True
        if constrains is not None:
            if not constrains.is_type_allowed(id):
                add = False

        if factory.add_permission:
            if factory.add_permission in PERMISSIONS_CACHE:
                permission = PERMISSIONS_CACHE[factory.add_permission]
            else:
                permission = query_utility(IPermission,
                                           name=factory.add_permission)
                PERMISSIONS_CACHE[factory.add_permission] = permission

            if permission is not None and \
                    not IInteraction(request).check_permission(
                        permission.id, context):
                add = False
        if add:
            result.append({
                '@id': base_url + '/@types/' + id,
                'addable': True,
                'title': id
            })
    return result
Example #9
0
async def get_blocks(context, request):
    result = []
    for key, item in app_settings['available_blocks'].items():
        result.append({
            "@id": join(IAbsoluteURL(context)(), "@blocks", item["name"]),
            "title": item['title'],
            "description": item['description']
        })
    return result
Example #10
0
    async def __call__(self, include=[], omit=[]):
        self.include = include
        self.omit = omit

        parent = self.context.__parent__
        if parent is not None:
            # We render the summary of the parent
            try:
                parent_summary = await get_multi_adapter(
                    (parent, self.request), IResourceSerializeToJsonSummary)()
            except ComponentLookupError:
                parent_summary = {}
        else:
            parent_summary = {}

        result = {
            '@id': IAbsoluteURL(self.context, self.request)(),
            '@type': self.context.type_name,
            '@name': self.context.__name__,
            '@uid': self.context.uuid,
            'parent': parent_summary,
            'is_folderish': IFolder.providedBy(self.context),
            'creation_date': json_compatible(self.context.creation_date),
            'modification_date':
            json_compatible(self.context.modification_date),
            'UID': self.context.uuid,
        }

        factory = get_cached_factory(self.context.type_name)

        main_schema = factory.schema
        await self.get_schema(main_schema, self.context, result, False)

        # include can be one of:
        # - <field name> on content schema
        # - namespace.IBehavior
        # - namespace.IBehavior.field_name
        included_ifaces = [name for name in self.include if '.' in name]
        included_ifaces.extend(
            [name.rsplit('.', 1)[0] for name in self.include if '.' in name])
        for behavior_schema, behavior in await get_all_behaviors(self.context,
                                                                 load=False):
            dotted_name = behavior_schema.__identifier__
            if (dotted_name in self.omit
                    or (len(included_ifaces) > 0
                        and dotted_name not in included_ifaces)):
                # make sure the schema isn't filtered
                continue
            if (not getattr(behavior, 'auto_serialize', True)
                    and dotted_name not in included_ifaces):
                continue
            if IAsyncBehavior.implementedBy(behavior.__class__):
                # providedBy not working here?
                await behavior.load(create=False)
            await self.get_schema(behavior_schema, behavior, result, True)

        return result
Example #11
0
 async def __call__(self):
     result = []
     container = self.request.container
     async for content in container.async_values():
         if IResource.providedBy(content):
             result.append({
                 'title': content.title,
                 '@id': IAbsoluteURL(content, self.request)()
             })
     return {"@id": self.request.url.human_repr(), "items": result}
Example #12
0
    async def __call__(self):

        summary = json_compatible({
            '@id': IAbsoluteURL(self.context)(),
            '@name': self.context.__name__,
            '@type': self.context.type_name,
            '@uid': self.context.uuid,
            'UID': self.context.uuid
        })
        return summary
Example #13
0
    async def tus_create(self):
        try:
            self.field.context.data._p_register()  # register change...
        except AttributeError:
            self.context._p_register()

        # This only happens in tus-java-client, redirect this POST to a PATCH
        if self.request.headers.get('X-HTTP-Method-Override') == 'PATCH':
            return await self.tus_patch()

        file = self.field.get(self.field.context or self.context)
        if not isinstance(file, self.file_class):
            file = self.file_class(content_type=self.request.content_type)
            self.field.set(self.field.context or self.context, file)
        if 'CONTENT-LENGTH' in self.request.headers:
            file._current_upload = int(self.request.headers['CONTENT-LENGTH'])
        else:
            file._current_upload = 0
        if 'UPLOAD-LENGTH' in self.request.headers:
            file._size = int(self.request.headers['UPLOAD-LENGTH'])
        else:
            raise AttributeError('We need upload-length header')

        if 'UPLOAD-MD5' in self.request.headers:
            file._md5 = self.request.headers['UPLOAD-MD5']

        if 'UPLOAD-EXTENSION' in self.request.headers:
            file._extension = self.request.headers['UPLOAD-EXTENSION']

        if 'TUS-RESUMABLE' not in self.request.headers:
            raise AttributeError('TUS needs a TUS version')

        if 'UPLOAD-METADATA' not in self.request.headers:
            file.filename = uuid.uuid4().hex
        else:
            filename = self.request.headers['UPLOAD-METADATA']
            file.filename = base64.b64decode(
                filename.split()[1]).decode('utf-8')

        file._resumable_uri_date = datetime.now(tz=tzutc())

        await file.init_upload(self.context)
        # Location will need to be adapted on aiohttp 1.1.x
        resp = Response(
            headers={
                'Location':
                IAbsoluteURL(self.context, self.request)() + '/@tusupload/' +
                self.field.__name__,  # noqa
                'Tus-Resumable':
                '1.0.0',
                'Access-Control-Expose-Headers':
                'Location,Tus-Resumable'
            },
            status=201)
        return resp
Example #14
0
    async def __call__(self):

        summary = json_compatible({
            "@id": IAbsoluteURL(self.context)(),
            "@type": self.context.type_name,
            "@name": self.context.__name__,
            "@uid": self.context.uuid,
            "UID": self.context.uuid,
            "title": self.context.title,
        })
        return summary
Example #15
0
async def get_vocabularies(context, request):
    result = []
    vocabulary_registry = getVocabularyRegistry()
    for key, item in vocabulary_registry._map.items():
        result.append({
            "@id":
            join(IAbsoluteURL(context)(), "@vocabularies", key),
            "title":
            key
        })
    return result
Example #16
0
 def get_task_payload(self):
     name = self.__parent__.__name__
     function = app_settings['hive_tasks'][name]
     if isinstance(function, dict) and 'klass' in function:
         function = get_dotted_name(function['klass'])
     return {
         'name': name,
         'task_uri': IAbsoluteURL(self)(relative=True),
         'function': function,
         'args': self.params,
         'persistent': True
     }
Example #17
0
    async def __call__(self):
        result = []
        context = self.context
        while context is not None and not IDatabase.providedBy(context):
            result.append({
                'title': context.title,
                '@id': IAbsoluteURL(context, self.request)()
            })
            context = getattr(context, '__parent__', None)
        result.reverse()

        return {"@id": self.request.url.human_repr(), "items": result}
Example #18
0
    async def __call__(self):
        result = []
        context = self.context
        while context is not None and not ISite.providedBy(context):
            result.append({
                "title": context.title,
                "@id": IAbsoluteURL(context, self.request)()
            })
            context = getattr(context, "__parent__", None)
        result.reverse()

        return {"@id": self.request.url, "items": result}
Example #19
0
 async def __call__(self):
     if not hasattr(self, 'value'):
         workflow = {
             'history': [],
             'transitions': []
         }
     else:
         obj_url = IAbsoluteURL(self.context, self.request)()
         workflow = {
             '@id': obj_url + '/@workflow/' + self.workflow_id,
             'items': await self.value()
         }
     return workflow
Example #20
0
async def get_block_schema(context, request):
    key = request.matchdict['key']
    vocabulary_registry = getVocabularyRegistry()
    try:
        vocab = vocabulary_registry.get(context, key)
    except VocabularyRegistryError:
        return HTTPNotFound()

    result = {}
    result['@id'] = join(IAbsoluteURL(context)(), "@vocabularies", key)
    result['items'] = []
    for term in vocab.keys():
        result['items'].append({'title': vocab.getTerm(term), 'token': term})
    return result
Example #21
0
    async def clone_request(self, method, endpoint, payload, headers):
        container_url = IAbsoluteURL(self.request.container, self.request)()
        url = posixpath.join(container_url, endpoint)
        parsed = urlparse(url)
        dct = {'method': method, 'url': URL(url), 'path': parsed.path}
        dct['headers'] = CIMultiDict(headers)
        dct['raw_headers'] = tuple(
            (k.encode('utf-8'), v.encode('utf-8')) for k, v in headers.items())

        message = self.request._message._replace(**dct)

        payload_writer = mock.Mock()
        payload_writer.write_eof.side_effect = noop
        payload_writer.drain.side_effect = noop

        protocol = mock.Mock()
        protocol.transport = test_utils._create_transport(None)
        protocol.writer = payload_writer

        request = self.request.__class__(
            message,
            SimplePayload(payload),
            protocol,
            payload_writer,
            self.request._task,
            self.request._loop,
            client_max_size=self.request._client_max_size,
            state=self.request._state.copy(),
            scheme=self.request.scheme,
            host=self.request.host,
            remote=self.request.remote)

        request._db_write_enabled = True
        request._db_id = self.request._db_id
        request._tm = self.request._tm
        request._txn = self.request._txn

        request._container_id = self.context.id
        request.container = self.context
        annotations_container = IAnnotations(self.context)
        request.container_settings = await annotations_container.async_get(
            REGISTRY_DATA_KEY)
        layers = request.container_settings.get(ACTIVE_LAYERS_KEY, [])
        for layer in layers:
            try:
                alsoProvides(request, import_class(layer))
            except ModuleNotFoundError:
                pass
        request._futures = self.request._futures
        return request
Example #22
0
 def _get_items_from_result(self, container, request, result):
     items = []
     container_url = IAbsoluteURL(container, request)()
     for item in result['hits']['hits']:
         data = format_hit(item)
         data.update({
             '@absolute_url': container_url + data.get('path', ''),
             '@type': data.get('type_name'),
             '@uid': item['_id'],
             '@name': data.get('id', data.get('path', '').split('/')[-1])
         })
         sort_value = item.get('sort')
         if sort_value:
             data.update({'sort': sort_value})
         items.append(data)
     return items
Example #23
0
async def get_tile_schema(context, request):
    key = request.matchdict['key']
    vocabulary_registry = getVocabularyRegistry()
    try:
        vocab = vocabulary_registry.get(context, key)
    except VocabularyRegistryError:
        return HTTPNotFound()

    result = {}
    for term in vocab.keys():
        result[term] = {
            '@id': join(IAbsoluteURL(context)(), "@vocabularies", key, term),
            'title': vocab.getTerm(term),
            'token': term
        }
    return result
Example #24
0
async def get_all_indices(context, request):
    base_url = IAbsoluteURL(context, request)()
    result = {"@id": base_url, "types": {}, "behaviors": {}}
    for type_name, type_schema in FACTORY_CACHE.items():
        indices = merged_tagged_value_dict(type_schema.schema, index.key)
        result["types"][type_name] = {
            key: value["type"]
            for key, value in indices.items()
        }  # noqa

    for behavior, utility in get_utilities_for(IBehavior):
        indices = merged_tagged_value_dict(utility.interface, index.key)
        result["behaviors"][behavior] = {
            key: value["type"]
            for key, value in indices.items()
        }  # noqa
    return result
Example #25
0
def create_apply_task(name, ob, function, request=None, commit=False,
                      args=None, kwargs=None, task_config=None):
    if task_config is None:
        task_config = {}
    if args is None:
        args = []
    if kwargs is None:
        kwargs = {}
    from guillotina_hive.model.task import Task

    if request is None:
        request = get_current_request()

    user_data = {}
    try:
        participation = request.security.participations[0]
        user = participation.principal
        user_data = {
            'id': user.id,
            'roles': user.roles,
            'groups': user.groups,
            'Authorization': request.headers.get('Authorization'),
            'data': getattr(user, 'data', {})
        }
    except (AttributeError, IndexError):
        pass

    if request.container:
        user_data['container_url'] = IAbsoluteURL(request.container, request)()

    data = {
        "name": name,
        "guillotina": True,
        "args": {
            "path": get_full_content_path(request, ob),
            "function": get_dotted_name(function),
            'commit': commit,
            'args': args,
            'kwargs': kwargs,
            'user_data': user_data
        }
    }
    data.update(task_config)
    task_info = Task(data=data)
    return task_info
Example #26
0
    async def __call__(self):
        parent = self.context.__parent__
        if parent is not None:
            # We render the summary of the parent
            try:
                parent_summary = await getMultiAdapter(
                    (parent, self.request), IResourceSerializeToJsonSummary)()
            except ComponentLookupError:
                parent_summary = {}
        else:
            parent_summary = {}

        result = {
            '@id': IAbsoluteURL(self.context, self.request)(),
            '@type': self.context.portal_type,
            'parent': parent_summary,
            'created': json_compatible(self.context.created),
            'modified': json_compatible(self.context.modified),
            'UID': self.context.uuid,
        }

        factory = get_cached_factory(self.context.portal_type)

        main_schema = factory.schema
        await self.get_schema(main_schema, self.context, result, False)

        for behavior_schema in factory.behaviors or ():
            behavior = behavior_schema(self.context)
            if IAsyncBehavior.implementedBy(behavior.__class__):
                # providedBy not working here?
                await behavior.load()
            await self.get_schema(behavior_schema, behavior, result, True)

        for dynamic_behavior in self.context.__behaviors__ or ():
            dynamic_behavior_obj = BEHAVIOR_CACHE[dynamic_behavior]
            behavior = dynamic_behavior_obj(self.context)
            if IAsyncBehavior.implementedBy(dynamic_behavior_obj.__class__):
                # providedBy not working here?
                await behavior.load()
            await self.get_schema(dynamic_behavior_obj, behavior, result, True)

        return result
Example #27
0
async def sharing_get(context, request):
    roleperm = IRolePermissionMap(context)
    prinperm = IPrincipalPermissionMap(context)
    prinrole = IPrincipalRoleMap(context)
    result = {'local': {}, 'inherit': []}
    result['local']['roleperm'] = roleperm._bycol
    result['local']['prinperm'] = prinperm._bycol
    result['local']['prinrole'] = prinrole._bycol
    for obj in iter_parents(context):
        roleperm = IRolePermissionMap(obj, None)
        if roleperm is not None:
            prinperm = IPrincipalPermissionMap(obj)
            prinrole = IPrincipalRoleMap(obj)
            result['inherit'].append({
                '@id': IAbsoluteURL(obj, request)(),
                'roleperm': roleperm._bycol,
                'prinperm': prinperm._bycol,
                'prinrole': prinrole._bycol,
            })
    await notify(ObjectPermissionsViewEvent(context))
    return result
Example #28
0
    async def __call__(self):
        obj_url = IAbsoluteURL(self.context, self.request)()

        workflow = {
            '@id': obj_url + '/@workflow',
            'history': [],
            'transitions': []
        }

        workflow_obj = IWorkflow(self.context)
        async for action_name, action in workflow_obj.available_actions(
                self.request):
            workflow['transitions'].append({
                '@id': obj_url + '/@workflow/' + action_name,
                'title': action['title']
            })

        cms_obj = ICMSBehavior(self.context)
        await cms_obj.load()
        workflow['history'] = cms_obj.history

        return workflow
Example #29
0
def serialize_request(request: IRequest) -> SerializedRequest:
    """Serializes request data so that it can be sent to rabbitmq
    """
    req_data = {
        "url": str(request.url),
        "headers": dict(request.headers),
        "method": request.method,
        "annotations": getattr(request, "annotations", {}),
    }
    user = get_authenticated_user()
    if user is not None:
        try:
            req_data["user"] = {
                "id":
                user.id,
                "roles": [
                    name
                    for name, setting in user.roles.items()  # type: ignore
                    if setting == Allow
                ],
                "groups":
                user.groups,
                "headers":
                dict(request.headers),
                "data":
                getattr(user, "data", {}),
            }
        except AttributeError:
            pass

    container = task_vars.container.get()
    if container is not None:
        req_data["container_url"] = IAbsoluteURL(container,
                                                 request)()  # type: ignore

    return cast(SerializedRequest, req_data)
Example #30
0
    async def tus_create(self, *args, **kwargs):
        await self.dm.load()
        # This only happens in tus-java-client, redirect this POST to a PATCH
        if self.request.headers.get('X-HTTP-Method-Override') == 'PATCH':
            return await self.tus_patch()

        md5 = extension = size = None

        deferred_length = False
        if self.request.headers.get('Upload-Defer-Length') == '1':
            deferred_length = True

        if 'UPLOAD-LENGTH' in self.request.headers:
            size = int(self.request.headers['UPLOAD-LENGTH'])
        else:
            if not deferred_length:
                raise HTTPPreconditionFailed(
                    content={'reason': 'We need upload-length header'})

        if 'UPLOAD-MD5' in self.request.headers:
            md5 = self.request.headers['UPLOAD-MD5']

        if 'UPLOAD-EXTENSION' in self.request.headers:
            extension = self.request.headers['UPLOAD-EXTENSION']

        if 'TUS-RESUMABLE' not in self.request.headers:
            raise HTTPPreconditionFailed(
                content={'reason': 'TUS needs a TUS version'})

        if 'X-UPLOAD-FILENAME' in self.request.headers:
            filename = self.request.headers['X-UPLOAD-FILENAME']
        elif 'UPLOAD-FILENAME' in self.request.headers:
            filename = self.request.headers['UPLOAD-FILENAME']
        elif 'UPLOAD-METADATA' not in self.request.headers:
            filename = uuid.uuid4().hex
        else:
            filename = self.request.headers['UPLOAD-METADATA']
            filename = base64.b64decode(filename.split()[1]).decode('utf-8')
        if extension is None and '.' in filename:
            extension = filename.split('.')[-1]

        await self.dm.start()
        await self.dm.update(content_type=self.request.content_type,
                             md5=md5,
                             filename=filename,
                             extension=extension,
                             size=size,
                             deferred_length=deferred_length,
                             offset=0)

        await self.file_storage_manager.start(self.dm)
        await self.dm.save()

        return Response(
            status=201,
            headers={
                'Location':
                posixpath.join(
                    IAbsoluteURL(self.context, self.request)(), '@tusupload',
                    self.field.__name__),  # noqa
                'Tus-Resumable':
                '1.0.0',
                'Access-Control-Expose-Headers':
                'Location,Tus-Resumable'
            })