Пример #1
0
    def as_dict(self):
        representation = HALRepresentation({"query": self.search, "size": self.size})
        url_kwargs = {}
        if self.search:
            url_kwargs["q"] = self.search
        if self.type:
            url_kwargs["type"] = self.type
        if self.type_exact:
            url_kwargs["type_exact"] = self.type_exact
        if self.facets:
            url_kwargs["facet"] = self.facets.keys()
        if self.other_args:
            url_kwargs.update(self.other_args)

        representation.add_link("self", url_for(self.endpoint, start=self.start, count=self.count, **url_kwargs))
        representation.add_links(get_nav_links(self.endpoint, self.start, self.count, self.size, **url_kwargs))
        representation.add_embed("pois", [HALPOIRepresentation(r, "places.poidetail").as_dict() for r in self.results])
        if self.facets:
            for field_name, facet_counts in self.facets.items():
                curie = FACET_CURIE
                friendly_name = field_name
                if field_name in FACET_RENAME:
                    curie, friendly_name = FACET_RENAME[field_name]
                for val, count in facet_counts.items():
                    kwargs = {"value": val, "count": count}
                    if field_name in FACET_BY_TYPE:
                        kwargs["title"] = find_type_name(val)
                        kwargs["name"] = val
                    url_kwargs[field_name] = val
                    representation.update_link(
                        "%s:%s" % (curie, friendly_name), url_for(self.endpoint, **url_kwargs), **kwargs
                    )
                    del url_kwargs[field_name]
        return representation.as_dict()
Пример #2
0
 def test_hal_json_helper_representation(self):
     representation = HALRepresentation({'a':'b'})
     representation.add_link('self', '/a/b')
     representation.add_link('list', '/list', templated=True)
     representation.update_link('child', '/child/1')
     representation.update_link('child', '/child/2')
     representation.add_curie('cu', 'http://curie.com')
     representation.add_embed('rel', [HALRepresentation({'embed': 'yes'},
         links={'self':{'href':'a'}}).as_dict()])
     self.assertDictContainsSubset({'_links': {
         'self': {'href': '/a/b'},
         'list': {'href': '/list', 'templated': True},
         'child': [{'href': '/child/1'}, {'href': '/child/2'}],
         'curie': {'href': 'http://curie.com', 'name': 'cu', 'templated': True}}},
         representation.as_dict())
     self.assertDictContainsSubset(
         {'_embedded': {'rel': [{'embed': 'yes',
         '_links': {'self': {'href': 'a'}}}]}},
         representation.as_dict())