Passed
Push — develop ( b3bfc7...6fb855 )
by Dean
02:36
created

UpdateLSession.__call__()   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
cc 1
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 2
rs 9.6666
1
from plugin.core.helpers.variable import to_integer, merge
2
from plugin.core.session_status import SessionStatus
3
from plugin.managers.core.base import Manager
0 ignored issues
show
Bug introduced by
The name base does not seem to exist in module plugin.managers.core.
Loading history...
Configuration introduced by
Unable to import 'plugin.managers.core.base' (invalid syntax (<string>, line 39))

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.client import ClientManager
5
from plugin.managers.core.exceptions import FilteredException
6
from plugin.managers.session.base import GetSession, UpdateSession
0 ignored issues
show
Bug introduced by
The name base does not seem to exist in module plugin.managers.session.
Loading history...
Configuration introduced by
Unable to import 'plugin.managers.session.base' (invalid syntax (<string>, line 63))

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
from plugin.managers.user import UserManager
8
from plugin.models import Session
9
10
from datetime import datetime
11
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...
12
import logging
13
import peewee
14
15
log = logging.getLogger(__name__)
16
17
18
class GetLSession(GetSession):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
19
    def __call__(self, info):
20
        machine_identifier = info.get('machineIdentifier')
21
22
        if not machine_identifier:
23
            return None
24
25
        return super(GetLSession, self).__call__(
26
            Session.session_key == machine_identifier
27
        )
28
29
    def or_create(self, info, fetch=False):
30
        machine_identifier = info.get('machineIdentifier')
31
32
        if not machine_identifier:
33
            return None
34
35
        try:
36
            # Create new session
37
            obj = self.manager.create(
0 ignored issues
show
Bug introduced by
The Instance of GetLSession does not seem to have a member named manager.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
38
                rating_key=to_integer(info.get('ratingKey')),
39
                session_key=machine_identifier,
40
41
                state='create'
42
            )
43
44
            # Update newly created object
45
            self.manager.update(obj, info, fetch)
0 ignored issues
show
Bug introduced by
The Instance of GetLSession does not seem to have a member named manager.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
46
47
            # Update active sessions
48
            SessionStatus.on_created(obj)
49
50
            return obj
51
        except (apsw.ConstraintError, peewee.IntegrityError):
52
            # Return existing object
53
            return self(info)
54
55
56
class UpdateLSession(UpdateSession):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
57
    def __call__(self, obj, info, fetch=False):
58
        data = self.to_dict(obj, info, fetch)
59
60
        success = super(UpdateLSession, self).__call__(
61
            obj, data
62
        )
63
64
        SessionStatus.on_updated(obj)
65
        return success
66
67
    def to_dict(self, obj, info, fetch=False):
68
        view_offset = to_integer(info.get('time'))
69
        rating_key = info.get('ratingKey')
70
71
        result = {
72
            'view_offset': view_offset,
73
74
            'updated_at': datetime.utcnow()
75
        }
76
77
        if not fetch:
78
            # Return simple update
79
            return merge(result, {
80
                'progress': self.get_progress(obj.duration, view_offset)
0 ignored issues
show
Bug introduced by
The Instance of UpdateLSession does not seem to have a member named get_progress.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
81
            })
82
83
        # Retrieve session
84
        # Retrieve metadata and guid
85
        p_metadata, guid = self.get_metadata(rating_key)
0 ignored issues
show
Bug introduced by
The Instance of UpdateLSession does not seem to have a member named get_metadata.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
86
87
        if not p_metadata:
88
            log.warn('Unable to retrieve metadata for rating_key %r', rating_key)
89
            return result
90
91
        if not guid:
92
            return merge(result, {
93
                'duration': p_metadata.duration,
94
                'progress': self.get_progress(p_metadata.duration, view_offset)
0 ignored issues
show
Bug introduced by
The Instance of UpdateLSession does not seem to have a member named get_progress.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
95
            })
96
97
        try:
98
            # Create/Retrieve `Client` for session
99
            result['client'] = ClientManager.get.or_create({
0 ignored issues
show
Bug introduced by
It seems like a value for argument player is missing in the unbound method call.
Loading history...
100
                'key': info.get('machineIdentifier'),
101
                'title': info.get('client')
102
            }, fetch=True)
103
104
            # Create/Retrieve `User` for session
105
            result['user'] = UserManager.get.or_create({
0 ignored issues
show
Bug introduced by
It seems like a value for argument user is missing in the unbound method call.
Loading history...
106
                'key': to_integer(info.get('user_id')),
107
                'title': info.get('user_name')
108
            }, fetch=True)
109
110
            # Pick account from `client` or `user` objects
111
            result['account'] = self.get_account(result)
0 ignored issues
show
Bug introduced by
The Instance of UpdateLSession does not seem to have a member named get_account.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
112
        except FilteredException:
113
            log.debug('Activity has been filtered')
114
115
            result['client'] = None
116
            result['user'] = None
117
118
            result['account'] = None
119
120
        return merge(result, {
121
            'duration': p_metadata.duration,
122
            'progress': self.get_progress(p_metadata.duration, view_offset)
0 ignored issues
show
Bug introduced by
The Instance of UpdateLSession does not seem to have a member named get_progress.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
123
        })
124
125
126
class LSessionManager(Manager):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
127
    get = GetLSession
128
    update = UpdateLSession
129
130
    model = Session
131