|
1
|
1 |
|
from plugin.managers.core.base import Manager, Update |
|
2
|
1 |
|
from plugin.managers.core.exceptions import PlexAccountExistsException |
|
3
|
1 |
|
from plugin.managers.m_plex.credential import PlexBasicCredentialManager |
|
4
|
1 |
|
from plugin.models import PlexAccount, PlexBasicCredential |
|
5
|
|
|
|
|
6
|
1 |
|
import apsw |
|
7
|
1 |
|
import inspect |
|
8
|
1 |
|
import logging |
|
9
|
1 |
|
import peewee |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
1 |
|
log = logging.getLogger(__name__) |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
1 |
|
class UpdateAccount(Update): |
|
16
|
1 |
|
keys = ['username'] |
|
17
|
|
|
|
|
18
|
1 |
|
def from_dict(self, p_account, changes): |
|
19
|
|
|
# Resolve `account` |
|
20
|
|
|
if inspect.isfunction(p_account): |
|
21
|
|
|
p_account = p_account() |
|
22
|
|
|
|
|
23
|
|
|
# Update `PlexAccount` |
|
24
|
|
|
username = changes.pop('username', None) |
|
25
|
|
|
|
|
26
|
|
|
if not super(UpdateAccount, self).from_dict(p_account, changes): |
|
27
|
|
|
return False |
|
28
|
|
|
|
|
29
|
|
|
# Update credentials |
|
30
|
|
|
authorization = changes.get('authorization', {}) |
|
31
|
|
|
|
|
32
|
|
|
# Update `PlexBasicCredential` |
|
33
|
|
|
p_account.basic = PlexBasicCredentialManager.get.or_create( |
|
34
|
|
|
PlexBasicCredential.account == p_account, |
|
35
|
|
|
account=p_account |
|
36
|
|
|
) |
|
37
|
|
|
|
|
38
|
|
|
# Update `TraktBasicCredential` (if there are changes) |
|
39
|
|
|
if 'basic' in authorization: |
|
40
|
|
|
PlexBasicCredentialManager.update.from_dict( |
|
41
|
|
|
p_account.basic, |
|
42
|
|
|
authorization['basic'], |
|
43
|
|
|
save=False |
|
44
|
|
|
) |
|
45
|
|
|
|
|
46
|
|
|
# Update `PlexAccount` username |
|
47
|
|
|
try: |
|
48
|
|
|
self.update_username(p_account, username) |
|
49
|
|
|
except (apsw.ConstraintError, peewee.IntegrityError), ex: |
|
50
|
|
|
log.debug('Plex account already exists - %s', ex, exc_info=True) |
|
51
|
|
|
|
|
52
|
|
|
raise PlexAccountExistsException('Plex account already exists') |
|
53
|
|
|
|
|
54
|
|
|
# Refresh `TraktAccount` |
|
55
|
|
|
p_account.refresh( |
|
56
|
|
|
force=True |
|
57
|
|
|
) |
|
58
|
|
|
|
|
59
|
|
|
# Save credentials |
|
60
|
|
|
if 'basic' in authorization: |
|
61
|
|
|
p_account.basic.save() |
|
62
|
|
|
|
|
63
|
|
|
# Refresh `Account` |
|
64
|
|
|
p_account.account.refresh( |
|
65
|
|
|
force=True |
|
66
|
|
|
) |
|
67
|
|
|
|
|
68
|
|
|
log.info('Updated account authorization for %r', p_account) |
|
69
|
|
|
return True |
|
70
|
|
|
|
|
71
|
1 |
|
def update_username(self, p_account, username, save=True): |
|
72
|
|
|
self(p_account, {'username': username}, save=save) |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
1 |
|
class PlexAccountManager(Manager): |
|
76
|
1 |
|
update = UpdateAccount |
|
77
|
|
|
|
|
78
|
1 |
|
model = PlexAccount |
|
79
|
|
|
|
|
80
|
1 |
|
@classmethod |
|
81
|
|
|
def delete(cls, *query, **kwargs): |
|
82
|
|
|
# Retrieve account |
|
83
|
|
|
try: |
|
84
|
|
|
account = cls.get(*query, **kwargs) |
|
85
|
|
|
except Exception, ex: |
|
86
|
|
|
log.warn('Unable to find plex account (query: %r, kwargs: %r): %r', query, kwargs, ex) |
|
87
|
|
|
return False |
|
88
|
|
|
|
|
89
|
|
|
# Clear plex account |
|
90
|
|
|
cls.update(account, { |
|
91
|
|
|
'key': None, |
|
92
|
|
|
'username': None, |
|
93
|
|
|
|
|
94
|
|
|
'title': None, |
|
95
|
|
|
'thumb': None, |
|
96
|
|
|
|
|
97
|
|
|
'refreshed_at': None |
|
98
|
|
|
}) |
|
99
|
|
|
|
|
100
|
|
|
# Delete plex credentials |
|
101
|
|
|
PlexBasicCredentialManager.delete( |
|
102
|
|
|
account=account.id |
|
103
|
|
|
) |
|
104
|
|
|
|
|
105
|
|
|
return True |
|
106
|
|
|
|