Completed
Push — master ( 0b3f92...b9effa )
by Fox
01:26
created

ServiceTodoist.auth()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
dl 0
loc 8
rs 9.4285
1
# coding: utf-8
2
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['trigger_id']
65
        data = list()
66
        cache.set('th_todoist_' + str(trigger_id), data)
67
68
    def save_data(self, trigger_id, **data):
69
        """
70
            let's save the data
71
            :param trigger_id: trigger ID from which to save data
72
            :param data: the data to check to be used and save
73
            :type trigger_id: int
74
            :type data:  dict
75
            :return: the status of the save statement
76
            :rtype: boolean
77
        """
78
        kwargs = {}
79
80
        title, content = super(ServiceTodoist, self).save_data(trigger_id,
81
                                                               data, **kwargs)
82
83
        if self.token:
84
            if title or content or \
85
                            (data.get('link') and len(data.get('link'))) > 0:
86
                content = title + ' ' + content + ' ' + data.get('link')
87
88
                self.todoist.add_item(content)
89
90
                sentence = str('todoist {} created').format(data.get('link'))
91
                logger.debug(sentence)
92
                status = True
93
            else:
94
                status = False
95
        else:
96
            logger.critical("no token or link provided for "
97
                            "trigger ID {} ".format(trigger_id))
98
            status = False
99
        return status
100