예제 #1
0
 def testDeleteTournament_hands_removed(self):
   self.loginUser()
   id = self.AddBasicTournament()
   id2 = self.AddBasicTournament()
   params = {'calls': {}, 'ns_score': 25, 'ew_score': 75}
   self.testapp.put_json("/api/tournaments/{}/hands/1/2/3".format(id), params)
   params = {'calls': {"south" : "T"}, 'ns_score': 225, 'ew_score': -25}
   self.testapp.put_json("/api/tournaments/{}/hands/1/2/3".format(id2), params)
   
   
   self.assertEqual(2, len(ndb.Query(kind = "HandScore").fetch()))
   response = self.testapp.delete("/api/tournaments/{}".format(id))
   self.assertEqual(response.status_int, 204)
   response = self.testapp.get("/api/tournaments/{}".format(id),
                               expect_errors=True)
   self.assertEqual(response.status_int, 404)
   self.assertEqual(1, len(ndb.Query(kind = "HandScore").fetch()))
   response = self.testapp.get("/api/tournaments")
   tourneys = json.loads(response.body)
   self.assertIsNotNone(tourneys["tournaments"])
   self.assertEqual(1, len(tourneys["tournaments"]))
   self.assertEqual(id2, tourneys["tournaments"][0]["id"])
   response = self.testapp.get("/api/tournaments/{}".format(id2),
                               expect_errors=True)
   response_dict = json.loads(response.body)
   self.CheckBasicTournamentMetadataUnchanged(response_dict)
   self.assertEqual({"south" : "T"}, response_dict["hands"][0]['calls'])
   self.assertEqual(225, response_dict["hands"][0]['ns_score'])
   self.assertEqual(-25, response_dict["hands"][0]['ew_score'])
   self.assertEqual(1, response_dict["hands"][0]['board_no'])
   self.assertEqual(2, response_dict["hands"][0]['ns_pair'])
   self.assertEqual(3, response_dict["hands"][0]['ew_pair'])
예제 #2
0
    def testOnlyDeleteKind(self):
        anomaly.Anomaly(
            id='anomaly',
            test=utils.TestMetadataKey('master/bot/suite/measurement')).put()
        page_state.PageState(id='page_state').put()
        self.assertEqual(
            1, len(ndb.Query(kind='PageState').fetch(keys_only=True)))
        self.assertEqual(1,
                         len(ndb.Query(kind='Anomaly').fetch(keys_only=True)))

        delete_all_entities.DeleteAllEntities('PageState')
        self.assertEqual(
            0, len(ndb.Query(kind='PageState').fetch(keys_only=True)))
        self.assertEqual(1,
                         len(ndb.Query(kind='Anomaly').fetch(keys_only=True)))
    def get_responses(self, participant_short_id, stimulus_number=None):
        '''
		Returns a list of responses.
		List contains all existing responses if stimulus_number is None.
		Otherwise list contains one response specified by stimulus_number. 
		'''

        participant_key = self._get_participant_key(participant_short_id)

        if stimulus_number is not None:
            # Check if the stimulus number is out of range
            if stimulus_number not in range(
                    0,
                    participant_key.get().max_number_stimuli):
                raise IndexError('Stimulus index out of range.')

            # Query the database to get the response
            q = Response.query(Response.stimulus_index == stimulus_number,
                               ancestor=participant_key)

        else:
            # Query the database to get all responses
            q = ndb.Query(kind='Response', ancestor=participant_key)

        # Convert the responses returned by the query to a list of dictionaries
        response_list = [response.to_dict() for response in q.iter()]

        # Check if we are getting all responses or if the list of responses is not empty
        if stimulus_number is None or len(response_list) > 0:
            # It's ok to return an empty list if we were getting all responses
            return response_list
        else:
            # If we were getting a specific response, then returning an empty list not allowed
            raise LookupError('Response %s not found.' % stimulus_number)
    def get_stimuli(self, participant_short_id, stimulus_number=None):
        '''
		Returns a list of stimuli.
		List contains all existing stimuli if stimulus_number is None.
		Otherwise list contains one stimulus specified by stimulus_number. 
		'''

        participant_key = self._get_participant_key(participant_short_id)

        if stimulus_number is not None:
            # Check if the stimulus number is out of range
            if stimulus_number not in range(
                    0,
                    participant_key.get().max_number_stimuli):
                raise IndexError('Stimulus index out of range.')

            # Query the database to get the stimulus
            q = Stimulus.query(Stimulus.stimulus_index == stimulus_number,
                               ancestor=participant_key)

        else:
            # Query the database to get all stimuli
            q = ndb.Query(kind='Stimulus', ancestor=participant_key)

        # Convert the stimuli returned by the query to a list of dictionaries
        stimuli_list = [stimulus.to_dict() for stimulus in q.iter()]

        # Check if we are getting all stimuli or if the list of stimuli is not empty
        if stimulus_number is None or len(stimuli_list) > 0:
            # It's ok to return an empty list if we were getting all stimuli
            return stimuli_list
        else:
            # If we were getting a specific stimulus, then returning an empty list not allowed
            raise LookupError('Stimulus %s not found.' % stimulus_number)
