Example #1
0
def update_monthly_games_played():
    gc = gspread.login('*****@*****.**', 'muatkienjwxfpnxn')
    player_cell_locations = {'thewarmth00':'B51',
                                'rob_chainsaw':'B53',
                                'mashley93':'B55',
                                'peachy36west':'B57',
                                'm_sibs':'B59',
                                'rc_van':'B61',
                                'soviet_canuck':'B63',
                                'undertheblanket':'B65',
                                'vsmewen':'B67',
                                'hokagesama1':'B69',
                                'lnferno31':'H86'}
    sheet = gc.open_by_key('0Ak-m4uT6aXL1dExuUHdLV0x2aTNFSGNRMTV2WWdLX2c').get_worksheet(9)
    session = Session()
    our_players = session.query(Player).all()
    for player in our_players:
        cell_location = player_cell_locations[player.username.lower()]
        games_played = len(session.query(GamePlayed).filter_by(player_id=player.id).all())
        value = sheet.acell(cell_location).value
        sheet.update_acell(cell_location, str(games_played))
        if sheet.acell(cell_location).value != value:
            pass
        else:
            pass
    def _getSessions(self, request):
        """Return sessions by conference and optionally filter by typeOfSession or duartion or date. 
        Or return sessions by speaker."""

        # Get conference
        if hasattr(request, 'websafeConferenceKey') and request.websafeConferenceKey:
          urlkey = request.websafeConferenceKey
          conf_key = ndb.Key(urlsafe = urlkey)
          conf = conf_key.get()

          # create ancestor query for all key matches for this user
          sessions = Session.query(ancestor = conf_key)

          # if typeOfSession has been specified, filter by that
          if hasattr(request, 'typeOfSession') and request.typeOfSession:
            sessions = sessions.filter(Session.typeOfSession == request.typeOfSession)

          # if duration has been specified, filter by that
          if hasattr(request, 'duration') and request.duration:
            sessions = sessions.filter(Session.duration <= int(request.duration))
            sessions = sessions.filter(Session.duration > 0)

          # if date has been specified, filter by that
          if hasattr(request, 'date') and request.date:
            sessions = sessions.filter(Session.date == datetime.strptime(request.date[:10], "%Y-%m-%d").date())

        elif request.speaker:
          sessions = Session.query()
          sessions = sessions.filter(Session.speaker == request.speaker)

        return SessionForms(
            items=[self._copySessionToForm(session) for session in sessions]
        )
Example #3
0
    def _createSessionObject(self, request):
        """Create or update Session object, returning SessionForm/request."""
        # Load user data and check to see if authorized
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization required')
        user_id = user.email()

        # Check that Session name exists
        if not request.name:
            raise endpoints.BadRequestException("Session 'name' field required")
        # Check that Session speaker's name exists
        if not request.speaker:
            raise endpoints.BadRequestException("Session 'speaker' field required")

        # Copy SessionForm/ProtoRPC message into dictionary
        data = {field.name: getattr(request, field.name) for field in request.all_fields()}
        data['conference'] = ndb.Key(urlsafe=request.websafeConferenceKey)
        del data['websafeConferenceKey']
        del data['sessionKey']

        # Convert date from string to date object
        if data['startDate']:
            data['startDate'] = datetime.strptime(data['startDate'][:10], "%Y-%m-%d").date()

        # Create Session
        new_key = Session(**data).put()

        # Add get_featured_speaker to task queue
        taskqueue.add(params={'conferenceKey': request.websafeConferenceKey,
                              'speaker': data['speaker']},
                      url='/tasks/get_featured_speaker')

        request.sessionKey = new_key.urlsafe()
        return self._copySessionToForm(request)
    def doubleInequalityFilter(self, request):
        """ Queries for non-workshop sessions before 7PM.
            Handling queries with multiple inequality filters.
        """

        # define time object for 7PM
        time_seven_pm = datetime.strptime('19', '%H').time()

        # First inequality query
        #   Get sessions which are NOT 'Workshop'
        non_workshop_keys = Session.query(
            Session.session_type != 'Workshop').fetch(keys_only=True)

        # Second inequality query
        #   Get sessions which start before 7PM
        before_seven_pm_keys = Session.query(
            Session.startTime < time_seven_pm).fetch(keys_only=True)

        # find sets of matching keys between the two queries
        #   and retrieve their entities.
        matching_sessions = ndb.get_multi(
            set(non_workshop_keys).intersection(before_seven_pm_keys))

        return SessionForms(items=[self._copySessionToForm(sess)
                            for sess in matching_sessions])
    def createSession(self, request):
        """ Creates Session from given SESSION_POST_REQUEST_FOR_CONFERENCE and returns a SessionForm."""
        # (SessionForm, websafeConferenceKey) -- open only to the organizer of the conference
        profile = self._getProfileFromUser()
        conf = ndb.Key(urlsafe=request.websafeConferenceKey).get()
        if not conf:
            raise endpoints.NotFoundException(
                'No conference found with key: %s' % request.websafeConferenceKey)
        if conf.key.parent().get() != profile:
            raise endpoints.UnauthorizedException('Session creation is open only to the organizer of the conference!')

        # copy SESSION_POST_REQUEST_FOR_CONFERENCE/ProtoRPC Message into dict
        data = {field.name: getattr(request, field.name) for field in request.all_fields()}
        session = Session(name=data['name'],parent=conf.key)

        # convert dates from strings to Date objects; set month based on start_date
        if data['dateTime']:
            session.date = data['dateTime'].date()
            session.time = data['dateTime'].time()
        if data['highlights']:
            session.highlights = data['highlights']
        if data['typeOfSession']:
            session.typeOfSession = data['typeOfSession']
        if data['duration']:
            session.duration = data['duration']
        if data['speakers']:
            session.speakers = data['speakers']

        session.put()
        
        if data['speakers']:
            session.speakers = data['speakers']
            logging.debug("Creating session with speakers, adding task to taskqueue.", str(session))
            taskqueue.add(url='/tasks/add_featured_speaker', params={'sessionKeyUrlSafe': session.key.urlsafe()})
        return self._copySessionToForm(session)
Example #6
0
def save(request):
    results = simplejson.loads(request.POST['results'])

    session_key = request.session.session_key    #request.COOKIES[settings.SESSION_COOKIE_NAME]
    session = Session(key=session_key, global_time=time())
    session.save()

    # Convert user results to Result table row
    db_results = []
    for result in results:
        db_result = Result(
            session = session,
            time = result['time'],
            selection = result['selection'],
        )
        db_result.image_id = result['id']
        db_results.append(db_result)

    try:
        # This could be done better with a transaction
        for db_result in db_results:
            db_result.save()
    except Exception as e:
        print e
        pass

    return HttpResponseRedirect('/static/thankyou.html')
    def getFeaturedSpeaker(self, request):
        """Returns the sessions of the featured speaker"""
        data = memcache.get("featured_speaker")
        from pprint import pprint

        pprint(data)
        sessions = []
        sessionNames = []
        speaker = None

        if data and data.has_key("speaker") and data.has_key("sessionNames"):
            speaker = data["speaker"]
            sessionNames = data["sessionNames"]
        else:
            upcoming_session = (
                Session.query(Session.date >= datetime.now()).order(Session.date, Session.startTime).get()
            )
            if upcoming_session:
                speaker = upcoming_session.speaker
                sessions = Session.query(Session.speaker == speaker)
                sessionNames = [session.name for session in sessions]

        # create speaker form
        sf = SpeakerForm()
        for field in sf.all_fields():
            if field.name == "sessionNames":
                setattr(sf, field.name, sessionNames)
            elif field.name == "speaker":
                setattr(sf, field.name, speaker)
        sf.check_initialized()
        return sf
    def _cacheFeaturedSpeaker(websafeConferenceKey, speaker):
        """Assign featured speaker to memcache"""
        # logging.info('1: This is the websafeConferenceKey: ' + websafeConferenceKey)
        # logging.info('2: This is the speaker: ' + speaker)

        conf = ndb.Key(urlsafe=websafeConferenceKey)

        print "3. This is the conf: ", conf

        sessions = Session.query(ancestor=conf)

        sessions = Session.query(Session.speaker == speaker).fetch()

        count = len(sessions)

        if count > 1:
            featured = "Todays featured speaker is %s at session %s" % (
                speaker,
                ", ".join(session.name for session in sessions),
            )
            memcache.set(MEMCACHE_FEATURED_KEY, featured)
        else:
            # If there are is no featured speaker,
            # delete the memcache featured entry
            featured = ""
            memcache.delete(MEMCACHE_FEATURED_KEY)

        return featured
