Ejemplo n.º 1
0
    def archive(cls):
        """Delete all Todo items that are done."""

        req = datastore.BeginTransactionRequest()
        resp = datastore.begin_transaction(req)
        tx = resp.transaction
        req = datastore.RunQueryRequest()
        req.read_options.transaction = tx
        q = req.query
        set_kind(q, kind='Todo')
        add_projection(q, '__key__')
        set_composite_filter(
            q.filter, datastore.CompositeFilter.AND,
            set_property_filter(datastore.Filter(), 'done',
                                datastore.PropertyFilter.EQUAL, True),
            set_property_filter(datastore.Filter(), '__key__',
                                datastore.PropertyFilter.HAS_ANCESTOR,
                                default_todo_list.key))
        resp = datastore.run_query(req)
        keys = [r.entity.key for r in resp.batch.entity_result]
        req = datastore.CommitRequest()
        req.transaction = tx
        req.mutation.delete.extend(keys)
        resp = datastore.commit(req)
        return ''
Ejemplo n.º 2
0
    def save(self):
        """ Saves an entity to the datastore. """

        _log.debug('Saving %s entity with id %s.',
                   type(self).__name__, self._get_id())
        req = googledatastore.BeginTransactionRequest()
        resp = googledatastore.begin_transaction(req)
        tx = resp.transaction
        self.modified_time = datetime.utcnow()
        resp = self.__lookup(tx)
        if not resp.found:
            self.created_time = self.modified_time
        self.__upsert(tx)
Ejemplo n.º 3
0
    def testBeginTransaction(self):
        request = datastore.BeginTransactionRequest()
        payload = request.SerializeToString()
        response = datastore.BeginTransactionResponse()
        self.expectRequest(
            'https://example.com/datastore/v1/projects/foo:beginTransaction',
            method='POST',
            body=payload,
            headers=self.makeExpectedHeaders(payload)).AndReturn(
                (TestResponse(status=200,
                              reason='Found'), response.SerializeToString()))
        self.mox.ReplayAll()

        resp = self.conn.begin_transaction(request)
        self.assertEqual(response, resp)
        self.mox.VerifyAll()
Ejemplo n.º 4
0
  def testBeginTransaction(self):
    request = datastore.BeginTransactionRequest()
    payload = request.SerializeToString()
    response = datastore.BeginTransactionResponse()
    self.expectRequest(
        'https://datastore.com/datastore/v1beta1/datasets/foo/'
        'beginTransaction',
        method='POST', body=payload,
        headers={'Content-Type': 'application/x-protobuf',
                 'Content-Length': str(len(payload))}).AndReturn(
                     (TestResponse(status=200, reason='Found'),
                      response.SerializeToString()))
    self.mox.ReplayAll()

    resp = self.conn.begin_transaction(request)
    self.assertEqual(response, resp)
    self.mox.VerifyAll()
Ejemplo n.º 5
0
    def testBeginTransaction(self):
        request = datastore.BeginTransactionRequest()
        payload = request.SerializeToString()
        proto_response = datastore.BeginTransactionResponse()
        response = httplib2.Response({
            'status': 200,
            'content-type': 'application/x-protobuf',
        })

        self.expectRequest(
            'https://example.com/datastore/v1/projects/foo:beginTransaction',
            method='POST',
            body=payload,
            headers=self.makeExpectedHeaders(payload)).AndReturn(
                (response, proto_response.SerializeToString()))
        self.mox.ReplayAll()

        resp = self.conn.begin_transaction(request)
        self.assertEqual(proto_response, resp)
        self.mox.VerifyAll()
Ejemplo n.º 6
0
 def save_batch(entities):
     req = googledatastore.BeginTransactionRequest()
     resp = googledatastore.begin_transaction(req)
     transaction = resp.transaction
     # Bulk lookup
     req = googledatastore.LookupRequest()
     req.key.extend([entity.__get_key() for entity in entities])
     req.read_options.transaction = transaction
     resp = googledatastore.lookup(req)
     # Update created / modified times.
     missing = set()
     for result in resp.missing:
         key = result.entity.key.path_element[0]
         missing.add(key.id or key.name)
     for entity in entities:
         entity.modified_time = time
         if entity._get_id() in missing:
             entity.created_time = time
     # Bulk upsert
     req = googledatastore.CommitRequest()
     req.transaction = transaction
     req.mutation.upsert.extend(
         [entity._get_entity() for entity in entities])
     googledatastore.commit(req)
