|
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: |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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
|
|
|
|
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: