def test_get_search_cursor(self): """Tests the creation of a search cursor with a web_safe_string.""" expected_cursor_web_safe_string = 'False:ODUxODBhNTgyYTQ2ZmI0MDU' returned_cursor = ( search_utils.get_search_cursor(expected_cursor_web_safe_string)) self.assertEqual(expected_cursor_web_safe_string, returned_cursor.web_safe_string)
def list_shelves(self, request): """List enabled or all shelves based on any shelf attribute.""" self.check_xsrf_token(self.request_state) query, sort_options, returned_fields = ( search_utils.set_search_query_options(request.query)) if not query: query = search_utils.to_query(request, shelf_model.Shelf) cursor = search_utils.get_search_cursor(request.page_token) search_results = shelf_model.Shelf.search( query_string=query, query_limit=request.page_size, cursor=cursor, sort_options=sort_options, returned_fields=returned_fields) new_search_cursor = None if search_results.cursor: new_search_cursor = search_results.cursor.web_safe_string shelves_messages = [] for document in search_results.results: message = search_utils.document_to_message(document, shelf_messages.Shelf()) message.shelf_request = shelf_messages.ShelfRequest() message.shelf_request.urlsafe_key = document.doc_id message.shelf_request.location = message.location shelves_messages.append(message) return shelf_messages.ListShelfResponse( shelves=shelves_messages, additional_results=bool(new_search_cursor), page_token=new_search_cursor)
def list_devices(self, request): """Lists all devices based on any device attribute.""" self.check_xsrf_token(self.request_state) query, sort_options, returned_fields = ( search_utils.set_search_query_options(request.query)) if not query: shelf_query = '' if request.shelf: shelf_urlsafe_key = request.shelf.shelf_request.urlsafe_key if not shelf_urlsafe_key: shelf_urlsafe_key = shelf_api.get_shelf( request.shelf.shelf_request).key.urlsafe() request.shelf = None shelf_query = ':'.join(('shelf', shelf_urlsafe_key)) query = search_utils.to_query(request, device_model.Device) query = ' '.join((query, shelf_query)) cursor = search_utils.get_search_cursor(request.page_token) search_results = device_model.Device.search( query_string=query, query_limit=request.page_size, cursor=cursor, sort_options=sort_options, returned_fields=returned_fields) new_search_cursor = None if search_results.cursor: new_search_cursor = search_results.cursor.web_safe_string guest_permitted = config_model.Config.get('allow_guest_mode') messages = [] for document in search_results.results: message = search_utils.document_to_message( document, device_messages.Device()) message.guest_permitted = guest_permitted if message.current_ou == constants.ORG_UNIT_DICT['GUEST']: message.guest_enabled = True messages.append(message) return device_messages.ListDevicesResponse( devices=messages, has_additional_results=bool(new_search_cursor), page_token=new_search_cursor)
def list_devices(self, request): """Lists all devices based on any device attribute.""" self.check_xsrf_token(self.request_state) query, sort_options, returned_fields = ( search_utils.set_search_query_options(request.query)) if not query: shelf_query = '' if request.shelf: shelf_urlsafe_key = request.shelf.shelf_request.urlsafe_key if not shelf_urlsafe_key: shelf_urlsafe_key = shelf_api.get_shelf( request.shelf.shelf_request).key.urlsafe() request.shelf = None shelf_query = ':'.join(('shelf', shelf_urlsafe_key)) query = search_utils.to_query(request, device_model.Device) query = ' '.join((query, shelf_query)) cursor = search_utils.get_search_cursor(request.page_token) search_results = device_model.Device.search( query_string=query, query_limit=request.page_size, cursor=cursor, sort_options=sort_options, returned_fields=returned_fields) new_search_cursor = None if search_results.cursor: new_search_cursor = search_results.cursor.web_safe_string guest_permitted = config_model.Config.get('allow_guest_mode') messages = [] for document in search_results.results: device = api_utils.get_ndb_key(document.doc_id).get() messages.append( api_utils.build_device_message_from_model(device, guest_permitted)) return device_message.ListDevicesResponse( devices=messages, additional_results=bool(new_search_cursor), page_token=new_search_cursor)
def test_get_search_cursor_error(self, mock_cursor): """Tests the creation of a search cursor when an error occurs.""" mock_cursor.side_effect = ValueError with self.assertRaisesWithLiteralMatch(endpoints.BadRequestException, search_utils._CORRUPT_KEY_MSG): search_utils.get_search_cursor(None)