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

The following are 30 code examples of google.appengine.ext.ndb.Key(). 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: __init__.py    From sndlatr with 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 #2
Source File: user_model.py    From loaner with 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 #3
Source File: api_utils.py    From loaner with 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 #4
Source File: template_model.py    From loaner with 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 #5
Source File: auth_test.py    From loaner with 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 #6
Source File: test_object_identification.py    From graphene-gae with 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 #7
Source File: all.py    From ray with 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 #8
Source File: test_object_identification.py    From graphene-gae with 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 #9
Source File: test_object_identification.py    From graphene-gae with 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 #10
Source File: test_object_identification.py    From graphene-gae with 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
Source File: appengine.py    From earthengine with MIT License 6 votes vote down vote up
def _delete_entity(self):
    """Delete entity from datastore.

    Attempts to delete using the key_name stored on the object, whether or not
    the given key is in the datastore.
    """
    if self._is_ndb():
      ndb.Key(self._model, self._key_name).delete()
    else:
      entity_key = db.Key.from_path(self._model.kind(), self._key_name)
      db.delete(entity_key) 
Example #12
Source File: main.py    From Sony-PMCA-RE with 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 #13
Source File: read_tweepy.py    From locality-sensitive-hashing with 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 #14
Source File: background.py    From RSSNewsGAE with 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 #15
Source File: __init__.py    From sndlatr with 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 #16
Source File: admin.py    From RSSNewsGAE with Apache License 2.0 5 votes vote down vote up
def keyword_switch():
    content = request.get_json(silent=True)
    if content is not None:
        action = content['action']
        key = content['key']
        keyword = ndb.Key(urlsafe=key).get()
        keyword.enable = not keyword.enable
        keyword.put()
        flush_keyword_cache()
        return jsonify(result=True, key=key, action=action)
    else:
        return jsonify(result=False) 
Example #17
Source File: admin.py    From RSSNewsGAE with Apache License 2.0 5 votes vote down vote up
def del_keyword(key):
    ndb.Key(urlsafe=key).delete()
    flush_keyword_cache()
    return redirect(url_for("admin")) 
Example #18
Source File: dschat.py    From dschat with MIT License 5 votes vote down vote up
def sessions_key():
    """Constructs a Datastore key for the Sessions table.
    """
    return ndb.Key('Sessions', 'All') 
Example #19
Source File: background.py    From RSSNewsGAE with Apache License 2.0 5 votes vote down vote up
def collect_keyword_for_one_news():
    user_key_word = get_pure_keyword()
    key = request.values.get('key')
    news = ndb.Key(urlsafe=key).get()
    form_fields = {
        "text": news.summary.encode("utf-8"),
        "topK": app.config["TOP_KEYWORD"],
        "withWeight": 0
    }
    form_data = urllib.urlencode(form_fields)
    result = urlfetch.fetch(url=app.config["JIEBA_API"],
                            payload=form_data,
                            method=urlfetch.POST,
                            headers={'Content-Type': 'application/x-www-form-urlencoded'},
                            follow_redirects=False)
    json_content = json.loads(result.content)
    key_words = json_content["result"]
    del news.key_word[:]
    news.key_word = key_words
    tmp = [val for val in key_words if val in user_key_word]
    if tmp:
        news.important = True
    if tmp and app.config["PUSHOVER"]:
        taskqueue.add(queue_name='push-msg-queue',
                      url=url_for("push_important_news"),
                      method='POST',
                      params={"key": key})
    news.put()
    return "Done", 200 
Example #20
Source File: utils_db.py    From gsc-logger with Apache License 2.0 5 votes vote down vote up
def site_key(site):
    return ndb.Key('GSCSites', site)