Example #9
0
    def _createSessionObject(self, request):
        """Create a session object, return SessionForm"""
        # check for authentication
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization required')
        user_id = getUserId(user)

        # get parent Conference from request; raise exception if not found
        c_key = ndb.Key(urlsafe=request.websafeConferenceKey)
        conf = c_key.get()
        if not conf:
            raise endpoints.NotFoundException(
                'No conference found with key: %s' %
                request.websafeConferenceKey)

        if not request.sessionName:
            raise endpoints.BadRequestException(
                "Session 'sessionName' field required")

        # check that user is owner of given conference
        if user_id != conf.organizerUserId:
            raise endpoints.ForbiddenException(
                'Only the owner can add a session to the conference.')

        # copy ConferenceForm/ProtoRPC Message into dict
        data = {field.name: getattr(request, field.name)
                for field in request.all_fields()}
        del data['websafeConferenceKey']
        del data['websafeKey']

        # add default values for those missing
        for df in SDEFAULTS:
            if data[df] in (None, []):
                data[df] = SDEFAULTS[df]
                setattr(request, df, SDEFAULTS[df])

        # convert dates from strings; set month based on start_date
        if data['date']:
            data['date'] = datetime.strptime(data['date'][:10],
                                             "%Y-%m-%d").date()

        # convert type of session to uppercase
        data['typeOfSession'] = data['typeOfSession'].upper()

        # generate Session ID based on Conference ID
        s_id = Conference.allocate_ids(size=1, parent=c_key)[0]
        s_key = ndb.Key(Session, s_id, parent=c_key)
        data['key'] = s_key

        # return a session form with the same data as in the datastore
        newSess = Session(**data)
        newSess.put()

        # TASK 4
        # Check for featured speaker
        taskqueue.add(params={'sessionKey': s_key.urlsafe()},
                      url='/tasks/set_featured')

        return self._copySessionToForm(newSess)
Example #10
0
    def server_event_callback(server, event_info, **kwargs):
        if event_info.event not in [ErlyvideoEvent.STREAM_STARTED, ErlyvideoEvent.STREAM_STOPPED,
                                    ErlyvideoEvent.USER_PLAY, ErlyvideoEvent.USER_STOP]:
            return

        try:
            active_session = Session.objects.get(server=server, stream=event_info.stream,
                stream_name=event_info.stream_name, finish_at=None)
        except Session.DoesNotExist:
            active_session = None

        if event_info.event in [ErlyvideoEvent.STREAM_STARTED, ErlyvideoEvent.USER_PLAY]:
            if active_session:
                logger.warning('Session "%s-%s" already exists and active.' % (event_info.stream, event_info.stream_name))
                active_session.finish_at = datetime.today()
                active_session.save()

            session_type = Session.TYPE_BROADCAST if ErlyvideoEvent.STREAM_STARTED == event_info.event else Session.TYPE_PLAY
            session = Session(server=server, type=session_type, stream=event_info.stream,
                stream_name=event_info.stream_name, user=event_info.user_id)
            session.save()

        if event_info.event in [ErlyvideoEvent.STREAM_STOPPED, ErlyvideoEvent.USER_STOP]:
            if active_session:
                active_session.finish_at = datetime.today()
                active_session.save()
            else:
                logger.warning('Session "%s-%s" not found.' % (event_info.stream, event_info.stream_name))
Example #11
0
    def _getSessionsNotTypeBeforeHour(self, request):
        """Find sessions for the conference before the given hour (24 hour) and
        not of type specified."""
        # get Conference key from request; bail if not found
        c_key = ndb.Key(urlsafe=request.websafeConferenceKey)
        if not c_key:
            raise endpoints.NotFoundException(
                'No conference found with key: %s' %
                request.websafeConferenceKey)

        # Get a list of all types except the type specified
        nottype = Session.query(
            Session.typeOfSession != request.nottype).fetch()
        if not nottype:
            raise endpoints.NotFoundException(
                'No sessions found with type not: %s' % request.nottype)
        notlist = [r.typeOfSession for r in nottype]

        # For all types in nottype, filter sessionBefore for these types
        sessnt = Session.query(Session.typeOfSession.IN(notlist))
        # Filter sessions for those in the specified conference
        c_sessnt = sessnt.filter(Session.conferenceKey == c_key)
        # Filter query for sessions that occur before the time
        qtime = datetime.time(int(request.hour), 0, 0)
        c_sessntBefore = c_sessnt.filter(Session.startTime < qtime)

        # return set of SessionForm objects per Session
        return SessionForms(
            items=[self._copySessionToForm(s) for s in c_sessntBefore])
Example #12
0
 def _cacheFeaturedSpeaker(session_key):
     """Assign Featured Speaker to memcache; used by getFeaturedSpeaker"""
     # Use the urlsafe key to grab the session key
     session = ndb.Key(urlsafe=session_key).get()
     # Set the key for the memcache based on the confKey
     featured = 'fs_' + str(session.key.parent().urlsafe())     
     print 'In cacheFeaturedSpeaker'
     # Get all of the sessions for the conference
     sessions = Session.query(ancestor=session.key.parent()).fetch()
     count = 0
     # Iterate through the sessions to find the speaker with the most sessions
     for sess in sessions:
         spk_sess = Session.query(ancestor=session.key.parent()).filter(Session.speaker==sess.speaker)
         spk_count = spk_sess.count()
         if spk_count > count:
             count = spk_count
             # Save the speaker and sessions for the speaker with the most
             featured_speaker = sess.speaker
             fs_sessions = spk_sess
     # Grab the speaker
     speaker = ndb.Key(urlsafe=featured_speaker).get()
     # Set the speaker name and their sessions in a string
     fs_data = {}
     fs_data['name'] = speaker.name
     fs_data['sessions'] = []
     for sess in fs_sessions:
         fs_data['sessions'].append(sess.name)
     mem_val = json.dumps(fs_data)
     # Set the created json string in memcache
     memcache.set(key=featured, value=fs_data)
