def test_nav_links(self): app.add_url_rule('/', endpoint='a') with app.test_request_context(): links = get_nav_links('a', 0, 10, 20) self.assertEqual(links['hl:first']['href'], '/?count=10') self.assertEqual(links['hl:next']['href'], '/?count=10&start=10') self.assertEqual(links['hl:last']['href'], '/?count=10&start=10') self.assertFalse('hl:prev' in links) self.assertEqual(len(links['curies']), 1) self.assertEqual(links['curies'][0]['name'], 'hl') links2 = get_nav_links('a', 20, 10, 30) self.assertEqual(links2['hl:first']['href'], '/?count=10') self.assertEqual(links2['hl:prev']['href'], '/?count=10&start=10') self.assertEqual(links2['hl:last']['href'], '/?count=10&start=20') self.assertFalse('hl:next' in links2) self.assertEqual(len(links['curies']), 1) self.assertEqual(links2['curies'][0]['name'], 'hl') links3 = get_nav_links('a', 0, 10, 5) self.assertEqual(links3['hl:first']['href'], '/?count=10') self.assertEqual(links3['hl:last']['href'], '/?count=10') self.assertFalse('hl:next' in links3) self.assertFalse('hl:prev' in links3) self.assertEqual(len(links['curies']), 1) self.assertEqual(links3['curies'][0]['name'], 'hl') links4 = get_nav_links('a', 20, 20, 30) self.assertEqual(links4['hl:first']['href'], '/?count=20') self.assertEqual(links4['hl:last']['href'], '/?count=20&start=10') self.assertFalse('hl:next' in links4) self.assertEqual(links4['hl:prev']['href'], '/?count=20&start=0') self.assertEqual(len(links4['curies']), 1) self.assertEqual(links4['curies'][0]['name'], 'hl')
def as_dict(self): representation = HALRepresentation({}) representation.add_embed('events', [ HALEventRepresentation(e, 'events.event').as_dict() for e in self.events ]) if self.day and self.month and self.year: representation.add_links( get_nav_links(self.endpoint, self.start, self.count, self.size, day=self.day, month=self.month, year=self.year)) representation.add_link( 'self', url_for(self.endpoint, day=self.day, month=self.month, year=self.year)) else: representation.add_links( get_nav_links(self.endpoint, self.start, self.count, self.size)) representation.add_link('self', url_for(self.endpoint)) return representation.as_dict()
def as_dict(self): representation = HALRepresentation({}) representation.add_embed('events', [HALEventRepresentation(e, 'events.event').as_dict() for e in self.events]) if self.day and self.month and self.year: representation.add_links(get_nav_links(self.endpoint, self.start, self.count, self.size, day=self.day, month=self.month, year=self.year)) representation.add_link('self', url_for(self.endpoint, day=self.day, month=self.month, year=self.year)) else: representation.add_links(get_nav_links(self.endpoint, self.start, self.count, self.size)) representation.add_link('self', url_for(self.endpoint)) return representation.as_dict()
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()
def test_nav_links_with_facet(self): app.add_url_rule('/', endpoint='a') with app.test_request_context(): links = get_nav_links('a', 0, 10, 20, facet=['this']) self.assertEqual(len(links['curies']), 2) curie_names = set([c['name'] for c in links['curies']]) self.assertTrue('facet' in curie_names) self.assertTrue('hl' in curie_names)
def as_dict(self): response = { 'query': self.query, } # Need to have the '.' before 'course' to correctly pick the URL courses = [HALCourseRepresentation(r, '.course').as_dict() for r in self.courses] representation = HALRepresentation(response) representation.add_embed('courses', courses) representation.add_link('self', url_for(self.endpoint, q=self.query)) representation.add_links(get_nav_links(self.endpoint, self.start, self.count, self.size, q=self.query)) return representation.as_dict()
def as_dict(self): response = { 'title': self.title, 'author': self.author, 'isbn': self.isbn, 'size': self.size, } items = [HALItemRepresentation(r, 'library.item').as_dict() for r in self.results] links = {'self': { 'href': url_for(self.endpoint, title=self.title, author=self.author, isbn=self.isbn) } } links.update(get_nav_links(self.endpoint, self.start, self.count, self.size, title=self.title, author=self.author, isbn=self.isbn)) return HALRepresentation(response, links, {'items': items}).as_dict()