From 045d71f12f88ff657a4bb2c4db50f988ffbc6511 Mon Sep 17 00:00:00 2001 From: Alain Adler Date: Fri, 23 Jan 2015 01:19:03 +0200 Subject: [PATCH] Plugin to create tasks in Asana --- PluginDirectories/1/asana.bundle/examples.txt | 1 + PluginDirectories/1/asana.bundle/info.json | 11 +++ PluginDirectories/1/asana.bundle/options.json | 31 +++++++ PluginDirectories/1/asana.bundle/plugin.py | 87 +++++++++++++++++++ .../1/asana.bundle/preferences.json | 5 ++ 5 files changed, 135 insertions(+) create mode 100644 PluginDirectories/1/asana.bundle/examples.txt create mode 100644 PluginDirectories/1/asana.bundle/info.json create mode 100644 PluginDirectories/1/asana.bundle/options.json create mode 100644 PluginDirectories/1/asana.bundle/plugin.py create mode 100644 PluginDirectories/1/asana.bundle/preferences.json diff --git a/PluginDirectories/1/asana.bundle/examples.txt b/PluginDirectories/1/asana.bundle/examples.txt new file mode 100644 index 00000000..b4cc36fa --- /dev/null +++ b/PluginDirectories/1/asana.bundle/examples.txt @@ -0,0 +1 @@ +asana ~task(do the dishes) \ No newline at end of file diff --git a/PluginDirectories/1/asana.bundle/info.json b/PluginDirectories/1/asana.bundle/info.json new file mode 100644 index 00000000..a655aaca --- /dev/null +++ b/PluginDirectories/1/asana.bundle/info.json @@ -0,0 +1,11 @@ +{ + "categories" : [ + "Utilities" + ], + "displayName" : "Asana - Create a New Task", + "name" : "asana", + "description" : "Create a new task in Asana", + "examples" : [ + "asana do the dishes" + ] +} \ No newline at end of file diff --git a/PluginDirectories/1/asana.bundle/options.json b/PluginDirectories/1/asana.bundle/options.json new file mode 100644 index 00000000..738f3534 --- /dev/null +++ b/PluginDirectories/1/asana.bundle/options.json @@ -0,0 +1,31 @@ +{ + "options" : [ + { + "text": "Workspace:", + "type": "dropdown", + "key": "workspace", + "options": [ + {"text": "Personal Projects", "value": "personal"}, + {"text": "Organization", "value": "organization"} + ] + }, + { + "type":"hint", + "text":"If you chose 'Organization', enter its name" + }, + { + "text": "Organization Name", + "type": "text", + "key": "orgainzation_name" + }, + { + "type":"hint", + "text":"Your API key is in Asana => Account Settings => Apps" + }, + { + "text":"API Key", + "type":"text", + "key":"api_key" + } + ] +} \ No newline at end of file diff --git a/PluginDirectories/1/asana.bundle/plugin.py b/PluginDirectories/1/asana.bundle/plugin.py new file mode 100644 index 00000000..051d8413 --- /dev/null +++ b/PluginDirectories/1/asana.bundle/plugin.py @@ -0,0 +1,87 @@ +import json, urllib2, base64 + +def results(fields, original_query): + task = fields['~task'] + settings = json.load(open("preferences.json")) + api_key = settings.get('api_key') + if api_key is None or api_key == '': + return { + "title": "asana '{0}'".format(task), + "run_args": ['NO_CREDENTIALS', ''], + "html": "

Enter your Asana API key in the plugin settings

" + } + else: + return { + "title": "asana '{0}'".format(task), + "run_args": [task, api_key], + "html": "

Create task '{0}'

".format(task) + } + +def run(task, api_key): + if task == 'NO_CREDENTIALS': + return + workspaces = get_all_workspaces(api_key) + if workspaces is None: + post_notification('Please check your Asana API key') + return + chosen_space = get_chosen_workspace() + space_id = get_workspace_id(chosen_space, workspaces) + if space_id is None: + post_notification('Failed to find your Asana workspace') + return + user_id = get_user_id(api_key) + create_task(user_id, space_id, task, api_key) + post_notification('Asana task has been created!') + +def get_workspace_id(chosen_space, workspaces): + space_id = None + for x in xrange(0,len(workspaces)): + if workspaces[x].get('name') == chosen_space: + space_id = workspaces[x].get('id') + break + return space_id + +def get_all_workspaces(api_key): + request = urllib2.Request("https://app.asana.com/api/1.0/workspaces") + base64string = base64.encodestring('%s:%s' % (api_key, '')).replace('\n', '') + request.add_header("Authorization", "Basic %s" % base64string) + try: + result = urllib2.urlopen(request) + workspaces = json.load(result).get('data') + except: + workspaces = None + return workspaces + +def create_task(user_id, space_id, task, api_key): + task_data = {"data" : {"workspace" : space_id, "name" : task, "assignee": {"id": user_id}}} + request = urllib2.Request(url="https://app.asana.com/api/1.0/tasks", data=json.dumps(task_data)) + base64string = base64.encodestring('%s:%s' % (api_key, '')).replace('\n', '') + request.add_header("Authorization", "Basic %s" % base64string) + request.add_header('Content-Type', 'application/json') + result = urllib2.urlopen(request) + +def get_user_id(api_key): + request = urllib2.Request("https://app.asana.com/api/1.0/users/me") + base64string = base64.encodestring('%s:%s' % (api_key, '')).replace('\n', '') + request.add_header("Authorization", "Basic %s" % base64string) + result = urllib2.urlopen(request) + user_id = json.load(result).get('data').get('id') + return user_id + +def get_chosen_workspace(): + settings = json.load(open("preferences.json")) + workspace = settings.get('workspace') + chosen_space = 'Personal Projects' + if workspace != 'personal': + chosen_space = settings.get('orgainzation_name') + if chosen_space is None or chosen_space == '': + chosen_space = 'Personal Projects' + return chosen_space + +def post_notification(message, title="Flashlight"): + import os, json, pipes + # do string escaping: + message = json.dumps(message) + title = json.dumps(title) + script = 'display notification {0} with title {1}'.format(message, title) + os.system("osascript -e {0}".format(pipes.quote(script))) diff --git a/PluginDirectories/1/asana.bundle/preferences.json b/PluginDirectories/1/asana.bundle/preferences.json new file mode 100644 index 00000000..43735f0b --- /dev/null +++ b/PluginDirectories/1/asana.bundle/preferences.json @@ -0,0 +1,5 @@ +{ + "workspace" : "personal", + "orgainzation_name": "", + "api_key":"" +} \ No newline at end of file