Example #13
0
    def _createSessionObject(self, request):
        """Create or update Session object, returning SessionForm/request."""
        # preload necessary data items
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException("Authorization required")
        user_id = getUserId(user)

        if not request.sessionName:
            raise endpoints.BadRequestException("Session 'name' field required")

        # update existing conference
        conf = ndb.Key(urlsafe=request.websafeConferenceKey).get()
        # check that conference exists
        if not conf:
            raise endpoints.NotFoundException("No conference found with key: %s" % request.websafeConferenceKey)

        # check that user is owner
        if user_id != conf.organizerUserId:
            raise endpoints.ForbiddenException("Only the owner can update the conference.")

        data = {field.name: getattr(request, field.name) for field in request.all_fields()}
        wsck = data["websafeConferenceKey"]
        p_key = ndb.Key(Conference, request.websafeConferenceKey)
        c_id = Session.allocate_ids(size=1, parent=p_key)[0]
        c_key = ndb.Key(Session, c_id, parent=p_key)

        data["startTime"] = datetime.strptime(data["startTime"], "%H:%M:%S").time()

        data["date"] = datetime.strptime(data["date"], "%Y-%m-%d").date()

        data["key"] = c_key
        data["websafeConferenceKey"] = request.websafeConferenceKey

        speaker_name = data["speaker"]

        # Query sessions by speaker and get all of the ones that are currently in the datastore and add them
        # to the memcache
        sessions_by_speaker = Session.query(
            ndb.AND(Session.speaker == speaker_name, Session.websafeConferenceKey == request.websafeConferenceKey)
        ).fetch()

        speaker_sessions = []

        speaker_sessions = FEATURED_SPEAKER.format(
            speaker_name, ",".join([session.sessionName for session in sessions_by_speaker])
        )

        print speaker_sessions

        if len(sessions_by_speaker) >= 1:
            # add the speaker and the sessions they are in into the memcache
            self._speaker_to_memcache(speaker_sessions)
        else:
            print "this speaker has 0 sessions"

        # add the new session data to datastore
        Session(**data).put()

        return request
    def getQueryProblem(self, request):
        # make sure user is Authorized
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization required')

        # create query all non-workshop sessions before 7pm
        # User would set Equal_to_Type = False, meaning they don't want that type 
        # User would set Before_OR_After = Before(string), and set the starttime to 7pm. 
        if request.Before_OR_After == "Before" :
            Sessions = Session.query(Session.startTime <= request.startTime)
            Sessions = Sessions.order(Session.startTime)
            temp = []
            for sess in Sessions:
                if request.typeOfSession in sess.typeOfSession and request.matchSessionType:
                    temp.append(sess)
                elif request.typeOfSession not in sess.typeOfSession and not request.matchSessionType:
                    temp.append(sess)
            Sessions = temp
        else:
            Sessions = Session.query(Session.startTime >= request.startTime)
            Sessions = Sessions.order(Session.startTime)
            temp = []
            for sess in Sessions:
                if request.typeOfSession in sess.typeOfSession and request.matchSessionType:
                    temp.append(sess)
                elif request.typeOfSession not in sess.typeOfSession and not request.matchSessionType:
                    temp.append(sess)
            Sessions = temp

        return SessionForms(
            items=[self._copySessionToForm(sess) for sess in Sessions]
        )
Example #15
0
class Post(bourbon.ModelInterface):
    @staticmethod
    def all():
        sess = Session()
        return [post.id for post in sess.query(PostModel).all()]
    
    @staticmethod
    def open(identifier):
        self = Post()
        self.sess = Session()
        self.post = self.sess.query(PostModel).filter_by(id=identifier).scalar()
        if self.post is None:
           self.post = PostModel()
        return self

    @staticmethod
    def stat(identifier):
        post = Post.open(identifier)
        return bourbon.Stat.as_file(len(post.read())).as_dict()

    def close(self):
        self.sess.add(self.post)
        self.sess.commit()
        del self.sess
        del self.post
        return True
    
    def read(self):
        return self.post.text

    def write(self, data):
        self.post.text = data
        
    def getattr(self):
        return bourbon.FILE
    def _getConferenceSessions(self, request, typeOfSession=None, speaker=None):
        """Its a multi purpose method which will return session forms for below three combination
         - websafeConferenceKey
         - websafeConferenceKey with typeOfSession
         - speaker  """
        wsck = request.websafeConferenceKey

        # If type of session provided without conference key then its an error
        if typeOfSession and not wsck:
            raise endpoints.BadRequestException("If typeOfSession given then confernce key should also be provided.")

        if wsck:
            # if conf key availabe then get all its child sessions
            conf = ndb.Key(urlsafe=wsck).get()
            conf_key = conf.key
            if not conf:
                raise endpoints.NotFoundException('conference is invalid')

            sessions = Session.query(ancestor=conf_key)
            # filter type of session if provided
            if typeOfSession is not None:
                sessions = sessions.filter(Session.typeOfSession == typeOfSession)
        else: # if conf key is none then filter by speaker only
            sessions = Session.query(Session.speaker == speaker)
        return SessionForms(
            sessions = [self._copySessionToForm(session)for session in sessions])
    def addFeaturedSession(speaker, sessionName, confKey):
        """
        This is an task that can add a session into the
        FeaturedSpeakerQueryForm in memcache
        """
        if not speaker:
            return

        c_key = ndb.Key(urlsafe=confKey)
        speakerSessionQuant = Session.query(
                ancestor=c_key).filter(Session.speaker == speaker).count()
        if speakerSessionQuant > 1:
            cacheForm = memcache.get(MEMCACHE_FEATUREDSPEAKER_KEY)
            # if the input speaker is the featured speaker, we add the session
            # to the featured sessions list and save it to memcache
            if cacheForm and cacheForm.featuredSpeaker == speaker:
                cacheForm.featuredSessions.append(sessionName)
                memcache.set(MEMCACHE_FEATUREDSPEAKER_KEY, cacheForm)
            else:
                # if the input speaker is not featured speaker, we have to
                # scan the conference and add associated sessions
                ancestor_key = ndb.Key(urlsafe=confKey)
                sessions = Session.query(ancestor=ancestor_key).fetch()
                featuredSessions = []
                for session in sessions:
                    if session.speaker == speaker:
                        featuredSessions.append(session.name)
                cacheForm = FeaturedSpeakerQueryForm(
                                featuredSpeaker=speaker,
                                featuredSessions=featuredSessions
                                                    )
                memcache.set(MEMCACHE_FEATUREDSPEAKER_KEY, cacheForm)
Example #18
0
    def _handleFeaturedSpeaker(websafeConferenceKey, websafeSessionKey):
        """Add given speaker to given conference's featured speaker memcache."""
        # get conference and session keys
        conf = ndb.Key(urlsafe=websafeConferenceKey).get()
        sess = ndb.Key(urlsafe=websafeSessionKey).get()
        
        # if this session's speaker has more than one session in this 
        # conference, add them and their sessions to memcache announcement
        for s in sess.speaker:
            # get other sessions in this conference with same speaker
            other_sess = Session.query(ancestor=conf.key)
            other_sess = other_sess.filter(Session.speaker==s)
            
            # if more than one, add announcement
            if other_sess.count() > 1:
                # make unique memcache key of conference and speaker keys
                sp_key = '_'.join(('sk', websafeConferenceKey, s.urlsafe()))
                speaker = s.get()
                # create value to be string of speaker's name and sessions
                val = speaker.name + ' - ' + '; '.join(ss.name for ss \
                            in Session.query(Session.speaker==s))

                memcache.set(sp_key, val)

                # add this speaker's key to this conference's list of speaker 
                # keys (there can be multiple speakers with many sessions)
                mem_key = '_'.join((MEMCACHE_FEATURED_SPEAKERS_KEY, 
                                    websafeConferenceKey))
                speaker_keys = memcache.get(mem_key)
                if speaker_keys:
                    speaker_keys.add(sp_key)
                else:
                    speaker_keys = set([sp_key])
                memcache.set(mem_key, speaker_keys)
