def list_shelves(self, request): """List enabled or all shelves based on any shelf attribute.""" self.check_xsrf_token(self.request_state) if request.page_size <= 0: raise endpoints.BadRequestException( 'The value for page_size must be greater than 0.') 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) offset = search_utils.calculate_page_offset( page_size=request.page_size, page_number=request.page_number) search_results = shelf_model.Shelf.search( query_string=query, query_limit=request.page_size, offset=offset, sort_options=sort_options, returned_fields=returned_fields) total_pages = search_utils.calculate_total_pages( page_size=request.page_size, total_results=search_results.number_found) 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, total_results=search_results.number_found, total_pages=total_pages)
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 test_set_search_query_options(self, request, expected_values): """Tests setting the query options without sort options from message.""" returned_query, returned_sort_options, returned_returned_fields = ( search_utils.set_search_query_options(request)) expected_query, expected_sort_options, expcted_returned_fields = ( expected_values) self.assertEqual(expected_sort_options, returned_sort_options) self.assertEqual(expected_query, returned_query) self.assertEqual(expcted_returned_fields, returned_returned_fields)
def test_set_search_query_options_with_sort_options( self, request, expected_sort_options_expressions): """Tests setting query options with sort options from message.""" returned_query, returned_sort_options, returned_returned_fields = ( search_utils.set_search_query_options(request)) del returned_query # Unused. del returned_returned_fields # Unused. for i in range(len(returned_sort_options.expressions)): self.assertEqual(returned_sort_options.expressions[i].expression, expected_sort_options_expressions[i].expression) self.assertEqual(returned_sort_options.expressions[i].direction, expected_sort_options_expressions[i].direction)
def list_devices(self, request): """Lists all devices based on any device attribute.""" self.check_xsrf_token(self.request_state) if request.page_size <= 0: raise endpoints.BadRequestException( 'The value for page_size must be greater than 0.') 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)) offset = search_utils.calculate_page_offset( page_size=request.page_size, page_number=request.page_number) search_results = device_model.Device.search( query_string=query, query_limit=request.page_size, offset=offset, sort_options=sort_options, returned_fields=returned_fields) total_pages = search_utils.calculate_total_pages( page_size=request.page_size, total_results=search_results.number_found) 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 messages.append(message) return device_messages.ListDevicesResponse( devices=messages, total_results=search_results.number_found, total_pages=total_pages)
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)