Python google.appengine.ext.ndb.Key() Examples

The following are 30 code examples for showing how to use google.appengine.ext.ndb.Key(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

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
Project: loaner   Author: google   File: auth_test.py    License: Apache License 2.0 6 votes vote down vote up
def setUp(self):
    super(LoanerEndpointsTest, self).setUp()
    self.service = FakeApiPermissionChecks()
    self.request = message_types.VoidMessage()

    user_model.Role.create(
        'technical_admin',
        role_permissions=[permissions.Permissions.AUDIT_SHELF],
        associated_group=loanertest.TECHNICAL_ADMIN_EMAIL)

    user_model.User(
        id=loanertest.SUPER_ADMIN_EMAIL, superadmin=True).put()
    user_model.User(
        id=loanertest.TECHNICAL_ADMIN_EMAIL,
        roles=[ndb.Key(user_model.Role, 'technical_admin')]).put()
    user_model.User(id=loanertest.USER_EMAIL).put() 
Example 2
Project: loaner   Author: google   File: template_model.py    License: Apache License 2.0 6 votes vote down vote up
def create(cls, name, title=None, body=None):
    """Creates a model and entity."""
    if not name:
      raise datastore_errors.BadValueError(
          'The Template name must not be empty.')
    entity = cls(title=title,
                 body=body)
    template = cls.get_by_id(name)
    if template is not None:
      raise datastore_errors.BadValueError(
          'Create template: A Template entity with name %r already exists.' %
          name)
    entity.key = ndb.Key(cls, name)
    entity.put()
    logging.info('Creating a new template with name %r.', name)
    cls.cached_templates = []
    return entity 
Example 3
Project: loaner   Author: google   File: user_model.py    License: Apache License 2.0 6 votes vote down vote up
def create(cls, name, role_permissions=None, associated_group=None):
    """Creates a new role.

    Args:
      name: str, name of the new role.
      role_permissions: list|str|, zero or more Permissions to include.
      associated_group: str, name of the Google Group (or other permission
        container) used to associate this group of permissions to users.

    Returns:
      Created Role.

    Raises:
      CreateRoleError: If a role fails to be created.
    """
    if name == 'superadmin':
      raise CreateRoleError(_SUPERADMIN_RESERVED_ERROR)
    if cls.get_by_name(name):
      raise CreateRoleError(_ROLE_ALREADY_EXISTS, name)
    new_role = cls(
        key=ndb.Key(cls, name),
        permissions=role_permissions or [],
        associated_group=associated_group)
    new_role.put()
    return new_role 
Example 4
Project: loaner   Author: google   File: api_utils.py    License: Apache License 2.0 6 votes vote down vote up
def get_ndb_key(urlsafe_key):
  """Builds an ndb.Key from a urlsafe key.

  Args:
    urlsafe_key: str, A urlsafe ndb.Key to cast into an ndb.Key.

  Returns:
    An ndb.Key instance.

  Raises:
    endpoints.BadRequestException: if the creation of the ndb.Key fails.
  """
  try:
    return ndb.Key(urlsafe=urlsafe_key)
  except Exception:  # pylint: disable=broad-except
    raise endpoints.BadRequestException(_CORRUPT_KEY_MSG) 
Example 5
Project: Sony-PMCA-RE   Author: ma1co   File: main.py    License: MIT License 6 votes vote down vote up
def post(self):
  data = self.request.body
  dataDict = marketserver.parsePostData(data)
  taskKey = int(dataDict.get('session', {}).get('correlationid', 0))
  task = ndb.Key(Task, taskKey).get()
  if not task:
   return self.error(404)
  if not task.completed and task.app:
   response = marketserver.getJsonInstallResponse('App', self.uri_for('appSpk', appId = task.app, _full = True))
  else:
   response = marketserver.getJsonResponse()
  task.completed = True
  task.response = data
  task.put()
  updateAppStats(dataDict)
  self.output(marketserver.constants.jsonMimeType, response) 
Example 6
Project: locality-sensitive-hashing   Author: singhj   File: read_tweepy.py    License: MIT License 6 votes vote down vote up
def launch_lsh_calc(self):
        # store tweets and kick off run_lsh

        tw_from = self.cursor
        tw_till = len(self.tweets)
        dui = ndb.Key(urlsafe = self.duik).get()
        dui = dui.extend_tweets(self.tweets[tw_from:tw_till])
        self.cursor = len(self.tweets)

        if not self.matrix:
            Matrix._initialize()
            MatrixRow._initialize()
            self.matrix = Matrix.create(filename = dui.filename(), source = 'tweets', file_key = self.duik)
            if self.matrix:
                dui = dui.set_ds_key(self.matrix.ds_key)
        if self.matrix:
            timestamp = datetime.datetime.utcnow().isoformat()
            deferred.defer(run_lsh, self.duik, self.tweets[tw_from:tw_till], self.matrix.ds_key, tw_from, timestamp)
        else:
            frameinfo = getframeinfo(currentframe())
            logging.error('file %s, line %s Matrix is missing', frameinfo.filename, frameinfo.lineno+1) 
Example 7
Project: sndlatr   Author: Schibum   File: __init__.py    License: Apache License 2.0 6 votes vote down vote up
def init_zoneinfo():
    """
    Add each zone info to the datastore. This will overwrite existing zones.

    This must be called before the AppengineTimezoneLoader will work.
    """
    import os, logging
    from zipfile import ZipFile
    zoneobjs = []

    zoneinfo_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
      'zoneinfo.zip'))

    with ZipFile(zoneinfo_path) as zf:
        for zfi in zf.filelist:
            key = ndb.Key('Zoneinfo', zfi.filename, namespace=NDB_NAMESPACE)
            zobj = Zoneinfo(key=key, data=zf.read(zfi))
            zoneobjs.append(zobj)

    logging.info("Adding %d timezones to the pytz-appengine database" %
        len(zoneobjs)
        )

    ndb.put_multi(zoneobjs) 
