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 as 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
|
|
|
data = {'code': pin} |
71
|
|
|
|
72
|
|
|
# Exchange `pin` for token authorization |
73
|
|
|
authorization = Trakt['oauth'].token_exchange(pin, 'urn:ietf:wg:oauth:2.0:oob') |
74
|
|
|
|
75
|
|
|
if not authorization: |
76
|
|
|
log.warn('Token exchange failed for %r', oauth.account) |
77
|
|
|
|
78
|
|
|
# Update credential with `code` (to avoid future re-authentication attempts with the same pin) |
79
|
|
|
self(oauth, data, save=save) |
80
|
|
|
return False |
81
|
|
|
|
82
|
|
|
# Update credential with authorization parameters |
83
|
|
|
data.update(authorization) |
84
|
|
|
|
85
|
|
|
self(oauth, data, save=save) |
86
|
|
|
return True |
87
|
|
|
|
88
|
|
|
|
89
|
1 |
|
class TraktOAuthCredentialManager(Manager): |
90
|
1 |
|
update = UpdateOAuthCredential |
91
|
|
|
|
92
|
1 |
|
model = TraktOAuthCredential |
93
|
|
|
|
94
|
1 |
|
@classmethod |
95
|
|
|
def delete(cls, *query, **kwargs): |
96
|
|
|
# Retrieve oauth credential |
97
|
|
|
try: |
98
|
|
|
credential = cls.get(*query, **kwargs) |
99
|
|
|
except Exception as ex: |
|
|
|
|
100
|
|
|
log.warn('Unable to find oauth credential (query: %r, kwargs: %r): %r', query, kwargs, ex) |
101
|
|
|
return False |
102
|
|
|
|
103
|
|
|
# Clear oauth credential |
104
|
|
|
cls.update(credential, { |
105
|
|
|
'code': None, |
106
|
|
|
|
107
|
|
|
'access_token': None, |
108
|
|
|
'refresh_token': None, |
109
|
|
|
|
110
|
|
|
'created_at': None, |
111
|
|
|
'expires_in': None, |
112
|
|
|
|
113
|
|
|
'token_type': None, |
114
|
|
|
'scope': None |
115
|
|
|
}) |
116
|
|
|
|
117
|
|
|
return True |
118
|
|
|
|
Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.
So, unless you specifically plan to handle any error, consider adding a more specific exception.