Python google.appengine.ext.ndb.OR Examples

The following are 12 code examples of google.appengine.ext.ndb.OR(). 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.ext.ndb , or try the search function .
Example #1
Source File: binary.py    From upvote with Apache License 2.0 6 votes vote down vote up
def CalculateInstallerState(self):
    """Returns the blockable's installer state as prescribed by Upvote.

    NOTE: Due to the ancestor query, this method will not reflect changes within
    uncommitted transactions.

    Returns:
      The current installer state prescribed by Upvote.
    """
    # pylint: disable=g-explicit-bool-comparison, singleton-comparison
    query = rule_models.Bit9Rule.query(
        rule_models.Bit9Rule.in_effect == True,
        ndb.OR(
            rule_models.Bit9Rule.policy == _POLICY.FORCE_INSTALLER,
            rule_models.Bit9Rule.policy == _POLICY.FORCE_NOT_INSTALLER),
        ancestor=self.key
    ).order(-rule_models.Bit9Rule.updated_dt)
    # pylint: enable=g-explicit-bool-comparison, singleton-comparison

    installer_rule = query.get()
    if installer_rule is None:
      return self.detected_installer
    else:
      return installer_rule.policy == _POLICY.FORCE_INSTALLER 
Example #2
Source File: models.py    From sndlatr with Apache License 2.0 5 votes vote down vote up
def query_display(cls, user_id, delta_minutes=60):
        """
        Query all jobs that have state scheduled, queued or sent (but not done)
        OR are done and have been scheduled for no longer than delta_minutes
        ago.
        """
        shortly_ago = datetime.datetime.utcnow() - datetime.timedelta(
            minutes=delta_minutes)

        # query all jobs that are
        return cls.query(ndb.OR(cls.state.IN(['scheduled', 'queued', 'sent']),
                                ndb.AND(cls.scheduled_at >= shortly_ago,
                                        cls.state == 'done')),
                         cls.user_id == user_id) 
Example #3
Source File: snippets.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def query_article_inequality_explicit():
    query = Article.query(ndb.OR(Article.tags < 'perl',
                                 Article.tags > 'perl'))
    return query 
Example #4
Source File: snippets.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def query_article_in_equivalent():
    query = Article.query(ndb.OR(Article.tags == 'python',
                                 Article.tags == 'ruby',
                                 Article.tags == 'php'))
    return query 
Example #5
Source File: snippets.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def query_article_nested():
    query = Article.query(ndb.AND(Article.tags == 'python',
                                  ndb.OR(Article.tags.IN(['ruby', 'jruby']),
                                         ndb.AND(Article.tags == 'php',
                                                 Article.tags != 'perl'))))
    return query 
Example #6
Source File: task_result.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def task_bq_run(start, end):
  """Sends TaskRunResult to BigQuery swarming.task_results_run table.

  Multiple queries are run one after the other. This is because ndb.OR() cannot
  be used when the subqueries are inequalities on different fields.
  """
  def _convert(e):
    """Returns a tuple(bq_key, row)."""
    out = swarming_pb2.TaskResult()
    e.to_proto(out)
    return (e.task_id, out)

  failed = 0
  total = 0
  seen = set()

  # Completed
  q = TaskRunResult.query(
      TaskRunResult.completed_ts >= start,
      TaskRunResult.completed_ts <= end,
      # Disable cache for consistency.
      default_options=ndb.QueryOptions(use_cache=False, use_memcache=False))
  cursor = None
  more = True
  while more:
    entities, cursor, more = q.fetch_page(500, start_cursor=cursor)
    rows = [_convert(e) for e in entities]
    seen.update(e.task_id for e in entities)
    total += len(rows)
    failed += bq_state.send_to_bq('task_results_run', rows)

  return total, failed 
