Python Create New Task and Insert Onto List














































Python Create New Task and Insert Onto List




Python Create New Task and Insert Onto List


Previous Article: javascript:nicTemp();

Prerequisites for Getting started:

  1. Python 2.6 or greater

  2. Pip package management tool

  3. A Google account with Google Tasks enabled

 

For details about connecting to the Google Tasks API and storing serialized credentials, check out the javascript:nicTemp(); article in this series.


To insert a new task in an already existing task list, we will use the method insert()  provided by the Tasks API. This function requires two arguments, tasklist, a task list identifier and body, the request body with details of the new task. The function also takes two optional parameters: parent, a Parent task identifier and previous, the previous sibling task identifier. This object takes the form of:



status

string 

Status of the task. This is either "needsAction" or "completed"

kind

string

Type of the resource. This is always "tasks#task"

updated

string

Last modification time of the task (as a RFC 3339 timestamp)

parent

string 

Parent task identifier. This field is omitted if it is a top-level task. This field is read-only. (Use the "move" method to move the task under a different parent or to the top level).

title

string

Title of the task

deleted

boolean

Flag indicator whether the task has been deleted. The default is False

completed

string

Completion date of the task (as a RFC 3339 timestamp). This field is omitted if the task has not been completed.

due

string

Due date of the task (as a  RFC 3339 timestamp). Optional.

etag

string

ETag of the resource.

notes

string

Notes describing the task

position

string

String indicating the position of the task among its sibling tasks under the same parent task or at the top level. If this string is greater than another task's corresponding position string according to lexicographical ordering, the task is positioned after the other task under the same parent task (or at the top level). This field is read-only. Use the "move" method to move the task to another position.

hidden 

boolean

Flag indicating whether the task is hidden. This is the case if the task had been marked completed when the task list was last cleared. The default is False. This field is read-only.

id

string

Task identifier

selfLink

string

URL pointing to this task. Used to retrieve, update, or delete this task.



This function returns a response object of the same form.


Now, using this method we will insert a new task into the specified tasklist and print the returned task ID.


task = {
            'title': 'First Task',
            'notes': 'Please complete me.',
            'due': '2010-10-15T12:00:00.000Z'
        }

result = service.tasks().insert(tasklist='@default', body=task).execute()
print(result['id'])


Using %u2018@default%u2019 as tasklist identifier inserts the new task into the default tasklist for your Google account.



For more information about the RFC 3339 timestamp format, refer:javascript:nicTemp();



Source Code:


import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

class GoogleTaskPython:

    def __init__(self):
        self.SCOPES = ['https://www.googleapis.com/auth/tasks']
   
    def add_task(self):
        creds = None

        if os.path.exists('token.pickle'):
            with open('token.pickle', 'rb') as token:
                creds = pickle.load(token)
       
        if not creds or not creds.valid:
            if creds and creds.expired and creds.refresh_token:
                creds.refresh(Request())
            else:
                flow = InstalledAppFlow.from_client_secrets_file('credentials.json', self.SCOPES)
                creds = flow.run_local_server(port=0)
           
            with open('token.pickle', 'wb') as token:
                pickle.dump(creds, token)
           
        service = build('tasks', 'v1', credentials=creds)

        task = {
            'title': 'First Task',
            'notes': 'Please complete me.',
            'due': '2010-10-15T12:00:00.000Z'
        }

        result = service.tasks().insert(tasklist='@default', body=task).execute()
        print(result['id'])
       

def main():
    obj = GoogleTaskPython()
    obj.add_task()


if __name__=='__main__':
    main()


Comments