Example #1
0
def _get_events(cal_id, time_min, time_max):
    """
    Retrieve events from the Google API

    @param cal_id: str, calendar id
    @param time_min: datetime, from time
    @param time_max: datetime, to time
    @return: list(events)
    """
    params = dict(
        calendarId=cal_id,
        singleEvents=True,
        timeMin=time_min.isoformat('T') + 'Z',
        timeMax=time_max.isoformat('T') + 'Z',
        )

    return get_all_items(service.events(), params, decorator.http())
Example #2
0
    def get(self):
        logging.info('Analyzing for: %s', users.get_current_user().nickname())

        try:
            weeks = int(self.request.get('weeks'))
        except ValueError:
            weeks = DEFAULT_WEEKS

        time_min = datetime.utcnow() - timedelta(weeks=weeks)
        time_max = datetime.utcnow()

        cal_id = self.request.get('cal', default_value=DEFAULT_CAL_ID)
        # TODO: The get_events and get_cal_name calls should be concurrent
        try:
            events = _get_events(cal_id, time_min, time_max)
        except AccessTokenRefreshError:
            return self.redirect('/')
        cal_name = get_cal_name(cal_id, service, decorator.http())

        stats = _generate_stats(time_min, time_max, events)

        params = {
            'cal_id': cal_id,
            'time_min': time_min,
            'time_max': time_max,
            'weeks': weeks,
            'work_day_start': WORK_DAY_START,
            'work_day_end': WORK_DAY_END,
            }
        data = {
            'events': events,
            'params': params,
            'stats': stats,
            'stats_json': json.dumps(stats),
            'cal_name': cal_name,
            'pformat': pformat,
            'title': cal_name,
            'page': 'analyze',
            'js': 'analyze',
            'css': 'analyze',
            }
        template = template_engine.get_template('analyze.html')
        self.response.out.write(template.render(data))
Example #3
0
    def get(self):
        logging.info('Choosing calendar for: %s', users.get_current_user().nickname())

        has_credentials = decorator.has_credentials(),

        if has_credentials:
            try:
                items = _get_calendar_list()
                primary = get_cal_name('primary', service, decorator.http())
            except AccessTokenRefreshError:
                return self.redirect('/choose_cal')

        data = {
            'title': 'Choose Calendar',
            'page': 'analyze',
            'has_credentials': has_credentials,
            'auth_url': decorator.authorize_url(),
            'revoke_url': 'https://www.google.com/accounts/b/0/IssuedAuthSubTokens',
            'primary': primary,
            'items': items,
            }
        template = template_engine.get_template('choose_cal.html')
        self.response.out.write(template.render(data))
Example #4
0
def _get_calendar_list():
    params = dict()
    return get_all_items(service.calendarList(), params, decorator.http())