Example #19
0
    def post(self, id = None):
	request = self.wsgi_request
	response = self.wsgi_response
	c = self.context

	param_dict = dict(
		title = request.params.title,
		conf_id = request.params.conf_id,
		desc = request.params.desc,
		venue = request.params.venue,
		talk_type = request.params.talk_type,
		start_date = parse(request.params.start_date),
		end_date = parse(request.params.end_date),
		duration = request.params.duration,
		speaker_title = request.params.speaker_title)
	content = param_dict
	session = Session(**param_dict)
	session = session.save()

	conf = Conference.get(session.conf_id)
	if conf:
	    conf.add_session(session)

	self.set_body(session.to_json())
	response.headers['content-type'] = 'application/json'
	return self.render()
Example #20
0
 def filterPlayground2(self, request):
     """Filter Playground 2 - Task 3 Problem"""
     
     #First filter by time, query all session before 7pm
     timefilter = datetime.strptime('19:00', '%H:%M').time()
     sessions1 = Session.query(Session.startTime < timefilter)
     
     #second filter by session type, query all session by the type
     sessions2 = Session.query(Session.typeOfSession != "WORKSHOP")
     
     #fetch the keys of the first filter
     s1_key = sessions1.fetch(keys_only=True)
     
     #fetch the keys of the second filter
     s2_key = sessions2.fetch(keys_only=True)
     
     #find the intersect of both filter.
     #which will return a list with keys that satisfy both filter condition           
     s1ns2 = set(s1_key) & set(s2_key)
     
     #get the sessions from the keys
     sessions = ndb.get_multi(s1ns2)
     
     return SessionForms(
         items=[self._copySessionToForm(session) for session in sessions]
     )
Example #21
0
 def save(self):
     session = Session(
         session_key = self.session_key,
         session_data = self.encode(self._session),
         expire_date = self.get_expiry_date()
     )
     session.put()
    def getFeaturedSpeaker(self, request):
        """Returns the sessions of the featured speaker"""
        # attempt to get data from memcache
        data = memcache.get('featured_speaker')
        from pprint import pprint
        pprint(data)
        sessions = []
        sessionNames = []
        speaker = None

        if data and data.has_key('speaker') and data.has_key('sessionNames'):
            speaker = data['speaker']
            sessionNames = data['sessionNames']

        # if memcache fails or is empty, pull speaker from upcoming session
        else:
            upcoming_session = Session.query(Session.date >= datetime.now())\
                                    .order(Session.date, Session.startTime).get()
            if upcoming_session:
                speaker = upcoming_session.speaker
                sessions = Session.query(Session.speaker == speaker)
                sessionNames = [session.name for session in sessions]

        # populate speaker form
        sf = SpeakerForm()
        for field in sf.all_fields():
            if field.name == 'sessionNames':
                setattr(sf, field.name, sessionNames)
            elif field.name == 'speaker':
                setattr(sf, field.name, speaker)
        sf.check_initialized()
        return sf
    def getConferenceSessionsByType(self, request):
        """Given a conference, return all sessions of a specified type
        (eg lecture, keynote, workshop)"""

        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException(
                'You must be logged in to call this method.')
        user_id = getUserId(user)

        conf_key = ndb.Key(
            urlsafe=request.websafeConferenceKey)

        # verify websafekey points to Conference entity.
        if conf_key.kind() != 'Conference':
            raise endpoints.BadRequestException(
                'websafeKey must point to Conference entity.')
        sessions = Session.query(ancestor=conf_key)

        q = Session.query(
            ancestor=ndb.Key(urlsafe=request.websafeConferenceKey))
        q = q.filter(request.type == Session.typeOfSession)

        return SessionForms(
            items=[self._copySessionToForm(sess) for sess in q])
Example #24
0
    def NonWorkshopSessionsBefore7pm(self, request):
        """Return Non-Workshop Sessions Before 7pm."""
        # make sure user is authed
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization required')
        # user_id = getUserId(user)

        theStartTime = datetime.strptime("19:00", "%H:%M").time()

        # idea from reading answers from Tim Hoffman
        # (http://stackoverflow.com/users/1201324/tim-hoffman)
        # and Brent Washburne
        # (http://stackoverflow.com/users/584846/brent-washburne)
        # specifically Brent's answer here:
        # https://stackoverflow.com/questions/33549573/combining-results-of-multiple-ndb-inequality-queries

        # create two separate inequality queries and get the keys from each
        # then use set.intersection method to get the
        # intersection of the two sets
        query1 = Session.query(Session.typeOfSession != "Workshop").fetch(
                               keys_only=True)
        query2 = Session.query(Session.startTime < theStartTime).fetch(
                               keys_only=True)
        sessions = ndb.get_multi(set(query1).intersection(query2))

        # return set of SessionForm objects per Conference
        return SessionForms(
            items=[self._copySessionToForm(session) for session in sessions]
        )
Example #25
0
def add_session(request, instance_name):
    start_date = request.POST['start_date']
    end_date = request.POST['end_date']
    instance = get_object_or_404(ExperimentInstance, name=instance_name)
    session = Session(instance=instance, start_date=start_date, end_date=end_date)
    session.save()
    return JsonResponse({'ok': 'ok'})
    def getFeaturedSpeaker(self, request):
        """Returns the sessions of the featured speaker"""
        # try to get data from memcache
        data = memcache.get(MEMCACHE_FEATURED_SPEAKER_KEY)
        sessions = []
        sessionNames = []
        speaker = None

        if data and data.has_key('speaker') and data.has_key('sessionNames'):
            speaker = data['speaker']
            sessionNames = data['sessionNames']

        # if data is not on memcache, get speaker from upcoming session
        else:
            nextSession = Session.query(Session.date >= datetime.now()).order(Session.date, Session.startTime).get()
            if nextSession:
                speaker = nextSession.speaker
                sessions = Session.query(Session.speaker == speaker)
                sessionNames = [session.name for session in sessions]

        # fill speaker form
        speaker_form = SpeakerForm()
        for field in speaker_form.all_fields():
            if field.name ==  'sessionNames':
                setattr(speaker_form, field.name, sessionNames)
            elif field.name == 'speaker':
                setattr(speaker_form, field.name, speaker)
        speaker_form.check_initialized()
        return speaker_form
 def getSessionsAfterTimeExcludingType(self, request):
     """ This query returns sessions for given conference starting after given time,
         and excluding the given type. """
     # Solves the problem:
     # Let's say you dont like workshops and you sont like sessions before 7pm.
     #  How would you handle a query for all non-workshop sessions before 7 pm?
     #  is the problem for implementing this query? What ways to solve it did you think of?
     # Solution:
     #  The following query is my proposal. Ancestor query for time and typeOfSession (indexed).
     #  The encountered problem is "BadRequestError: Only one inequality filter per query is supported."
     #  which can be solved by using twp separate queries for each inequality, getting the intersection
     #  of the keys, and then retrieving the entities from the intersected keys.
     conf = ndb.Key(urlsafe=request.websafeConferenceKey).get()
     if not conf:
         raise endpoints.NotFoundException(
             'No conference found with key: %s' % request.websafeConferenceKey)
     if not request.date and request.date.time():
         raise endpoints.NotFoundException(
             'Query must include a valid date and time.')
     sessionsByTime = Session.query(Session.time >= request.date.time(),ancestor=conf.key)
     sessKeysByTime = sessionsByTime.fetch(None, keys_only=True)
     sessionsByType = Session.query(Session.typeOfSession != request.typeOfSession,ancestor=conf.key)
     sessKeysByType = sessionsByType.fetch(None, keys_only=True)
     validSessKeys  =  set(sessKeysByTime).intersection(set(sessKeysByType))
     sessions       = ndb.get_multi(validSessKeys)
     return SessionForms(
         items=[self._copySessionToForm(sess) for sess in sessions]
     )