Example #7
Source File: task_result.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def task_bq_summary(start, end):
  """Sends TaskResultSummary to BigQuery swarming.task_results_summary table.

  Multiple queries are run one after the other. This is because ndb.OR() cannot
  be used when the subqueries are inequalities on different fields.
  """
  def _convert(e):
    """Returns a tuple(bq_key, row)."""
    out = swarming_pb2.TaskResult()
    e.to_proto(out)
    if not out.HasField('end_time'):
      logging.warning('crbug.com/1064833: task %s does not have end_time %s',
                      e.task_id, out)
    return (e.task_id, out)

  failed = 0
  total = 0
  seen = set()

  # Completed
  q = TaskResultSummary.query(
      TaskResultSummary.completed_ts >= start,
      TaskResultSummary.completed_ts <= end,
      # Disable cache for consistency.
      default_options=ndb.QueryOptions(use_cache=False, use_memcache=False))
  cursor = None
  more = True
  while more:
    entities, cursor, more = q.fetch_page(500, start_cursor=cursor)
    rows = [_convert(e) for e in entities]
    seen.update(e.task_id for e in entities)
    total += len(rows)
    failed += bq_state.send_to_bq('task_results_summary', rows)

  return total, failed 
Example #8
Source File: love_count.py    From love with MIT License 5 votes vote down vote up
def top_lovers_and_lovees(utc_week_start, dept=None, limit=20):
    """Synchronously return a list of (employee key, sent love count) and a list of
    (employee key, received love count), each sorted in descending order of love sent
    or received.
    """
    sent_query = LoveCount.query(LoveCount.week_start == utc_week_start)
    if dept:
        sent_query = sent_query.filter(ndb.OR(LoveCount.meta_department == dept, LoveCount.department == dept))

    sent = sent_query.order(-LoveCount.sent_count).fetch()
    lovers = []
    for c in sent:
        if len(lovers) == limit:
            break
        if c.sent_count == 0:
            continue
        employee_key = c.key.parent()
        lovers.append((employee_key, c.sent_count))

    received = sorted(sent, key=lambda c: c.received_count, reverse=True)
    lovees = []
    for c in received:
        if len(lovees) == limit:
            break
        if c.received_count == 0:
            continue
        employee_key = c.key.parent()
        lovees.append((employee_key, c.received_count))

    return (lovers, lovees) 
Example #9
Source File: blockables.py    From upvote with Apache License 2.0 5 votes vote down vote up
def get(self, blockable_id):
    blockable_id = blockable_id.lower()
    blockable = binary_models.Blockable.get_by_id(blockable_id)
    if not blockable:
      self.abort(httplib.NOT_FOUND, explanation='Blockable not found.')

    platform = blockable.GetPlatformName()
    if platform != constants.PLATFORM.WINDOWS:
      self.respond_json(False)
      return

    # Get uncommitted Rules for this blockable that are relevant to the user.
    # Relevant Rules are either global Rules or local Rules that the user was
    # responsible for creating.
    # pylint: disable=g-explicit-bool-comparison, singleton-comparison
    pending_rule_query = rule_models.Bit9Rule.query(
        rule_models.Bit9Rule.in_effect == True,
        rule_models.Bit9Rule.is_committed == False,
        rule_models.Bit9Rule.policy.IN(constants.RULE_POLICY.SET_EXECUTION),
        ndb.OR(
            rule_models.Bit9Rule.host_id == '',               # Global rule
            rule_models.Bit9Rule.user_key == self.user.key),  # User's rule
        ancestor=blockable.key)
    # pylint: enable=g-explicit-bool-comparison, singleton-comparison
    has_pending_rules = bool(pending_rule_query.count(limit=1))

    self.respond_json(has_pending_rules) 
