我们从Python开源项目中,提取了以下39个代码示例,用于说明如何使用google.appengine.ext.ndb.get_multi()。
def get_count(name): """Retrieve the value for a given sharded counter. Args: name: The name of the counter. Returns: Integer; the cumulative count of all sharded counters for the given counter name. """ total = memcache.get(name) if total is None: total = 0 all_keys = GeneralCounterShardConfig.all_keys(name) for counter in ndb.get_multi(all_keys): if counter is not None: total += counter.count memcache.add(name, total, 60) return total
def list(self, d): days = self.request.get_range('days', default=4) before_date = datetime.today() _before_date = self.request.get('before_date') if _before_date: before_date = tools.fromISODate(_before_date) cursor = before_date journal_keys = [] for i in range(days): iso_date = tools.iso_date(cursor) journal_keys.append(ndb.Key('MiniJournal', iso_date, parent=self.user.key)) cursor -= timedelta(days=1) journals = ndb.get_multi(journal_keys) self.set_response({ 'journals': [j.json() for j in journals if j] }, success=True)
def Search(cls, user, term, limit=20): kind = cls._get_kind() index = User.get_search_index(user.key, kind) message = None success = False items = [] try: query_options = search.QueryOptions(limit=limit) query = search.Query(query_string=term, options=query_options) search_results = index.search(query) except Exception, e: logging.debug("Error in search api: %s" % e) message = str(e) else: keys = [ndb.Key(kind, sd.doc_id, parent=user.key) for sd in search_results.results if sd] items = ndb.get_multi(keys) success = True return (success, message, items)
def Range(user, habits, since_date, until_date=None): ''' Fetch habit days for specified habits in date range Args: habits (list of Habit() objects) ... Returns: list: HabitDay() ordered sequentially ''' today = datetime.today() if not until_date: until_date = today cursor = since_date ids = [] while cursor <= until_date: for h in habits: ids.append(ndb.Key('HabitDay', HabitDay.ID(h, cursor), parent=user.key)) cursor += timedelta(days=1) if ids: return [hd for hd in ndb.get_multi(ids) if hd] return []
def fetchEntities(self): return dict(zip(self.to_fetch, ndb.get_multi(self.to_fetch)))
def get(self): crash_keys = util.param('crash_keys', list) if crash_keys: crash_db_keys = [ndb.Key(urlsafe=k) for k in crash_keys] crash_dbs = ndb.get_multi(crash_db_keys) return helpers.make_response(crash_dbs, model.crash.FIELDS) crash_dbs, crash_cursor = model.Crash.get_dbs() return helpers.make_response(crash_dbs, model.Crash.FIELDS, crash_cursor)
def get(self): project_keys = util.param('project_keys', list) if project_keys: project_db_keys = [ndb.Key(urlsafe=k) for k in project_keys] project_dbs = ndb.get_multi(project_db_keys) return helpers.make_response(project_dbs, model.project.FIELDS) project_dbs, project_cursor = model.Project.get_dbs() return helpers.make_response(project_dbs, model.Project.FIELDS, project_cursor)
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)
def merge_user_dbs(user_db, deprecated_keys): # TODO: Merge possible user data before handling deprecated users deprecated_dbs = ndb.get_multi(deprecated_keys) for deprecated_db in deprecated_dbs: deprecated_db.auth_ids = [] deprecated_db.active = False deprecated_db.verified = False if not deprecated_db.username.startswith('_'): deprecated_db.username = '_%s' % deprecated_db.username ndb.put_multi(deprecated_dbs)
def get(self): person_keys = util.param('person_keys', list) if person_keys: person_db_keys = [ndb.Key(urlsafe=k) for k in person_keys] person_dbs = ndb.get_multi(person_db_keys) return helpers.make_response(person_dbs, model.person.FIELDS) person_dbs, person_cursor = model.Person.get_dbs() return helpers.make_response(person_dbs, model.Person.FIELDS, person_cursor)
def get(self): post_keys = util.param('post_keys', list) if post_keys: post_db_keys = [ndb.Key(urlsafe=k) for k in post_keys] post_dbs = ndb.get_multi(post_db_keys) return helpers.make_response(post_dbs, model.post.FIELDS) post_dbs, post_cursor = model.Post.get_dbs() return helpers.make_response(post_dbs, model.Post.FIELDS, post_cursor)
def get(self): vote_keys = util.param('vote_keys', list) if vote_keys: vote_db_keys = [ndb.Key(urlsafe=k) for k in vote_keys] vote_dbs = ndb.get_multi(vote_db_keys) return helpers.make_response(vote_dbs, model.vote.FIELDS) vote_dbs, vote_cursor = model.Vote.get_dbs() return helpers.make_response(vote_dbs, model.Vote.FIELDS, vote_cursor)
def get(self): comment_keys = util.param('comment_keys', list) if comment_keys: comment_db_keys = [ndb.Key(urlsafe=k) for k in comment_keys] comment_dbs = ndb.get_multi(comment_db_keys) return helpers.make_response(comment_dbs, model.comment.FIELDS) comment_dbs, comment_cursor = model.Comment.get_dbs() return helpers.make_response(comment_dbs, model.Comment.FIELDS, comment_cursor)
def get(self): language_keys = util.param('language_keys', list) if language_keys: language_db_keys = [ndb.Key(urlsafe=k) for k in language_keys] language_dbs = ndb.get_multi(language_db_keys) return helpers.make_response(language_dbs, model.language.FIELDS) language_dbs, language_cursor = model.Language.get_dbs() return helpers.make_response(language_dbs, model.Language.FIELDS, language_cursor)
def remove_preferences(user, updated_preferences, subscription_key): """ Parameters ---------- user - ndb.User preferences - {SubscriptionDateTime.key:Boolean} subscription_key - ndb.Key Returns ------- set(SubscriptionDateTime.Key) """ removed = set() for preference in ndb.get_multi(user.subscription_preferences): if preference.subscription == subscription_key: if not updated_preferences.get(preference.preference, True): index = user.subscription_preferences.index(preference.key) removed.add(user.subscription_preferences[index]) del user.subscription_preferences[index] user.put() for record in removed: record.delete() return removed
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 get_cart_plus_offers(acct): cart = CartItem.query(CartItem.account == acct.key).fetch() offers = SpecialOffer.query().fetch(10) ndb.get_multi([item.inventory for item in cart] + [offer.inventory for offer in offers]) return cart, offers
def get_cart_plus_offers_async(acct): cart_future = CartItem.query(CartItem.account == acct.key).fetch_async() offers_future = SpecialOffer.query().fetch_async(10) cart = cart_future.get_result() offers = offers_future.get_result() ndb.get_multi([item.inventory for item in cart] + [offer.inventory for offer in offers]) return cart, offers # [START cart_offers_tasklets]
def companies(self): rels = ContactCompany.query(ancestor=self.key.parent()).filter( ContactCompany.contact == self.key) keys = [rel.company for rel in rels] return ndb.get_multi(keys)
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 get_by_ids(cls, id_list, keys_only=False): if not id_list: return [] keys = [ndb.Key(cls, x) for x in id_list] if keys_only: return cls.query(cls.key.IN(keys)).fetch(len(keys), keys_only=True) else: return ndb.get_multi(keys)
def resolve_key_reference(self, entity, info): key_value = self.__ndb_key_prop._get_user_value(entity) if not key_value: return None if isinstance(key_value, list): return ndb.get_multi(key_value) return key_value.get()
def transform_to_reader_edges(edges, args, context): article_readers = [edge.node for edge in edges] readers = ndb.get_multi([article_reader.reader_key for article_reader in article_readers]) transformed_edges = [] for edge, reader in zip(edges, readers): if reader.is_alive: edge.node = reader transformed_edges.append(edge) return transformed_edges
def schedule(test_run, shard_count, shard): # read tests from body test_names = flask.request.get_json(force=True)['tests'] # first see if we have a scedule already schedule_id = "%s-%d" % (test_run, shard_count) schedule = Schedule.get_by_id(schedule_id) if schedule is not None: return flask.json.jsonify(tests=schedule.shards[str(shard)]) # if not, do simple greedy algorithm test_times = ndb.get_multi( ndb.Key(Test, test_name) for test_name in test_names) def avg(test): if test is not None: return test.cost() return 1 test_times = [(test_name, avg(test)) for test_name, test in zip(test_names, test_times)] test_times_dict = dict(test_times) test_times.sort(key=operator.itemgetter(1)) shards = {i: [] for i in xrange(shard_count)} while test_times: test_name, time = test_times.pop() # find shortest shard and put it in that s, _ = min( ((i, sum(test_times_dict[t] for t in shards[i])) for i in xrange(shard_count)), key=operator.itemgetter(1)) shards[s].append(test_name) # atomically insert or retrieve existing schedule schedule = Schedule.get_or_insert(schedule_id, shards=shards) return flask.json.jsonify(tests=schedule.shards[str(shard)])
def random_batch(self, d): ''' Return a random batch, optionally filtered ''' BATCH_SIZE = 50 sample_keys = Readable.Fetch(self.user, with_notes=True, limit=500, keys_only=True) if len(sample_keys) > BATCH_SIZE: sample_keys = random.sample(sample_keys, BATCH_SIZE) readables = ndb.get_multi(sample_keys) self.set_response({ 'readables': [r.json() for r in readables] }, success=True)
def random_batch(self, d): ''' Return a random batch, optionally filtered ''' BATCH_SIZE = 50 sample_keys = Quote.Fetch(self.user, limit=500, keys_only=True) if len(sample_keys) > BATCH_SIZE: sample_keys = random.sample(sample_keys, BATCH_SIZE) quotes = ndb.get_multi(sample_keys) self.set_response({ 'quotes': [q.json() for q in quotes] }, success=True)
def Fetch(user, start, end): journal_keys = [] iso_dates = [] if start < end: date_cursor = start while date_cursor < end: date_cursor += timedelta(days=1) iso_date = tools.iso_date(date_cursor) journal_keys.append(ndb.Key('MiniJournal', iso_date, parent=user.key)) iso_dates.append(iso_date) return ([j for j in ndb.get_multi(journal_keys) if j], iso_dates)
def Current(user, which="all"): date = tools.local_time(user.get_timezone(), datetime.today()) keys = [] if which in ["all", "year"]: annual_id = ndb.Key('Goal', datetime.strftime(date, "%Y"), parent=user.key) keys.append(annual_id) if which in ["all", "month"]: monthly_id = ndb.Key('Goal', datetime.strftime(date, "%Y-%m"), parent=user.key) keys.append(monthly_id) if which in ["all", "longterm"]: longterm_id = ndb.Key('Goal', datetime.strftime(date, "longterm"), parent=user.key) keys.append(longterm_id) goals = ndb.get_multi(keys) return [g for g in goals]
def _habit_status(self): habits = Habit.All(self.user) today = datetime.today().date() habitday_keys = [ndb.Key('HabitDay', HabitDay.ID(h, today), parent=self.user.key) for h in habits] habitdays = ndb.get_multi(habitday_keys) n_habits_done = 0 habits_committed_undone = [] habits_done = [] for hd in habitdays: if hd: habit = hd.habit.get() if hd.committed and not hd.done: if habit: habits_committed_undone.append(habit.name) if hd.done: habits_done.append(habit.name) n_habits_done += 1 if habits: if n_habits_done: text = "Good work on doing %d %s (%s)!" % ( n_habits_done, tools.pluralize('habit', n_habits_done), tools.english_list(habits_done) ) else: text = "No habits done yet." if habits_committed_undone: text += " Don't forget you've committed to %s." % tools.english_list(habits_committed_undone) else: text = "You haven't added any habits yet. Try saying 'add habit run'" return text
def user_merge(): args = parser.parse({ 'user_key': wf.Str(missing=None), 'user_keys': wf.DelimitedList(wf.Str(), delimiter=',', required=True), }) user_db_keys = [ndb.Key(urlsafe=k) for k in args['user_keys']] user_dbs = ndb.get_multi(user_db_keys) if len(user_dbs) < 2: flask.abort(400) user_dbs.sort(key=lambda user_db: user_db.created) merged_user_db = user_dbs[0] auth_ids = [] permissions = [] is_admin = False is_active = False for user_db in user_dbs: auth_ids.extend(user_db.auth_ids) permissions.extend(user_db.permissions) is_admin = is_admin or user_db.admin is_active = is_active or user_db.active if user_db.key.urlsafe() == args['user_key']: merged_user_db = user_db auth_ids = sorted(list(set(auth_ids))) permissions = sorted(list(set(permissions))) merged_user_db.permissions = permissions merged_user_db.admin = is_admin merged_user_db.active = is_active merged_user_db.verified = False form_obj = copy.deepcopy(merged_user_db) form_obj.user_key = merged_user_db.key.urlsafe() form_obj.user_keys = ','.join(args['user_keys']) form = UserMergeForm(obj=form_obj) if form.validate_on_submit(): form.populate_obj(merged_user_db) merged_user_db.auth_ids = auth_ids merged_user_db.put() deprecated_keys = [k for k in user_db_keys if k != merged_user_db.key] merge_user_dbs(merged_user_db, deprecated_keys) return flask.redirect( flask.url_for('user_update', user_id=merged_user_db.key.id()), ) return flask.render_template( 'user/user_merge.html', title='Merge Users', html_class='user-merge', user_dbs=user_dbs, merged_user_db=merged_user_db, form=form, auth_ids=auth_ids, api_url=flask.url_for('api.admin.user.list'), )