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

ServicePushbullet.callback()   A

Complexity

Conditions 1

Size

Total Lines 5

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 5
rs 9.4285
1
# coding: utf-8
2
3
# Pushbullet
4
from pushbullet import Pushbullet as Pushb
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
from th_pushbullet.models import Pushbullet
14
15
16
"""
17
    handle process with pushbullet
18
    put the following in settings.py
19
20
    TH_PUSHBULLET = {
21
        'client_id': 'abcdefghijklmnopqrstuvwxyz',
22
        'client_secret': 'abcdefghijklmnopqrstuvwxyz',
23
    }
24
    TH_SERVICES = (
25
        ...
26
        'th_pushbullet.my_pushbullet.ServicePushbullet',
27
        ...
28
    )
29
"""
30
31
logger = getLogger('django_th.trigger_happy')
32
33
cache = caches['th_pushbullet']
34
35
36
class ServicePushbullet(ServicesMgr):
37
38 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...
39
        super(ServicePushbullet, self).__init__(token, **kwargs)
40
        self.AUTH_URL = 'https://pushbullet.com/authorize'
41
        self.ACC_TOKEN = 'https://pushbullet.com/access_token'
42
        self.REQ_TOKEN = 'https://api.pushbullet.com/oauth2/token'
43
        self.consumer_key = settings.TH_PUSHBULLET['client_id']
44
        self.consumer_secret = settings.TH_PUSHBULLET['client_secret']
45
        self.scope = 'everything'
46
        self.service = 'ServicePushbullet'
47
        self.oauth = 'oauth2'
48
        if token:
49
            self.token = token
50
            self.pushb = Pushb(token)
51
52
    def read_data(self, **kwargs):
53
        """
54
            get the data from the service
55
            as the pushbullet service does not have any date
56
            in its API linked to the note,
57
            add the triggered date to the dict data
58
            thus the service will be triggered when data will be found
59
60
            :param kwargs: contain keyword args : trigger_id at least
61
            :type kwargs: dict
62
63
            :rtype: list
64
        """
65
        trigger_id = kwargs['trigger_id']
66
        data = list()
67
        cache.set('th_pushbullet_' + str(trigger_id), data)
68
69
    def save_data(self, trigger_id, **data):
70
        """
71
            let's save the data
72
            :param trigger_id: trigger ID from which to save data
73
            :param data: the data to check to be used and save
74
            :type trigger_id: int
75
            :type data:  dict
76
            :return: the status of the save statement
77
            :rtype: boolean
78
        """
79
        kwargs = {}
80
81
        title, content = super(ServicePushbullet, self).save_data(trigger_id,
82
                                                                  data,
83
                                                                  **kwargs)
84
85
        if self.token:
86
            trigger = Pushbullet.objects.get(trigger_id=trigger_id)
87
            if trigger.type == 'note':
88
                status = self.pushb.push_note(title=title, body=content)
89
            elif trigger.type == 'link':
90
                status = self.pushb.push_link(title=title, body=content,
91
                                              url=data.get('link'))
92
                sentence = str('pushbullet {} created').format(title)
93
                logger.debug(sentence)
94
            else:
95
                # no valid type of pushbullet specified
96
                logger.critical("no valid type of pushbullet specified")
97
                status = False
98
        else:
99
            logger.critical("no token or link provided for "
100
                            "trigger ID {} ".format(trigger_id))
101
            status = False
102
        return status
103