Example #10
Source File: role_syncing.py    From upvote with Apache License 2.0 5 votes vote down vote up
def _ChangeModeForGroup(self, mode, group, honor_lock=True):
    """Loads all users in the group and sets the client_mode for their hosts.

    This will make sure that hosts are in the right mode if they are members of
    a group, but will not change mode for non-members. Users can be left out of
    groups to be manaully managed.

    Args:
      mode: The new client_mode to set.
      group: The group of users whose hosts should have a mode change.
      honor_lock: bool, whether the client_mode will be honored.
    """
    logging.info('Changing mode to %s for %s', mode, group)

    group_client = group_utils.GroupManager()
    roster = group_client.AllMembers(group)
    logging.info('Fetched %d user(s) from group %s', len(roster), group)

    # Generate the NDB Keys for all users in the roster.
    user_keys = [
        ndb.Key(user_models.User, email) for email in roster if email]

    # ndb.OR falls over if it gets an empty iterable...
    if not user_keys:
      return

    for user_key_group in iter_utils.Grouper(user_keys, BATCH_SIZE):
      user_key_group = filter(None, user_key_group)
      deferred.defer(
          _ChangeModeForHosts, mode, user_key_group, honor_lock,
          _queue=constants.TASK_QUEUE.DEFAULT) 
Example #11
Source File: role_syncing.py    From upvote with Apache License 2.0 5 votes vote down vote up
def _ChangeModeForHosts(mode, user_keys, honor_lock=True):
  """Performs a client mode change for the specified users' hosts.

  Args:
    mode: The new client_mode to set.
    user_keys: The users whose host modes are to be changed.
    honor_lock: bool, whether the client_mode_lock property will be honored.
  """
  predicates = [
      host_models.SantaHost.primary_user == user_utils.EmailToUsername(key.id())
      for key in user_keys]
  query = host_models.SantaHost.query(ndb.OR(*predicates))
  hosts = query.fetch()
  updated_hosts = []

  for host in hosts:

    # If lock is honored, skip locked users.
    if honor_lock and host.client_mode_lock:
      continue

    # Ignore non-changes also.
    if host.client_mode == mode:
      continue

    # Proceed with the mode change.
    host.client_mode = mode
    host.client_mode_lock = False
    updated_hosts.append(host)

  ndb.put_multi(updated_hosts)
  logging.info(
      'Client mode changed to %s for %d host(s)', mode, len(updated_hosts)) 
Example #12
Source File: handlers_endpoints.py    From luci-py with Apache License 2.0 4 votes vote down vote up
def cancel(self, request):
    """Cancel a subset of pending tasks based on the tags.

    Cancellation happens asynchronously, so when this call returns,
    cancellations will not have completed yet.
    """
    logging.debug('request %s', request)
    if not request.tags:
      # Prevent accidental cancellation of everything.
      raise endpoints.BadRequestException(
          'You must specify tags when cancelling multiple tasks.')

    # Check permission.
    # If the caller has global permission, it can access all tasks.
    # Otherwise, it requires a pool tag to check ACL.
    realms.check_tasks_cancel_acl(request.tags)

    now = utils.utcnow()
    cond = task_result.TaskResultSummary.state == task_result.State.PENDING
    if request.kill_running:
      cond = ndb.OR(
          cond,
          task_result.TaskResultSummary.state == task_result.State.RUNNING)
    q = task_result.TaskResultSummary.query(cond).order(
        task_result.TaskResultSummary.key)
    for tag in request.tags:
      q = q.filter(task_result.TaskResultSummary.tags == tag)

    tasks, cursor = datastore_utils.fetch_page(q, request.limit, request.cursor)

    if tasks:
      payload = json.dumps(
          {
            'tasks': [t.task_id for t in tasks],
            'kill_running': request.kill_running or False,
          })
      ok = utils.enqueue_task(
          '/internal/taskqueue/important/tasks/cancel', 'cancel-tasks',
          payload=payload)
      if not ok:
        raise endpoints.InternalServerErrorException(
            'Could not enqueue cancel request, try again later')
    else:
      logging.info('No tasks to cancel.')

    return swarming_rpcs.TasksCancelResponse(
        cursor=cursor,
        matched=len(tasks),
        now=now)