1
|
|
|
# coding: utf-8 |
2
|
|
|
# Trello API |
3
|
|
|
from trello import TrelloClient, ResourceUnavailable |
4
|
|
|
|
5
|
|
|
# django classes |
6
|
|
|
from django.conf import settings |
7
|
|
|
from django.utils.translation import ugettext as _ |
8
|
|
|
from django.utils.log import getLogger |
9
|
|
|
from django.core.cache import caches |
10
|
|
|
|
11
|
|
|
# django_th classes |
12
|
|
|
from django_th.apps import DjangoThConfig |
13
|
|
|
from django_th.services.services import ServicesMgr |
14
|
|
|
from django_th.models import update_result, UserService |
15
|
|
|
|
16
|
|
|
""" |
17
|
|
|
handle process with Trello |
18
|
|
|
put the following in settings.py |
19
|
|
|
|
20
|
|
|
TH_TRELLO = { |
21
|
|
|
'consumer_key': 'abcdefghijklmnopqrstuvwxyz', |
22
|
|
|
'consumer_secret': 'abcdefghijklmnopqrstuvwxyz', |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
TH_SERVICES = ( |
26
|
|
|
... |
27
|
|
|
'th_trello.my_trello.ServiceTrello', |
28
|
|
|
... |
29
|
|
|
) |
30
|
|
|
|
31
|
|
|
""" |
32
|
|
|
|
33
|
|
|
logger = getLogger('django_th.trigger_happy') |
34
|
|
|
|
35
|
|
|
cache = caches['th_trello'] |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
class ServiceTrello(ServicesMgr): |
39
|
|
|
""" |
40
|
|
|
Serivce Trello |
41
|
|
|
""" |
42
|
|
|
# Boards own Lists own Cards |
43
|
|
|
|
44
|
|
|
def __init__(self, token=None, **kwargs): |
45
|
|
|
super(ServiceTrello, self).__init__(token, **kwargs) |
46
|
|
|
# app name |
47
|
|
|
self.app_name = DjangoThConfig.verbose_name |
48
|
|
|
# expiration |
49
|
|
|
self.expiry = "30days" |
50
|
|
|
# scope define the rights access |
51
|
|
|
self.scope = 'read,write' |
52
|
|
|
self.oauth = 'oauth1' |
53
|
|
|
self.service = 'ServiceTrello' |
54
|
|
|
|
55
|
|
|
base = 'https://www.trello.com' |
56
|
|
|
self.AUTH_URL = '{}/1/OAuthAuthorizeToken'.format(base) |
57
|
|
|
self.REQ_TOKEN = '{}/1/OAuthGetRequestToken'.format(base) |
58
|
|
|
self.ACC_TOKEN = '{}/1/OAuthGetAccessToken'.format(base) |
59
|
|
|
self.consumer_key = settings.TH_TRELLO['consumer_key'] |
60
|
|
|
self.consumer_secret = settings.TH_TRELLO['consumer_secret'] |
61
|
|
|
if token: |
62
|
|
|
token_key, token_secret = token.split('#TH#') |
63
|
|
|
try: |
64
|
|
|
self.trello_instance = TrelloClient(self.consumer_key, |
65
|
|
|
self.consumer_secret, |
66
|
|
|
token_key, |
67
|
|
|
token_secret) |
68
|
|
|
except ResourceUnavailable as e: |
69
|
|
|
us = UserService.objects.get(token=token) |
70
|
|
|
logger.error(e.msg, e.error_code) |
71
|
|
|
update_result(us.trigger_id, msg=e.msg, status=False) |
72
|
|
|
|
73
|
|
|
def read_data(self, **kwargs): |
74
|
|
|
""" |
75
|
|
|
get the data from the service |
76
|
|
|
|
77
|
|
|
:param kwargs: contain keyword args : trigger_id at least |
78
|
|
|
:type kwargs: dict |
79
|
|
|
""" |
80
|
|
|
trigger_id = kwargs.get('trigger_id') |
81
|
|
|
data = list() |
82
|
|
|
cache.set('th_trello_' + str(trigger_id), data) |
83
|
|
|
return data |
84
|
|
|
|
85
|
|
|
def save_data(self, trigger_id, **data): |
86
|
|
|
""" |
87
|
|
|
let's save the data |
88
|
|
|
|
89
|
|
|
:param trigger_id: trigger ID from which to save data |
90
|
|
|
:param data: the data to check to be used and save |
91
|
|
|
:type trigger_id: int |
92
|
|
|
:type data: dict |
93
|
|
|
:return: the status of the save statement |
94
|
|
|
:rtype: boolean |
95
|
|
|
""" |
96
|
|
|
from th_trello.models import Trello |
97
|
|
|
|
98
|
|
|
data['output_format'] = 'md' |
99
|
|
|
title, content = super(ServiceTrello, self).save_data(trigger_id, |
100
|
|
|
**data) |
101
|
|
|
|
102
|
|
|
if len(title): |
103
|
|
|
# get the data of this trigger |
104
|
|
|
t = Trello.objects.get(trigger_id=trigger_id) |
105
|
|
|
# footer of the card |
106
|
|
|
footer = self.set_card_footer(data, t) |
107
|
|
|
content += footer |
108
|
|
|
|
109
|
|
|
# 1 - we need to search the list and board where we will |
110
|
|
|
# store the card so ... |
111
|
|
|
|
112
|
|
|
# 1.a search the board_id by its name |
113
|
|
|
# by retrieving all the boards |
114
|
|
|
boards = self.trello_instance.list_boards() |
115
|
|
|
|
116
|
|
|
board_id = '' |
117
|
|
|
my_list = '' |
118
|
|
|
for board in boards: |
119
|
|
|
if t.board_name == board.name: |
120
|
|
|
board_id = board.id |
121
|
|
|
break |
122
|
|
|
|
123
|
|
|
if board_id: |
124
|
|
|
# 1.b search the list_id by its name |
125
|
|
|
my_board = self.trello_instance.get_board(board_id) |
126
|
|
|
lists = my_board.open_lists() |
127
|
|
|
# just get the open list ; not all the archive ones |
128
|
|
|
for list_in_board in lists: |
129
|
|
|
# search the name of the list we set in the form |
130
|
|
|
if t.list_name == list_in_board.name: |
131
|
|
|
# return the (trello) list object |
132
|
|
|
# to be able to add card at step 3 |
133
|
|
|
my_list = my_board.get_list(list_in_board.id) |
134
|
|
|
break |
135
|
|
|
# we didnt find the list in that board |
136
|
|
|
# create it |
137
|
|
|
if my_list == '': |
138
|
|
|
my_list = my_board.add_list(t.list_name) |
139
|
|
|
|
140
|
|
|
else: |
141
|
|
|
# 2 if board_id and/or list_id does not exist, create it/them |
142
|
|
|
my_board = self.trello_instance.add_board(t.board_name) |
143
|
|
|
# add the list that didnt exists and |
144
|
|
|
# return a (trello) list object |
145
|
|
|
my_list = my_board.add_list(t.list_name) |
146
|
|
|
|
147
|
|
|
# 3 create the card |
148
|
|
|
# create the Trello card |
149
|
|
|
my_list.add_card(title, content) |
150
|
|
|
|
151
|
|
|
sentence = str('trello {} created').format(data['link']) |
152
|
|
|
logger.debug(sentence) |
153
|
|
|
status = True |
154
|
|
|
else: |
155
|
|
|
sentence = "no token or link provided for trigger ID " \ |
156
|
|
|
"{}".format(trigger_id) |
157
|
|
|
update_result(trigger_id, msg=sentence, status=False) |
158
|
|
|
status = False |
159
|
|
|
|
160
|
|
|
return status |
161
|
|
|
|
162
|
|
|
@staticmethod |
163
|
|
|
def set_card_footer(data, trigger): |
164
|
|
|
""" |
165
|
|
|
handle the footer of the note |
166
|
|
|
""" |
167
|
|
|
footer = '' |
168
|
|
|
if data.get('link'): |
169
|
|
|
provided_by = _('Provided by') |
170
|
|
|
provided_from = _('from') |
171
|
|
|
footer_from = "<br/><br/>{} <em>{}</em> {} <a href='{}'>{}</a>" |
172
|
|
|
|
173
|
|
|
description = trigger.trigger.description |
174
|
|
|
footer = footer_from.format( |
175
|
|
|
provided_by, description, provided_from, |
176
|
|
|
data.get('link'), data.get('link')) |
177
|
|
|
|
178
|
|
|
import pypandoc |
179
|
|
|
footer = pypandoc.convert(footer, 'md', format='html') |
180
|
|
|
|
181
|
|
|
return footer |
182
|
|
|
|
183
|
|
|
def auth(self, request): |
184
|
|
|
""" |
185
|
|
|
let's auth the user to the Service |
186
|
|
|
:param request: request object |
187
|
|
|
:return: callback url |
188
|
|
|
:rtype: string that contains the url to redirect after auth |
189
|
|
|
""" |
190
|
|
|
request_token = super(ServiceTrello, self).auth(request) |
191
|
|
|
callback_url = self.callback_url(request) |
192
|
|
|
|
193
|
|
|
# URL to redirect user to, to authorize your app |
194
|
|
|
auth_url_str = '{auth_url}?oauth_token={token}' |
195
|
|
|
auth_url_str += '&scope={scope}&name={name}' |
196
|
|
|
auth_url_str += '&expiration={expiry}&oauth_callback={callback_url}' |
197
|
|
|
auth_url = auth_url_str.format(auth_url=self.AUTH_URL, |
198
|
|
|
token=request_token['oauth_token'], |
199
|
|
|
scope=self.scope, |
200
|
|
|
name=self.app_name, |
201
|
|
|
expiry=self.expiry, |
202
|
|
|
callback_url=callback_url) |
203
|
|
|
|
204
|
|
|
return auth_url |
205
|
|
|
|
206
|
|
|
def callback(self, request, **kwargs): |
207
|
|
|
""" |
208
|
|
|
Called from the Service when the user accept to activate it |
209
|
|
|
:param request: request object |
210
|
|
|
:return: callback url |
211
|
|
|
:rtype: string , path to the template |
212
|
|
|
""" |
213
|
|
|
return super(ServiceTrello, self).callback(request, **kwargs) |
214
|
|
|
|