Example #28
0
def new_account(request):
    # pull out the POSTed fields
    username = request.POST.get('username', None)
    password = request.POST.get('password', None)
    person_name = request.POST.get('real_name', '')
    person_loc = request.POST.get('location', '')
    person_bio = request.POST.get('bio', '')
    person_img = request.POST.get('b64_img', '')

    # ensure both the username and password were given
    if username is None or password is None:
        return HttpResponseBadRequest("Missing username or password")
    
    # create a new account 
    u_norm = username.lower()
    p_hash = hash_password(password)
    n_account = Account(account_key=u_norm, passphrase=p_hash)
    n_account.save()
    # and then a profile for this new account
    n_profile = Profile(name=person_name, location=person_loc,
        bio=person_bio, img_data=person_img, account=n_account)
    n_profile.save()

    # create a session for the new account and send the key
    u_session = Session(key=session_hash(), account=n_account)
    u_session.save()
    return redirect('existing-session', key=u_session.key)
 def _cacheFeaturedSpeaker(self):
     """
     Create memcache for a featured Speaker of a conference.
     """
     # Get the conference that the speaker is speaking
     # at from the websafeKey provided in the request
     conf_k = ndb.Key(urlsafe=self.request.get('key'))
     conf = conf_k.get()
     # The speaker that was just added
     addedSpeaker = self.request.get('speaker')
     # Find the speaker in the most sessions giving a Confenence
     # and return (name, sessionsCount)
     featuredSpeaker = Session.countspeakers(conf_k)
     speakerName = featuredSpeaker[0]
     sessionsSpeakersIn = featuredSpeaker[1]
     speakerMemKey = SPEAKER_ANNOUNCEMENTS_KEY + self.request.get('key')
     # Query that gets the name of the sessions the features speaker
     sessionsInfo = [session.name for session in \
         Session.query(ancestor=conf_k, projection=[Session.name]).\
         filter(Session.speaker == speakerName)]
     # Turn array into string then remove []
     if sessionsSpeakersIn > 1 and speakerName == addedSpeaker:
         speaker = {
             "name": speakerName.title(),
             "conf_name": conf.name.title(),
             "sessions": sessionsInfo
         }
         memcache.add(key=speakerMemKey, value=speaker, time=600)
     else:
         # If no feature speaker
         speaker = ""
         memcache.delete(speakerMemKey)
     return speaker
    def post(self):
        game_id = self.get_argument('game_id')
        game_password = self.get_argument('game_password')
        username = self.get_argument('username')

        session = Session()
        try:
            user = get_user(username)
            game = get_game(game_id=game_id, game_password=game_password)
            try:
                usergame = get_usergame(user.id, game.id)
                if usergame is not None:
                    response_dict = get_response_dict(True)
            except NoResultFound:
                if not game.started:
                    game.add_user(user)
                    response_dict = get_response_dict(True)
                else:
                    response_dict = get_response_dict(False, 'Game has started; you cannot join.')
                    
        except Exception as e:
            session.rollback()
            response_dict = get_response_dict(False, e.message)
        finally:
            Session.remove()
            self.finish(simplejson.dumps(response_dict))
Example #31
0
def login():
    # If the user is already logged in, just display the index
    if already_logged_in(request):
        flash('Already logged in.')
        return redirect(url_for('index'))

    if request.method == 'GET':
        return TemplateManager.get_login_template()
    else:
        username = request.form['username'].strip()
        otptoken = request.form['otptoken'].strip()
        password = request.form['password']

        user = User.check_password(username, password)
        if not user:
            return TemplateManager.get_login_template(
                ["Invalid Login or password"])

        match, used_recovery_code = user.verify_twofa(otptoken)
        if not match:
            return TemplateManager.get_login_template(
                ['2FA token invalid.'])

        result, session_token, csrf_token = Session.new_session(user)
        if not result:
            return TemplateManager.get_login_template(
                ["Could not create session"])

        # Make the response and set the cookie
        if used_recovery_code:
            url = url_for('retrieve_2fa_code')
        else:
            url = url_for('index')

        response = make_response(redirect(url, code=httplib.SEE_OTHER))
        set_cookie(response, Session.SESSION_KEY, session_token, app.config['MAX_SESSION_AGE'])
        return response
Example #32
0
def response():
    if not current_user.is_anonymous():
        user_data = asana_api.exchange_token(current_user.refresh_token,
                                             "refresh_token")
        next = request.args.get("next", "/")
    elif not request.args.get("code"):
        return redirect(constants.AUTH_ENDPOINT +
                        "&state=%s" % request.args.get("next", "/"))
    else:
        user_data = asana_api.exchange_token(request.args.get("code"), "code")
        print 'user data:', user_data
        next = request.args.get("state", "/")
    s = Session()
    user = User.from_json(user_data)
    s.merge(user)
    login_user(user)
    s.commit()
    return redirect(next)
    def _setFeaturedSpeaker(self, websafeConferenceKey, websafeSpeakerKey,
                            speaker):
        """
        Input: websafeConferenceKey, websafeSpeakerKey, speaker
        Returns: Doesn't return anything
        Description: this method checks if the speaker has more than one sessions,
        within the same conference and adds a message in memcache mentioning the
        speaker name as featured speaker and session names he/she is delivering.

        NOTE: This method is being executed using taskqueue from
        SetFeaturedSpeakerHandler() in main.py
        """
        # ---------  add featured speaker to memcache -----------

        conf_key = ndb.Key(urlsafe=websafeConferenceKey)
        # Gets all the sessions for current Conference
        sessions = Session.query(ancestor=conf_key)
        # Filters the returned sessions based on speaker
        sessions = sessions.filter(
            Session.websafeSpeakerKey == websafeSpeakerKey)
        sessions = sessions.fetch()

        # Checks if the more than one sessions were returned for current
        # speaker
        if len(sessions) > 1:
            featuredSpeakerMessage = speaker + " is featured speaker " + \
                "and he will be delivering talk in following sessions. "
            sessionsCSV = ""
            # building a comma separated list of session names where featured
            # speaker is speaking
            for session in sessions:
                sessionsCSV += session.name + ", "

            featuredSpeakerMessage = featuredSpeakerMessage + \
                sessionsCSV[:-2] + "."

            memcache.set(MEMCACHE_FEATURED_SPEAKER_KEY, featuredSpeakerMessage)
