Python google.appengine.ext.db.get() Examples

The following are 30 code examples of google.appengine.ext.db.get(). 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.db , or try the search function .
Example #1
Source File: input_readers.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def validate(cls, mapper_spec):
    """Validates mapper spec and all mapper parameters.

    Args:
      mapper_spec: The MapperSpec for this InputReader.

    Raises:
      BadReaderParamsError: required parameters are missing or invalid.
    """
    if mapper_spec.input_reader_class() != cls:
      raise BadReaderParamsError("Mapper input reader class mismatch")
    params = _get_params(mapper_spec)
    if cls.BLOB_KEY_PARAM not in params:
      raise BadReaderParamsError("Must specify 'blob_key' for mapper input")
    blob_key = params[cls.BLOB_KEY_PARAM]
    blob_info = blobstore.BlobInfo.get(blobstore.BlobKey(blob_key))
    if not blob_info:
      raise BadReaderParamsError("Could not find blobinfo for key %s" %
                                 blob_key) 
Example #2
Source File: model.py    From pledgeservice with Apache License 2.0 6 votes vote down vote up
def get_count(name):
    total = cache.GetShardedCounterTotal(name)
    if total is None:
      total = 0
      all_keys = ShardedCounter._get_keys_for(name)
      for counter in db.get(all_keys):
        if counter is not None:
          total += counter.count
      logging.info("recalculated counter %s to %s", name, total)

      # Add the stretch check total which is not reflected elsewhere in the counter
      # And is set manually
      # This is here so that is only read out on a cache miss
      stretchCheckTotal = StretchCheckTotal.get()
      if stretchCheckTotal < STRETCH_CACHE_MISS_TOTAL:
        stretchCheckTotal = STRETCH_CACHE_MISS_TOTAL

      total += stretchCheckTotal

      cache.SetShardedCounterTotal(name, total)

    return total 
Example #3
Source File: model.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def _get_descending_key(gettime=time.time):
  """Returns a key name lexically ordered by time descending.

  This lets us have a key name for use with Datastore entities which returns
  rows in time descending order when it is scanned in lexically ascending order,
  allowing us to bypass index building for descending indexes.

  Args:
    gettime: Used for testing.

  Returns:
    A string with a time descending key.
  """
  now_descending = int((_FUTURE_TIME - gettime()) * 100)
  request_id_hash = os.environ.get("REQUEST_ID_HASH")
  if not request_id_hash:
    request_id_hash = str(random.getrandbits(32))
  return "%d%s" % (now_descending, request_id_hash) 
Example #4
Source File: admin.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def UploadCategoryBrowsers(request):
  """Upload browser lists for each category and version level."""
  category = request.REQUEST.get('category')
  version_level = request.REQUEST.get('version_level')
  browsers_str = request.REQUEST.get('browsers')

  if not category:
    return http.HttpResponseServerError(
        'Must set "category".')
  if not version_level.isdigit() or int(version_level) not in range(4):
    return http.HttpResponseServerError(
        'Version level must be an integer 0..3.')
  if not browsers_str:
    return http.HttpResponseServerError(
        'Must set "browsers" (comma-separated list).')

  version_level = int(version_level)
  browsers = browsers_str.split(',')
  result_stats.CategoryBrowserManager.SetBrowsers(
      category, version_level, browsers)
  UpdateSummaryBrowsers(request)
  return http.HttpResponse('Success.') 
Example #5
Source File: model.py    From pledgeservice with Apache License 2.0 6 votes vote down vote up
def request(pledge_key):
    """Indicates that we are allowed to execute the charge at our leisure."""
    charge_key = ChargeStatus._get_charge_key(pledge_key)
    pledge = db.get(pledge_key)
    charge_status = db.get(charge_key)

    if not pledge:
      raise Error('No pledge found with key: %s' % pledge_key)

    if charge_status:
      logging.warning('Requesting already requested charge for pledge: %s',
                      pledge_key)
      return

    charge_status = ChargeStatus(key=charge_key,
                                 request_time=datetime.datetime.now())
    charge_status.put() 