Example 8
Project: sndlatr   Author: Schibum   File: __init__.py    License: Apache License 2.0 6 votes vote down vote up
def open_resource(name):
    """Load the object from the datastore"""
    import logging
    from cStringIO import StringIO
    try:
        data = ndb.Key('Zoneinfo', name, namespace=NDB_NAMESPACE).get().data
    except AttributeError:
        # Missing zone info; test for GMT - which would be there if the 
        # Zoneinfo has been initialized.
        if ndb.Key('Zoneinfo', 'GMT', namespace=NDB_NAMESPACE).get():
          # the user asked for a zone that doesn't seem to exist.
          logging.exception("Requested zone '%s' is not in the database." %
              name)
          raise

        # we need to initialize the database
        init_zoneinfo()
        return open_resource(name)

    return StringIO(data) 
Example 9
Project: RSSNewsGAE   Author: liantian-cn   File: background.py    License: Apache License 2.0 6 votes vote down vote up
def push_important_news():
    key = request.values.get('key')
    news = ndb.Key(urlsafe=key).get()
    form_fields = {
        "token": app.config["PUSHOVER_APP_KEY"],
        "user": app.config["PUSHOVER_USER_KEY"],
        "message": news.summary.encode("utf-8"),
        "url": news.link.encode("utf-8"),
        "url_title": u"点击访问正文".encode("utf-8"),
        "title": news.title.encode("utf-8"),
    }
    form_data = urllib.urlencode(form_fields)
    urlfetch.fetch(url=app.config["PUSH_OVER_URL"],
                   payload=form_data,
                   method=urlfetch.POST,
                   headers={'Content-Type': 'application/x-www-form-urlencoded'},
                   follow_redirects=False,
                   validate_certificate=False)
    return "Done", 200 
Example 10
Project: graphene-gae   Author: graphql-python   File: test_object_identification.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_correctly_fetches_id_name_rebels(self):
        query = '''
            query RebelsQuery {
              rebels {
                id,
                name
              }
            }
          '''
        expected = {
            'rebels': {
                'id': to_global_id('Faction', ndb.Key('Faction', 'rebels').urlsafe()),
                'name': 'Alliance to Restore the Republic'
            }
        }
        result = schema.execute(query)
        self.assertFalse(result.errors, msg=str(result.errors))
        self.assertDictEqual(result.data, expected) 
Example 11
Project: graphene-gae   Author: graphql-python   File: test_object_identification.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_correctly_refetches_rebels(self):
        rebels_key = to_global_id('Faction', ndb.Key('Faction', 'rebels').urlsafe())
        query = '''
            query RebelsRefetchQuery {
              node(id: "%s") {
                id
                ... on Faction {
                  name
                }
              }
            }
          ''' % rebels_key

        expected = {
            'node': {
                'id': rebels_key,
                'name': 'Alliance to Restore the Republic'
            }
        }
        result = schema.execute(query)
        self.assertFalse(result.errors, msg=str(result.errors))
        self.assertDictEqual(result.data, expected) 