Example #34
0
    def SessionsBySpeakerOnSpecificDate(self, request):
        """Return Conference sessions by Speaker on a specific date."""

        # make sure user is authed
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization required')
        # user_id = getUserId(user)

        sp_lastName = request.lastName
        sp_firstName = request.firstName
        theDate = datetime.strptime(request.conferenceDate, "%Y-%m-%d").date()

        if sp_firstName:
            # find by first and last name
            speaker = Speaker.query(ndb.AND(
                Speaker.lastName == sp_lastName,
                Speaker.firstName == sp_firstName))
        else:
            # find by last name only
            speaker = Speaker.query(Speaker.lastName == sp_lastName)

        speaker_keys = [sp.key for sp in speaker]

        # iterate over each key finding all sessions
        all_sessions = []
        for sp_k in speaker_keys:
            sessions = Session.query(ndb.AND(
                Session.speaker == sp_k,
                Session.date == theDate))
            for s in sessions:
                all_sessions.append(s)

        # return list of sessions that match each of the speaker_keys
        return SessionForms(
            items=[self._copySessionToForm(sess) for sess in all_sessions]
        )
Example #35
0
def toggle_default_platform(platform):
    """
    Toggle active state of platfrom for a game.
    :param game: - game name
    :param platform: - platform id (1 - windows, 2 - macos, 4 - linux)
    """
    app.logger.info("Requesting toggle of default platform: %s.", platform)
    _session = Session()
    _user = _session.query(User).one()
    _result = {}
    _platform_list = [1, 2, 4]
    _platform = int(platform)
    if _platform not in _platform_list:
        app.logger.error(
            "Unknown platform requested: %s", platform)
        return "Unknown platform requested", 400
    app.logger.info(
        "Requesting change of platform: %s.", platform)
    app.logger.debug("Current: %s", _user.platform)
    _state = _user.platform & _platform
    if _state == _platform:
        # Disable platform
        _mask = ~ _platform
        _user.platform = _user.platform & _mask
    else:
        _user.platform = _user.platform | _platform
    # Game in db - toggle platfrom
    db_games = _session.query(Game).filter(
        Game.platform_available.op('&')(_platform) == _platform).all()
    for db_game in db_games:
        # app.logger.debug("Game %s found in the DB.", game)
        if db_game.state == Status.running:
            return "Platform change during download is prohibited.", 400
        _selected = db_game.platform
        if _selected < 0:
            if (_user.platform & db_game.platform_available) != \
                    (db_game.platform_ondisk & _user.platform & db_game.platform_available) \
                    and db_game.state != Status.queued and \
                    db_game.state != Status.running:
                _result[db_game.name] = {'missing': True}
            else:
                _result[db_game.name] = {'missing': False}
    _session.commit()
    return jsonify(_result)
 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])
Example #37
0
    def getConferenceSessionsByDate(self, request):
        """Given a conference and a date, return all sessions,
        ordered by session start time."""
        # get Conference object from request; bail if not found
        conf = ndb.Key(urlsafe=request.websafeConferenceKey).get()
        if not conf:
            raise endpoints.NotFoundException(
                'No conference found with key: %s' %
                request.websafeConferenceKey)

        # convert date from string to Date object;
        queryDate = datetime.strptime(request.dateOfSession[:10],
                                      "%Y-%m-%d").date()

        # create ancestor query for all key matches for this conference
        sessions = Session.query(ancestor=ndb.Key(
            urlsafe=request.websafeConferenceKey))
        # filter sessions by dateOfSession
        sessions = sessions.filter(Session.date == queryDate)
        # order sessions by startTime
        sessions = sessions.order(Session.startTime)
        # return set of SessionForm objects per Session
        return SessionForms(
            items=[self._copySessionToForm(sess) for sess in sessions])
Example #38
0
    def getConferenceSessionsByType(self, request):
        """Given a conference, return all sessions of a specified type"""
        data = {
            field.name: getattr(request, field.name)
            for field in request.all_fields()
        }
        typeOfSession = data['typeOfSession']

        # Get conference by key
        conf = ndb.Key(urlsafe=request.websafeConferenceKey).get()

        # If no conference exists with that key, error out.
        if not conf:
            raise endpoints.NotFoundException(
                'No conference found with key: %s' %
                request.websafeConferenceKey)

        # Search for all sessions where ancestor is this conference
        sessions = Session.query(Session.typeOfSession == typeOfSession,
                                 ancestor=ndb.Key(Conference, conf.key.id()))

        # Return SessionForm objects
        return SessionForms(
            items=[self._copySessionToForm(session) for session in sessions])
    def querySessionByTypeAndStartTime(self, request):
        """
        querySessionByTypeAndStartTime solves the multiple inequality problem by
        first retrieving the sessions filtered by typeOfSession requested by
        client and then iterates through the results to check startTime which is
        lesser than the provided startTime
        Returns the resulting sessions in SessionForms
        """
        sessions = Session.query()
        # Filters session on typeOfSession
        sessions = sessions.filter(
            Session.typeOfSession != request.typeOfSession)

        # Checks if each session has startTime lower than the provided
        # startTime
        sessionsBeforeTime = [
            session for session in sessions
            if session.startTime < request.startTime
        ]

        # return individual SessionForm object per Session
        return SessionForms(sessions=[
            self._copySessionToForm(session) for session in sessionsBeforeTime
        ])
 def _getSessionsQuery(self, inequality_field, filter_set, conf_key):
     """ Return formatted query from the submitted filters for Sessions. 
         This is the function where we query all equality filters and
         the first inequality filter. We can only do one inequality
         operation for one filter query.
     """
     # Get query object for all sessions for a particular conference
     s = Session.query(ancestor=conf_key)
     # If we don't have an inequality filter,
     # If we have an inequality filter, then order by that property first
     # This is a requirement in ndb. First order by the property that we
     # are going to do the inequality filtering.
     if not inequality_field:
         s = s.order(Session.name)
     else:
         s = s.order(ndb.GenericProperty(inequality_field))
         s = s.order(Session.name)
     for filtr in filter_set:
         # format the values in the filter (for date, time, and duration which is an int)
         if filtr["field"] == "start_time":
             print "this is the value: ", filtr["value"]
             value = datetime.strptime(filtr["value"][:10], "%H:%M")
             print "Formatting the start time field: ", value
         elif filtr["field"] == "date":
             value = datetime.strptime(filtr["value"][:10], "%Y-%m-%d")
             print "Formatting the date: ", filtr["value"]
         elif filtr["field"] == "duration":
             value = int(filtr["value"])
         else:
             value = filtr["value"]
             print "Post formatting, the filters are: ", filtr
         # Get the filter node for that particular filter
         formatted_query = ndb.query.FilterNode(filtr["field"],
                                                filtr["operator"], value)
         s = s.filter(formatted_query)
     return s
Example #41
0
    def getConferenceSessions(self, request):
        """Given a conference, returns all sessions."""

        # copy the ConferenceForm/ProtoRPC Message into a dict object
        data = {
            field.name: getattr(request, field.name)
            for field in request.all_fields()
        }

        # get the existing conference
        conf = ndb.Key(urlsafe=request.websafeConferenceKey).get()

        # check to see if that conference exists
        if not conf:
            raise endpoints.NotFoundException(
                'Sorry but, no conference found with the key: %s' %
                request.websafeConferenceKey)

        # create query for all key matches for this conference
        sessions = Session.query(ancestor=ndb.Key(Conference, conf.key.id()))

        # return SessionForm objects per Session
        return SessionForms(
            items=[self._copySessionToForm(session) for session in sessions])
