def _querySessions(self, filters, key=None):
        """
        Gernal query used for searching Sessions.
        """
        if key:
            # get Conference object from request; bail if not found
            safeKey = ndb.Key(urlsafe=key)
            conf = safeKey.get()
            if not conf:
                raise endpoints.NotFoundException(
                    'No conference found with key: %s' % key)
            q = Session.query(ancestor=safeKey)
        else:
            q = Session.query()

        if filters:
            inequality_filter, filters, excluded_values = self._formatMutliInequality(
                filters)
            # If exists, sort on inequality filter first
            if inequality_filter:
                q = q.order(ndb.GenericProperty(inequality_filter))

            q = q.order(Session.startTime)
            q = q.order(Session.name)

            for filtr in filters:
                formatted_query = ndb.query.FilterNode(filtr["field"],
                                                       filtr["operator"],
                                                       filtr["value"])
                q = q.filter(formatted_query)

            if excluded_values:
                """
                This code is used to solve query extra credit problem.
                Create list of session types but exclude the not equal to type
                """
                # Create list by turning TypeOfSession EMUN into dict then
                # filtered out values that are in the excluded_values list.
                typeOfSession = [typeOfSession for typeOfSession in \
                                 TypeOfSession.to_dict() if typeOfSession\
                                 not in excluded_values]
                FilterList = []
                # Add additional filter to query
                for i in typeOfSession:
                    FilterList.append(
                        ndb.query.FilterNode('typeOfSession', '=', i))

                q = q.filter(ndb.OR(*FilterList))

        return q
    def _querySessions(self, filters, key=None):
        """
        Gernal query used for searching Sessions.
        """
        if key:
            # get Conference object from request; bail if not found
            safeKey = ndb.Key(urlsafe=key)
            conf = safeKey.get()
            if not conf:
                raise endpoints.NotFoundException(
                    'No conference found with key: %s' % key)
            q = Session.query(ancestor=safeKey)
        else:
            q = Session.query()

        if filters:
            inequality_filter, filters, excluded_values = self._formatMutliInequality(filters)
            # If exists, sort on inequality filter first
            if inequality_filter:
                q = q.order(ndb.GenericProperty(inequality_filter))

            q = q.order(Session.startTime)
            q = q.order(Session.name)

            for filtr in filters:
                formatted_query = ndb.query.FilterNode(filtr["field"], filtr["operator"], filtr["value"])
                q = q.filter(formatted_query)

            if excluded_values:
                """
                This code is used to solve query extra credit problem.
                Create list of session types but exclude the not equal to type
                """
                # Create list by turning TypeOfSession EMUN into dict then
                # filtered out values that are in the excluded_values list.
                typeOfSession = [typeOfSession for typeOfSession in \
                                 TypeOfSession.to_dict() if typeOfSession\
                                 not in excluded_values]
                FilterList = []
                # Add additional filter to query
                for i in typeOfSession:
                    FilterList.append(ndb.query.FilterNode('typeOfSession', '=', i))

                q = q.filter(ndb.OR(*FilterList))

        return q
 def getConferenceSessionsByType(self, request):
     """
     Given a conference, return all sessions of a specified type
     (eg lecture, keynote, workshop)
     """
     key = ndb.Key(urlsafe=request.websafeConferenceKey)
     conf = key.get()
     value = request.value.upper()
     if not conf:
         raise endpoints.NotFoundException(
             'No conference found with key: %s' % key)
     # Check if field and operation are valid
     if request.field != "TYPE" or request.operator not in OPERATORS:
         raise endpoints.NotFoundException('Can only filter by type or check operator')
     # Check if value is valid
     if value not in TypeOfSession.to_dict():
         raise endpoints.NotFoundException('Not a valid session type')
     sessions = Session.query(ancestor=key).\
         filter(ndb.query.FilterNode(FIELDS[request.field],\
         OPERATORS[request.operator], value)).\
         order(Session.startTime)
     return SessionForms(sessions=[self._copySessionToForm(sess)\
                         for sess in sessions])
 def getConferenceSessionsByType(self, request):
     """
     Given a conference, return all sessions of a specified type
     (eg lecture, keynote, workshop)
     """
     key = ndb.Key(urlsafe=request.websafeConferenceKey)
     conf = key.get()
     value = request.value.upper()
     if not conf:
         raise endpoints.NotFoundException(
             'No conference found with key: %s' % key)
     # Check if field and operation are valid
     if request.field != "TYPE" or request.operator not in OPERATORS:
         raise endpoints.NotFoundException(
             'Can only filter by type or check operator')
     # Check if value is valid
     if value not in TypeOfSession.to_dict():
         raise endpoints.NotFoundException('Not a valid session type')
     sessions = Session.query(ancestor=key).\
         filter(ndb.query.FilterNode(FIELDS[request.field],\
         OPERATORS[request.operator], value)).\
         order(Session.startTime)
     return SessionForms(sessions=[self._copySessionToForm(sess)\
                         for sess in sessions])