-
Notifications
You must be signed in to change notification settings - Fork 6
/
createTask.py
135 lines (115 loc) · 4.94 KB
/
createTask.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2020 Michael Schmidt-Korth
#
# GNU GPL v2.0 Licence. See https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
from __future__ import unicode_literals
import sys
# import argparse
import os
import json
import datetime
from main import DEBUG, formatNotificationText
from config import confNames, getConfigValue
from workflow import Workflow, Workflow3, web
from workflow.notify import notify
def main(wf):
if len(wf.args):
query = wf.args[0]
else:
query = None
taskParameters = json.loads(query)
getCurrentUser()
createTask(taskParameters['inputName'], taskParameters['inputContent'], taskParameters['inputDue'], taskParameters['inputPriority'], taskParameters['inputTags'], taskParameters['inputList'])
def getCurrentUser():
'''If not yet stored, retrieves the current user Id to assign tasks to by default (so they appear in Inbox).'''
if DEBUG > 0:
log.debug('[ Calling API to create task ]')
if not getConfigValue(confNames['confUser']):
url = 'https://api.clickup.com/api/v2/user'
params = None
headers = {}
headers['Authorization'] = getConfigValue(confNames['confApi'])
headers['Content-Type'] = 'application/json'
if DEBUG > 1:
log.debug(url)
log.debug(headers)
try:
request = web.get(url, params = params, headers = headers)
request.raise_for_status()
except:
log.debug('Error on HTTP request')
wf3.add_item(title = 'Error connecting to ClickUp.', subtitle = 'Open configuration to check your parameters?', valid = True, arg = 'cu:config ', icon = 'error.png')
wf3.send_feedback()
exit()
result = request.json()
if DEBUG > 1:
log.debug('Response: ' + str(result))
wf.settings['userId'] = result['user']['id']
def createTask(inputName, inputContent, inputDue, inputPriority, inputTags, inputList):
'''Creates a Task by sending it to the ClickUp API.
----------
@param str inputName: The user's input for the task title.
@param str inputContent: The user's input for the task decsription.
@param str inputDue: The user's input for the task due date.
@param str inputPriority: The user's input for the task priority.
@param str inputTags: The user's input for the task tags.
@param str inputList: The user's input for the task list.
'''
if DEBUG > 0:
log.debug('[ Calling API to create task ]')
if not inputList:
inputListId = getConfigValue(confNames['confList'])
else:
# Get value of first key in dictionary {Name, Id} by converting to List. The dict will always contain a single list name+Id the user specified.
inputListId = next(iter(inputList.items()))[1] # Get value for first key of dict
if inputDue != 'None':
if len(inputDue) == 26: # 2020-01-01T12:00:00.000000
inputDue = datetime.datetime.strptime(str(inputDue)[:len(inputDue) - 10], '%Y-%m-%d %H:%M') # Convert String to datetime. Remove seconds.milliseconds (e.g. :26.614286) from string
else: # 2020-01-01T12:00:00
inputDue = datetime.datetime.strptime(str(inputDue)[:len(inputDue)], '%Y-%m-%d %H:%M:%S')
inputDueMs = (inputDue - datetime.datetime.fromtimestamp(0)).total_seconds() * 1000.0 # Convert datetime into ms. Use fromtimestamp() to get local timezone instead of utcfromtimestamp()
url = 'https://api.clickup.com/api/v2/list/' + inputListId + '/task'
params = None
headers = {}
headers['Authorization'] = getConfigValue(confNames['confApi'])
headers['Content-Type'] = 'application/json'
data = {}
data['name'] = inputName
data['content'] = inputContent
if inputDue != 'None':
data['due_date'] = int(inputDueMs)
data['due_date_time'] = True # Translated into true
data['priority'] = inputPriority if inputPriority is not None else None # Translated into 'null'
data['tags'] = inputTags
if getConfigValue(confNames['confUser']): # Default assignee = current user
data['assignees'] = [getConfigValue(confNames['confUser'])]
if DEBUG > 1:
log.debug(url)
log.debug(headers)
log.debug(data)
try:
import json
request = web.post(url, params = params, data = json.dumps(data), headers = headers)
request.raise_for_status()
except:
log.debug('Error on HTTP request')
wf.add_item(title = 'Error connecting to ClickUp.', subtitle = 'Open configuration to check your parameters?', valid = True, arg = 'cu:config ', icon = 'error.png')
wf.send_feedback()
exit()
result = request.json()
if DEBUG > 1:
log.debug('Response: ' + str(result))
# If user pressed 'opt' (optInput == true), we do not want to show a notification, as the task is opened in the browser
hasUserNotPressedOpt = 'optInput' not in os.environ or os.environ['optInput'] == 'false'
if getConfigValue(confNames['confNotification']) == 'true' and (hasUserNotPressedOpt):
notify('Created: ' + inputName, formatNotificationText(inputContent, inputDue, inputTags, inputPriority, inputList, True))
elif os.environ['optInput'] and os.environ['optInput'] == 'true':
print(result['url'])
if __name__ == "__main__":
wf = Workflow()
wf3 = Workflow3()
log = wf.logger
sys.exit(wf.run(main))