Example #42
0
    def getConferenceSessionsByType(self, request):
        """Return all sessions of a conference by the type of session"""

        data = {
            field.name: getattr(request, field.name)
            for field in request.all_fields()
        }
        typeOfSession = data['typeOfSession']

        #fetch conference from key
        conf = ndb.Key(urlsafe=request.websafeConferenceKey).get()

        #Check that conference exists
        if not conf:
            raise endpoints.NotFoundException(
                'No conference found with this key: %s' %
                request.websafeConferenceKey)

        #Create ancestor query for all key matches for this conference
        sessions = Session.query(Session.typeOfSession == typeOfSession,
                                 ancestor=conf.key)

        return SessionForms(
            items=[self._copySessionToForm(session) for session in sessions])
Example #43
0
def login():
    if request.method == 'POST':
        errors = []
        username = request.form.get('username')
        password = request.form.get('password')

        if not username and password:
            errors.append('Missing required username or password field')

        try:
            user = User(username=username)
            user.authenticate(password=password)
        except AuthenticationError, e:
            errors.append(e)

        if not errors:
            next = request.form.get('next', '/')
            response = make_response(redirect(next))

            session = Session(user_id=user.user_id)
            response.set_cookie('session', value=session.session_id)
            return response
        else:
            return render_template("login.html", errors=errors)
Example #44
0
 def getConferenceSessions(self, request):
     """Given a conference, return all sessions."""
     # Ensure that user is authed
     user = endpoints.get_current_user()
     if not user:
         raise endpoints.UnauthorizedException('Authorization Required')
     # Retrieve the Conference key
     try:
         c_key = ndb.Key(urlsafe=request.websafeConferenceKey)
     except Exception:
         raise endpoints.BadRequestException(
             'The websafeConferenceKey given is invalid.')
     # Verify that the Conference exists
     conf = c_key.get()
     if not conf:
         raise endpoints.NotFoundException(
             'No conference found for the key provided: %s' %
             request.websafeConferenceKey)
     # Store the Sessions that are ancestors of the Conference
     sessions = Session.query(ancestor=c_key)
     # Return a SessionForm for each Session
     return SessionForms(items=[
         self._copyConferenceSessionToForm(sesh) for sesh in sessions
     ])
    def getNonWorkshopsBefore7(self, request):
        """Return sesssions where the title or the highlights
        contain the given topic keyword.
        """
        # NOTE: we want "localTime < 7pm and session is not a workshop",
        # however, we can only have one inequality per query, so we
        # query on time first (so that we can sort on it), and then
        # filter the workshop sessions out with an in-memory loop.
        # ALSO NOTE that we allow None session types and times through.
        sevenPM = time(19)
        sessions = Session.query(not Session.localTime or Session.localTime <= sevenPM) \
                          .order(Session.localTime)

        result = []
        for session in sessions:
            # skip workshop sessions
            if session.typeOfSession and session.typeOfSession == 'WORKSHOP':
                continue
            result.append(session)

        # return set of ConferenceForm objects per Conference
        return SessionForms(
            items=[self._copySessionToForm(session) for session in result]
        )
Example #46
0
    def decorated_function(*args, **kwargs):
        access_token = request.headers.get('Authorization')
        if access_token is not None:  
            try:
                payload = jwt.decode(access_token, SECRET_KEY, ALGORITHM)
            except jwt.InvalidTokenError:
                 payload = None

            if payload is None:
                raise GraphQLError('Invalid_Token')
            
            user_id = payload
            master = Session.query(User).filter_by(id=user_id['user_id']).first()
            
            if master.is_master == 1:
                g.master_id = user_id
                
            else:
                raise GraphQLError('Not_Master_Token')
            
        else:
            raise GraphQLError('Invalid_Token')

        return func(*args, **kwargs)
Example #47
0
    def getConferenceSessions(self, request):
        """Given a conference, returns all sessions."""

        # copy ConferenceForm into dict
        data = {
            field.name: getattr(request, field.name)
            for field in request.all_fields()
        }

        # get existing conference
        conf = ndb.Key(urlsafe=request.websafeConferenceKey).get()

        # check that this conference exists
        if not conf:
            raise endpoints.NotFoundException(
                'No conference found with key: %s' %
                request.websafeConferenceKey)

        # create a query that returns all sessions descended from this ancestor conference
        sessions = Session.query(ancestor=ndb.Key(Conference, conf.key.id()))

        # return the set of SessionForm objects per Session
        return SessionForms(
            items=[self._copySessionToForm(session) for session in sessions])
Example #48
0
    def getConferenceSessions(self, request):
        """Return sessions for a Conference (by websafeConferenceKey)."""
        # make sure user is authed
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization required')
        # user_id = getUserId(user)
        wsck = request.websafeConferenceKey
        try:
            conf = ndb.Key(urlsafe=wsck).get()
        except:
            conf = None

        if not conf:
            raise endpoints.NotFoundException(
                'No conference found for key: %s' % wsck)

        # create ancestor query for all key matches for this conference
        sessions = Session.query(ancestor=conf.key)

        # return set of SessionForm objects for conference
        return SessionForms(
            items=[self._copySessionToForm(session) for session in sessions]
        )
Example #49
0
    def task3Test(self, request):
        """an endpoint just to test solutions for project's Task 3"""
        # first query is a simple property filter
        # second query more problematic...how to compare TimeProperties?

        # this solution did not work; datastore not correctly comparing time obj
        # good_sessions = Session.query(Session.typeOfSession != 'Workshop')
        # cutoff = time(19)
        # print 'HEY cutoff time is %s', cutoff
        # good_sessions.filter(Session.startTime < cutoff)

        # this solution works but may be problematic performance-wise
        good_sessions = Session.query(
            Session.typeOfSession != 'Workshop').fetch()
        if good_sessions:
            cutoff = time(19)
            good_sessions = [
                session for session in good_sessions
                if session.startTime < cutoff
            ]

        return SessionForms(items=[
            self._copySessionToForm(session) for session in good_sessions
        ])
    def querySessionTime(self, request):
        """Return sessions given a conference object, time and type"""
        # get conference object from Datastore
        conf = self._getDataStoreObject(request.websafeConferenceKey)
        # query sessions using ancestor conf Key, order sessions by start time
        sessions = Session.query(ancestor=conf.key).order(Session.startTime)

        # filter sessions by time (before/after/equal certain time)
        if (request.operator and request.time):
            node = ndb.query.FilterNode(
                                    'startTime',
                                    OPERATORS[request.operator],
                                    datetime(1970, 01, 01, request.time, 00))
            sessions = sessions.filter(node)
        else:
            raise endpoints.BadRequestException("You need to define both "
                                                "operator and time")

        # only return session types that are not equal to what the user provided
        return SessionForms(
            items=[self._copySessionToForm(x)
                   for x in sessions
                   if x.sessionType != request.sessionType]
        )
Example #51
0
    def _getSessionQuery(self, request):
        """Return formatted query from the submitted filters.
        """

        q = Session.query()
        inequality_filter, filters = self._formatSessionFilters(
            request.filters)

        # If exists, sort on inequality filter first
        if not inequality_filter:
            q = q.order(Session.name)
        else:
            q = q.order(ndb.GenericProperty(inequality_filter))
            q = q.order(Session.name)

        for filtr in filters:
            if filtr["field"] in ["durationInMinutes"]:
                filtr["value"] = int(filtr["value"])
            formatted_query = ndb.query.FilterNode(
                filtr["field"], 
                filtr["operator"], 
                filtr["value"])
            q = q.filter(formatted_query)
        return q
