1
|
|
|
from plugin.api.core.base import Service, expose |
2
|
|
|
from plugin.api.core.exceptions import ApiError |
3
|
|
|
from plugin.managers.account import AccountManager |
|
|
|
|
4
|
|
|
from plugin.managers.core.exceptions import TraktAccountExistsException, PlexAccountExistsException |
5
|
|
|
|
6
|
|
|
import apsw |
|
|
|
|
7
|
|
|
import logging |
8
|
|
|
import peewee |
9
|
|
|
|
10
|
|
|
log = logging.getLogger(__name__) |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class TraktAccountExistsError(ApiError): |
14
|
|
|
code = 'account.trakt.account_exists' |
15
|
|
|
message = 'Trakt account is already in use' |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class PlexAccountExistsError(ApiError): |
19
|
|
|
code = 'account.plex.account_exists' |
20
|
|
|
message = 'Plex account is already in use' |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
class NameConflictError(ApiError): |
24
|
|
|
code = 'account.name_conflict' |
25
|
|
|
message = 'Name conflicts with an existing account' |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class DeletionBlockedError(ApiError): |
29
|
|
|
code = 'account.deletion_blocked' |
30
|
|
|
message = 'Deletion of system accounts is not allowed' |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
class UpdateFailedError(ApiError): |
34
|
|
|
code = 'account.update_failed' |
35
|
|
|
message = 'Unable to update account' |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
class AccountService(Service): |
39
|
|
|
__key__ = 'account' |
40
|
|
|
|
41
|
|
|
@expose |
42
|
|
|
def create(self, name): |
|
|
|
|
43
|
|
|
try: |
44
|
|
|
AccountManager.create( |
45
|
|
|
name=name |
46
|
|
|
) |
47
|
|
|
except (apsw.ConstraintError, peewee.IntegrityError): |
48
|
|
|
raise NameConflictError |
49
|
|
|
|
50
|
|
|
return True |
51
|
|
|
|
52
|
|
|
@expose |
53
|
|
|
def delete(self, id): |
|
|
|
|
54
|
|
|
if id <= 1: |
55
|
|
|
# Block deletion of system/administrator accounts |
56
|
|
|
raise DeletionBlockedError |
57
|
|
|
|
58
|
|
|
# Delete account |
59
|
|
|
return AccountManager.delete( |
60
|
|
|
id=id |
61
|
|
|
) |
62
|
|
|
|
63
|
|
|
@expose |
64
|
|
|
def get(self, full=False, **kwargs): |
|
|
|
|
65
|
|
|
query = dict([ |
66
|
|
|
(key, value) |
67
|
|
|
for (key, value) in kwargs.items() |
68
|
|
|
if key in ['id', 'username'] |
69
|
|
|
]) |
70
|
|
|
|
71
|
|
|
if len(query) < 1: |
72
|
|
|
return None |
73
|
|
|
|
74
|
|
|
return AccountManager.get(**query).to_json(full=full) |
75
|
|
|
|
76
|
|
|
@expose |
77
|
|
|
def list(self, full=False): |
|
|
|
|
78
|
|
|
return [ |
79
|
|
|
account.to_json(full=full) |
80
|
|
|
for account in AccountManager.get.all() |
81
|
|
|
] |
82
|
|
|
|
83
|
|
|
@expose |
84
|
|
|
def update(self, id, data): |
|
|
|
|
85
|
|
|
# Retrieve current account |
86
|
|
|
account = AccountManager.get.by_id(id) |
87
|
|
|
|
88
|
|
|
try: |
89
|
|
|
# Update `account` with changes |
90
|
|
|
if not AccountManager.update.from_dict(account, data): |
91
|
|
|
raise UpdateFailedError |
92
|
|
|
except PlexAccountExistsException: |
93
|
|
|
# Raise as an API-safe error |
94
|
|
|
raise PlexAccountExistsError |
95
|
|
|
except TraktAccountExistsException: |
96
|
|
|
# Raise as an API-safe error |
97
|
|
|
raise TraktAccountExistsError |
98
|
|
|
|
99
|
|
|
# Return updated `account` |
100
|
|
|
return account.to_json(full=True) |
101
|
|
|
|