예제 #1
0
    def test_freeblock_longer_range(self):
        """
        Tests for full day (within hour range) return when less days have events
        than those in the freeblock range.
        """
        begin_time = datetime.time(9,0)
        end_time = datetime.time(17,0)
        begin_date = datetime.date(2015, 9, 1)
        end_date = datetime.date(2015, 9, 18)

        oneWeekAgenda = Agenda()
        for day in range(10,19):
            oneWeekAgenda.append(Appt(datetime.date(2015, 9, day),datetime.time(12, 30), datetime.time(14,20),"12:30-2:20pm"))

        expected = Agenda()
        for day in range(1,10):
            expected.append(Appt(datetime.date(2015, 9, day),datetime.time(9, 0), datetime.time(17,0),"all day"))
        for day in range(10,19):
            expected.append(Appt(datetime.date(2015, 9, day),datetime.time(9, 0), datetime.time(12, 30),"9:00-12:30am"))
            expected.append(Appt(datetime.date(2015, 9, day),datetime.time(14,20), datetime.time(17, 0),"2:20pm-5pm"))

        expected.normalize()
        result = oneWeekAgenda.freeblocks(begin_date, end_date, begin_time, end_time)

        self.assertEqual(result.__len__(),expected.__len__())
        self.assertTrue(result.__eq__(expected))
예제 #2
0
def blocktimes():
    """
    Retrieves the events from the selected calendars using the Google Calendar API.
    Events are inserted into an Agenda as Appointements. Agenda.freeblock then returns
    an Agenda of the possible meeting times, which is then rendered on the webpage.
    """
    app.logger.debug("Entering blocktimes")

    # Get ids of selected calendars from form
    calids = request.form.getlist('calid')

    credentials = valid_credentials()
    service = get_gcal_service(credentials)
    app.logger.debug("Returned from get_gcal_service")

    # Agenda of all the events that occur within the specified date range
    busy_times = Agenda()

    # For each calendar, get the events within the specified range
    for cal in calids:
        app.logger.debug("Getting free times between "+flask.session['begin_date']+" and "+flask.session['end_date'])
        events = service.events().list(calendarId=cal,singleEvents=True,timeMin=flask.session['begin_date'],timeMax=flask.session['end_date']).execute()

        for event in events['items']:
            busy = Appt(arrow.get(event['start']['dateTime']).date(),arrow.get(event['start']['dateTime']).time(),arrow.get(event['end']['dateTime']).time(),"busy")
            busy_times.append(busy)

    begin_date = arrow.get(flask.session['begin_date'])
    end_date = arrow.get(flask.session['end_date'])

    begin_time = arrow.get(flask.session['begin_time']).time()
    end_time = arrow.get(flask.session['end_time']).time()


    # Calling normalize makes sure that overlapping events are merged
    busy_times.normalize()

    # Make a new Agenda with all the free times
    meetings = busy_times.freeblocks(begin_date, end_date, begin_time, end_time)

    # Merge overlapping free times (if they exist) as a precaution
    meetings.normalize()

    return flask.render_template('index.html',meetings=meetings)