Search code examples
pythonpython-3.xpypiasanaasana-api

Exporting Asana tasks to CSV via PyPi's Asana API module


I need to export all Asana tasks within specific projects of my workspace to a csv file. I have been doing this manually via Asana's advanced search but the Asana generated csv file only supports < 2000 entries which no longer suits my needs. The following code (which I found online and slightly adapted) almost does what I want except I need an additional ‘Tags’ column (just like the Asana generated csv file has) displaying a comma-separated list of each task’s tags. I need the names of the tags not the tag IDs. I have basic knowledge of Python but I don't have experience of APIs so this really is beyond my capabilities. I would be extremely grateful if someone could help. Here is the relevant bit of the code:

import sys
import csv
import asana

def process_project_tasks(client, project, ws_dict):
    """Add each task for the current project to the records list."""
    task_list = []
    while True:
        tasks = client.tasks.find_by_project(project['id'] 
{"opt_fields":"name, projects, workspace, id, due_on, created_at, 
modified_at, completed, completed_at, assignee, assignee_status, parent, 
notes"})

        for task in tasks:
            ws_name = ws_dict[task['workspace']['id']]
            assignee = task['assignee']['id'] if task['assignee'] is not 
None else ''
            created_at = task['created_at'][0:10] + ' ' + 
task['created_at'][11:16] if \
                    task['created_at'] is not None else None
            modified_at = task['modified_at'][0:10] + ' ' + 
task['modified_at'][11:16] if \
                    task['modified_at'] is not None else None
            completed_at = task['completed_at'][0:10] + ' ' + 
task['completed_at'][11:16] if \
                task['completed_at'] is not None else None
            rec = [task['name'], project['name'], ws_name,task['due_on'], 
created_at, \
                modified_at, task['completed'], completed_at, assignee, \
                task['assignee_status'], task['parent'], task['notes'], 
task['id']]
            rec = ['' if s is None else s for s in rec]
            task_list.append(rec)
        if 'next_page' not in tasks:
            break
    return task_list

Solution

  • I worked out how to add the tags. You just need to add 'tags' as one of the 'opt_fields'. If you just do that it is not possible to get the names of the tags so you need to change 'opt_fields' to 'opt_expand' then created a comma-separated list of each task's tag name.

    def process_project_tasks(client, project, ws_dict):
        """Add each task for the current project to the records list."""
    
        while True:
            tasks = client.tasks.find_by_project(project['gid'], {"opt_expand":"name, \
                projects, workspace, gid, due_on, created_at, modified_at, completed, \
                completed_at, assignee, assignee_status, parent, notes, tags"})
    
            for task in tasks:
                ws_name = ws_dict[task['workspace']['gid']]
    
                #get a comma-separated list of the names of each tag
                tags = task['tags']
                if tags is not None:
                    tagname=''
                    i=0
                    for tag in tags:
                        if i==0:
                            tagname = tag['name']
                        else:
                            tagname = tagname + ', ' + tag['name']
                        i=i+1
                assignee = task['assignee']['gid'] if task['assignee'] is not None else 
    ''
                created_at = task['created_at'][0:10] + ' ' + task['created_at'][11:16] 
    if \
                        task['created_at'] is not None else None
                modified_at = task['modified_at'][0:10] + ' ' + task['modified_at'] 
       [11:16] if \
                        task['modified_at'] is not None else None
                completed_at = task['completed_at'][0:10] + ' ' + task['completed_at'] 
       [11:16] if \
                    task['completed_at'] is not None else None
                rec = [task['name'], project['name'], ws_name, task['due_on'], 
    created_at, \
                    modified_at, task['completed'], completed_at, assignee, \
                    task['assignee_status'], task['parent'], task['notes'], task['gid'], 
    tags]
                rec = ['' if s is None else s for s in rec]
                task_list.append(rec)
            if 'next_page' not in tasks:
                break
       return task_list