Example #6
Source File: handlers.py    From pledgeservice with Apache License 2.0 6 votes vote down vote up
def get(self):
    util.EnableCors(self)

    WP_PLEDGES = 4099
    VERSION_12_AND_UNDER = 59009 

    count = memcache.get('TOTAL-PLEDGES')
    if not count:
      query = model.Pledge.all(keys_only=True).filter('model_version >', 12)
      i = 0
      while True:
          result = query.fetch(1000)
          i = i + len(result)
          if len(result) < 1000:
              break
          cursor = query.cursor()
          query.with_cursor(cursor)
      count = i + WP_PLEDGES + VERSION_12_AND_UNDER
      memcache.set('TOTAL-PLEDGES', count, 120)

    self.response.headers['Content-Type'] = 'application/json'
    json.dump({'count':count}, self.response) 
Example #7
Source File: test_e2e.py    From pledgeservice with Apache License 2.0 6 votes vote down vote up
def testCreateAddsPledge(self):
    resp = self.makeDefaultRequest()
    pledge = db.get(resp.json['id'])
    self.assertEquals(4200, pledge.amountCents)
    self.assertEquals(resp.json['auth_token'], pledge.url_nonce)
    self.assertEquals('fake_1234', pledge.stripeCustomer)
    self.assertEquals('charge_2468', pledge.stripe_charge_id)
    self.assertEquals('rocket', pledge.team)

    user = model.User.get_by_key_name('pika@pokedex.biz')

    def assertEqualsSampleProperty(prop_name, actual):
      self.assertEquals(self.pledge[prop_name], actual)
    assertEqualsSampleProperty('email', user.email)
    self.assertEquals(u'Pik\u00E1', user.first_name)
    self.assertEquals('Chu', user.last_name)
    assertEqualsSampleProperty('occupation', user.occupation)
    assertEqualsSampleProperty('employer', user.employer)
    assertEqualsSampleProperty('phone', user.phone)
    assertEqualsSampleProperty('target', user.target)
    assertEqualsSampleProperty('subscribe', user.mail_list_optin)
    assert user.url_nonce
    assert not user.from_import 
Example #8
Source File: handlers.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def _processing_limit(self, spec):
    """Get the limit on the number of map calls allowed by this slice.

    Args:
      spec: a Mapreduce spec.

    Returns:
      The limit as a positive int if specified by user. -1 otherwise.
    """
    processing_rate = float(spec.mapper.params.get("processing_rate", 0))
    slice_processing_limit = -1
    if processing_rate > 0:
      slice_processing_limit = int(math.ceil(
          _SLICE_DURATION_SEC*processing_rate/int(spec.mapper.shard_count)))
    return slice_processing_limit

  # Deprecated. Only used by old test cases.
  # TODO(user): clean up tests. 
Example #9
Source File: handlers.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def _schedule_slice(cls,
                      shard_state,
                      tstate,
                      queue_name=None,
                      eta=None,
                      countdown=None):
    """Schedule slice scanning by adding it to the task queue.

    Args:
      shard_state: An instance of ShardState.
      tstate: An instance of TransientShardState.
      queue_name: Optional queue to run on; uses the current queue of
        execution or the default queue if unspecified.
      eta: Absolute time when the MR should execute. May not be specified
        if 'countdown' is also supplied. This may be timezone-aware or
        timezone-naive.
      countdown: Time in seconds into the future that this MR should execute.
        Defaults to zero.
    """
    queue_name = queue_name or os.environ.get("HTTP_X_APPENGINE_QUEUENAME",
                                              "default")
    task = cls._state_to_task(tstate, shard_state, eta, countdown)
    cls._add_task(task, tstate.mapreduce_spec, queue_name) 
Example #10
Source File: handlers.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def _get_required_param(self, param_name):
    """Get a required request parameter.

    Args:
      param_name: name of request parameter to fetch.

    Returns:
      parameter value

    Raises:
      errors.NotEnoughArgumentsError: if parameter is not specified.
    """
    value = self.request.get(param_name)
    if not value:
      raise errors.NotEnoughArgumentsError(param_name + " not specified")
    return value 
