Python google.appengine.datastore.datastore_query.Cursor() Examples

The following are 30 code examples of google.appengine.datastore.datastore_query.Cursor(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module google.appengine.datastore.datastore_query , or try the search function .
Example #1
Source File: guestbook.py    From python-docs-samples with Apache License 2.0 7 votes vote down vote up
def get(self):
        """Handles requests like /list?cursor=1234567."""
        cursor = Cursor(urlsafe=self.request.get('cursor'))
        greets, next_cursor, more = Greeting.query().fetch_page(
            self.GREETINGS_PER_PAGE, start_cursor=cursor)

        self.response.out.write('<html><body>')

        for greeting in greets:
            self.response.out.write(
                '<blockquote>%s</blockquote>' % cgi.escape(greeting.content))

        if more and next_cursor:
            self.response.out.write('<a href="/list?cursor=%s">More...</a>' %
                                    next_cursor.urlsafe())

        self.response.out.write('</body></html>') 
Example #2
Source File: handler_utils.py    From upvote with Apache License 2.0 6 votes vote down vote up
def respond_with_page(self, content, cursor, has_more):
    """Sets the handler response to the page contents provided.

    Args:
      content: list, A list of the objects to be used as the page contents.
      cursor: datastore_query.Cursor, The cursor representing the resumption
          point for the next page of the query.
      has_more: bool, Whether there are more results after this page.
    """
    safe_cursor = cursor.urlsafe() if cursor else None
    response_dict = {
        'content': content,
        'cursor': safe_cursor,
        'more': has_more,
        'per_page': self.per_page}
    logging.info('Responding with a page of %d item(s)', len(content))
    self.respond_json(response_dict) 
Example #3
Source File: tag_model_test.py    From loaner with Apache License 2.0 6 votes vote down vote up
def test_list_tags_more(self):
    (page_one_result, first_cursor,
     has_additional_results), total_pages = tag_model.Tag.list(
         page_size=1, cursor=None, include_hidden_tags=False)
    self.assertListEqual(page_one_result, [self.tag1])
    self.assertTrue(has_additional_results)
    self.assertEqual(total_pages, 3)

    (page_two_result, next_cursor,
     has_additional_results), total_pages = tag_model.Tag.list(
         page_size=tag_model.Tag.query().count(), cursor=first_cursor,
         include_hidden_tags=False)
    self.assertListEqual(page_two_result, [self.tag2, self.tag5])
    self.assertIsInstance(next_cursor, datastore_query.Cursor)
    self.assertFalse(has_additional_results)
    self.assertEqual(total_pages, 1) 
Example #4
Source File: datastore_range_iterators.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def to_json(self):
    """Inherit doc."""
    cursor_object = False
    if self._query is not None:
      if isinstance(self._query, db.Query):
        self._cursor = self._query.cursor()
      else:
        cursor_object = True
        self._cursor = self._query.cursor_after().to_websafe_string()
    else:
      self._cursor = None

    return {"property_range": self._property_range.to_json(),
            "query_spec": self._query_spec.to_json(),
            "cursor": self._cursor,
            "ns_range": self._ns_range.to_json_object(),
            "name": self.__class__.__name__,
            "cursor_object": cursor_object}

  # TODO(user): it sucks we need to handle cursor_to_str in many places.
  # In the long run, datastore adaptor refactor will take care of this as
  # we will only need to deal with low level datastore API after that.
  # Thus we will not add Cursor as a json primitive MR should understand. 
Example #5
Source File: ts_mon_metrics.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def __init__(self, payload):
    self.start_time = utils.utcnow()
    self.cursor = None
    self.task_start = self.start_time
    self.task_count = 0
    self.count = 0
    if not payload:
      return
    try:
      params = json.loads(payload)
      if params['cursor']:
        self.cursor = Cursor(urlsafe=params['cursor'])
      self.task_start = datetime.datetime.strptime(
          params['task_start'], utils.DATETIME_FORMAT)
      self.task_count = params['task_count']
      self.count = params['count']
    except (ValueError, KeyError) as e:
      logging.error('_ShardParams: bad JSON: %s: %s', payload, e)
      # Stop the task chain and let the request fail.
      raise 
Example #6
Source File: fixit.py    From cloud-playground with Apache License 2.0 6 votes vote down vote up
def post(self):  # pylint:disable-msg=invalid-name,missing-docstring
    assert self.request.environ[common.HTTP_X_APPENGINE_QUEUENAME]
    query = model.Project.query(namespace=settings.PLAYGROUND_NAMESPACE)
    cursor = self.request.get('cursor', None)
    if cursor:
      cursor = Cursor(urlsafe=cursor)
    projects, next_cursor, more = query.fetch_page(_CURSOR_PAGE_SIZE,
                                                   start_cursor=cursor)
    if more and next_cursor:
      taskqueue.add(queue_name='fixit',
                    url='/playground/fix/project',
                    params={'cursor': next_cursor.urlsafe()})
    for project in projects:
      FixProject(project)
    if not next_cursor:
      shared.w('REACHED END OF QUERY CURSOR, '
               'ALTHOUGH OTHER TASKS MAY STILL BE EXECUTING') 
Example #7
Source File: domain_user.py    From googleapps-message-recall with Apache License 2.0 6 votes vote down vote up
def FetchOnePageOfActiveUsersForTask(cls, task_key_id, cursor):
    """Utility to query and fetch all active users.

    Used to retrieve the entire list of (not suspended) domain users to process.

    Args:
      task_key_id: Int unique id of the task record.
      cursor: Cursor from previous fetch_page() calls.

    Returns:
      Iterable of one page of DomainUserToCheckModel users.
    """
    return cls.GetQueryForAllTaskUsers(
        task_key_id=task_key_id,
        user_state_filters=ACTIVE_USER_STATES).fetch_page(
            _USER_ROWS_FETCH_PAGE, start_cursor=cursor) 
Example #8
Source File: domain_user.py    From googleapps-message-recall with Apache License 2.0 6 votes vote down vote up
def FetchOneUIPageOfUsersForTask(cls, task_key_urlsafe, urlsafe_cursor,
                                   user_state_filters=None,
                                   message_state_filters=None):
    """Utility to query and fetch all users for UI pages.

    Args:
      task_key_urlsafe: String unique id of the task record.
      urlsafe_cursor: String cursor from previous fetch_page() calls.
                      This is a publishable version acquired via '.urlsafe()'.
      user_state_filters: List of strings to filter users.
      message_state_filters: List of strings to filter users.

    Returns:
      Iterable of one page of DomainUserToCheckModel users.
    """
    return cls.GetQueryForAllTaskUsers(
        task_key_id=ndb.Key(urlsafe=task_key_urlsafe).id(),
        user_state_filters=user_state_filters,
        message_state_filters=message_state_filters).fetch_page(
            _USER_ROWS_FETCH_PAGE,
            start_cursor=Cursor(urlsafe=urlsafe_cursor)) 
Example #9
Source File: handlers.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def get(self):
    limit = int(self.request.get('limit', 100))
    cursor = datastore_query.Cursor(urlsafe=self.request.get('cursor'))
    errors_found, cursor, more = models.Error.query().order(
        -models.Error.created_ts).fetch_page(limit, start_cursor=cursor)
    params = {
      'cursor': cursor.urlsafe() if cursor and more else None,
      'errors': errors_found,
      'limit': limit,
      'now': utils.utcnow(),
    }
    self.response.out.write(template.render('ereporter2/errors.html', params)) 
Example #10
Source File: handlers.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def get(self):
    limit = int(self.request.get('limit', 100))
    cursor = datastore_query.Cursor(urlsafe=self.request.get('cursor'))
    errors_found, cursor, more = models.Error.query().order(
        -models.Error.created_ts).fetch_page(limit, start_cursor=cursor)
    params = {
      'cursor': cursor.urlsafe() if cursor and more else None,
      'errors': errors_found,
      'limit': limit,
      'now': utils.utcnow(),
    }
    self.response.out.write(template.render('ereporter2/errors.html', params)) 
Example #11
Source File: utils.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def fetch_page(query, batch_size, cursor_str, **kwargs):
  """Fetches a page from a query.

  Arguments:
    query: ndb.Query.
    batch_size: Maximum number of items to return.
    cursor_str: query-dependent string encoded cursor to continue a previous
        search.

  Returns:
  - items
  - str encoded cursor if relevant or None.
  """
  assert isinstance(query, ndb.Query), query
  if not 0 < batch_size <= 1000 or not isinstance(batch_size, int):
    raise ValueError(
        'batch_size must be between 1 and 1000, got %r', batch_size)
  if cursor_str:
    if not isinstance(cursor_str, basestring):
      raise ValueError(
          'cursor must be between valid string, got %r', cursor_str)
    cursor = datastore_query.Cursor(urlsafe=cursor_str)
  else:
    cursor = None
  items, cursor, more = query.fetch_page(
      batch_size, start_cursor=cursor, **kwargs)
  if not more:
    return items, None
  return items, cursor.urlsafe() 
Example #12
Source File: handlers.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def get(self):
    limit = int(self.request.get('limit', 100))
    cursor = datastore_query.Cursor(urlsafe=self.request.get('cursor'))
    errors_found, cursor, more = models.Error.query().order(
        -models.Error.created_ts).fetch_page(limit, start_cursor=cursor)
    params = {
      'cursor': cursor.urlsafe() if cursor and more else None,
      'errors': errors_found,
      'limit': limit,
      'now': utils.utcnow(),
    }
    self.response.out.write(template.render('ereporter2/errors.html', params)) 
Example #13
Source File: utils.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def fetch_page(query, batch_size, cursor_str, **kwargs):
  """Fetches a page from a query.

  Arguments:
    query: ndb.Query.
    batch_size: Maximum number of items to return.
    cursor_str: query-dependent string encoded cursor to continue a previous
        search.

  Returns:
  - items
  - str encoded cursor if relevant or None.
  """
  assert isinstance(query, ndb.Query), query
  if not 0 < batch_size <= 1000 or not isinstance(batch_size, int):
    raise ValueError(
        'batch_size must be between 1 and 1000, got %r', batch_size)
  if cursor_str:
    if not isinstance(cursor_str, basestring):
      raise ValueError(
          'cursor must be between valid string, got %r', cursor_str)
    cursor = datastore_query.Cursor(urlsafe=cursor_str)
  else:
    cursor = None
  items, cursor, more = query.fetch_page(
      batch_size, start_cursor=cursor, **kwargs)
  if not more:
    return items, None
  return items, cursor.urlsafe() 
Example #14
Source File: rest_api.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def get(self):
    target = self.request.get('target')
    if target and not change_log.TARGET_RE.match(target):
      self.abort_with_error(400, text='Invalid \'target\' param')

    auth_db_rev = self.request.get('auth_db_rev')
    if auth_db_rev:
      try:
        auth_db_rev = int(auth_db_rev)
        if auth_db_rev <= 0:
          raise ValueError('Outside of allowed range')
      except ValueError as exc:
        self.abort_with_error(
            400, text='Invalid \'auth_db_rev\' param: %s' % exc)

    try:
      limit = int(self.request.get('limit', 50))
      if limit <= 0 or limit > 1000:
        raise ValueError('Outside of allowed range')
    except ValueError as exc:
      self.abort_with_error(400, text='Invalid \'limit\' param: %s' % exc)

    try:
      cursor = datastore_query.Cursor(urlsafe=self.request.get('cursor'))
    except (datastore_errors.BadValueError, ValueError) as exc:
      self.abort_with_error(400, text='Invalid \'cursor\' param: %s' % exc)

    q = change_log.make_change_log_query(target=target, auth_db_rev=auth_db_rev)
    try:
      changes, cursor, more = q.fetch_page(limit, start_cursor=cursor)
    except datastore_errors.NeedIndexError:
      # This is expected for users of components.auth that did not update
      # index.yaml. Return a friendlier message pointing them to instructions.
      self.abort_with_error(500, text=self.NEED_INDEX_ERROR_MESSAGE)

    self.send_response({
      'changes': [c.to_jsonish() for c in changes],
      'cursor': cursor.urlsafe() if cursor and more else None,
    }) 
Example #15
Source File: handlers.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def get(self):
    limit = int(self.request.get('limit', 100))
    cursor = datastore_query.Cursor(urlsafe=self.request.get('cursor'))
    errors_found, cursor, more = models.Error.query().order(
        -models.Error.created_ts).fetch_page(limit, start_cursor=cursor)
    params = {
      'cursor': cursor.urlsafe() if cursor and more else None,
      'errors': errors_found,
      'limit': limit,
      'now': utils.utcnow(),
    }
    self.response.out.write(template.render('ereporter2/errors.html', params)) 
Example #16
Source File: pagination.py    From love with MIT License 5 votes vote down vote up
def paginate(cls, order_by, prev_cursor_str, next_cursor_str):
        if not prev_cursor_str and not next_cursor_str:
            objects, next_cursor, more = cls.query().order(order_by).fetch_page(cls.ITEMS_PER_PAGE)
            prev_cursor_str = ''
            if next_cursor:
                next_cursor_str = next_cursor.urlsafe()
            else:
                next_cursor_str = ''
            next_ = True if more else False
            prev = False
        elif next_cursor_str:
            cursor = Cursor(urlsafe=next_cursor_str)
            objects, next_cursor, more = cls.query().order(order_by).fetch_page(
                cls.ITEMS_PER_PAGE,
                start_cursor=cursor,
            )
            prev_cursor_str = next_cursor_str
            next_cursor_str = next_cursor.urlsafe()
            prev = True
            next_ = True if more else False
        elif prev_cursor_str:
            cursor = Cursor(urlsafe=prev_cursor_str)
            objects, next_cursor, more = cls.query().order(-order_by).fetch_page(
                cls.ITEMS_PER_PAGE,
                start_cursor=cursor,
            )
            objects.reverse()
            next_cursor_str = prev_cursor_str
            prev_cursor_str = next_cursor.urlsafe()
            prev = True if more else False
            next_ = True

        return PaginationResult(
            collection=objects,
            prev_cursor=prev_cursor_str,
            next_cursor=next_cursor_str,
            prev=prev,
            next=next_,
        ) 
Example #17
Source File: datastore.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def GetCursor(self):
    """Get the cursor from the last run of this query.

    The source of this cursor varies depending on what the last call was:
      - Run: A cursor that points immediately after the last result pulled off
        the returned iterator.
      - Get: A cursor that points immediately after the last result in the
        returned list.
      - Count: A cursor that points immediately after the last result counted.

    Returns:
      A datastore_query.Cursor object that can be used in subsequent query
      requests.

    Raises:
      AssertionError: The query has not yet been run or cannot be compiled.
    """


    cursor_function = self.__cursor_source
    if cursor_function:
      cursor = cursor_function()
      if cursor:
        return cursor
    raise AssertionError('No cursor available, either this query has not '
                         'been executed or there is no compilation '
                         'available for this kind of query') 
Example #18
Source File: datastore_range_iterators.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def from_json(cls, json):
    """Inherit doc."""
    obj = cls(property_range.PropertyRange.from_json(json["property_range"]),
              namespace_range.NamespaceRange.from_json_object(json["ns_range"]),
              model.QuerySpec.from_json(json["query_spec"]))
    cursor = json["cursor"]


    if cursor and json["cursor_object"]:
      obj._cursor = datastore_query.Cursor.from_websafe_string(cursor)
    else:
      obj._cursor = cursor
    return obj 
Example #19
Source File: datastore_range_iterators.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def to_json(self):
    """Serializes all states into json form.

    Returns:
      all states in json-compatible map.
    """
    cursor = self._get_cursor()
    cursor_object = False
    if cursor and isinstance(cursor, datastore_query.Cursor):
      cursor = cursor.to_websafe_string()
      cursor_object = True
    return {"key_range": self._key_range.to_json(),
            "query_spec": self._query_spec.to_json(),
            "cursor": cursor,
            "cursor_object": cursor_object} 
Example #20
Source File: datastore_range_iterators.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def from_json(cls, json):
    """Reverse of to_json."""
    obj = cls(key_range.KeyRange.from_json(json["key_range"]),
              model.QuerySpec.from_json(json["query_spec"]))
    cursor = json["cursor"]


    if cursor and json["cursor_object"]:
      obj._cursor = datastore_query.Cursor.from_websafe_string(cursor)
    else:
      obj._cursor = cursor
    return obj 
Example #21
Source File: datastore_range_iterators.py    From appengine-mapreduce with Apache License 2.0 5 votes vote down vote up
def to_json(self):
    """Inherit doc."""
    cursor = self._cursor
    if self._query is not None:
      if isinstance(self._query, db.Query):
        cursor = self._query.cursor()
      else:
        cursor = self._query.cursor_after()

    if cursor is None or isinstance(cursor, basestring):
      cursor_object = False
    else:
      cursor_object = True
      cursor = cursor.to_websafe_string()

    return {"property_range": self._property_range.to_json(),
            "query_spec": self._query_spec.to_json(),
            "cursor": cursor,
            "ns_range": self._ns_range.to_json_object(),
            "name": self.__class__.__name__,
            "cursor_object": cursor_object}

  # TODO(user): it sucks we need to handle cursor_to_str in many places.
  # In the long run, datastore adaptor refactor will take care of this as
  # we will only need to deal with low level datastore API after that.
  # Thus we will not add Cursor as a json primitive MR should understand. 
Example #22
Source File: datastore_range_iterators.py    From appengine-mapreduce with Apache License 2.0 5 votes vote down vote up
def from_json(cls, json):
    """Inherit doc."""
    obj = cls(property_range.PropertyRange.from_json(json["property_range"]),
              namespace_range.NamespaceRange.from_json_object(json["ns_range"]),
              model.QuerySpec.from_json(json["query_spec"]))
    cursor = json["cursor"]
    # lint bug. Class method can access protected fields.
    # pylint: disable=protected-access
    if cursor and json["cursor_object"]:
      obj._cursor = datastore_query.Cursor.from_websafe_string(cursor)
    else:
      obj._cursor = cursor
    return obj 
Example #23
Source File: datastore_range_iterators.py    From appengine-mapreduce with Apache License 2.0 5 votes vote down vote up
def to_json(self):
    """Serializes all states into json form.

    Returns:
      all states in json-compatible map.
    """
    cursor = self._get_cursor()
    cursor_object = False
    if cursor and isinstance(cursor, datastore_query.Cursor):
      cursor = cursor.to_websafe_string()
      cursor_object = True
    return {"key_range": self._key_range.to_json(),
            "query_spec": self._query_spec.to_json(),
            "cursor": cursor,
            "cursor_object": cursor_object} 
Example #24
Source File: datastore_range_iterators.py    From appengine-mapreduce with Apache License 2.0 5 votes vote down vote up
def from_json(cls, json):
    """Reverse of to_json."""
    obj = cls(key_range.KeyRange.from_json(json["key_range"]),
              model.QuerySpec.from_json(json["query_spec"]))
    cursor = json["cursor"]
    # lint bug. Class method can access protected fields.
    # pylint: disable=protected-access
    if cursor and json["cursor_object"]:
      obj._cursor = datastore_query.Cursor.from_websafe_string(cursor)
    else:
      obj._cursor = cursor
    return obj 
Example #25
Source File: handler_utils.py    From upvote with Apache License 2.0 5 votes vote down vote up
def cursor(self):
    if not hasattr(self, '_cursor'):
      cursor = Cursor(urlsafe=self.request.get('cursor'))
      self._cursor = cursor
    return self._cursor 
Example #26
Source File: datastore_range_iterators.py    From locality-sensitive-hashing with MIT License 5 votes vote down vote up
def from_json(cls, json):
    """Reverse of to_json."""
    obj = cls(key_range.KeyRange.from_json(json["key_range"]),
              model.QuerySpec.from_json(json["query_spec"]))
    cursor = json["cursor"]
    # lint bug. Class method can access protected fields.
    # pylint: disable=protected-access
    if cursor and json["cursor_object"]:
      obj._cursor = datastore_query.Cursor.from_websafe_string(cursor)
    else:
      obj._cursor = cursor
    return obj 
Example #27
Source File: tag_model_test.py    From loaner with Apache License 2.0 5 votes vote down vote up
def test_list_tags_exclude_hidden(self):
    (query_results, cursor,
     has_additional_results), total_pages = tag_model.Tag.list(
         page_size=tag_model.Tag.query().count(), include_hidden_tags=False)
    self.assertListEqual(query_results, [self.tag1, self.tag2, self.tag5])
    self.assertNotIn(self.tag3, query_results)
    self.assertNotIn(self.tag4, query_results)
    self.assertIsInstance(cursor, datastore_query.Cursor)
    self.assertFalse(has_additional_results)
    self.assertEqual(total_pages, 1) 
Example #28
Source File: tag_model_test.py    From loaner with Apache License 2.0 5 votes vote down vote up
def test_list_tags_middle_page(self):
    (page_result, next_cursor,
     has_additional_results), total_pages = tag_model.Tag.list(
         page_size=1, page_index=2, include_hidden_tags=False)
    self.assertListEqual(page_result, [self.tag2])
    self.assertIsInstance(next_cursor, datastore_query.Cursor)
    self.assertTrue(has_additional_results)
    self.assertEqual(total_pages, 3) 
Example #29
Source File: tag_model_test.py    From loaner with Apache License 2.0 5 votes vote down vote up
def test_list_tags_last_page(self):
    (page_result, next_cursor,
     has_additional_results), total_pages = tag_model.Tag.list(
         page_size=2, page_index=2, include_hidden_tags=False)
    self.assertListEqual(page_result, [self.tag5])
    self.assertIsInstance(next_cursor, datastore_query.Cursor)
    self.assertFalse(has_additional_results)
    self.assertEqual(total_pages, 2) 
Example #30
Source File: datastore_range_iterators.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def from_json(cls, json):
    """Inherit doc."""
    obj = cls(property_range.PropertyRange.from_json(json["property_range"]),
              namespace_range.NamespaceRange.from_json_object(json["ns_range"]),
              model.QuerySpec.from_json(json["query_spec"]))
    cursor = json["cursor"]
    # lint bug. Class method can access protected fields.
    # pylint: disable=protected-access
    if cursor and json["cursor_object"]:
      obj._cursor = datastore_query.Cursor.from_websafe_string(cursor)
    else:
      obj._cursor = cursor
    return obj