Пример #1
0
def my_schema():
    """A test schema with."""
    class LinksSchema(Schema):
        self = fields.Link(
            template=URITemplate("/records{?params*}"),
            params=lambda o: {
                "params": {
                    # type is expected to show in the query string as type=A&type=B
                    "type": ["A", "B"],
                    "sort": "newest",
                    "subtype": ["1"],
                    "size": 10,
                }
            })
        publish = fields.Link(
            template=URITemplate("/admin{/pid}"),
            params=lambda o: {"pid": o.get("pid")},
            permission="admin",
        )
        prev = fields.Link(template=URITemplate("/prev"),
                           params=lambda o: {},
                           when=lambda o: o.get("allowed", True))

    class MySchema(Schema):
        links = fields.Links()

    factory = LinksFactory(host="localhost", config={"search": LinksSchema})

    return MySchema(context={
        "links_factory": factory,
        "links_namespace": "search"
    })
Пример #2
0
def test_links():
    """Test links factory with links field."""
    class EntitySchema(Schema):
        links = fields.Links()

    class ItemLinkSchema1(Schema):
        self = fields.Link(template=URITemplate("/1/{?pid}"),
                           params=lambda o: {'pid': o.get("pid")},
                           permission="read")

    class ItemLinkSchema2(Schema):
        self = fields.Link(template=URITemplate("/2/{?pid}"),
                           params=lambda o: {'pid': o.get("pid")},
                           permission="create")

    # Test: Create links during dumping
    f = LinksFactory(host="localhost", config={"item": ItemLinkSchema1})
    context = {
        "links_factory": f,
        "links_namespace": "item",
        "field_permission_check": lambda o: True  # allows anything
    }
    assert ({
        'links': {
            'self': 'https://localhost/1/?pid=1'
        }
    } == EntitySchema(context=context).dump({"pid": 1}))

    # Test: Create links with other config during dumping
    f = LinksFactory(host="localhost", config={"item": ItemLinkSchema2})
    context = {
        "links_factory": f,
        "links_namespace": "item",
        "field_permission_check": lambda o: True  # allows anything
    }
    assert ({
        'links': {
            'self': 'https://localhost/2/?pid=1'
        }
    } == EntitySchema(context=context).dump({"pid": 1}))
Пример #3
0
    def entries(self):
        """Iterator over the hits."""
        links = LinksFactory(host=_current_host, config=self._links_config)
        for entry in self._results:
            # Project the record
            projection = self._service.file_schema.dump(
                self._identity,
                entry,
                links_namespace="file",
                links_factory=links,
            )

            yield projection
Пример #4
0
    def results(self):
        """Iterator over the hits."""
        links = LinksFactory(host=_current_host, config=self._links_config)

        for res in self._results:
            # Project the record
            projection = self._service.schema_secret_link.dump(
                res.to_dict(),
                context=dict(
                    identity=self._identity,
                    links_namespace="secret_link",
                    links_factory=links,
                ))
            yield projection
Пример #5
0
    def data(self):
        """Property to get the record."""
        if self._data:
            return self._data

        links = LinksFactory(host=_current_host, config=self._links_config)

        self._data = self._service.schema.dump(
            self._identity,
            self._record,
            links_namespace="record",
            links_factory=links,
        )

        return self._data
Пример #6
0
    def data(self):
        """Property to get the link's dumped data."""
        if self._data:
            return self._data

        links = LinksFactory(host="", config=self._links_config)
        self._data = self._service.schema_secret_link.dump(
            self._link.to_dict(),
            context=dict(
                identity=self._identity,
                # TODO is this required in any way?
                links_namespace="secret_link",
                links_factory=links,
            ))

        return self._data
Пример #7
0
    def hits(self):
        """Iterator over the hits."""
        links = LinksFactory(host=_current_host, config=self._links_config)

        for hit in self._results:
            # Load dump
            record = self._service.record_cls.loads(hit.to_dict())

            # Project the record
            projection = self._service.schema.dump(self._identity,
                                                   record,
                                                   pid=record.pid,
                                                   record=record,
                                                   links_namespace="record",
                                                   links_factory=links)

            yield projection
Пример #8
0
    def links(self):
        """Get the list result links."""
        if self._links:
            return self._links

        links = LinksFactory(host=_current_host, config=self._links_config)
        schema = self._service.schema_files_links

        data = schema.dump(
            self._identity,
            self._record,
            links_factory=links,
            links_namespace="files",
        )
        self._links = data.get("links")

        return self._links
Пример #9
0
    def links(self):
        """Get the search result links.

        TODO: Would be nicer if this were a parallel of data above.
        """
        links = LinksFactory(host=_current_host, config=self._links_config)
        schema = self._service.schema_search_links

        data = schema.dump(
            self._identity,
            # It ain't pretty but it will do
            {**self._params, "_pagination": self.pagination},
            links_factory=links,
            links_namespace="search",
        )

        return data.get("links")