Example #11
Source File: handlers.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def _get_required_param(self, param_name):
    """Get a required request parameter.

    Args:
      param_name: name of request parameter to fetch.

    Returns:
      parameter value

    Raises:
      errors.NotEnoughArgumentsError: if parameter is not specified.
    """
    value = self.request.get(param_name)
    if not value:
      raise errors.NotEnoughArgumentsError(param_name + " not specified")
    return value 
Example #12
Source File: pipeline.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def get_callback_task(self, *args, **kwargs):
    """Returns a task for calling back this Pipeline.

    Args:
      params: Keyword argument containing a dictionary of key/value pairs
        that will be passed to the callback when it is executed.
      args, kwargs: Passed to the taskqueue.Task constructor. Use these
        arguments to set the task name (for idempotence), etc.

    Returns:
      A taskqueue.Task instance that must be enqueued by the caller.
    """
    if not self.async:
      raise UnexpectedPipelineError(
          'May only call get_callback_task() method for asynchronous pipelines.')

    params = kwargs.get('params', {})
    kwargs['params'] = params
    params['pipeline_id'] = self._pipeline_key.name()
    kwargs['url'] = self.base_path + '/callback'
    kwargs['method'] = 'POST'
    return taskqueue.Task(*args, **kwargs) 
Example #13
Source File: input_readers.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def __iter__(self):
    """Iterate over records in file.

    Yields records as strings.
    """
    ctx = context.get()

    while self._reader:
      try:
        start_time = time.time()
        record = self._reader.read()
        if ctx:
          operation.counters.Increment(
              COUNTER_IO_READ_MSEC, int((time.time() - start_time) * 1000))(ctx)
          operation.counters.Increment(COUNTER_IO_READ_BYTES, len(record))(ctx)
        yield record
      except (files.ExistenceError), e:
        raise errors.FailJobError("ExistenceError: %s" % e)
      except (files.UnknownError), e:
        raise errors.RetrySliceError("UnknownError: %s" % e) 
Example #14
Source File: input_readers.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def _get_query_spec(cls, mapper_spec):
    """Construct a model.QuerySpec from model.MapperSpec."""
    params = _get_params(mapper_spec)
    entity_kind = params[cls.ENTITY_KIND_PARAM]
    filters = params.get(cls.FILTERS_PARAM)
    app = params.get(cls._APP_PARAM)
    ns = params.get(cls.NAMESPACE_PARAM)

    return model.QuerySpec(
        entity_kind=cls._get_raw_entity_kind(entity_kind),
        keys_only=bool(params.get(cls.KEY_RANGE_PARAM, False)),
        filters=filters,
        batch_size=int(params.get(cls.BATCH_SIZE_PARAM, cls._BATCH_SIZE)),
        model_class_path=entity_kind,
        app=app,
        ns=ns) 
Example #15
Source File: input_readers.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def split_input(cls, mapper_spec):
    """Returns a list of input readers for the input spec.

    Args:
      mapper_spec: The MapperSpec for this InputReader.

    Returns:
      A list of InputReaders.
    """
    batch_size = int(_get_params(mapper_spec).get(
        cls.BATCH_SIZE_PARAM, cls._BATCH_SIZE))
    shard_count = mapper_spec.shard_count
    namespace_ranges = namespace_range.NamespaceRange.split(shard_count,
                                                            contiguous=True)
    return [NamespaceInputReader(ns_range, batch_size)
            for ns_range in namespace_ranges] 
Example #16
Source File: input_readers.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def _read(self, entry):
    """Read entry content.

    Args:
      entry: zip file entry as zipfile.ZipInfo.
    Returns:
      Entry content as string.
    """
    start_time = time.time()
    content = self._zip.read(entry.filename)

    ctx = context.get()
    if ctx:
      operation.counters.Increment(COUNTER_IO_READ_BYTES, len(content))(ctx)
      operation.counters.Increment(
          COUNTER_IO_READ_MSEC, int((time.time() - start_time) * 1000))(ctx)

    return content 
Example #17
Source File: test_e2e.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def testNoPledgeType(self):
    del self.pledge['pledgeType']
    resp = self.makeDefaultRequest()
    pledge = db.get(resp.json['id'])
    self.assertEquals('CONDITIONAL', pledge.pledge_type) 