Example 12
Project: graphene-gae   Author: graphql-python   File: test_object_identification.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_correctly_fetches_id_name_empire(self):
        empire_key = to_global_id('Faction', ndb.Key('Faction', 'empire').urlsafe())
        query = '''
          query EmpireQuery {
            empire {
              id
              name
            }
          }
        '''
        expected = {
            'empire': {
                'id': empire_key,
                'name': 'Galactic Empire'
            }
        }
        result = schema.execute(query)
        self.assertFalse(result.errors, msg=str(result.errors))
        self.assertDictEqual(result.data, expected) 
Example 13
Project: graphene-gae   Author: graphql-python   File: test_object_identification.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_correctly_refetches_id_name_empire(self):
        empire_key = to_global_id('Faction', ndb.Key('Faction', 'empire').urlsafe())
        query = '''
            query EmpireRefetchQuery {
              node(id: "%s") {
                id
                ... on Faction {
                  name
                }
              }
            }
          ''' % empire_key
        expected = {
            'node': {
                'id': empire_key,
                'name': 'Galactic Empire'
            }
        }
        result = schema.execute(query)
        self.assertFalse(result.errors, msg=str(result.errors))
        self.assertDictEqual(result.data, expected) 
Example 14
Project: ray   Author: felipevolpone   File: all.py    License: MIT License 6 votes vote down vote up
def to_instance(cls, json_attributes):
        keys_and_properties = cls._get_keys_and_properties()

        ancestor_kind, ancestor_key = cls.ancestor()
        if not ancestor_kind:
            instance = cls()
        else:
            if ancestor_key not in json_attributes:
                raise Exception('the ancestor key was not found')

            instance = cls(parent=ndb.Key(ancestor_kind, json_attributes[ancestor_key]))
            del json_attributes[ancestor_key]

        for field_name in json_attributes.keys():
            value = json_attributes[field_name]
            if field_name in keys_and_properties:
                json_value = json_attributes[field_name]
                kind, repeated = keys_and_properties[field_name]
                if repeated:
                    value = [ndb.Key(kind, val) for val in json_value]
                else:
                    value = ndb.Key(kind, json_value)

            setattr(instance, field_name, value)
        return instance 
Example 15
Project: hackernewsbot   Author: phil-r   File: main.py    License: MIT License 5 votes vote down vote up
def story_redirect(short_id):
  """Redirect to story url"""
  try:
    story_id = str(shortener.decode(short_id))
  except:
    return abort(400)
  redirect_url = memcache.get(story_id)
  if not redirect_url:
    story = ndb.Key(StoryPost, story_id).get()
    if not story:
      return make_response('<h1>Service Unavailable</h1><p>Try again later</p>', 503, {'Retry-After': 5})
    story.add_memcache()
    redirect_url = story.url
  return redirect(redirect_url) 
Example 16
Project: loaner   Author: google   File: base_model.py    License: Apache License 2.0 5 votes vote down vote up
def _to_search_fields(self, key, value):
    """Converts an ndb.Property into a search document field.

    Args:
      key: str, the name of the field.
      value: ndb.Property, the value to convert.

    Returns:
      A list of search fields created from the specified property. Repeated
          properties will create one field per item.
    """
    if value is None:
      return []

    if isinstance(value, list):
      search_fields = []
      for val in value:
        search_fields.extend(self._to_search_fields(key, val))
      return search_fields

    if isinstance(value, ndb.Key):
      return [search.AtomField(name=key, value=value.urlsafe())]

    if isinstance(value, (datetime.datetime, datetime.date)):
      return [search.DateField(name=key, value=value)]

    if isinstance(value, bool):
      return [search.AtomField(name=key, value=str(value))]

    if isinstance(value, numbers.Number) and not isinstance(value, bool):
      return [search.NumberField(name=key, value=value)]

    if isinstance(value, ndb.GeoPt):
      return [search.GeoField(
          name=key, value=search.GeoPoint(value.lat, value.lon))]

    return [
        search.TextField(name=key, value=six.text_type(value)),
        search.AtomField(name=key, value=six.text_type(value))
    ] 
Example 17
Project: loaner   Author: google   File: base_model.py    License: Apache License 2.0 5 votes vote down vote up
def _serialize(value):
  """Serializes a complex ndb type.

  Args:
    value: A ndb type to be serialized.

  Returns:
    Value serialized to simple data type such as integer or string.
  """
  if isinstance(value, datetime.datetime):
    return utils.datetime_to_unix(value)
  elif isinstance(value, (ndb.Key, ndb.GeoPt, messages.Enum)):
    return str(value)
  else:
    return value 
