예제 #1
0
  def _select_tasklist(self):
    # selects tasklist, assigns to TASKLIST_NAME
    userid, creds = util.load_session_credentials(self)
    tasks_service = util.create_service('tasks', 'v1', creds)

    tasklist_id = self.request.get('select')
    logging.info("select")
    logging.info(self.request.get('select'))

    if tasklist_id == '':
      return "Please select a tasklist before trying to add it."
    else:
      #set name/id to db
      my_tasklist = TasklistStore(owner=self.userid)
      my_tasklist.my_id = tasklist_id
      #TASKLIST_NAMES.append(tasklist_title)

      tasklists = tasks_service.tasklists().list().execute()
      for tasklist in tasklists['items']:
        if tasklist_id == tasklist['id']:
          my_tasklist.my_name = tasklist['title']
            #TASKLIST_IDS[tasklist_title] = tasklist['id']

      my_tasklist.put()

      return my_tasklist.my_name + " selected successfully"
예제 #2
0
  def get(self):
    fitbit_oauth = util.create_fitbit_oauth_service()
    
    request_token, request_token_secret = fitbit_oauth.get_request_token(header_auth=True)

    #store token and secret in DB
    userid = util.load_session_credentials(self)[0]

    token_info = OAuthRequestToken(key_name=userid)
    token_info.request_token=request_token
    token_info.request_token_secret=request_token_secret
    token_info.put()

    authorize_url = fitbit_oauth.get_authorize_url(request_token)

    # Perform the redirect.
    self.redirect(str(authorize_url))
예제 #3
0
  def _render_template(self, message=None):
    """Render the main page template."""

    userid, creds = util.load_session_credentials(self)

    template_values = {'userId': self.userid}

    if message:
      template_values['message'] = message
    
    # self.mirror_service is initialized in util.auth_required
    subscriptions = self.mirror_service.subscriptions().list().execute()
    for subscription in subscriptions.get('items', []):
      collection = subscription.get('collection')
      if collection == 'timeline':
        template_values['timelineSubscriptionExists'] = True
    
    template = jinja_environment.get_template('templates/index.html')
    self.response.out.write(template.render(template_values))
예제 #4
0
  def _new_calendar(self):
    userid, creds = util.load_session_credentials(self)
    calendar_service = util.create_service("calendar", "v3", creds)
    mirror_service = util.create_service('mirror', 'v1', creds)
    
    calendar_list = calendar_service.calendarList().list().execute()
    for calendar in calendar_list['items']:
      if 'primary' in calendar:
        if calendar['primary']:
          calendar_id = calendar['id'] # grab only primary calendar      
          calendar_title = calendar['summary']
    
    #get events, only some of them
    bundle_html, event_htmls = get_html_from_calendar(calendar_service, calendar_id, calendar_title)

    body = {
            'notification': {'level': 'DEFAULT'},
            'title': calendar_title,  #stash calendar title for notify
            'text': calendar_id,      #stash calendar id for notify
            'html': bundle_html,
            'htmlPages': event_htmls,  #array
            'isBundleCover': True,
            'menuItems': [
                {
                    'action': 'CUSTOM',
                    'id': 'refresh',
                    'values': [{
                        'displayName': 'Refresh',
                        'iconUrl': util.get_full_url(self, '/static/images/refresh3.png')}]
                },
                {'action': 'TOGGLE_PINNED'},
                {'action': 'DELETE'}
            ]
        }

    try:
        result = mirror_service.timeline().insert(body=body).execute()
        if result:
          item_id = result['id']
          deferred.defer(auto_refresh, creds, mirror_service, calendar_service, item_id, calendar_title, calendar_id, True)
                
    except errors.HttpError, error:
        logging.info ('an error has occured %s ', error)
예제 #5
0
  def _render_template(self, message=None):
    """Render the main page template."""

    userid, creds = util.load_session_credentials(self)
    tasks_service = util.create_service('tasks', 'v1', creds)

    template_values = {'userId': self.userid}

    if message:
      template_values['message'] = message
    
    # self.mirror_service is initialized in util.auth_required
    subscriptions = self.mirror_service.subscriptions().list().execute()
    for subscription in subscriptions.get('items', []):
      collection = subscription.get('collection')
      if collection == 'timeline':
        template_values['timelineSubscriptionExists'] = True
    
    # pull from tasks api, list of tasklists
    tasklists = tasks_service.tasklists().list().execute()
    template_values['tasklists'] = tasklists['items']

    #load the tasklist names and ids from db if exists
    #q = db.GqlQuery("SELECT * FROM TasklistStore " + 
    #                "WHERE owner = " + userid)
    q = TasklistStore.all()
    q.filter("owner = ",self.userid)
    TASKLIST_NAMES = []
    for p in q.run():
      TASKLIST_NAMES.append(p.my_name)
    if TASKLIST_NAMES == []:
      TASKLIST_NAMES.append("None")

    template_values['synced'] = TASKLIST_NAMES

    template = jinja_environment.get_template('templates/index.html')
    self.response.out.write(template.render(template_values))
