def create_posts(posts: [int, str, str]): tag = s.query(Tag).filter_by(name='Истории').one() for post in posts: post = Post(*post) post.tags.append(tag) s.add(post) s.commit()
def query_sessions(self, request): """Query for sessions. This uses some filters to query the NDB Datastore, and applies other filters in memory. An arbitrary list of filters can be passed in, but the query won't work if the required indices for the query sent to NDB have not been created. Args: request (QueryForms): List of filters. Each filter has: - field: one of "TYPE" (type of session), "TIME" (in "HH:MM:SS" format) - operator: one of "EQ" (=), "GT" (>), "GTEQ" (>=), "LT" (<), "LTEQ" (<=), "NE" (!=) - value: the desired value to compare with Returns: SessionForms: List of sessions matching all the filters. """ # Run query with filters applied in memory if necessary plain_query = Session.query() sessions = self._generic_query(plain_query, request.filters, QUERY_FIELDS) return SessionForms( items = [s.to_form() for s in sessions] )
def get(self): """ Returns a list of available lobbies. """ data = json.loads(self.request.body) cid = data["cid"] session = Session.query(Session.channel_id == cid).get() target_gold_amount = session.target_gold_amount print target_gold_amount response = {'target_gold_amount': target_gold_amount} self.response.out.write(json.dumps(response))
def get_conference_sessions(self, websafe_conference_key): """Given a conference, return all sessions. Args: websafe_conference_key (string) Returns: SessionForms """ # Get Conference object conference = self.get_conference(websafe_conference_key) # Query sessions by ancestor conference sessions = Session.query(ancestor = conference.key) return SessionForms( items = [s.to_form() for s in sessions] )
def get_sessions_by_speaker(self, websafe_speaker_key): """Given a speaker, return all sessions given by this particular speaker, across all conferences. Args: websafe_speaker_key (string) Returns: SessionForms """ # Get Speaker object speaker = self.get_speaker(websafe_speaker_key) # Query sessions by speaker key property query = Session.query() sessions = query.filter(Session.speakerKey == speaker.key) return SessionForms( items = [s.to_form() for s in sessions] )
def get(self): user = CustomUser.get_current_user(self) # If not logged in, redirect if not user: self.redirect("/") # If there is a cookie, delete it and delete the session entry in database user_token = self.request.cookies.get("token") session = Session.query(Session.token == user_token).fetch() if session: session[0].key.delete() self.response.delete_cookie("token") return self.redirect("/") # Else if it's a google user, redirect to the logout url else: logout_url = users.create_logout_url("/") return self.redirect(logout_url)
def get_conference_sessions_by_type(self, websafe_conference_key, type_of_session): """Given a conference, return all sessions of a specified type (e.g. lecture, keynote, workshop). Args: websafe_conference_key (string) type_of_session (string) Returns: SessionForms """ # Get Conference object conference = self.get_conference(websafe_conference_key) # Query sessions by ancestor and type property query = Session.query(ancestor = conference.key) sessions = query.filter(Session.typeOfSession == type_of_session) return SessionForms( items = [s.to_form() for s in sessions] )
def get_sessions_by_conference_speaker(self, websafe_speaker_key, websafe_conference_key): """Given a speaker and a conference, return all sessions given by the speaker at the conference. Args: websafe_speaker_key (string) websafe_conference_key (string) Returns: SessionForms """ # Get Speaker object speaker = self.get_speaker(websafe_speaker_key) # Get Conference object conference = self.get_conference(websafe_conference_key) # Load sessions query = Session.query(ancestor = conference.key) sessions = query.filter(Session.speakerKey == speaker.key) return SessionForms( items = [s.to_form() for s in sessions] )
def get_conference_speakers(self, websafe_conference_key): """Given a conference, get the list of all speakers. Args: websafe_conference_key (string) Returns: SpeakerForms """ # Get Conference object conference = self.get_conference(websafe_conference_key) # Get all sessions for the conference sessions = Session.query(ancestor = conference.key).fetch() # Only use valid keys and ignore duplicates speaker_keys = set([s.speakerKey for s in sessions if s.speakerKey]) # Get the speakers speakers = ndb.get_multi(speaker_keys) return SpeakerForms( items = [s.to_form() for s in speakers] )
def _cache_featured_speaker(websafe_speaker_key, websafe_conference_key): """Create featured speaker announcement & assign to memcache. Only adds an announcement if the speaker has more than one session by the speaker at the conference. Args: websafe_speaker_key (string) websafe_conference_key (string) Returns: Announcement message (string) """ # Get speaker speaker = ndb.Key(urlsafe = websafe_speaker_key).get() # Get conferenc e conference = ndb.Key(urlsafe = websafe_conference_key).get() # Get speaker sessions q = Session.query(ancestor = conference.key) sessions = q.filter(Session.speakerKey == speaker.key).fetch() # Only feature if the speaker has more than on session if len(sessions) > 1: announcement = "Featured speaker: " + speaker.name announcement += "\nSessions:" for s in sessions: announcement += "\n- " + s.name memcache.set(MEMCACHE_FEATURED_SPEAKER_KEY, announcement) else: # TODO: For a real app, do not replace the previous speaker with this announcement = "Not a featured speaker..." memcache.set(MEMCACHE_FEATURED_SPEAKER_KEY, announcement) return announcement
def get_current_user(handler): # Check if user is logged in via google or there's a session token user = users.get_current_user() user_token = handler.request.cookies.get("token") session = Session.query(Session.token == user_token).fetch() # If there is a google user return the user if user: user_id = User.query( User.email == user.email()).fetch()[0].key.id() user = CustomUser(user.email(), user_id, users.is_current_user_admin()) return user # If there's a session token get the user from the database and return the custom user model elif session: user = User.query(User.email == session[0].user_email).fetch() user = user[0] user = CustomUser(user.email, user.key.id(), user.is_admin, user.is_active) return user else: return False
def load_user(admin_id): return Session.query(Admin).filter_by(id=admin_id).one_or_none()