Beispiel #1
0
  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)
Beispiel #2
0
    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)
Beispiel #3
0
 def test_document_to_message(self, message, test_search_document,
                              expected_message, log_call_count):
     """Tests the creation of a protorpc message from a search document."""
     with mock.patch.object(search_utils, 'logging',
                            autospec=True) as mock_logging:
         response_message = search_utils.document_to_message(
             test_search_document, message)
         self.assertEqual(response_message, expected_message)
         self.assertEqual(mock_logging.error.call_count, log_call_count)
Beispiel #4
0
    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)
Beispiel #5
0
    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 test_document_to_message(self, mock_logging):
        """Tests the creation of a protorpc message from a search document."""
        test_search_document = search.ScoredDocument(
            doc_id='test_doc_id',
            fields=[
                search.NumberField(name='capacity', value=20.0),
                search.TextField(name='location', value='US MTV'),
                search.AtomField(name='location', value='US-MTV'),
                search.AtomField(name='enabled', value='True'),
                search.GeoField(name='lat_long',
                                value=search.GeoPoint(52.37, 4.88)),
                search.TextField(name='not_present', value='MTV')
            ])
        expected_message = shelf_messages.Shelf(enabled=True,
                                                location='US-MTV',
                                                capacity=20,
                                                latitude=52.37,
                                                longitude=4.88)

        response_message = search_utils.document_to_message(
            test_search_document, shelf_messages.Shelf())
        self.assertEqual(response_message, expected_message)
        self.assertTrue(response_message.enabled)
        assert mock_logging.error.call_count == 1