Passed
Push — develop ( e15ceb...5f9839 )
by Dean
02:51
created

TraktAccountExistsError

Complexity

Total Complexity 0

Size/Duplication

Total Lines 3
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 0
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
1
from plugin.api.core.base import Service, expose
2
from plugin.api.core.exceptions import ApiError
3
from plugin.managers.account import AccountManager
0 ignored issues
show
Bug introduced by
The name account does not seem to exist in module plugin.managers.
Loading history...
Configuration introduced by
Unable to import 'plugin.managers.account' (invalid syntax (<string>, line 56))

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
4
from plugin.managers.core.exceptions import TraktAccountExistsException, PlexAccountExistsException
5
6
import apsw
0 ignored issues
show
Configuration introduced by
The import apsw could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
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):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in id.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
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):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in id.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
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