1 | # coding: utf-8 |
||
2 | import arrow |
||
3 | # TodoistAPI |
||
4 | from todoist.api import TodoistAPI |
||
5 | |||
6 | # django classes |
||
7 | from django.conf import settings |
||
8 | from logging import getLogger |
||
9 | from django.core.cache import caches |
||
10 | |||
11 | # django_th classes |
||
12 | from django_th.services.services import ServicesMgr |
||
13 | |||
14 | |||
15 | """ |
||
16 | handle process with todoist |
||
17 | put the following in settings.py |
||
18 | |||
19 | TH_TODOIST = { |
||
20 | 'client_id': 'abcdefghijklmnopqrstuvwxyz', |
||
21 | 'client_secret': 'abcdefghijklmnopqrstuvwxyz', |
||
22 | } |
||
23 | TH_SERVICES = ( |
||
24 | ... |
||
25 | 'th_todoist.my_todoist.ServiceTodoist', |
||
26 | ... |
||
27 | ) |
||
28 | """ |
||
29 | |||
30 | logger = getLogger('django_th.trigger_happy') |
||
31 | |||
32 | cache = caches['django_th'] |
||
33 | |||
34 | |||
35 | class ServiceTodoist(ServicesMgr): |
||
36 | """ |
||
37 | service Todoist |
||
38 | """ |
||
39 | View Code Duplication | def __init__(self, token=None, **kwargs): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
40 | super(ServiceTodoist, self).__init__(token, **kwargs) |
||
41 | self.AUTH_URL = 'https://todoist.com/oauth/authorize' |
||
42 | self.ACC_TOKEN = 'https://todoist.com/oauth/access_token' |
||
43 | self.REQ_TOKEN = 'https://todoist.com/oauth/access_token' |
||
44 | self.consumer_key = settings.TH_TODOIST_KEY['client_id'] |
||
45 | self.consumer_secret = settings.TH_TODOIST_KEY['client_secret'] |
||
46 | self.scope = 'task:add,data:read,data:read_write' |
||
47 | self.service = 'ServiceTodoist' |
||
48 | self.oauth = 'oauth2' |
||
49 | if token: |
||
50 | self.token = token |
||
51 | self.todoist = TodoistAPI(token) |
||
52 | |||
53 | def read_data(self, **kwargs): |
||
54 | """ |
||
55 | get the data from the service |
||
56 | as the pocket service does not have any date |
||
57 | in its API linked to the note, |
||
58 | add the triggered date to the dict data |
||
59 | thus the service will be triggered when data will be found |
||
60 | |||
61 | :param kwargs: contain keyword args : trigger_id at least |
||
62 | :type kwargs: dict |
||
63 | |||
64 | :rtype: list |
||
65 | """ |
||
66 | trigger_id = kwargs.get('trigger_id') |
||
67 | date_triggered = kwargs.get('date_triggered') |
||
68 | data = [] |
||
69 | project_name = 'Main Project' |
||
70 | items = self.todoist.sync() |
||
71 | try: |
||
72 | for item in items.get('items'): |
||
73 | date_added = arrow.get(item.get('date_added'), |
||
74 | 'ddd DD MMM YYYY HH:mm:ss ZZ') |
||
75 | if date_added > date_triggered: |
||
76 | for project in items.get('projects'): |
||
77 | if item.get('project_id') == project.get('id'): |
||
78 | project_name = project.get('name') |
||
79 | title = 'From TodoIst Project {0}:'.format(project_name) |
||
80 | data.append({'title': title, |
||
81 | 'content': item.get('content')}) |
||
82 | |||
83 | # digester |
||
84 | self.send_digest_event(trigger_id, |
||
85 | title, |
||
86 | '') |
||
87 | |||
88 | cache.set('th_todoist_' + str(trigger_id), data) |
||
89 | except AttributeError: |
||
90 | logger.error(items) |
||
91 | |||
92 | return data |
||
93 | |||
94 | def save_data(self, trigger_id, **data): |
||
95 | """ |
||
96 | let's save the data |
||
97 | :param trigger_id: trigger ID from which to save data |
||
98 | :param data: the data to check to be used and save |
||
99 | :type trigger_id: int |
||
100 | :type data: dict |
||
101 | :return: the status of the save statement |
||
102 | :rtype: boolean |
||
103 | """ |
||
104 | title, content = super(ServiceTodoist, self).save_data(trigger_id, |
||
105 | **data) |
||
106 | |||
107 | if self.token: |
||
108 | if title or content or \ |
||
109 | (data.get('link') and len(data.get('link'))) > 0: |
||
110 | content = title + ' ' + content + ' ' + data.get('link') |
||
111 | |||
112 | self.todoist.add_item(content) |
||
113 | |||
114 | sentence = str('todoist {} created').format(data.get('link')) |
||
115 | logger.debug(sentence) |
||
116 | status = True |
||
117 | else: |
||
118 | status = False |
||
119 | else: |
||
120 | logger.critical("no token or link provided for " |
||
121 | "trigger ID {} ".format(trigger_id)) |
||
122 | status = False |
||
123 | return status |
||
124 |