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_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 test_calculate_page_offset(self): """Tests the calculation of page offset.""" page_size = 10 page_number = 5 offset = search_utils.calculate_page_offset(page_size, page_number) self.assertEqual(40, offset)