def test_get_results_by_id(self): with mock.patch('moxie.places.services.searcher') as mock_searcher: with mock.patch('moxie.places.services.doc_to_poi') as mock_doc_to_poi: poi_service = POIService() mock_doc_to_poi.return_value = None poi_service.get_place_by_identifier('123') mock_searcher.get_by_ids.assert_called_with(['123'])
def get_rti(self, ident, rti_type): """TODO: We should have this perform the same redirect as when accessing POI detail view """ # Should we improve this API to only return the doc? poi_service = POIService.from_context() poi = poi_service.get_place_by_identifier(ident) if poi: return self.get_rti_from_poi(poi, rti_type) else: raise NotFound
def as_dict(self): base = super(HALItemRepresentation, self).as_dict() links = {'self': { 'href': url_for(self.endpoint, id=self.item.control_number) } } embedded = None try: poi_service = POIService.from_context() except NoConfiguredService: pass else: embedded = {} for location in self.item.libraries: poi = poi_service.search_place_by_identifier('{key}:{value}' .format(key=self.place_identifier, value='-'.join(location.location))) if poi: embedded['/'.join(location.location)] = HALPOIRepresentation(poi, 'places.poidetail', add_parent_children_links=False).as_dict() return HALRepresentation(base, links, embedded).as_dict()
def test_transform_args_reversibile(self): POIService.key_transforms = [('foo', 'bar')] poi_service = POIService() args = ['foobar', 'foofoo'] self.assertEqual(args, poi_service._args_to_friendly(poi_service._args_to_internal(args)))
def test_transform_args_friendly(self): POIService.key_transforms = [('foo', 'bar')] poi_service = POIService() before = ['barbar'] after = poi_service._args_to_friendly(before) self.assertEqual(after, ['foobar'])
def as_dict(self): base = super(HALPOIRepresentation, self).as_dict() representation = HALRepresentation(base) representation.add_link("self", url_for(self.endpoint, ident=self.poi.id)) if self.poi.files: reps = [FileRepresentation(r).as_dict() for r in self.poi.files] representation.add_embed("files", reps) try: poi_service = POIService.from_context() except NoConfiguredService: poi_service = None if poi_service and self.add_parent_children_links: # Merging all IDs (parent and children) into one set to # do only one query to the service pois_ids = set(self.poi.children) pois_ids.update(self.poi.parent) if self.poi.primary_place: pois_ids.add(self.poi.primary_place) if pois_ids: pois_objects = poi_service.get_places_by_identifiers(pois_ids) # ease lookup by having a dict with ID as key if pois_objects: pois = dict((poi.id, poi) for poi in pois_objects) else: pois = {} def add_link(relation, method, identifier): """Add a link w/ or w/o title depending if we found a POI :param relation: link rel (parent or child) :param method: method to apply (add_link or update if it should be an array) :param identifier: ID of the POI for lookup """ poi = pois.get(identifier, None) if poi and poi.name: method( relation, url_for(self.endpoint, ident=identifier), title=poi.name, type=poi.type, type_name=poi.type_name, ) else: method(relation, url_for(self.endpoint, ident=identifier)) if self.poi.parent: for parent in self.poi.parent: add_link("parent", representation.update_link, parent) if self.poi.primary_place: add_link("primary_place", representation.add_link, self.poi.primary_place) if self.poi.children: for child in self.poi.children: add_link("child", representation.update_link, child) try: transport_service = TransportService.from_context() except NoConfiguredService: # Transport service not configured so no RTI information logger.warning("No configured Transport service", exc_info=True) else: try: provider = transport_service.get_provider(self.poi) except ProviderException: logger.debug("No single provider found for: %s" % self.poi.id) else: representation.add_curie("rti", RTI_CURIE) for rtitype, title in provider.provides.items(): representation.add_link( "rti:%s" % rtitype, url_for("places.rti", ident=self.poi.id, rtitype=rtitype), title=title ) return representation.as_dict()