Example #18
Source File: test_e2e.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def testUserInfoNoPledge(self):
    self.makeDefaultRequest()
    self.assertEquals(1, model.Pledge.all().count())
    user = model.User.get_by_key_name('pika@pokedex.biz')
    model.Pledge.all().filter('email =', user.email)[0].delete()
    resp = self.app.get('/user-info/%s' % user.url_nonce, status=404)
    self.assertEquals('user not found', resp.body) 
Example #19
Source File: test_e2e.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def testTotal(self):
    resp = self.app.get('/r/total')
    self.assertEquals(self.balance_baseline, resp.json['totalCents'])
    self.makeDefaultRequest()

    resp = self.app.get('/r/total')
    self.assertEquals(self.balance_baseline + 4200, resp.json['totalCents']) 
Example #20
Source File: model.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def get():
    return Secrets.get_or_insert(key_name=Secrets.SINGLETON_KEY) 
Example #21
Source File: pipeline.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def transition_aborted(self, pipeline_key):
    """Makes the given pipeline as having aborted.

    Does nothing if the pipeline is in a bad state.

    Args:
      pipeline_key: db.Key of the _PipelineRecord that needs to be retried.
    """
    def txn():
      pipeline_record = db.get(pipeline_key)
      if pipeline_record is None:
        logging.warning(
            'Tried to abort pipeline ID "%s" but it does not exist.',
            pipeline_key.name())
        raise db.Rollback()
      if pipeline_record.status not in (
             _PipelineRecord.WAITING, _PipelineRecord.RUN):
        logging.warning(
            'Tried to abort pipeline ID "%s", found bad state: %s',
            pipeline_key.name(), pipeline_record.status)
        raise db.Rollback()

      pipeline_record.status = _PipelineRecord.ABORTED
      pipeline_record.finalized_time = self._gettime()
      pipeline_record.put()

    db.run_in_transaction(txn)

################################################################################ 
Example #22
Source File: pipeline.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def transition_complete(self, pipeline_key):
    """Marks the given pipeline as complete.

    Does nothing if the pipeline is no longer in a state that can be completed.

    Args:
      pipeline_key: db.Key of the _PipelineRecord that has completed.
    """
    def txn():
      pipeline_record = db.get(pipeline_key)
      if pipeline_record is None:
        logging.warning(
            'Tried to mark pipeline ID "%s" as complete but it does not exist.',
            pipeline_key.name())
        raise db.Rollback()
      if pipeline_record.status not in (
             _PipelineRecord.WAITING, _PipelineRecord.RUN):
        logging.warning(
            'Tried to mark pipeline ID "%s" as complete, found bad state: %s',
            pipeline_key.name(), pipeline_record.status)
        raise db.Rollback()

      pipeline_record.status = _PipelineRecord.DONE
      pipeline_record.finalized_time = self._gettime()
      pipeline_record.put()

    db.run_in_transaction(txn) 
Example #23
Source File: pipeline.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def begin_abort(self, root_pipeline_key, abort_message):
    """Kicks off the abort process for a root pipeline and all its children.

    Args:
      root_pipeline_key: db.Key of the root pipeline to abort.
      abort_message: Message explaining why the abort happened, only saved
          into the root pipeline.

    Returns:
      True if the abort signal was sent successfully; False otherwise.
    """
    def txn():
      pipeline_record = db.get(root_pipeline_key)
      if pipeline_record is None:
        logging.warning(
            'Tried to abort root pipeline ID "%s" but it does not exist.',
            root_pipeline_key.name())
        raise db.Rollback()
      if pipeline_record.status == _PipelineRecord.ABORTED:
        logging.warning(
            'Tried to abort root pipeline ID "%s"; already in state: %s',
            root_pipeline_key.name(), pipeline_record.status)
        raise db.Rollback()
      if pipeline_record.abort_requested:
        logging.warning(
            'Tried to abort root pipeline ID "%s"; abort signal already sent.',
            root_pipeline_key.name())
        raise db.Rollback()

      pipeline_record.abort_requested = True
      pipeline_record.abort_message = abort_message
      pipeline_record.put()

      task = taskqueue.Task(
          url=self.fanout_abort_handler_path,
          params=dict(root_pipeline_key=root_pipeline_key))
      task.add(queue_name=self.queue_name, transactional=True)
      return True

    return db.run_in_transaction(txn) 