예제 #5
0
 def handle_get(self, scope, package, version):
   # FIXME: Make deletion transactional with check on library that tag is excluded.
   version_key = ndb.Key(Library, Library.id(scope, package), Version, version)
   ndb.delete_multi(ndb.Query(ancestor=version_key).iter(keys_only=True))
   if VersionCache.update(version_key.parent()):
     task_url = util.update_indexes_task(scope, package)
     util.new_task(task_url, target='manage')
예제 #6
0
def _UpdateEntities(kind, master, bot, start_cursor=None):
    """Update `internal_only` flags of `kind` entities to match their Bot.
  """
    datastore_hooks.SetPrivilegedRequest()
    internal_only = graph_data.Bot.GetInternalOnlySync(master, bot)

    query = ndb.Query(kind=kind).filter(
        ndb.GenericProperty('master_name') == master,
        ndb.GenericProperty('bot_name') == bot,
        ndb.GenericProperty('internal_only') == (not internal_only))
    entities, next_cursor, more = query.fetch_page(PAGE_LIMIT,
                                                   start_cursor=start_cursor)
    for entity in entities:
        entity.internal_only = internal_only
    ndb.put_multi(entities)

    logging.info('updated %d entities', len(entities))
    if more and next_cursor:
        logging.info('continuing')
        deferred.defer(_UpdateEntities,
                       kind,
                       master,
                       bot,
                       next_cursor,
                       _queue=QUEUE_NAME)
    else:
        logging.info('complete')
예제 #7
0
def cron_delete_old_bot():
    """Deletes stale BotRoot entity groups."""
    start = utils.utcnow()
    # Run for 4.5 minutes and schedule the cron job every 5 minutes. Running for
    # 9.5 minutes (out of 10 allowed for a cron job) results in 'Exceeded soft
    # private memory limit of 512 MB with 512 MB' even if this loop should be
    # fairly light on memory usage.
    time_to_stop = start + datetime.timedelta(seconds=int(4.5 * 60))
    total = 0
    deleted = []
    try:
        q = BotRoot.query(default_options=ndb.QueryOptions(keys_only=True))
        for bot_root_key in q:
            # Check if it has any BotEvent left. If not, it means that the entity is
            # older than _OLD_BOT_EVENTS_CUF_OFF, so the whole thing can be deleted
            # now.
            # In particular, ignore the fact that BotInfo may still exist, since if
            # there's no BotEvent left, it's probably a broken entity or a forgotten
            # dead bot.
            if BotEvent.query(ancestor=bot_root_key).count(limit=1):
                continue
            deleted.append(bot_root_key.string_id())
            # Delete the whole group. An ancestor query will retrieve the entity
            # itself too, so no need to explicitly delete it.
            keys = ndb.Query(ancestor=bot_root_key).fetch(keys_only=True)
            ndb.delete_multi(keys)
            total += len(keys)
            if utils.utcnow() >= time_to_stop:
                break
        return total
    except runtime.DeadlineExceededError:
        pass
    finally:
        logging.info('Deleted %d entities from the following bots:\n%s', total,
                     ', '.join(sorted(deleted)))
