1+ from datetime import datetime
2+
13from nylas .client .restful_model_collection import RestfulModelCollection
24from nylas .client .errors import FileUploadError
5+ from nylas .utils import timestamp_from_dt
36from six import StringIO
47
58# pylint: disable=attribute-defined-outside-init
69
710
811class NylasAPIObject (dict ):
912 attrs = []
13+ datetime_attrs = {}
14+ datetime_filter_attrs = {}
1015 # The Nylas API holds most objects for an account directly under '/',
1116 # but some of them are under '/a' (mostly the account-management
1217 # and billing code). api_root is a tiny metaprogramming hack to let
@@ -44,6 +49,9 @@ def create(cls, api, **kwargs):
4449 attr = attr_name [1 :]
4550 if attr in kwargs :
4651 obj [attr_name ] = kwargs [attr ]
52+ for dt_attr , ts_attr in cls .datetime_attrs .items ():
53+ if obj .get (ts_attr ):
54+ obj [dt_attr ] = datetime .utcfromtimestamp (obj [ts_attr ])
4755 if 'id' not in kwargs :
4856 obj ['id' ] = None
4957
@@ -54,6 +62,9 @@ def as_json(self):
5462 for attr in self .cls .attrs :
5563 if hasattr (self , attr ):
5664 dct [attr ] = getattr (self , attr )
65+ for dt_attr , ts_attr in self .cls .datetime_attrs .items ():
66+ if self .get (dt_attr ):
67+ dct [ts_attr ] = timestamp_from_dt (self [dt_attr ])
5768 return dct
5869
5970 def child_collection (self , cls , ** filters ):
@@ -83,6 +94,13 @@ class Message(NylasAPIObject):
8394 "account_id" , "object" , "snippet" , "starred" , "subject" ,
8495 "thread_id" , "to" , "unread" , "starred" , "_folder" , "_labels" ,
8596 "headers" ]
97+ datetime_attrs = {
98+ "received_at" : "date" ,
99+ }
100+ datetime_filter_attrs = {
101+ "received_before" : "received_before" ,
102+ "received_after" : "received_after" ,
103+ }
86104 collection_name = 'messages'
87105
88106 def __init__ (self , api ):
@@ -206,8 +224,21 @@ class Thread(NylasAPIObject):
206224 attrs = ["draft_ids" , "id" , "message_ids" , "account_id" , "object" ,
207225 "participants" , "snippet" , "subject" , "subject_date" ,
208226 "last_message_timestamp" , "first_message_timestamp" ,
227+ "last_message_received_timestamp" , "last_message_sent_timestamp" ,
209228 "unread" , "starred" , "version" , "_folders" , "_labels" ,
210229 "received_recent_date" ]
230+ datetime_attrs = {
231+ "first_message_at" : "first_message_timestamp" ,
232+ "last_message_at" : "last_message_timestamp" ,
233+ "last_message_received_at" : "last_message_received_timestamp" ,
234+ "last_message_sent_at" : "last_message_sent_timestamp" ,
235+ }
236+ datetime_filter_attrs = {
237+ "last_message_before" : "last_message_before" ,
238+ "last_message_after" : "last_message_after" ,
239+ "started_before" : "started_before" ,
240+ "started_after" : "started_after" ,
241+ }
211242 collection_name = 'threads'
212243
213244 def __init__ (self , api ):
@@ -313,6 +344,9 @@ class Draft(Message):
313344 "account_id" , "object" , "subject" , "thread_id" , "to" ,
314345 "unread" , "version" , "file_ids" , "reply_to_message_id" ,
315346 "reply_to" , "starred" , "snippet" , "tracking" ]
347+ datetime_attrs = {
348+ "last_modified_at" : "date" ,
349+ }
316350 collection_name = 'drafts'
317351
318352 def __init__ (self , api , thread_id = None ): # pylint: disable=unused-argument
@@ -407,6 +441,9 @@ class Event(NylasAPIObject):
407441 "read_only" , "when" , "busy" , "participants" , "calendar_id" ,
408442 "recurrence" , "status" , "master_event_id" , "owner" ,
409443 "original_start_time" , "object" , "message_id" ]
444+ datetime_attrs = {
445+ "original_start_at" : "original_start_time" ,
446+ }
410447 collection_name = 'events'
411448
412449 def __init__ (self , api ):
0 commit comments