Ejemplo n.º 7
0
    def checkDB(self, gitkit_user):
        req = datastore.BeginTransactionRequest()
        resp = datastore.begin_transaction(req)
        tx = resp.transaction

        req = datastore.LookupRequest()
        key = datastore.Key()
        path = key.path_element.add()
        path.kind = 'PSUsers'
        path.name = gitkit_user.user_id
        req.key.extend([key])

        req.read_options.transaction = tx

        resp = datastore.lookup(req)
        req = datastore.CommitRequest()

        req.transaction = tx

        if resp.missing:
            user = req.mutation.insert.add()
            user.key.CopyFrom(key)

            userid_property = user.property.add()
            userid_property.name = 'userid'
            userid_property.value.string_value = gitkit_user.user_id

            display_name_property = user.property.add()
            display_name_property.name = 'display_name'
            display_name_property.value.string_value = gitkit_user.name

            photo_url_property = user.property.add()
            photo_url_property.name = 'photo_url'
            if gitkit_user.photo_url:
                photo_url_property.value.string_value = gitkit_user.photo_url
            else:
                photo_url_property.value.string_value = "/images/home/slider/slide1/cloud1.png"

            email_property = user.property.add()
            email_property.name = 'email'
            email_property.value.string_value = gitkit_user.email

            last_login_property = user.property.add()
            last_login_property.name = 'last_login'
            last_login_property.value.timestamp_microseconds_value = long(
                time.time() * 1e6)

        elif resp.found:
            user = resp.found[0].entity
            last_login_property = datastore.Property()
            last_login_property.name = 'last_login'

            for prop in user.property:
                if prop.name == 'last_login':
                    prop.value.timestamp_microseconds_value = long(
                        time.time() * 1e6)

            req.mutation.update.extend([user])

        resp = datastore.commit(req)

        return None
Ejemplo n.º 8
0
Archivo: gcd.py Proyecto: mach21/GLUDB
 def __enter__(self):
     """Start transction."""
     req = datastore.BeginTransactionRequest()
     resp = datastore.begin_transaction(req)
     self.tx = resp.transaction
     return self
Ejemplo n.º 9
0
def main():
  # Set dataset id from command line argument.
  if len(sys.argv) < 2:
    print 'Usage: adams.py <DATASET_ID>'
    sys.exit(1)
  # Set the dataset from the command line parameters.
  datastore.set_options(dataset=sys.argv[1])
  try:
    # Create a RPC request to begin a new transaction.
    req = datastore.BeginTransactionRequest()
    # Execute the RPC synchronously.
    resp = datastore.begin_transaction(req)
    # Get the transaction handle from the response.
    tx = resp.transaction
    # Create a RPC request to get entities by key.
    req = datastore.LookupRequest()
    # Create a new entity key.
    key = datastore.Key()
    # Set the entity key with only one `path_element`: no parent.
    path = key.path_element.add()
    path.kind = 'Trivia'
    path.name = 'hgtg'
    # Add one key to the lookup request.
    req.key.extend([key])
    # Set the transaction, so we get a consistent snapshot of the
    # entity at the time the transaction started.
    req.read_options.transaction = tx
    # Execute the RPC and get the response.
    resp = datastore.lookup(req)
    # Create a RPC request to commit the transaction.
    req = datastore.CommitRequest()
    # Set the transaction to commit.
    req.transaction = tx
    if resp.found:
      # Get the entity from the response if found.
      entity = resp.found[0].entity
    else:
      # If no entity was found, insert a new one in the commit request mutation.
      entity = req.mutation.insert.add()
      # Copy the entity key.
      entity.key.CopyFrom(key)
      # Add two entity properties:
      # - a utf-8 string: `question`
      prop = entity.property.add()
      prop.name = 'question'
      prop.value.string_value = 'Meaning of life?'
      # - a 64bit integer: `answer`
      prop = entity.property.add()
      prop.name = 'answer'
      prop.value.integer_value = 42
    # Execute the Commit RPC synchronously and ignore the response:
    # Apply the insert mutation if the entity was not found and close
    # the transaction.
    datastore.commit(req)
    props = get_property_dict(entity)
    # Get question property value.
    question = props['question'].string_value
    # Get answer property value.
    answer = props['answer'].integer_value
    # Print the question and read one line from stdin.
    print question
    result = raw_input('> ')
    if result == str(answer):
      print ('fascinating, extraordinary and, '
             'when you think hard about it, completely obvious.')
    else:
      print "Don't Panic!"
  except datastore.RPCError as e:
    # RPCError is raised if any error happened during a RPC.
    # It includes the `method` called and the `reason` of the
    # failure as well as the original `HTTPResponse` object.
    logging.error('Error while doing datastore operation')
    logging.error('RPCError: %(method)s %(reason)s',
                  {'method': e.method,
                   'reason': e.reason})
    logging.error('HTTPError: %(status)s %(reason)s',
                  {'status': e.response.status,
                   'reason': e.response.reason})
    return