我们从Python开源项目中,提取了以下42个代码示例,用于说明如何使用google.appengine.ext.ndb.AND。
def scoring(): #go through all predictions, check if should be scored predictions = Prediction.query( ndb.AND(Prediction.outcome != "UNKNOWN", Prediction.resolved == False)).fetch() audit = [] # Get all trades by prediction_id for p in predictions: resolve = p.outcome trades = Trade.query(Trade.prediction_id == p.key).fetch() # Get user id from those trades users = [trade.user_id.get() for trade in trades] for u in users: # check user ledger, map outcome to 1 or 0 based on prediction outcome ledger = [i for i in u.user_ledger if i.prediction_id == p.key.urlsafe()] if resolve == 'CONTRACT_ONE': earned = ledger[0].contract_one else: earned = ledger[0].contract_two u.balance += earned audit.append({'user': u, 'earned': earned}) u.put() p.resolved = True p.put() return audit
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 query_display(cls, user_id, delta_minutes=60): """ Query all jobs that have state scheduled, queued or sent (but not done) OR are done and have been scheduled for no longer than delta_minutes ago. """ shortly_ago = datetime.datetime.utcnow() - datetime.timedelta( minutes=delta_minutes) # query all jobs that are return cls.query(ndb.OR(cls.state.IN(['scheduled', 'queued', 'sent']), ndb.AND(cls.scheduled_at >= shortly_ago, cls.state == 'done')), cls.user_id == user_id)
def price(): predictions = Prediction.query( ndb.AND(Prediction.outcome == "UNKNOWN", Prediction.resolved == False)).fetch() input_date = datetime.datetime.now() for p in predictions: price = Price(prediction_id=p.key, date=input_date, value=p.GetPriceByPredictionId()) price.put()
def GetTradesForPredictionId(prediction_id): user = users.get_current_user() trades = Trade.query(ndb.AND(Trade.prediction_id == ndb.Key(urlsafe=prediction_id), Trade.user_id == ndb.Key('Profile', user.user_id()))).fetch() return str(trades)
def query_article_nested(): query = Article.query(ndb.AND(Article.tags == 'python', ndb.OR(Article.tags.IN(['ruby', 'jruby']), ndb.AND(Article.tags == 'php', Article.tags != 'perl')))) return query
def exist_by_user_ids( cls, user_a_id, user_b_id ): count = cls.query( ndb.AND( cls.friends == user_a_id, cls.friends == user_b_id )).count( 1 ) return count > 0
def get_key_by_user_ids( cls, user_a_id, user_b_id ): return cls.query( ndb.AND( cls.friends == user_a_id, cls.friends == user_b_id )).get( keys_only = True ) #=== UTILITIES ================================================================
def query_by_user_id( cls, user_id ): return cls.query( ndb.OR( ndb.AND( cls.user_id == user_id, cls.time_updated > ( datetime.datetime.now() - datetime.timedelta( seconds = settings.SESSION_MAX_IDLE_AGE ))), ndb.AND( cls.user_id == user_id, cls.stay_logged_in == True, cls.time_updated > ( datetime.datetime.now() - datetime.timedelta( seconds = settings.SESSION_MAX_IDLE_AGE_STAY_LOGGED_IN )))))
def fetch_keys_expired( cls ): return cls.query( ndb.OR( cls.time_updated > ( datetime.datetime.now() - datetime.timedelta( seconds = settings.SESSION_MAX_IDLE_AGE_STAY_LOGGED_IN )), ndb.AND( cls.stay_logged_in == False, cls.time_updated > ( datetime.datetime.now() - datetime.timedelta( seconds = settings.SESSION_MAX_IDLE_AGE )))) ).fetch( keys_only = True ) #=== UTILITIES ================================================================
def get_by_user_id_app_id_data_type_data_id( cls, user_id, app_id, data_type, data_id ): return cls.query( ndb.AND( cls.user_id == user_id, cls.app_id == app_id, cls.data_type == data_type, cls.data_id == data_id )).get()
def fetch_by_user_id_app_id( cls, user_id, app_id ): return cls.query( ndb.AND( cls.user_id == user_id, cls.app_id == app_id )).fetch( keys_only = True )
def fetch_by_user_id_app_id_data_type_read_access_not_expired( cls, user_id, app_id, data_type, read_access ): return cls.query( ndb.AND( cls.user_id == user_id, cls.app_id == app_id, cls.data_type == data_type, cls.read_access == read_access, cls.time_expires > datetime.datetime.now())).fetch()
def fetch_by_app_id_data_type_read_access_not_expired( cls, app_id, data_type, read_access ): return cls.query( ndb.AND( cls.app_id == app_id, cls.data_type == data_type, cls.read_access == read_access, cls.time_expires > datetime.datetime.now())).fetch()
def fetch_by_user_id_app_id_data_type_data_id( cls, user_id, app_id, data_type, data_id ): return cls.query( ndb.AND( cls.user_id == user_id, cls.app_id == app_id, cls.data_type == data_type, cls.data_id == data_id )).fetch( keys_only = True )
def exist_product_by_activator( cls, user_id, product_name ): count = cls.query( ndb.AND( cls.activated_by_user == user_id, cls.product_name == product_name )).count( 1 ) return count > 0
def exist_by_purchaser_not_activated( cls, user_id ): count = cls.query( ndb.AND( cls.purchaser_user_id == user_id, cls.activated_by_user == -1 )).count( 1 ) return count > 0
def count_by_purchaser_not_activated( cls, user_id ): return cls.query( ndb.AND( cls.purchaser_user_id == user_id, cls.activated_by_user == -1)).count()
def count_by_shop_name_order_type_activated( cls, shop_name, order_type ): return cls.query( ndb.AND( cls.shop_name == shop_name, cls.order_type == order_type, cls.activated_by_user >= 0 )).count()
def count_by_shop_name_order_type_not_activated( cls, shop_name, order_type ): return cls.query( ndb.AND( cls.shop_name == shop_name, cls.order_type == order_type, cls.activated_by_user == -1 )).count() #=== UTILITIES ================================================================
def get_by_user_id_email( cls, user_id, email ): return cls.query( ndb.AND( cls.user_id == user_id, cls.email == email )).get()
def get_by_user_id_route( cls, user_id, route ): return cls.query( ndb.AND( cls.user_id == user_id, cls.route == route )).get()
def exist_by_sender_recipient( cls, sender_id, recipient_id ): count = cls.query( ndb.AND( cls.sender == sender_id, cls.recipient == recipient_id )).count( 1 ) return count > 0
def get_key_by_sender_recipient( cls, sender_id, recipient_id ): return cls.query( ndb.AND( cls.sender == sender_id, cls.recipient == recipient_id )).get( keys_only = True )
def get_by_sender_recipient( cls, sender_id, recipient_id ): return cls.query( ndb.AND( cls.sender == sender_id, cls.recipient == recipient_id )).get()
def get_by_user_id_token( cls, user_id, token ): return cls.query( ndb.AND( cls.user_id == user_id, cls.token == token )).get()
def fetch_by_prefix_lower_current( cls, prefix_lower ): return cls.query( ndb.AND( cls.prefix_lower == prefix_lower, cls.current == True )).fetch()
def exist_by_prefix_lower_suffix( cls, prefix_lower, suffix ): count = cls.query( ndb.AND( cls.prefix_lower == prefix_lower, cls.suffix == suffix )).count( 1 ) return count > 0
def get_by_user_id_email_type( cls, user_id, email, type ): return cls.query( ndb.AND( cls.user_id == user_id, cls.email == email, cls.type == type )).get()
def get_by_user_id_auth_id_type( cls, user_id, auth_id, type ): return cls.query( ndb.AND( cls.user_id == user_id, cls.auth_ids_provider == auth_id, cls.type == type )).get()
def get_by_user_id_type( cls, user_id, type ): return cls.query( ndb.AND( cls.user_id == user_id, cls.type == type )).get( )
def count_by_user_id_type( cls, user_id, type ): return cls.query( ndb.AND( cls.user_id == user_id, cls.type == type )).count()
def fetch_by_user_id_type( cls, user_id, type ): return cls.query( ndb.AND( cls.user_id == user_id, cls.type == type )).order( -cls.time_created ).fetch()
def fetch_keys_by_user_id_type( cls, user_id, type ): return cls.query( ndb.AND( cls.user_id == user_id, cls.type == type )).fetch( keys_only = True )
def fetch_keys_by_user_id_except_type( cls, user_id, type ): return cls.query( ndb.AND( cls.user_id == user_id, cls.type != type )).fetch( keys_only = True )
def fetch_keys_by_email_type( cls, email, type ): return cls.query( ndb.AND( cls.email == email, cls.type == type )).fetch( keys_only = True )
def exist_by_user_id_token( cls, user_id, token ): count = cls.query( ndb.AND( cls.user_id == user_id, cls.token == token )).count( 1 ) return count > 0
def fetch_keys_old_tokens_by_types( cls, days_old, types ): return cls.query( ndb.AND( cls.type.IN( types ) , cls.time_created <= ( datetime.datetime.now( ) - datetime.timedelta( days = days_old )))).fetch( keys_only = True ) #=== UTILITIES ================================================================
def get_by_token_type( cls, token, type, retry = 0 ): entity = cls.query(ndb.AND(cls.token == token, cls.type == type)).get() if retry and not entity: timeout = cls.TIMEOUT_S for i in range(retry): entity = cls.query(ndb.AND(cls.token == token, cls.type == type)).get() if entity: break else: time.sleep(timeout) timeout *= 2 return entity
def delete_by_user_id_token( cls, user_id, token ): key = cls.query(ndb.AND(cls.user_id == user_id, cls.token == token)).fetch(keys_only = True) if key: key[ 0 ].delete() return True return False
def get_by_user_id_token_valid_age( cls, user_id, token ): return cls.query( ndb.AND( cls.user_id == user_id, cls.token == token, cls.time_created > ( datetime.datetime.now() - datetime.timedelta( minutes = cls.MAX_AGE )))).get()
def get_previous_meetings(subscription, cooldown=None): if cooldown is None: cooldown = get_config()['meeting_cooldown_weeks'] meetings = defaultdict(list) # get all meeting specs from x weeks ago til now time_threshold_for_meetings = datetime.now() - timedelta(weeks=cooldown) meeting_spec_keys = [ spec.key for spec in MeetingSpec.query( ndb.AND(MeetingSpec.datetime > time_threshold_for_meetings, MeetingSpec.meeting_subscription == subscription) ).fetch() ] logging.info('Previous Meeting History: ') logging.info([meeting.get().datetime.strftime("%Y-%m-%d %H:%M") for meeting in meeting_spec_keys]) if meeting_spec_keys == []: return set([]) # get all meetings from meeting specs meeting_keys = [meeting.key for meeting in Meeting.query().filter( Meeting.meeting_spec.IN(meeting_spec_keys)).fetch()] if meeting_keys == []: return set([]) # get all participants from meetings participants = MeetingParticipant.query().filter( MeetingParticipant.meeting.IN(meeting_keys) ).fetch() if participants == []: return set([]) # group by meeting Id for participant in participants: meetings[participant.meeting.id()].append(participant.user) # ids are sorted, all matches should be in increasing order by id for the matching algorithm to work disallowed_meetings = set([tuple(sorted(meeting, key=lambda Key: Key.id())) for meeting in meetings.values()]) logging.info('Past Meetings') logging.info([tuple([meeting.get().get_username() for meeting in meeting]) for meeting in disallowed_meetings]) disallowed_meetings = {tuple([meeting.id() for meeting in meeting]) for meeting in disallowed_meetings} return disallowed_meetings