Passed
Push — develop ( 2798d2...d1b463 )
by Dean
02:48
created

TraktBasicCredential.state()   A

Complexity

Conditions 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 8.2077

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 1
cts 6
cp 0.1666
rs 9.6666
c 0
b 0
f 0
cc 3
crap 8.2077
1 1
from plugin.models.m_trakt.account import TraktAccount
2 1
from plugin.models.core import db
3
4 1
from exception_wrappers.libraries.playhouse.apsw_ext import *
0 ignored issues
show
Coding Style introduced by
The usage of wildcard imports like exception_wrappers.libraries.playhouse.apsw_ext should generally be avoided.
Loading history...
5
6
7 1
class TraktBasicCredential(Model):
8 1
    class Meta:
9 1
        database = db
10 1
        db_table = 'trakt.credential.basic'
11
12 1
    account = ForeignKeyField(TraktAccount, 'basic_credentials', unique=True)
13
14 1
    password = CharField(null=True)
15
16
    # Authorization
17 1
    token = CharField(null=True)
18
19 1
    @property
20
    def state(self):
21
        if self.token is not None:
22
            return 'valid'
23
24
        if self.password is not None:
25
            return 'warning'
26
27
        return 'empty'
28
29 1
    def is_valid(self):
30
        return self.token is not None
31
32 1
    def to_json(self, account):
33
        result = {
34
            'state': self.state,
35
36
            'username': account.username
37
        }
38
39
        if self.password:
40
            result['password'] = '*' * len(self.password)
41
        elif self.token:
42
            result['password'] = '*' * 8
43
44
        return result
45
46
47 1
class TraktOAuthCredential(Model):
48 1
    class Meta:
49 1
        database = db
50 1
        db_table = 'trakt.credential.oauth'
51
52 1
    account = ForeignKeyField(TraktAccount, 'oauth_credentials', unique=True)
53
54 1
    code = CharField(null=True)
55
56
    # Authorization
57 1
    access_token = CharField(null=True)
58 1
    refresh_token = CharField(null=True)
59
60 1
    created_at = IntegerField(null=True)
61 1
    expires_in = IntegerField(null=True)
62
63 1
    token_type = CharField(null=True)
64 1
    scope = CharField(null=True)
65
66 1
    @property
67
    def state(self):
68
        if self.is_valid():
69
            return 'valid'
70
71
        if self.code is not None:
72
            return 'warning'
73
74
        return 'empty'
75
76 1
    def is_valid(self):
77
        # TODO check token hasn't expired
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
78
        return self.access_token is not None
79
80 1
    def to_json(self):
81
        result = {
82
            'state': self.state
83
        }
84
85
        if self.code:
86
            result['code'] = '*' * len(self.code)
87
88
        return result
89
90 1
    def to_response(self):
91
        return {
92
            'access_token': self.access_token,
93
            'refresh_token': self.refresh_token,
94
95
            'created_at': self.created_at,
96
            'expires_in': self.expires_in,
97
98
            'token_type': self.token_type,
99
            'scope': self.scope
100
        }
101