-
Notifications
You must be signed in to change notification settings - Fork 167
/
account.py
206 lines (156 loc) · 6.09 KB
/
account.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from plugin.managers.core.base import Manager, Update
from plugin.managers.core.exceptions import TraktAccountExistsException
from plugin.managers.m_trakt.credential import TraktOAuthCredentialManager, TraktBasicCredentialManager
from plugin.models import TraktAccount, TraktOAuthCredential, TraktBasicCredential
from trakt import Trakt
import apsw
import inspect
import logging
import peewee
log = logging.getLogger(__name__)
class UpdateAccount(Update):
keys = ['username']
def from_dict(self, t_account, changes):
# Resolve `account`
if inspect.isfunction(t_account):
t_account = t_account()
# Update `TraktAccount`
if not super(UpdateAccount, self).from_dict(t_account, changes):
return False
# Update credentials
authorization = changes.get('authorization', {})
if 'username' in changes:
# Provide username change in basic authorization update
if 'basic' not in authorization:
authorization['basic'] = {}
authorization['basic']['username'] = changes['username']
# Retrieve `TraktBasicCredential`
t_account.basic = TraktBasicCredentialManager.get.or_create(
TraktBasicCredential.account == t_account,
account=t_account
)
# Update `TraktBasicCredential` (if there are changes)
if 'basic' in authorization:
TraktBasicCredentialManager.update.from_dict(
t_account.basic,
authorization['basic'],
save=False
)
# Retrieve `TraktOAuthCredential`
t_account.oauth = TraktOAuthCredentialManager.get.or_create(
TraktOAuthCredential.account == t_account,
account=t_account
)
# Update `TraktOAuthCredential` (if there are changes)
if 'oauth' in authorization:
TraktOAuthCredentialManager.update.from_dict(
t_account.oauth,
authorization['oauth'],
save=False
)
# Validate the account authorization
with t_account.authorization().http(retry=True):
settings = Trakt['users/settings'].get()
if not settings:
log.warn('Unable to retrieve account details for authorization')
return None
# Update `TraktAccount` username
try:
self.update_username(t_account, settings)
except (apsw.ConstraintError, peewee.IntegrityError), ex:
log.debug('Trakt account already exists - %s', ex, exc_info=True)
raise TraktAccountExistsException('Trakt account already exists')
# Refresh `TraktAccount`
t_account.refresh(
force=True,
settings=settings
)
# Save credentials
if 'basic' in authorization:
t_account.basic.save()
if 'oauth' in authorization:
t_account.oauth.save()
# Refresh `Account`
t_account.account.refresh(
force=True
)
log.info('Updated account authorization for %r', t_account)
return True
def from_pin(self, t_account, pin):
if not pin:
log.debug('"pin" parameter is empty, ignoring account authorization update')
return t_account
# Retrieve current account `OAuthCredential`
oauth = t_account.oauth_credentials.first()
if oauth and oauth.code == pin:
log.debug("PIN hasn't changed, ignoring account authorization update")
return t_account
if not oauth:
# Create new `OAuthCredential` for the account
oauth = TraktOAuthCredential(
account=t_account
)
# Update `OAuthCredential`
if not TraktOAuthCredentialManager.update.from_pin(oauth, pin, save=False):
log.warn("Unable to update OAuthCredential (token exchange failed, hasn't changed, etc..)")
return None
# Validate the account authorization
with t_account.oauth_authorization(oauth).http(retry=True):
settings = Trakt['users/settings'].get()
if not settings:
log.warn('Unable to retrieve account details for authorization')
return None
# Update `TraktAccount` username
try:
self.update_username(t_account, settings)
except (apsw.ConstraintError, peewee.IntegrityError), ex:
log.debug('Trakt account already exists - %s', ex, exc_info=True)
raise TraktAccountExistsException('Trakt account already exists')
# Save oauth credential changes
oauth.save()
# Refresh `TraktAccount`
t_account.refresh(
force=True,
settings=settings
)
# Refresh `Account`
t_account.account.refresh(
force=True
)
log.info('Updated account authorization for %r', t_account)
return t_account
def update_username(self, t_account, settings, save=True):
username = settings.get('user', {}).get('username')
if not username:
log.warn('Unable to retrieve username for authorization')
return
if t_account.username == username:
return
self(t_account, {'username': username}, save=save)
class TraktAccountManager(Manager):
update = UpdateAccount
model = TraktAccount
@classmethod
def delete(cls, *query, **kwargs):
# Retrieve account
try:
account = cls.get(*query, **kwargs)
except Exception, ex:
log.warn('Unable to find trakt account (query: %r, kwargs: %r): %r', query, kwargs, ex)
return False
# Clear trakt account
cls.update(account, {
'username': None,
'thumb': None,
'cover': None,
'timezone': None,
'refreshed_at': None
})
# Delete trakt credentials
TraktBasicCredentialManager.delete(
account=account.id
)
TraktOAuthCredentialManager.delete(
account=account.id
)
return True