예제 #8
0
    def cancel_game(self, request):
        """Cancels a Game entity and its children Turn entities.  User can
        only cancel games in progress.  This API operates under the assumpion
        that it's better to just cancel games outright instead of somehow
        marking them as deleted in the database.

        :param urlsafe_key (req): The state token for a game of Shut The Box.
        :type urlsafe_key: string

        :returns cancelled: True if the game entity and Turn entities are
        deleted from the datastore; False if the game entity in question is
        completed.
        :rtype cancelled: boolean
        :returns error: Helpful error message.
        :rtype error: string

        :raises: BadRequestException, ValueError"""

        game = get_by_urlsafe(request.urlsafe_key, Game)
        if game.game_over:
            return CancelResultForm(
                cancelled=False,
                error="Can't cancel games that are already completed.")

        # This deletes both the parent game and the children turns
        ndb.delete_multi(ndb.Query(ancestor=game.key).iter(keys_only=True))
        return CancelResultForm(cancelled=True)
예제 #9
0
 def getByProductNameSerialNumberModuleIdSensorId(cls, product_name_data,
                                                  serial_number_data,
                                                  module_id_data,
                                                  sensor_id_data):
     assert isinstance(product_name_data, Data) and isinstance(
         serial_number_data, Data) and isinstance(
             module_id_data, Data) and isinstance(sensor_id_data, Data)
     query = ndb.Query(kind="Sensor")
     query = query.filter(cls.productName == product_name_data.dataId)
     query = query.filter(cls.serialNumber == serial_number_data.dataId)
     query = query.filter(cls.moduleId == module_id_data.dataId)
     query = query.filter(cls.sensorId == sensor_id_data.dataId)
     keys = query.fetch(keys_only=True, limit=2)
     if len(keys) == 0:
         raise EntityNotFound(
             "Sensor", {
                 "product_name_data": product_name_data,
                 "serial_number_data": serial_number_data,
                 "module_id_data": module_id_data,
                 "sensor_id_name": sensor_id_data
             })
     if len(keys) == 2:
         raise EntityDuplicated({
             "product_name_data": product_name_data,
             "serial_number_data": serial_number_data,
             "module_id_name": module_id_data,
             "sensor_id_data": sensor_id_data
         })
     return keys[0].get()
예제 #10
0
def delete_author(author_key, response_for_logging=None):
    keys = [author_key] + ndb.Query(ancestor=author_key).fetch(keys_only=True)
    ndb.delete_multi(keys)

    if response_for_logging is not None:
        for key in keys:
            response_for_logging.write(repr(key.flat()) + '\n')
        response_for_logging.write('\n')
예제 #11
0
 def queryByFieldAndString(cls, field, string):
     assert isinstance(field, unicode)
     assert isinstance(string, unicode)
     query = ndb.Query(kind="Data")
     query = query.order(cls.dataId)
     query = query.filter(cls.field == field)
     query = query.filter(cls.string == string)
     return query
예제 #12
0
def estrai_ultimo_contenuto(oggetto_articolo):
    """restituisce il contenuto più recente figlio dell'articolo"""
    q = ndb.Query(Contenuto).ancestor(
        oggetto_articolo.key()).order("data_contenuto")
    if q.get() == None:
        return None
    else:
        return q.get()
예제 #13
0
 def getByUniqueSensorId(cls, unique_sensor_id):
     query = ndb.Query(kind="Sensor")
     query = query.filter(Sensor.uniqueSensorId == unique_sensor_id)
     keys = query.get(keys_only=True, limit=2)
     if len(keys >= 2):
         raise EntityDuplicated({"equipmentId": unique_sensor_id})
     if len(keys == 0):
         raise EntityNotFound({"equipmentId": unique_sensor_id})
     return keys[0].get()
예제 #14
0
def main(project_id):
    remote_api_stub.ConfigureRemoteApiForOAuth(
        '{}.appspot.com'.format(project_id), '/_ah/remote_api')

    # List the first 10 keys in the datastore.
    keys = ndb.Query().fetch(10, keys_only=True)

    for key in keys:
        print(key)