# Main Cron log class. 
Example #21
Source File: background.py    From RSSNewsGAE with Apache License 2.0 5 votes vote down vote up
def fetch_one_feed():
    key = request.values.get('key')
    feed = ndb.Key(urlsafe=key).get()
    parser = url2parser(feed.url)
    if parser is not None:
        the_last_fetch = feed.latest_fetch
        feed.latest_fetch = datetime.now()
        list_of_news_entities = []
        ndb.put_multi(list_of_news_entities)
        for entry in parser.entries:
            if check_entry(entry):
                entry.published = datetime.fromtimestamp(mktime(entry.published_parsed))
                if entry.published > the_last_fetch:
                    news_entry = NewsEntry()
                    news_entry.published = entry.published
                    news_entry.title = entry.title
                    news_entry.link = entry.link
                    news_entry.summary = clean_html(entry.summary)
                    news_entry.feed = feed.title
                    list_of_news_entities.append(news_entry)
        feed.put()
        news_key_list = ndb.put_multi(list_of_news_entities)
        for news_key in news_key_list:
            taskqueue.add(queue_name='collect-queue',
                          url=url_for("collect_keyword_for_one_news"),
                          method='POST',
                          params={"key": news_key.urlsafe()}
                          )
        return "Done", 200
    else:
        return "parser is None", 200 
Example #22
Source File: main.py    From billing-export-python with Apache License 2.0 5 votes vote down vote up
def getInstance(cls):
        """Returns the single instance of this entity."""
        instance_key = ndb.Key(
            ProcessedNotifications, 'ProcessedNotifications')
        instance = instance_key.get()
        if instance is None:
            instance = ProcessedNotifications(key=instance_key)
            instance.put()
        return instance 
Example #23
Source File: main.py    From billing-export-python with Apache License 2.0 5 votes vote down vote up
def getInstance(cls, project):
        subscription_key = ndb.Key(Subscription, project)
        subscription = subscription_key.get()
        if subscription is None:
            subscription = Subscription(key=subscription_key)
            subscription.put()
        return subscription 
Example #24
Source File: main.py    From billing-export-python with Apache License 2.0 5 votes vote down vote up
def post(self):
        """Save alert to the datastore."""
        alert_obj = DeserializeAlert(self.request.body)
        logging.debug('deleting alert : ' + repr(alert_obj))
        alert = Alert.get_by_id(alert_obj['key'], parent=Alert.entity_group)
        if alert is not None:
            ndb.Key('Alert', alert_obj['key'],
                    parent=Alert.entity_group).delete()
        self.response.out.write(json.dumps({'status': 'success'})) 
Example #25
Source File: appengine.py    From billing-export-python with Apache License 2.0 5 votes vote down vote up
def _delete_entity(self):
    """Delete entity from datastore.

    Attempts to delete using the key_name stored on the object, whether or not
    the given key is in the datastore.
    """
    if self._is_ndb():
      ndb.Key(self._model, self._key_name).delete()
    else:
      entity_key = db.Key.from_path(self._model.kind(), self._key_name)
      db.delete(entity_key) 
Example #26
Source File: api.py    From sndlatr with Apache License 2.0 5 votes vote down vote up
def get_model(self):
        if not isinstance(self.json, dict):
            raise HTTPSuccess()
        key = self.json.get('key', None)
        if not key:
            logging.warn('send handler called without key')
            raise HTTPSuccess()
        key = ndb.Key(urlsafe=key)
        job = key.get()
        if job is None:
            logging.warn('job {} not found'.format(key))
            raise HTTPSuccess()
        return job 
Example #27
Source File: models.py    From sndlatr with Apache License 2.0 5 votes vote down vote up
def get_key(cls, user_id):
        """ Get Key by given user id """
        return ndb.Key(Account, user_id) 
Example #28
Source File: appengine.py    From sndlatr with Apache License 2.0 5 votes vote down vote up
def _delete_entity(self):
    """Delete entity from datastore.

    Attempts to delete using the key_name stored on the object, whether or not
    the given key is in the datastore.
    """
    if self._is_ndb():
      ndb.Key(self._model, self._key_name).delete()
    else:
      entity_key = db.Key.from_path(self._model.kind(), self._key_name)
      db.delete(entity_key) 
Example #29
Source File: main.py    From hackernewsbot with 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 #30
Source File: admin.py    From RSSNewsGAE with Apache License 2.0 5 votes vote down vote up
def feed_switch():
    content = request.get_json(silent=True)
    if content is not None:
        action = content['action']
        key = content['key']
        feed = ndb.Key(urlsafe=key).get()
        feed.enable = not feed.enable
        feed.put()
        flush_feed_cache()
        return jsonify(result=True, key=key, action=action)
    else:
        return jsonify(result=False)