Example 18
Project: loaner   Author: google   File: base_model_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_to_seach_fields(self):
    # Test list field generation.
    entity = TestEntity(test_repeatedprop=['item_1', 'item_2'])
    search_fields = entity._to_search_fields(
        'test_repeatedprop', ['item_1', 'item_2'])
    expected_fields = [
        search.TextField(name='test_repeatedprop', value='item_1'),
        search.AtomField(name='test_repeatedprop', value='item_1'),
        search.TextField(name='test_repeatedprop', value='item_2'),
        search.AtomField(name='test_repeatedprop', value='item_2')]
    self.assertEqual(expected_fields, search_fields)

    # Test ndb.Key field generation.
    test_key = ndb.Key('Test', 1)
    entity = TestEntity(test_keyproperty=test_key)
    search_field = entity._to_search_fields('test_keyproperty', test_key)
    expected_field = [search.AtomField(
        name='test_keyproperty', value=test_key.urlsafe())]
    self.assertEqual(expected_field, search_field)

    # Test datetime field generation.
    date = datetime.datetime(year=2017, month=1, day=5)
    entity = TestEntity(test_datetime=date)
    search_field = entity._to_search_fields('test_datetime', date)
    expected_field = [search.DateField(name='test_datetime', value=date)]
    self.assertEqual(expected_field, search_field)

    # Test boolean field generation.
    entity = TestEntity(test_bool=True)
    search_field = entity._to_search_fields('test_bool', True)
    expected_field = [search.AtomField(name='test_bool', value='True')]
    self.assertEqual(expected_field, search_field)

    # Test geopt field generation.
    geopt = ndb.GeoPt('52.37, 4.88')
    entity = TestEntity(test_geopt=geopt)
    search_field = entity._to_search_fields('test_geopt', geopt)
    expected_field = [search.GeoField(
        name='test_geopt', value=search.GeoPoint(52.37, 4.88))]
    self.assertEqual(expected_field, search_field) 
Example 19
Project: loaner   Author: google   File: fleet_model.py    License: Apache License 2.0 5 votes vote down vote up
def get_by_name(cls, name):
    """Gets a fleet by its name.

    Args:
      name: str, name of the fleet.

    Returns:
      Fleet object.
    """
    return ndb.Key(cls, name).get() 
Example 20
Project: loaner   Author: google   File: tag_model.py    License: Apache License 2.0 5 votes vote down vote up
def _pre_delete_hook(cls, key):
    """Cleans up any entities that reference the key.

    Note that this operation can have a long tail in that it requires a bulk tag
    disassociation across potentially many entities in Datastore.

    Args:
      key: ndb.Key, a Tag model key.
    """
    logging.info(
        'Destroying the tag with urlsafe key %r and name %r.',
        key.urlsafe(), key.get().name)
    for model in _MODELS_WITH_TAGS:
      deferred.defer(_delete_tags, model, key.get()) 
Example 21
Project: loaner   Author: google   File: user_model.py    License: Apache License 2.0 5 votes vote down vote up
def get_by_name(cls, name):
    """Gets a role by its name.

    Args:
      name: str, name of the role.

    Returns:
      Role object.
    """
    return ndb.Key(cls, name).get() 
Example 22
Project: loaner   Author: google   File: api_utils_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_get_ndb_key_not_found(self):
    """Test the get of an ndb.Key, raises endpoints.BadRequestException."""
    with self.assertRaisesRegexp(
        endpoints.BadRequestException,
        api_utils._CORRUPT_KEY_MSG):
      api_utils.get_ndb_key('corruptKey') 