Example #52
0
    def process_item(self, item, spider):
        """Save courses and sessions in the database.

        This method is called for every item pipeline component.

        """

        # TODO: Find a way to update rather than over write all the data
        # http://stackoverflow.com/questions/2546207/does-sqlalchemy-have-an-equivalent-of-djangos-get-or-create
        db_session = self.Session()

        course_data = {
            'number': item['number'],
            'title': item['title'],
            'deptTitle': item['deptTitle'],
            'department': item['department']
        }

        course = Course(**course_data)

        sessions_data = item['sessions']
        for session_data in sessions_data:
            session = Session(**session_data)
            course.sessions.append(session)

        try:
            db_session.add(course)
            # db_session.merge(course)
            db_session.commit()
        except:
            db_session.rollback()
            raise
        finally:
            db_session.close()

        return item
Example #53
0
def scan_token(start):
    tokenAddr = {
        "lead": "0x2866a32f364b67437c442f5a15fdc992be83cd6f",
        "beer": "0x3870c14F5a2Da7b67353831C33477E889C6e841D",
    }
    heco = HecoApi("http://192.168.1.123:16909/")
    block_number = heco.get_block_number()
    if block_number <= 0:
        return
    session = Session()
    for i in range(start, block_number + 1):
        block = heco.get_block(i, True)
        block_time = int(block['timestamp'], 16)
        for t in block['transactions']:
            token = get_token(t['to'])
            if token == "":
                continue
            try:
                if t['input'].startswith('0xc00007b'):
                    receipt = heco.get_tx_receipt(t['hash'])
                    level = 0
                    sub = ""
                    for l in receipt['logs']:
                        if l['address'].lower() == tokenAddr[token].lower():
                            session.add(
                                HecoSwapReward(
                                    symbol=token,
                                    blockNum=i,
                                    blockTime=block_time,
                                    addr='0x' + l['topics'][2][25:],
                                    reward=f"{int(l['data'], 16)/10**18:>.6f}",
                                    sub=sub,
                                    subLevel=level))
                            print(block_time, '0x' + l['topics'][2][25:],
                                  int(l['data'], 16) / 10**18)
                        if level == 0:
                            sub = l['address']
                        level += 1
                    session.commit()
            except Exception as e:
                print(str(e))
        if i % 1000 == 0:
            print(i)
Example #54
0
    def post(self):
        '''接收用户提交的信息,写入到数据库'''
        nickname = self.get_argument('nickname')
        password = self.get_argument('password')
        gender = self.get_argument('gender')
        city = self.get_argument('city')
        bio = self.get_argument('bio')
        '''产生安全密匙'''
        safe_password = self.gen_password(password)

        #将用户数据写入数据库
        session = Session()
        user = User(nickname=nickname,password=safe_password,
                    gender=gender,city=city,bio=bio)
        session.add(user)
        session.commit()

        '''注册完成跳转到登陆界面'''
        self.redirect('/user/login')
Example #55
0
def import_index_history_ohlcv():
    indices = ['000001', '399001', '399006', '000016']
    for i in indices:
        datas = read_tdx_ohlcv('../data/index_history/%s.txt' % i)
        records = [
            IndexOhlcv(code=i,
                       datetime=data[0],
                       period='d',
                       open=data[1],
                       high=data[2],
                       low=data[3],
                       close=data[4],
                       volume=data[5],
                       amount=data[6]) for data in datas
        ]

        ss = Session()
        ss.add_all(records)
        ss.commit()
Example #56
0
    def post(self):
        # 获取参数
        content = self.get_argument('content')  # 回复内容
        cmt_id = int(self.get_argument('cmt_id'))  # 所回复的原评论的 ID
        wb_id = int(self.get_argument('wb_id'))  # 对应的微博 ID
        user_id = int(self.get_cookie('user_id'))  # 当前用户的 ID

        # 添加数据
        session = Session()
        comment = Comment(user_id=user_id,
                          wb_id=wb_id,
                          cmt_id=cmt_id,
                          content=content,
                          created=datetime.datetime.now())
        session.add(comment)
        session.commit()

        # 发表完回复以后,页面回到原微博下
        return self.redirect('/weibo/show?weibo_id=%s' % wb_id)
Example #57
0
def main():
    session = Session()

    f = codecs.open('data/u.item', 'r', 'utf-8', 'ignore')
    for line in f:
        cols = [c.strip() for c in line.strip().split('|')]
        assert len(cols) == 24

        item = Item()
        item.dataset = Item.DATASET_MOVIELENS_100K

        key = cols[0]
        item.key = key

        name, year = _extract_name_and_year(cols[1], key)
        if name is None:
            raise Exception('Can\'t split name and year from 2nd column')
        item.name = name
        item.year = year

        release_date = _get_date(cols[2])
        item.release_date = release_date

        video_release_date = _get_date(cols[3])
        item.video_release_date = video_release_date

        old_imdb_url = cols[4]
        item.old_imdb_url = old_imdb_url

        # category가 unknown인 경우, 다른 valid한 category가 없어야 합니다.
        if cols[5] == '1':
            assert sum(int(d) for d in cols[6:]) == 0

        for j, key in zip(range(6, 24), [
                'action', 'adventure', 'animation', 'children', 'comedy',
                'crime', 'documentary', 'drama', 'fantasy', 'flim_noir',
                'horror', 'musical', 'imystery', 'romance', 'sci_fi',
                'thriller', 'war', 'western'
        ]):
            bool_value = (int(cols[j]) == 1)
            setattr(item, 'category_{}'.format(key), bool_value)
        session.add(item)
    session.commit()
def main():
    session = Session()

    for idx, item in enumerate(
            session.query(Item).filter(
                Item.price.is_(None),
                Item.rotten_movie_id.isnot(None), )):
        # if rotten_movie.id < 1623:
        #     continue

        print(">>", item.id)

        rotten_movie = item.rotten_movie
        if not rotten_movie:
            continue

        amazon_movie = rotten_movie.amazon_movie
        apple_movie = rotten_movie.apple_movie
        if not apple_movie and not amazon_movie:
            continue

        prices = []
        if amazon_movie:
            prices.append(amazon_movie.get_price())
        if apple_movie:
            prices.append(apple_movie.price)
        prices = [p for p in prices if p and p > 0.0]
        if not prices:
            continue

        price = sum(prices) / len(prices)
        item.price = price

        if idx % 100 == 0:
            session.commit()
    session.commit()
 def delete_session(self, args, guild, channel, sc, user):
     messages = []
     search = ''
     if len(args) == 1:
         if not sc:
             raise Exception('No session provided for deletion')
     else:
         search = ' '.join(args[1:])
         sc = Session().find(guild.name, str(channel.id), search)
     if not sc:
         return [f'{search} was not found. No changes made.']
     else:
         search = str(sc.name)
         channel_id = str(sc.channel_id) if sc.channel_id else ''
         sc.character.archive(user)
         sc.archived = True
         sc.updated_by = str(user.id)
         sc.updated = T.now()
         sc.save()
         messages.append(f'***{search}*** removed')
         if channel_id:
             channel = Channel().get_by_id(channel_id)
             messages.append(channel.get_string())
         return messages
Example #60
0
def plc_client(ip, rack, slot, plc_id):
    """
    建立plc连接的上下文管理器
    :param ip: 
    :param rack: 
    :param slot: 
    :return: 
    """
    client = Client()
    try:
        client.connect(ip, rack, slot)
    except Snap7Exception as e:
        logging.warning('连接plc失败' + str(e))
        session = Session()
        id_num = r.get('id_num')
        alarm = connect_plc_err(id_num, plc_id)
        session.add(alarm)
        session.commit()
    yield client
    client.disconnect()
    client.destroy()