Example #24
Source File: pipeline.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def _set_class_path(cls, module_dict=sys.modules):
    """Sets the absolute path to this class as a string.

    Used by the Pipeline API to reconstruct the Pipeline sub-class object
    at execution time instead of passing around a serialized function.

    Args:
      module_dict: Used for testing.
    """
    # Do not traverse the class hierarchy fetching the class path attribute.
    found = cls.__dict__.get('_class_path')
    if found is not None:
      return

    # Do not set the _class_path for the base-class, otherwise all children's
    # lookups for _class_path will fall through and return 'Pipeline' above.
    # This situation can happen if users call the generic Pipeline.from_id
    # to get the result of a Pipeline without knowing its specific class.
    if cls is Pipeline:
      return

    # This is a brute-force approach to solving the module reverse-lookup
    # problem, where we want to refer to a class by its stable module name
    # but have no built-in facility for doing so in Python.
    found = None
    for name, module in module_dict.items():
      if name == '__main__':
        continue
      found = getattr(module, cls.__name__, None)
      if found is cls:
        break
    else:
      # If all else fails, try the main module.
      name = '__main__'
      module = module_dict.get(name)
      found = getattr(module, cls.__name__, None)
      if found is not cls:
        raise ImportError('Could not determine path for Pipeline '
                          'function/class "%s"' % cls.__name__)

    cls._class_path = '%s.%s' % (name, cls.__name__) 
Example #25
Source File: model.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def update(cls, newTotal):
    total = cls.all().get()
    if total:
      total.dollars = newTotal
      total.put()
    else:
      newTotalForDB = cls(dollars = newTotal)
      newTotalForDB.put() 
Example #26
Source File: handlers.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def get(self, id):
    try:
      pledge = db.get(db.Key(id))
    except db.BadKeyError, e:
      logging.warning('Bad key error: %s', e)
      self.error(404)
      self.response.write('Not found')
      return 
Example #27
Source File: input_readers.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def split_input(cls, mapper_spec):
    """Splits input into key ranges."""
    readers = super(ConsistentKeyReader, cls).split_input(mapper_spec)
    start_time_us = _get_params(mapper_spec).get(
        cls.START_TIME_US_PARAM, long(time.time() * 1e6))
    for reader in readers:
      reader.start_time_us = start_time_us
    return readers 
Example #28
Source File: input_readers.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def _apply_jobs(self, unapplied_jobs):
    """Apply all jobs implied by the given keys."""
    # There were some unapplied jobs. Roll them forward.
    keys_to_apply = []
    for key in unapplied_jobs:
      # To apply the entity group we need to read something from it.
      # We use dummy kind and id because we don't actually need any data.
      path = key.to_path() + [ConsistentKeyReader.DUMMY_KIND,
                              ConsistentKeyReader.DUMMY_ID]
      keys_to_apply.append(
          db.Key.from_path(_app=key.app(), namespace=key.namespace(), *path))
    db.get(keys_to_apply, config=datastore_rpc.Configuration(
        deadline=self.UNAPPLIED_QUERY_DEADLINE,
        read_policy=datastore_rpc.Configuration.APPLY_ALL_JOBS_CONSISTENCY)) 
Example #29
Source File: handlers.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def get(self):
    util.EnableCors(self)
    env = self.app.config['env']
    if not env.stripe_public_key:
      raise Error('No stripe public key in DB')
    params = dict(testMode=(env.app_name == u'local'),
                  stripePublicKey=env.stripe_public_key)

    self.response.headers['Content-Type'] = 'application/json'
    json.dump(params, self.response) 
Example #30
Source File: pipeline.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def post(self):
    if 'HTTP_X_APPENGINE_TASKNAME' not in self.request.environ:
      self.response.set_status(403)
      return

    context = _PipelineContext.from_environ(self.request.environ)
    context.continue_abort(
        self.request.get('root_pipeline_key'),
        self.request.get('cursor'))