Example 23
Project: loaner   Author: google   File: sync_users.py    License: Apache License 2.0 5 votes vote down vote up
def _add_or_remove_user_roles(group_users, role):
  """Add or remove a user's role based on Google group membership.

  This will check the datastore users that are passed (ndb_users) against the
  users in the Google group that are passed (group_users). It will do nothing
  with the union of both. It will add the given role to the users that are in
  the Google group and not in the datastore users (creating a user object in
  datastore if nessesary). Similarly, it will remove the user permissions if
  the ndb_users are not in group_users.

  Args:
    group_users: list, a list of users from a Google Group.
    role: str, the role to add or remove from user.
  """
  if role == 'superadmin':
    users_keys = user_model.User.query(
        user_model.User.superadmin == True).fetch(keys_only=True)  # pylint: disable=g-explicit-bool-comparison,singleton-comparison
  else:
    role_key = ndb.Key(user_model.Role, role)
    users_keys = user_model.User.query(
        user_model.User.roles.IN([role_key])).fetch(keys_only=True)
  ndb_user_ids = [user.id() for user in users_keys]
  users_to_add_role = set(group_users) - set(ndb_user_ids)
  users_to_remove_role = set(ndb_user_ids) - set(group_users)
  for user_email in users_to_add_role:
    user = user_model.User.get_user(user_email)
    if role == 'superadmin':
      user.update(superadmin=True)
    else:
      new_roles = user.role_names
      new_roles.append(role)
      user.update(roles=new_roles)
  for user_email in users_to_remove_role:
    user = user_model.User.get_user(user_email)
    if role == 'superadmin':
      user.update(superadmin=False)
    else:
      new_roles = user.role_names
      new_roles.remove(role)
      user.update(roles=new_roles) 
Example 24
Project: gae-secure-scaffold-python   Author: google   File: models.py    License: Apache License 2.0 5 votes vote down vote up
def GetApplicationConfiguration():
  """Returns the application configuration, creating it if necessary."""
  key = ndb.Key(Config, 'config')
  entity = key.get()
  if not entity:
    entity = Config(key=key)
    entity.xsrf_key = os.urandom(16)
    entity.put()
  return entity 
Example 25
Project: browserscope   Author: elsigh   File: context.py    License: Apache License 2.0 5 votes vote down vote up
def _normalize_key(value):
  """Return a key from an entity, model instance, key, or key string."""
  if ndb is not None and isinstance(value, (ndb.Model, ndb.Key)):
    return None
  if getattr(value, "key", None):
    return value.key()
  elif isinstance(value, basestring):
    return datastore.Key(value)
  else:
    return value 
Example 26
Project: github-stats   Author: lipis   File: util.py    License: MIT License 5 votes vote down vote up
def param(name, cast=None):
  def switch(case):
    return {
      int: wf.Int(missing=None),
      float: wf.Float(missing=None),
      bool: wf.Bool(missing=None),
      list: wf.DelimitedList(wf.Str(), delimiter=',', missing=[]),
    }.get(case)
  if cast is None or cast is ndb.Key:
    cast_ = wf.Str(missing=None)
  else:
    cast_ = switch(cast) or cast
  args = parser.parse({name: cast_})
  value = args[name]
  return ndb.Key(urlsafe=value) if cast is ndb.Key and value else value 
Example 27
Project: github-stats   Author: lipis   File: user.py    License: MIT License 5 votes vote down vote up
def get(self):
    user_keys = util.param('user_keys', list)
    if user_keys:
      user_db_keys = [ndb.Key(urlsafe=k) for k in user_keys]
      user_dbs = ndb.get_multi(user_db_keys)
      return helpers.make_response(user_dbs, model.User.FIELDS)

    user_dbs, cursors = model.User.get_dbs(prev_cursor=True)
    return helpers.make_response(user_dbs, model.User.FIELDS, cursors) 
Example 28
Project: github-stats   Author: lipis   File: user.py    License: MIT License 5 votes vote down vote up
def delete(self):
    user_keys = util.param('user_keys', list)
    if not user_keys:
      helpers.make_not_found_exception('User(s) %s not found' % user_keys)
    user_db_keys = [ndb.Key(urlsafe=k) for k in user_keys]
    delete_user_dbs(user_db_keys)
    return flask.jsonify({
      'result': user_keys,
      'status': 'success',
    }) 
Example 29
Project: github-stats   Author: lipis   File: user.py    License: MIT License 5 votes vote down vote up
def get(self, user_key):
    user_db = ndb.Key(urlsafe=user_key).get()
    if not user_db:
      helpers.make_not_found_exception('User %s not found' % user_key)
    return helpers.make_response(user_db, model.User.FIELDS) 
Example 30
Project: github-stats   Author: lipis   File: repo.py    License: MIT License 5 votes vote down vote up
def get(self, repo_key):
    repo_db = ndb.Key(urlsafe=repo_key).get()
    if not repo_db:
      helpers.make_not_found_exception('Repo %s not found' % repo_key)
    return helpers.make_response(repo_db, model.Repo.FIELDS)


###############################################################################
# Admin
###############################################################################