Passed
Push — develop ( b44848...2798d2 )
by Dean
02:44
created

GetMessage.from_exception()   A

Complexity

Conditions 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 1
cts 2
cp 0.5
rs 9.4285
c 0
b 0
f 0
cc 1
crap 1.125
1 1
from plugin.core.constants import PLUGIN_VERSION_BASE, PLUGIN_VERSION_BRANCH
2 1
from plugin.managers.core.base import Get, Manager
3 1
from plugin.models import Message
4
5 1
from datetime import datetime
6 1
import logging
7
8 1
VERSION_BASE = '.'.join([str(x) for x in PLUGIN_VERSION_BASE])
9 1
VERSION_BRANCH = PLUGIN_VERSION_BRANCH
10
11 1
log = logging.getLogger(__name__)
12
13
14 1
class GetMessage(Get):
15 1
    def _log(self, message_type, *args, **kwargs):
16
        # Find matching (or create new dummy) error message
17 1
        message = self.or_create(
18
            Message.type == message_type,
19
            *args,
20
21
            type=message_type,
22
            last_logged_at=datetime.utcnow(),
23
24
            revision=0,
25
26
            version_base=VERSION_BASE,
27
            version_branch=VERSION_BRANCH,
28
            **kwargs
29
        )
30
31 1
        if message and not message._created:
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _created was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
32
            # Update summary/description
33
            if kwargs.get('summary'):
34
                message.summary = kwargs['summary']
35
36
            if kwargs.get('description'):
37
                message.description = kwargs['description']
38
39
            # Update `last_logged_at` timestamp
40
            message.last_logged_at = datetime.utcnow()
41
42
            # Update version details
43
            message.version_base = VERSION_BASE
44
            message.version_branch = VERSION_BRANCH
45
46
            # Save changes to message
47
            message.save()
48
49 1
        return message
50
51 1
    def from_exception(self, exception, message_type=Message.Type.Exception):
52
        # Log exception
53
        return self._log(
54
            message_type,
55
56
            # Query
57
            Message.exception_hash == exception.hash,
58
59
            # Parameters
60
            exception_hash=exception.hash,
61
62
            summary=exception.message,
63
            description=exception.traceback
64
        )
65
66 1
    def from_message(self, level, message, code=None, description=None):
67
        # Convert `level` to `Message.Type`
68 1
        if level == logging.INFO:
69
            type = Message.Type.Info
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in type.

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

Loading history...
70 1
        elif level == logging.WARNING:
71 1
            type = Message.Type.Warning
72
        elif level == logging.ERROR:
73
            type = Message.Type.Error
74
        elif level == logging.CRITICAL:
75
            type = Message.Type.Critical
76
        else:
77
            raise ValueError('Unknown value for "level" parameter')
78
79
        # Log message
80 1
        return self._log(
81
            type,
82
83
            # Query
84
            Message.code == code,
85
86
            # Parameters
87
            code=code,
88
89
            summary=message,
90
            description=description
91
        )
92
93
94 1
class MessageManager(Manager):
95 1
    get = GetMessage
96
97
    model = Message
98