예제 #6
0
  def get(self):
    """Handle code exchange."""
    token_info = util.get_oauth_token(self)

    if not token_info:
      logging.error('No OAuth token found in the storage')
      # TODO: Display error.
      return None

    oauth_verifier = self.request.get('oauth_verifier')
    if not oauth_verifier:
      logging.error('No OAuth verifier in the URL')
      # TODO: Display error.
      return None

    oauth_token = self.request.get('oauth_token')
    if not oauth_token or token_info.request_token != oauth_token:
      logging.error('Invalid OAuth token %s %s', oauth_token, token_info.request_token)
      # TODO: Display error.
      return None

    # get acceess token and store it in the database
    token_info.verifier = oauth_verifier
    fitbit_oauth = util.create_fitbit_oauth_service()
    access_token, access_token_secret = fitbit_oauth.get_access_token(token_info.request_token,  
                                      token_info.request_token_secret,
                                      data={'oauth_verifier': token_info.verifier},
                                      header_auth=True)

    token_info.access_token  = access_token
    token_info.access_token_secret = access_token_secret
    token_info.put()

    userid = util.load_session_credentials(self)[0]
    self._perform_post_auth_tasks(userid, token_info)
    #TODO: should we clean up all old subscriptions and add new one here?
    self.redirect('/')
예제 #7
0
  def _new_tasklist(self):
    userid, creds = util.load_session_credentials(self)
    tasks_service = util.create_service('tasks', 'v1', creds)
    mirror_service = util.create_service('mirror', 'v1', creds)

    logging.info('Inserting timeline items')
    # Note that icons will not show up when making counters on a
    # locally hosted web interface.
    #mirror_service = util.create_service('mirror', 'v1', creds)
    #tasks_service = util.create_service('tasks', 'v1', creds)


    ############################  TASKS API STUFF #######
    # create empty task list @glass if none selected or none exist
    #q = db.GqlQuery("SELECT * FROM TasklistStore " + 
    #                "WHERE owner = " + userid)
    q = TasklistStore.all()
    q.filter("owner = ",self.userid)
    q.run()
    #if no tasklists, insert a default one
    if q:
        logging.info('not inserting')
    else:
        logging.info('no tasklist selected, inserting @glass ')
        tasklist = {
          'title': '@glass'
        }
        result = tasks_service.tasklists().insert(body=tasklist).execute()
        my_tasklist = TasklistStore(owner = userid, my_name = tasklist_title,
                                    my_id = result['id'])
        my_tasklist.put()
        
    ## now for each selected tasklist, post tasks to timeline
    for p in q:
      tasklist_id = p.my_id
      tasklist_name = p.my_name

      # insert seed tasks
      task = {
        'title': 'Glass interface synced to this list!',
        'notes': 'Try adding a new task with the voice command!'
      }
      result = tasks_service.tasks().insert(tasklist=tasklist_id, body=task).execute()
      
      # grab all the tasks in tasklist to display
      result = tasks_service.tasks().list(tasklist=tasklist_id).execute()

      #filter out completed tasks
      tasks = []
      for i, task in enumerate(result['items']):
        if task['status'] != 'completed':
          tasks.append(task)

      #grabbing all tasks now instead of just 5
      #indx = 5 if len(tasks) > 4 else len(tasks)
      #tasks = tasks[0:indx]

      if len(tasks) == 0:
        tasks.append({'title': 'No tasks!'})
 
      #render html
      # new_fields = {
      #     'list_title': tasklist_name,
      #     'tasks': tasks
      # }
      body = {
          'notification': {'level': 'DEFAULT'},
          'title': tasklist_id,     #secret way of stashing the tasklist id in the timeline item
          'html': get_html_from_tasks(tasks_service, tasklist_id, tasklist_name),
          'menuItems': [
              {
                  'action': 'REPLY',
                  'id': 'create_task',
                  'values': [{
                      'displayName': 'New Task',
                      'iconUrl': util.get_full_url(self, '/static/images/new_task.png')}]
              },
              {
                  'action': 'CUSTOM',
                  'id': 'refresh',
                  'values': [{
                      'displayName': 'Refresh',
                      'iconUrl': util.get_full_url(self, '/static/images/refresh2.png')}]
              },
              {'action': 'TOGGLE_PINNED'},
              {'action': 'DELETE'}
          ]
      }
      # custom_item_fields.set_multiple(body, new_fields, TIMELINE_ITEM_TEMPLATE_URL)


      # self.mirror_service is initialized in util.auth_required.
      # add card to timeline
      try:
        result = self.mirror_service.timeline().insert(body=body).execute()
        if result:
          item_id = result['id']
          # logging.info('mainhandler about to defer')
          # deferred.defer(auto_refresh, creds, mirror_service, tasks_service, item_id, tasklist_name, tasklist_id, True)
          # logging.info('mainhandler deferred')

      except errors.HttpError, error:
        logging.info ('an error has occured %s ', error)