Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a new project with tasks in Asana using Python?

I see some questions that have been asked of how to create tasks using an API, but I want to know how to create a new project.

I have predetermined format of how I want to create a new project every time that I want to create a new one. There are specific tasks that I have and each one has its own subtasks. Right now I have a template on Asana and every time I want to create a new project I go to that project and make a copy of it and rename it to what my current project is.

Does anybody know of a way to automate this using Python, this way I can just run the script and put in the details of the name of the project and it will automatically create a new project on Asana (this way if I need to make 5 projects at once I can just make a list of all project names and loop through all of them)?

I know that you need to have a key and I have something which is called API_KEY and is 32 characters long.

ADD ON: Here is the code that I use in Python to access all of the tasks and subtasks in my workspace in Asana:

import asana
api_key = '################################' //my private key goes here
client = asana.Client.basic_auth(api_key)
me = client.users.me()
all_projects = next(workspace for workspace in me['workspaces'])
projects = client.projects.find_by_workspace(all_projects['id'], iterator_type=None)

for project in projects:
    if 'Template' not in project['name']:
        continue
    print(project['name'])
    project_id = project['id']
    tasks = client.tasks.find_by_project(project_id, iterator_type=None)

    for task in tasks:
        print("    " + task['name'])
        task_id = task['id']
        task_subtasks = client.tasks.subtasks(task_id, full_payload=True)

        for subtask in task_subtasks:
            print("        " + subtask['name'])

When I run this I get all of my tasks and subtasks for the projects that have the word 'template' in their titles. So, this is how to read, is there that if I save all of this in a JSON format, then every time I want to create a new project I can just upload that JSON and get a new project?

like image 329
Zachary Weixelbaum Avatar asked Mar 15 '23 20:03

Zachary Weixelbaum


1 Answers

It looks like you are using the python-asana client library. This library provide a nice wrapper that implements best practices for accessing the Asana API. I would encourage you to read the documentation to fully understand it's design and capabilities.

Creating a project requires the context of an workspace or organization and in the case of an organization additionally requires the context of a team for the project to belong to.

The following code uses the same library to create a project in the "Moon Landing" workspace, if the workspace is an organization it puts the project in the "Astronauts" team.

import asana

client = asana.Client.basic_auth('ASANA_API_KEY')

workspaces = client.workspaces.find_all({"opt_fields": "is_organization, name"})

workspace = next(workspace for workspace in workspaces if workspace['name'] == 'Moon Landing')

project = {'name':'Training','workspace': workspace['id']}

if workspace['is_organization'] :
    teams = client.teams.find_by_organization(workspace['id'])
    team = next(team for team in teams if team['name'] == 'Astronauts')
    project['team'] = team['id']

training = client.projects.create(project)

Once you have created the "Training" project, you can then add tasks to that project like this.

task = client.tasks.create_in_workspace(workspace['id'], {'projects': [training['id']], 'name': 'Learn to fly space craft'})

Then, adding subtasks

client.tasks.add_subtask(task['id'], {'name': 'Turning it on'})
like image 66
Andrew Noonan Avatar answered Apr 05 '23 20:04

Andrew Noonan