Completed
Push — master ( d94b17...87da46 )
by Fox
01:38
created

ServiceTodoist.read_data()   B

Complexity

Conditions 5

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
c 2
b 0
f 0
dl 0
loc 30
rs 8.0894
1
# coding: utf-8
2
import arrow
3
# TodoistAPI
4
from todoist import TodoistAPI
5
6
# django classes
7
from django.conf import settings
8
from django.utils.log 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['th_todoist']
33
34
35
class ServiceTodoist(ServicesMgr):
36
37 View Code Duplication
    def __init__(self, token=None, **kwargs):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
38
        super(ServiceTodoist, self).__init__(token, **kwargs)
39
        self.AUTH_URL = 'https://todoist.com/oauth/authorize'
40
        self.ACC_TOKEN = 'https://todoist.com/oauth/access_token'
41
        self.REQ_TOKEN = 'https://todoist.com/oauth/access_token'
42
        self.consumer_key = settings.TH_TODOIST['client_id']
43
        self.consumer_secret = settings.TH_TODOIST['client_secret']
44
        self.scope = 'task:add,data:read,data:read_write'
45
        self.service = 'ServiceTodoist'
46
        self.oauth = 'oauth2'
47
        if token:
48
            self.token = token
49
            self.todoist = TodoistAPI(token)
50
51
    def read_data(self, **kwargs):
52
        """
53
            get the data from the service
54
            as the pocket service does not have any date
55
            in its API linked to the note,
56
            add the triggered date to the dict data
57
            thus the service will be triggered when data will be found
58
59
            :param kwargs: contain keyword args : trigger_id at least
60
            :type kwargs: dict
61
62
            :rtype: list
63
        """
64
        trigger_id = kwargs.get('trigger_id')
65
        date_triggered = kwargs.get('date_triggered')
66
        data = []
67
        project_name = 'Main Project'
68
        items = self.todoist.sync()
69
        for item in items.get('items'):
70
            date_added = arrow.get(item.get('date_added'),
71
                                   'ddd DD MMM YYYY HH:mm:ss ZZ')
72
            if date_added > date_triggered:
73
                for project in items.get('projects'):
74
                    if item.get('project_id') == project.get('id'):
75
                        project_name = project.get('name')
76
                data.append({'title': "From TodoIst Project {0}"
77
                                      ":".format(project_name),
78
                             'content': item.get('content')})
79
        cache.set('th_todoist_' + str(trigger_id), data)
80
        return data
81
82
    def save_data(self, trigger_id, **data):
83
        """
84
            let's save the data
85
            :param trigger_id: trigger ID from which to save data
86
            :param data: the data to check to be used and save
87
            :type trigger_id: int
88
            :type data:  dict
89
            :return: the status of the save statement
90
            :rtype: boolean
91
        """
92
        kwargs = {}
93
94
        title, content = super(ServiceTodoist, self).save_data(trigger_id,
95
                                                               data, **kwargs)
96
97
        if self.token:
98
            if title or content or \
99
                            (data.get('link') and len(data.get('link'))) > 0:
100
                content = title + ' ' + content + ' ' + data.get('link')
101
102
                self.todoist.add_item(content)
103
104
                sentence = str('todoist {} created').format(data.get('link'))
105
                logger.debug(sentence)
106
                status = True
107
            else:
108
                status = False
109
        else:
110
            logger.critical("no token or link provided for "
111
                            "trigger ID {} ".format(trigger_id))
112
            status = False
113
        return status
114