我们从Python开源项目中,提取了以下33个代码示例,用于说明如何使用google.appengine.ext.ndb.delete_multi()。
def gae_ndb_delete_expired_sessions(dormant_for=86400, limit=500): """Deletes expired sessions A session is expired if it expires date is set and has passed or if it has not been accessed for a given period of time. max_age: seconds since last access to delete sessions, defaults to 24 hours. limit: amount to delete in one call of the method, the maximum and default for this is the NDB fetch limit of 500""" from vishnu.backend.client.ndb.gae import VishnuSession from google.appengine.ext import ndb from datetime import datetime from datetime import timedelta now = datetime.utcnow() last_accessed = now - timedelta(seconds=dormant_for) query = VishnuSession.query(ndb.OR( ndb.AND(VishnuSession.expires <= now, VishnuSession.expires != None), VishnuSession.last_accessed <= last_accessed )) results = query.fetch(keys_only=True, limit=limit) ndb.delete_multi(results) return len(results) < limit
def love_links_cleanup(): """ Deletes love links that are more than a month (30 days) old. """ earliest = datetime.datetime.now() - datetime.timedelta(days=30) love_links_keys = LoveLink.query(LoveLink.timestamp <= earliest).fetch(keys_only=True) logging.info('Preparing to delete love links older than {}.'.format(str(earliest))) ndb.delete_multi(love_links_keys) logging.info('Love links older than {} were deleted.'.format(str(earliest))) return
def rebuild_love_count(): set_toggle_state(LOVE_SENDING_ENABLED, False) logging.info('Rebuilding LoveCount table...') ndb.delete_multi(LoveCount.query().fetch(keys_only=True)) for l in Love.query().iter(batch_size=1000): LoveCount.update(l) logging.info('Done.') set_toggle_state(LOVE_SENDING_ENABLED, True)
def delete_user_dbs(user_db_keys): ndb.delete_multi(user_db_keys)
def _pre_delete_hook(cls, key): post_db = key.get() vote_keys = post_db.get_vote_dbs(keys_only=True, limit=-1)[0] comment_keys = post_db.get_comment_dbs(keys_only=True, limit=-1)[0] ndb.delete_multi(vote_keys + comment_keys)
def _pre_delete_hook(cls, key): language_db = key.get() post_keys = language_db.get_post_dbs(keys_only=True, limit=-1)[0] ndb.delete_multi(post_keys)
def delete(self): post_keys = util.param('post_keys', list) if not post_keys: helpers.make_not_found_exception('Post(s) %s not found' % post_keys) post_db_keys = [ndb.Key(urlsafe=k) for k in post_keys] ndb.delete_multi(post_db_keys) return flask.jsonify({ 'result': post_keys, 'status': 'success', })
def delete(self): vote_keys = util.param('vote_keys', list) if not vote_keys: helpers.make_not_found_exception('Vote(s) %s not found' % vote_keys) vote_db_keys = [ndb.Key(urlsafe=k) for k in vote_keys] ndb.delete_multi(vote_db_keys) return flask.jsonify({ 'result': vote_keys, 'status': 'success', })
def delete(self): comment_keys = util.param('comment_keys', list) if not comment_keys: helpers.make_not_found_exception('Comment(s) %s not found' % comment_keys) comment_db_keys = [ndb.Key(urlsafe=k) for k in comment_keys] ndb.delete_multi(comment_db_keys) return flask.jsonify({ 'result': comment_keys, 'status': 'success', })
def test_generate_meeting_specs(database): # delete current specs keys = [key for key in MeetingSpec.query().iter(keys_only=True)] ndb.delete_multi(keys) meeting_specs = MeetingSpec.query().fetch() assert len(meeting_specs) == 0 # ensure we create new specs generate_meeting_specs() meeting_specs = MeetingSpec.query().fetch() assert len(meeting_specs) == 2
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))
def operate_on_multiple_keys_at_once(list_of_entities): list_of_keys = ndb.put_multi(list_of_entities) list_of_entities = ndb.get_multi(list_of_keys) ndb.delete_multi(list_of_keys)
def test_phone_numbers(contact_key): """A test for 'phone_numbers' property.""" models.PhoneNumber(parent=contact_key, phone_type='home', number='(650) 555 - 2200').put() models.PhoneNumber(parent=contact_key, phone_type='mobile', number='(650) 555 - 2201').put() contact = contact_key.get() for phone in contact.phone_numbers: # it doesn't ensure any order if phone.phone_type == 'home': assert '(650) 555 - 2200' == phone.number elif phone.phone_type == 'mobile': assert phone.number == '(650) 555 - 2201' # filer the phone numbers by type. Note that this is an # ancestor query. query = contact.phone_numbers.filter( models.PhoneNumber.phone_type == 'home') entities = query.fetch() assert 1 == len(entities) assert entities[0].number == '(650) 555 - 2200' # delete the mobile phones query = contact.phone_numbers.filter( models.PhoneNumber.phone_type == 'mobile') ndb.delete_multi([e.key for e in query]) # make sure there's no mobile phones any more query = contact.phone_numbers.filter( models.PhoneNumber.phone_type == 'mobile') entities = query.fetch() assert 0 == len(entities)
def main(): user = users.get_current_user() if not user: return flask.redirect(users.create_login_url(flask.request.path)) if flask.request.method == 'POST': util.csrf_protect() tab_ids = flask.request.values.getlist('tab_id') keys = [ndb.Key(Session, tab_id) for tab_id in tab_ids] if 'delete' in flask.request.values: if not all(s and s.user_id == user.user_id() for s in ndb.get_multi(keys)): return 'Not authorized to delete some sessions', 403 ndb.delete_multi(keys) elif 'share' in flask.request.values: for key in keys: session = key.get() if session and session.user_id == user.user_id(): session.shared = True session.put() else: return 'Incorrect POST name', 400 date = flask.request.values.get('date', datetime.now().strftime('%Y-%m-%d')) cur_day = datetime.strptime(date, '%Y-%m-%d') next_day = cur_day + timedelta(days=1) sessions = (Session.query(Session.user_id == user.user_id(), Session.start_ts >= cur_day, Session.start_ts < next_day) .order(-Session.start_ts)) num_shared = Session.query(Session.user_id == user.user_id(), Session.shared == True).count() return flask.render_template('main.html', user=user, date=date, year=datetime.now().year, logout_url=users.create_logout_url('/'), sessions=sessions, num_shared=num_shared)
def _flush_ndb_deletes(self, items, options): """Flush all deletes to datastore.""" assert ndb is not None ndb.delete_multi(items, config=self._create_config(options))
def cleanup(): cutoffdate = datetime.datetime.now() - datetime.timedelta(days=30) keys = TaskProgress.query(TaskProgress.created < cutoffdate).fetch(keys_only=True) ndb.delete_multi(keys)
def DeleteAllData(): entries = [] entries.extend(Person.query().fetch(keys_only=True)) entries.extend(Troop.query().fetch(keys_only=True)) entries.extend(ScoutGroup.query().fetch(keys_only=True)) entries.extend(Meeting.query().fetch(keys_only=True)) entries.extend(TroopPerson.query().fetch(keys_only=True)) entries.extend(Semester.query().fetch(keys_only=True)) entries.extend(TaskProgress.query().fetch(keys_only=True)) entries.extend(UserPrefs.query().fetch(keys_only=True)) ndb.delete_multi(entries) ndb.get_context().clear_cache() # clear memcache
def revoke_user_authentications( cls, user_id ): tokens = cls.fetch_keys_by_user_id( user_id ) if tokens: ndb.delete_multi( tokens )
def delete_user_app_data( cls, user_id, app_id ): keys = cls.fetch_by_user_id_app_id( user_id, app_id ) ndb.delete_multi( keys )
def post( self ): jsonobject = json.loads( self.request.body ) success = False error = 'Invalid request' answer = {} if jsonobject: user_id = int( jsonobject.get( 'user_id', '')) auth_token = jsonobject.get( 'auth_token', '') app_secret = jsonobject.get( 'app_secret', '') data_type = jsonobject.get( 'data_type', '') data_id = jsonobject.get( 'data_id', '') if user_id and auth_token and app_secret and data_type and data_id: if EnkiModelApp.check_secret( user_id, auth_token, app_secret ): token_valid = EnkiModelRestAPITokenVerify.get_by_user_id_token( user_id, auth_token ) if token_valid: # user is valid data_stores = EnkiModelRestAPIDataStore.fetch_by_user_id_app_id_data_type_data_id( user_id, token_valid.app_id, data_type, data_id ) ndb.delete_multi( data_stores ) success = True error = '' else: error = 'Unauthorised user' else: error = 'Unauthorised app' answer.update({ 'success' : success, 'error' : error }) self.response.headers[ 'Content-Type' ] = 'application/json' self.response.write( json.dumps( answer, separators=(',',':') ))
def delete_user_messages( cls, user_id ): ndb.delete_multi( cls.fetch_keys_sent_or_received( user_id ))
def log_out( self ): # log out the currently logged in user self._just_checked_logged_in = False self._am_logged_in = False token = self.session.get( 'auth_token' ) token_key = EnkiModelTokenAuth.fetch_keys_by_user_id_token( self.user_id, token ) if token_key: # delete the token from the db ndb.delete_multi( token_key ) #delete the session self.session.clear()
def email_rollback( self, token ): email = token.email user_id = token.user_id # change the email user = self.set_email( email, user_id ) if user: # retrieve all rollback tokens that are more recent, including the current one, and delete them tokenDateCreated = token.time_created youngerTokens = EnkiModelTokenEmailRollback.fetch_keys_by_user_id_time( user_id, tokenDateCreated ) if youngerTokens: ndb.delete_multi( youngerTokens ) # delete all potential remaining email verify tokens for that user userTokens = EnkiModelTokenVerify.fetch_keys_by_user_id_type( user_id, 'emailchange' ) if userTokens: ndb.delete_multi( userTokens )
def delete_by_email_type( cls, email, type ): # delete all verify tokens for a given email and type (cleanup) entities = cls.fetch_keys_by_email_type(email, type) if entities: ndb.delete_multi(entities)
def get(self): # Example (use with care, and only locally): # ndb.delete_multi( # Score.query( # Score.languages == 'pt').fetch(keys_only=True, limit=5000)) self.response.out.write('OK\n')
def get(self): from models import Report cutoff = datetime.now() - timedelta(days=30) old_reports = Report.query().filter(Report.dt_created < cutoff).fetch(limit=None) n = 0 if old_reports: for report in old_reports: try: report.clean_delete(self_delete=False) except Exception, e: logging.info(str(e)) n = len(old_reports) ndb.delete_multi([dr.key for dr in old_reports]) logging.debug("Deleted %d old reports" % n)
def create_batch(cls, size, **kwargs): """Firstly deletes all entries from datastore and then creates batch of new entries""" ndb.delete_multi(cls._meta.model.query().fetch(keys_only=True)) super(BaseFactory, cls).create_batch(size, **kwargs)
def delete(self, tablename, query): """ This function was changed on 2010-05-04 because according to http://code.google.com/p/googleappengine/issues/detail?id=3119 GAE no longer supports deleting more than 1000 records. """ # self.db['_lastsql'] = self._delete(tablename, query) (items, tablename, fields) = self.select_raw(query) # items can be one item or a query if not isinstance(items, list): #use a keys_only query to ensure that this runs as a datastore # small operations leftitems = items.fetch(1000, keys_only=True) counter = 0 while len(leftitems): counter += len(leftitems) if self.use_ndb: ndb.delete_multi(leftitems) else: gae.delete(leftitems) leftitems = items.fetch(1000, keys_only=True) else: counter = len(items) if self.use_ndb: ndb.delete_multi([item.key for item in items]) else: gae.delete(items) return counter
def PutPlayers(self, player_list, no_pairs): ''' Create a new PlayerPair Entity corresponding to each player pair for pair numbers 1 ... no_pairs saving any useful information from player_list and put it into Datastore as a child of this Tournament. Also, the no_players has changed, generates a unique (for this tournament) id associated with each pair. Args: player_list: list of dicts with keys pair_no (req), name (opt), and email (opt) no_pairs: the total number of pairs in this tournament. Exactly this many PlayerPairs are created. ''' pair_dict = {} if player_list: for player in player_list : pair_no = player['pair_no'] del player['pair_no'] if pair_dict.get(pair_no): pair_dict[pair_no].append(player) else: pair_dict[pair_no]= [player] player_list = PlayerPair.query(ancestor=self.key).fetch() # If the number of players doesn't change, we just override some fields # in existing pairs. Otherwise, we delete existing pairs and create new # ones. override_existing = len(player_list) == no_pairs if not override_existing: random_ids = self._RandomId(no_pairs) ndb.delete_multi([p.key for p in player_list]) else: player_list.sort(key = lambda pp : pp.pair_no) # The create a PlayerPair and put it into Datastore for each possible # number. for i in range(1, no_pairs + 1): pair_members = pair_dict.get(i) str_pair_members = json.dumps(pair_members) if pair_members else '' if override_existing: player_pair = player_list[i-1] player_pair.players = str_pair_members else: player_pair = PlayerPair(players=str_pair_members, pair_no=i, id=random_ids[i-1], parent=self.key) player_pair.key = PlayerPair.CreateKey(self, i) player_pair.put()
def delete_account( self, delete_posts = False, token = '' ): token_to_save = 'accountdelete' if not token: # there is no token if the user has no email address: they are deleted immediately. They must be logged in. user_to_delete = self.enki_user else: # a user has followed a accountdelete token link. The user account associated with the token will be deleted tokenEntity = EnkiModelTokenVerify.get_by_token( token ) user_to_delete = EnkiModelUser.get_by_id( tokenEntity.user_id ) # delete all user related tokens except any verify token related to account deletion that's not yet been used if tokenEntity.type == token_to_save: token_to_save = 'accountandpostsdelete' verify_tokens_to_delete = EnkiModelTokenVerify.fetch_keys_by_user_id_except_type( user_to_delete.key.id(), token_to_save ) if verify_tokens_to_delete: ndb.delete_multi( verify_tokens_to_delete ) email_rollback_tokens_to_delete = EnkiModelTokenEmailRollback.fetch_keys_by_user_id( user_to_delete.key.id()) if email_rollback_tokens_to_delete: ndb.delete_multi( email_rollback_tokens_to_delete ) # Delete the user account and log them out. if not HandlerBase.account_is_active( user_to_delete.key.id()): # delete user if the account is inactive display_names = EnkiModelDisplayName.fetch_keys_by_user_id(user_to_delete.key.id()) if display_names: ndb.delete_multi( display_names ) user_to_delete.key.delete() else: # anonymise the user if user_to_delete.email: user_to_delete.email = None if user_to_delete.password: user_to_delete.password = None if user_to_delete.auth_ids_provider: user_to_delete.auth_ids_provider = [] user_to_delete.put() # keep all historical display_names. Add a new current display_name '[deleted]' (unless it's already been deleted) display_name = EnkiModelDisplayName.get_by_user_id_current( user_to_delete.key.id()) if display_name: if display_name.prefix != EnkiModelDisplayName.DELETED_PREFIX or display_name.suffix != EnkiModelDisplayName.DELETED_SUFFIX: EnkiModelDisplayName.set_display_name( user_to_delete.key.id(), EnkiModelDisplayName.DELETED_PREFIX, EnkiModelDisplayName.DELETED_SUFFIX ) # delete user's sent and received messages EnkiModelMessage.delete_user_messages( user_to_delete.key.id()) # delete user's posts if required if delete_posts: EnkiModelPost.delete_user_posts( user_to_delete.key.id()) # log the deleted user out if self.enki_user == user_to_delete.key.id(): self.log_out() EnkiModelTokenAuth.revoke_user_authentications( user_to_delete.key.id())
def get(self): """Return API available methods.""" # Remove old data cs = Category.query() ndb.delete_multi([x.key for x in cs]) bs = Book.query() ndb.delete_multi([x.key for x in bs]) # Count variables categories_count = 0 subcategories_count = 0 books_count = 0 # NOQA: lxml tree from web page response and search for category boxes. tree = html.fromstring(urllib2.urlopen(self.source_url).read()) category_boxes = tree.xpath('//div[@class="callout-row"]') for category_box in category_boxes: # Get box title by filtering all H3 tags without attributes. category_name = category_box.xpath('.//h3[not(@*)]/text()')[0] # Create category entity and update counters. category = Category(name=category_name, category=True, subcategory=False) # NOQA category.put() categories_count += 1 # Get subcategories by looking for all divs with the right class. subcategories = category_box.xpath('.//div[not(@class="product-row cover-showcase")]') # NOQA book_expression = './/a' # If categories, iterate each and append book with subcategory for subcategory_box in subcategories: # Ger subcategory title by filtering the first H3 tag. subcategory_name = subcategory_box.xpath('.//h3/text()')[0] # Create category entity and update counters. subcategory = Category(name=subcategory_name, category=False, subcategory=True) # NOQA subcategory.put() subcategories_count += 1 books = subcategory_box.xpath(book_expression) for book in books: self.append_book(book, category, subcategory) books_count += 1 # Else, get books and append each with category only if not subcategories: books = category_box.xpath(book_expression) for book in books: self.append_book(book, category) books_count += 1 return self.reponse({ 'status': 'ok', 'books_added': books_count, 'categories_added': categories_count, 'subcategories_added': subcategories_count, })