Test Setup Failed
Push — develop ( e459d6...12ef70 )
by Dean
02:19
created

UpdateBasicCredential.from_dict()   B

Complexity

Conditions 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 23.225
Metric Value
cc 5
dl 0
loc 19
ccs 1
cts 10
cp 0.1
crap 23.225
rs 8.5454
1 1
from plugin.managers.core.base import Manager, Update
2 1
from plugin.models import TraktBasicCredential, TraktOAuthCredential
3
4 1
from trakt import Trakt
5 1
import inspect
6 1
import logging
7
8 1
log = logging.getLogger(__name__)
9
10
11 1
class UpdateBasicCredential(Update):
12 1
    keys = ['password', 'token']
13
14 1
    def from_dict(self, basic_credential, changes, save=True):
15
        # Resolve `basic_credential`
16
        if inspect.isfunction(basic_credential):
17
            basic_credential = basic_credential()
18
19
        # Request new token on credential changes
20
        if 'username' in changes or 'password' in changes:
21
            # Retrieve credentials
22
            username = changes.get('username', basic_credential.account.username)
23
            password = changes.get('password', basic_credential.password)
24
25
            # Retrieve new token
26
            changes['token'] = Trakt['auth'].login(username, password)
27
28
        # Update `TraktBasicCredential`
29
        if not super(UpdateBasicCredential, self).from_dict(basic_credential, changes, save=save):
30
            return False
31
32
        return True
33
34
35 1
class TraktBasicCredentialManager(Manager):
36 1
    update = UpdateBasicCredential
37
38 1
    model = TraktBasicCredential
39
40 1
    @classmethod
41
    def delete(cls, *query, **kwargs):
42
        # Retrieve basic credential
43
        try:
44
            credential = cls.get(*query, **kwargs)
45
        except Exception, ex:
46
            log.warn('Unable to find basic credential (query: %r, kwargs: %r): %r', query, kwargs, ex)
47
            return False
48
49
        # Clear basic credential
50
        cls.update(credential, {
51
            'password': None,
52
53
            'token': None
54
        })
55
56
        return True
57
58
59 1
class UpdateOAuthCredential(Update):
60 1
    keys = ['code', 'access_token', 'refresh_token', 'created_at', 'expires_in', 'token_type', 'scope']
61
62 1
    def from_dict(self, oauth_credential, changes, save=True):
63
        # Update `TraktOAuthCredential`
64
        if not super(UpdateOAuthCredential, self).from_dict(oauth_credential, changes, save=save):
65
            return False
66
67
        return True
68
69 1
    def from_pin(self, oauth, pin, save=True):
70
        # Exchange `pin` for token authorization
71
        authorization = Trakt['oauth'].token_exchange(pin, 'urn:ietf:wg:oauth:2.0:oob')
72
73
        if not authorization:
74
            log.warn('Token exchange failed')
75
            return None
76
77
        # Update `OAuthCredential`
78
        data = {'code': pin}
79
        data.update(authorization)
80
81
        return self(oauth, data, save=save)
82
83
84 1
class TraktOAuthCredentialManager(Manager):
85 1
    update = UpdateOAuthCredential
86
87 1
    model = TraktOAuthCredential
88
89 1
    @classmethod
90
    def delete(cls, *query, **kwargs):
91
        # Retrieve oauth credential
92
        try:
93
            credential = cls.get(*query, **kwargs)
94
        except Exception, ex:
95
            log.warn('Unable to find oauth credential (query: %r, kwargs: %r): %r', query, kwargs, ex)
96
            return False
97
98
        # Clear oauth credential
99
        cls.update(credential, {
100
            'code': None,
101
102
            'access_token': None,
103
            'refresh_token': None,
104
105
            'created_at': None,
106
            'expires_in': None,
107
108
            'token_type': None,
109
            'scope': None
110
        })
111
112
        return True
113