예제 #15
0
 def fetchDateRangeAndData(cls, start, end, data_key):
     assert isinstance(start, datetime)
     assert isinstance(end, datetime)
     assert isinstance(data_key, ndb.Key)
     query = ndb.Query(kind="Metadata")
     query = query.filter(cls.receivedDateTime >= start)
     query = query.filter(cls.receivedDateTime <= end)
     query = query.order(cls.receivedDateTime)
     return query.fetch(keys_only=True)
예제 #16
0
 def querySender(cls, ip_address, port, protocol):
     assert isinstance(ip_address, unicode)
     assert isinstance(port, int)
     assert isinstance(protocol, unicode)
     query = ndb.Query(kind="Sender")
     query = query.filter(cls.ipAddress == ip_address)
     query = query.filter(cls.port == port)
     query = query.filter(cls.protocol == protocol)
     return query
예제 #17
0
 def GET(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     query = ndb.Query(kind="Sensor")
     query = query.order(-Sensor.uniqueSensorId)
     keys = query.fetch(keys_only=True, limit=100)
     for key in keys:
         jresponse.addResult(key.get())
     jresponse.setColumns(SensorColumns())
예제 #18
0
 def query_constructor(kind_str, prop, value):
     query = ndb.Query(kind=kind_str,
                       filters=ndb.GenericProperty(prop) == value)
     print query
     query_result = query.fetch()
     result_object = {}
     if query_result is not None:
         for item in query_result:
             result_object.update({item.key.id(): item.to_dict()})
     return result_object
예제 #19
0
 def get(self):
     deletekeyurlstr = self.request.get('TKEY')
     deletekey = ndb.Key(urlsafe=deletekeyurlstr)
     if users.get_current_user():
         identity = users.get_current_user().user_id()
         #Check we're logged in as the owner to prevent remote deletions
         if identity == deletekey.parent().id():
             ndb.delete_multi(
                 ndb.Query(ancestor=deletekey).iter(keys_only=True))
     self.redirect('/')
예제 #20
0
 def testDeleteTournament(self):
   self.loginUser()
   id = self.AddBasicTournament()
   id2 = self.AddBasicTournament()
   self.assertEqual(16, len(ndb.Query(kind = "PlayerPair").fetch()))
   response = self.testapp.delete("/api/tournaments/{}".format(id))
   self.assertEqual(response.status_int, 204)
   response = self.testapp.get("/api/tournaments/{}".format(id),
                               expect_errors=True)
   self.assertEqual(response.status_int, 404)
   self.assertEqual(8, len(ndb.Query(kind = "PlayerPair").fetch()))
   response = self.testapp.get("/api/tournaments")
   tourneys = json.loads(response.body)
   self.assertIsNotNone(tourneys["tournaments"])
   self.assertEqual(1, len(tourneys["tournaments"]))
   self.assertEqual(id2, tourneys["tournaments"][0]["id"])
   response = self.testapp.get("/api/tournaments/{}".format(id2),
                               expect_errors=True)
   self.CheckBasicTournamentMetadataUnchanged(json.loads(response.body))
예제 #21
0
 def getByEmailUserId(cls, email_user_id):
     assert isinstance(email_user_id, int)
     query = ndb.Query(kind="EmailUser")
     query = query.filter(cls.emailUserId == email_user_id)
     query = query.filter(cls.invalidatedDateTime == None)
     keys = query.fetch(limit=2, keys_only=True)
     if len(keys) == 0:
         raise EntityNotFound(cls, {"emailUserId": email_user_id})
     if len(keys) == 2:
         raise EntityDuplicated(cls, {"emailUserId": email_user_id})
     return keys[0].get()
    def remove_experiment(self, experiment_id):

        experiment_key = self._key_from_urlsafe_id(experiment_id)

        experiment = experiment_key.get()
        experiment_name = experiment.experiment_name

        ndb.delete_multi(
            ndb.Query(ancestor=experiment_key).iter(keys_only=True))
        experiment_key.delete()
        return True
예제 #23
0
 def validate_empty_tree(self, tree):
     """
     An empty tree should consist of only two entities, the tree
     itself and the root node. No indices, no other nodes.
     """
     first = tree.key
     last = ndb.Key(first.kind(), first.id() + u"\ufffd")
     q = ndb.Query(ancestor=first)
     q = q.filter(tree.__class__.key < last)
     keys = list(q.iter(keys_only=True))
     self.assertEqual(keys, [first, tree._make_node_key("root")])
예제 #24
0
def delete_library(library_key, response_for_logging=None):
  keys = [library_key] + ndb.Query(ancestor=library_key).fetch(keys_only=True)
  ndb.delete_multi(keys)

  if response_for_logging is not None:
    for key in keys:
      response_for_logging.write(repr(key.flat()) + '\n')
    response_for_logging.write('\n')

  index = search.Index('repo')
  index.delete([library_key.id()])
예제 #25
0
    def delete(self, id):
        user = users.get_current_user()
        tourney = GetTourneyWithIdAndMaybeReturnStatus(self.response, id)
        if not tourney:
            return

        if not CheckUserOwnsTournamentAndMaybeReturnStatus(
                self.response, user, tourney):
            return

        self.response.set_status(204)
        ndb.delete_multi(ndb.Query(ancestor=tourney.key).iter(keys_only=True))
예제 #26
0
def delete_everything():
    kinds = metadata.get_kinds()
    for kind in kinds:
        if kind.startswith('_'):
            pass  # Ignore kinds that begin with _, they are internal to GAE
        else:
            q = ndb.Query(kind=kind)
            keys = q.fetch(keys_only=True)

            # Delete 1000 entities at a time.
            for i in range(len(keys) / 1000 + 1):
                portion = keys[i * 1000:i * 1000 + 1000]
                ndb.delete_multi(portion)
예제 #27
0
 def queryRange(cls, start, end):
     debug("getRecentRawData start=%s end=%s" % (start, end))
     q = ndb.Query(kind="RawData")
     if start < end:
         q = q.order(cls.rawDataId)
         q = q.filter(cls.rawDataId >= start)
         q = q.filter(cls.rawDataId <= end)
         return q
     else:
         q = q.order(-cls.rawDataId)
         q = q.filter(cls.rawDataId <= start)
         q = q.filter(cls.rawDataId >= end)
         return q
예제 #28
0
 def queryPeriod(cls, start_rawdata_id, end_rawdata_id):
     assert isinstance(start_rawdata_id, int)
     assert isinstance(end_rawdata_id, int)
     q = ndb.Query(kind="RawData")
     if start_rawdata_id >= end_rawdata_id:
         q = q.order(-cls.rawDataId)
         q = q.filter(cls.rawDataId <= start_rawdata_id)
         q = q.filter(cls.rawDataId >= end_rawdata_id)
     else:
         q = q.order(-cls.rawDataId)
         q = q.filter(cls.rawDataId <= end_rawdata_id)
         q = q.filter(cls.rawDataId >= start_rawdata_id)
     return q
예제 #29
0
 def DeletePath(self, path):
   """Delete files with specified leading path."""
   normpath = self._NormalizeDirectoryPath(path)
   keys = ndb.Query(ancestor=self.root).fetch(keys_only=True)
   keys = [k for k in keys if
           k.id() == path or
           (k.string_id() and k.string_id().startswith(normpath)) or
           k.parent().id() == path or
           k.parent().id().startswith(normpath)]
   if not keys:
     return False
   ndb.delete_multi(keys)
   return True
예제 #30
0
 def queryRange(cls, start_data_id, end_data_id):
     assert isinstance(start_data_id, int)
     assert isinstance(end_data_id, int)
     query = ndb.Query(kind="Data")
     if start_data_id <= end_data_id:
         query = query.filter(cls.dataId >= start_data_id)
         query = query.filter(cls.dataId <= end_data_id)
         return query
     else:
         query = query.order(-cls.dataId)
         query = query.filter(cls.dataId <= start_data_id)
         query = query.filter(cls.dataId >= end_data_id)
         return query