ServiceSlack   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 55
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 2 1
A read_data() 0 10 1
B save_data() 0 37 4
1
# coding: utf-8
2
import requests
3
4
# django classes
5
from logging import getLogger
6
from django.core.cache import caches
7
8
# django_th classes
9
from django_th.services.services import ServicesMgr
10
from django_th.models import TriggerService
11
from th_slack.models import Slack
12
13
logger = getLogger('django_th.trigger_happy')
14
15
cache = caches['django_th']
16
17
18
class ServiceSlack(ServicesMgr):
19
    """
20
        Service Slack
21
    """
22
    def __init__(self, token=None, **kwargs):
23
        super(ServiceSlack, self).__init__(token, **kwargs)
24
25
    def read_data(self, **kwargs):
26
        """
27
            get the data from the service
28
29
            :param kwargs: contain keyword args : trigger_id and model name
30
            :type kwargs: dict
31
            :rtype: dict
32
        """
33
        data = ()
34
        return data
35
36
    def save_data(self, trigger_id, **data):
37
        """
38
            get the data from the service
39
40
            :param trigger_id: id of the trigger
41
            :params data, dict
42
            :rtype: dict
43
        """
44
        status = False
45
        service = TriggerService.objects.get(id=trigger_id)
46
        desc = service.description
47
48
        slack = Slack.objects.get(trigger_id=trigger_id)
49
50
        title = self.set_title(data)
51
        if title is None:
52
            title = data.get('subject')
53
        type_action = data.get('type_action')
54
55
        # set the bot username of Slack to the name of the
56
        # provider service
57
        username = service.provider.name.name.split('Service')[1]
58
        # 'build' a link
59
        title_link = ''
60
        if data.get('permalink'):
61
            title_link = ': <' + data.get('permalink') + '|' + title + '>'
62
        data = '*' + desc + '*: ' + type_action + title_link
63
64
        payload = {'username': username,
65
                   'text': data}
66
67
        r = requests.post(slack.webhook_url, json=payload)
68
69
        if r.status_code == requests.codes.ok:
70
            status = True
71
        # return the data
72
        return status
73