forked from sarumont/py-trello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomfield.py
359 lines (306 loc) · 9.32 KB
/
customfield.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# -*- coding: utf-8 -*-
from __future__ import with_statement, print_function, absolute_import
import time
import sys
from trello import TrelloBase
from trello.compat import force_str
is_python3 = sys.version_info.major == 3
if is_python3:
unicode = str
class CustomFieldDefinition(TrelloBase):
"""
Class representing a Trello CustomFieldDefinition.
"""
def __init__(self, client, customFieldDefinition_id, name, field_type, list_options):
super(CustomFieldDefinition, self).__init__()
self.client = client
self.id = customFieldDefinition_id
self.name = name
self.field_type = field_type
self.list_options = list_options
@classmethod
def from_json(cls, board, json_obj):
"""
Deserialize the board's custom field json object to a CustomFieldDefinition object
:param board: the parent board the custom field is on
:param json_obj: the board's customField json object
"""
list_options = {}
if json_obj['type'] == 'list':
for option in json_obj['options']:
list_options[option['id']] = option['value']['text']
customFieldDefinition = CustomFieldDefinition(
board.client,
customFieldDefinition_id=json_obj['id'],
name=json_obj['name'],
field_type=json_obj['type'],
list_options=list_options,
)
return customFieldDefinition
@classmethod
def from_json_list(cls, board, json_objs):
return [cls.from_json(board, obj) for obj in json_objs]
def __repr__(self):
return force_str(u'<CustomFieldDefinition %s>' % (self.name,))
class CustomField(TrelloBase):
"""
Class representing a Trello CustomField.
"""
_type = ''
def __init__(self, card, customField_id, customFieldDefinition_id, value):
super(CustomField, self).__init__()
self.client = card.client
self.card = card
self.id = customField_id
self.definition_id = customFieldDefinition_id
self._value = value
@property
def type(self):
"""
Returns the type of the custom field.
:return: either 'list', 'checkbox', 'date', 'text' or 'number', str
"""
return self._type
@property
def name(self):
"""
Returns the user-readable name of the custom field by querying the
custom field definitions.
:return: the name of the custom field as str or None
"""
for definition in self.card.board.get_custom_field_definitions():
if definition.id == self.definition_id:
return definition.name
return None
@classmethod
def from_json(cls, card, json_obj):
"""
Deserialize the custom field json object to a CustomField object
:param card: the parent card the custom field is on
:param json_obj: the customField json object
"""
raise Exception('Not Implemented')
@classmethod
def get_class(cls, board, json_obj):
"""
:param board: the board the custom field is used on
:param json_obj: the customField json object
:return: the python class that coresponds to the given custom field data
"""
definition_id = json_obj['idCustomField']
for definition in board.get_custom_field_definitions():
if definition.id == definition_id:
return {
'checkbox': CustomFieldCheckbox,
'date': CustomFieldDate,
'list': CustomFieldList,
'number': CustomFieldNumber,
'text': CustomFieldText,
}[definition.field_type]
return None
@classmethod
def from_json_list(cls, card, json_objs):
return [cls.get_class(card.board, obj).from_json(card, obj) for obj in json_objs]
def __repr__(self):
return force_str(u'<CustomField%s %s=%r>' % (self.type.capitalize(), self.name, self.value))
@property
def value(self):
raise Exception('Not Implemented')
@value.setter
def value(self, value):
raise Exception('Not Implemented')
class CustomFieldText(CustomField):
"""
Class representing a Trello text custom field.
"""
_type = 'text'
@classmethod
def from_json(cls, card, json_obj):
"""
Deserialize the custom field json object to a CustomField object
:card: the parent card the custom field is on
:json_obj: the customField json object
"""
customField = cls(
card,
customField_id=json_obj['id'],
customFieldDefinition_id=json_obj['idCustomField'],
value=json_obj['value']['text'],
)
return customField
@property
def value(self):
"""
Returns the custom field value as unicode
:return: the custom field value as unicode
"""
return self._value
@value.setter
def value(self, value):
"""
Sets the new value,
:param value: the new value as unicode
"""
assert isinstance(value, unicode), "Given value is no unicode!"
self.client.fetch_json(
'/card/' + self.card.id + '/customField/' + self.definition_id + '/item',
http_method='PUT',
post_args={'value': { 'text': value, }, }, )
self._value = value
class CustomFieldCheckbox(CustomField):
_type = 'checkbox'
@classmethod
def from_json(cls, card, json_obj):
"""
Deserialize the custom field json object to a CustomField object
:card: the parent board the custom field is on
:json_obj: the customField json object
"""
customField = cls(
card,
customField_id=json_obj['id'],
customFieldDefinition_id=json_obj['idCustomField'],
value=(json_obj['value']['checked'] == u'true'),
)
return customField
@property
def value(self):
"""
Returns the custom field value as bool
:return: the custom field value as bool
"""
return self._value
@value.setter
def value(self, value):
"""
Sets the new value,
:param value: the new value as bool
"""
assert isinstance(value, bool), "Given value is no bool!"
self.client.fetch_json(
'/card/' + self.card.id + '/customField/' + self.definition_id + '/item',
http_method='PUT',
post_args={'value': { 'checked': u"true" if value else u"false", }, }, )
self._value = value
class CustomFieldDate(CustomField):
_type = 'date'
@classmethod
def from_json(cls, card, json_obj):
"""
Deserialize the custom field json object to a CustomField object
:card: the parent card the custom field is on
:json_obj: the customField json object
"""
customField = cls(
card,
customField_id=json_obj['id'],
customFieldDefinition_id=json_obj['idCustomField'],
value=json_obj['value']['date'],
)
return customField
@property
def value(self):
"""
Returns the custom field value as unicode in the format
%Y-%m-%dT%H:%M:%S.000Z.
:return: the custom field value as unicode
"""
return self._value
@value.setter
def value(self, value):
"""
Sets the new value,
:param value: the new value as unicode in format %Y-%m-%dT%H:%M:%S.000Z
"""
assert isinstance(value, str) or isinstance(value, unicode), "Given value is no str or unicode!"
time.strptime(value, '%Y-%m-%dT%H:%M:%S.000Z')
self.client.fetch_json(
'/card/' + self.card.id + '/customField/' + self.definition_id + '/item',
http_method='PUT',
post_args={'value': { 'date': value, }, }, )
self._value = value
class CustomFieldList(CustomField):
_type = 'list'
@classmethod
def from_json(cls, card, json_obj):
"""
Deserialize the custom field json object to a CustomField object
:card: the parent card the custom field is on
:json_obj: the customField json object
"""
customField = cls(
card,
customField_id=json_obj['id'],
customFieldDefinition_id=json_obj['idCustomField'],
value=json_obj['idValue'],
)
return customField
def _id2str(self, _id):
for definition in self.card.board.get_custom_field_definitions():
if definition.id == self.definition_id:
return definition.list_options.get(_id)
raise Exception('Definition not found')
def _str2id(self, text):
for definition in self.card.board.get_custom_field_definitions():
if definition.id == self.definition_id:
for key, val in definition.list_options.items():
if val == text:
return key
return None
raise Exception('Definition not found')
@property
def value(self):
"""
Returns the custom field value as unicode.
:return: the custom field value as unicode
"""
return self._id2str(self._value)
@value.setter
def value(self, value):
"""
Sets the new value, that must exist in custom field definition.
:param value: the new value as unicode
"""
assert isinstance(value, str) or isinstance(value, unicode), "Given value is no str or unicode!"
newvalue = self._str2id(value)
assert newvalue is not None, "Unknown value has been specified!"
self.client.fetch_json(
'/card/' + self.card.id + '/customField/' + self.definition_id + '/item',
http_method='PUT',
post_args={'idValue': newvalue,}, )
self._value = newvalue
class CustomFieldNumber(CustomField):
_type = 'number'
@classmethod
def from_json(cls, card, json_obj):
"""
Deserialize the custom field json object to a CustomField object
:card: the parent card the custom field is on
:json_obj: the customField json object
"""
customField = cls(
card,
customField_id=json_obj['id'],
customFieldDefinition_id=json_obj['idCustomField'],
value=float(json_obj['value']['number']),
)
return customField
@property
def value(self):
"""
Returns the custom field value as unicode.
:return: the custom field value as unicode
"""
return self._value
@value.setter
def value(self, value):
"""
Sets the new value,
:param value: the new value as unicode
"""
assert isinstance(value, int) or isinstance(value, float), "Given value is no int or float!"
self.client.fetch_json(
'/card/' + self.card.id + '/customField/' + self.definition_id + '/item',
http_method='PUT',
post_args={'value': { 'number': str(value), }, }, )
self._value = value