Show More
@@ -1,70 +1,71 b'' | |||||
1 | # Copyright (C) 2016-2016 RhodeCode GmbH |
|
1 | # Copyright (C) 2016-2016 RhodeCode GmbH | |
2 | # |
|
2 | # | |
3 | # This program is free software: you can redistribute it and/or modify |
|
3 | # This program is free software: you can redistribute it and/or modify | |
4 | # it under the terms of the GNU Affero General Public License, version 3 |
|
4 | # it under the terms of the GNU Affero General Public License, version 3 | |
5 | # (only), as published by the Free Software Foundation. |
|
5 | # (only), as published by the Free Software Foundation. | |
6 | # |
|
6 | # | |
7 | # This program is distributed in the hope that it will be useful, |
|
7 | # This program is distributed in the hope that it will be useful, | |
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | # GNU General Public License for more details. |
|
10 | # GNU General Public License for more details. | |
11 | # |
|
11 | # | |
12 | # You should have received a copy of the GNU Affero General Public License |
|
12 | # You should have received a copy of the GNU Affero General Public License | |
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
14 | # |
|
14 | # | |
15 | # This program is dual-licensed. If you wish to learn more about the |
|
15 | # This program is dual-licensed. If you wish to learn more about the | |
16 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
16 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
17 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
17 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
18 |
|
18 | |||
19 | from datetime import datetime |
|
19 | from datetime import datetime | |
20 | from marshmallow import Schema, fields |
|
20 | from marshmallow import Schema, fields | |
21 | from pyramid.threadlocal import get_current_request |
|
21 | from pyramid.threadlocal import get_current_request | |
22 | from rhodecode.lib.utils2 import AttributeDict |
|
22 | from rhodecode.lib.utils2 import AttributeDict | |
23 |
|
23 | |||
24 |
|
24 | |||
|
25 | # this is a user object to be used for events caused by the system (eg. shell) | |||
25 | SYSTEM_USER = AttributeDict(dict( |
|
26 | SYSTEM_USER = AttributeDict(dict( | |
26 | username='__SYSTEM__' |
|
27 | username='__SYSTEM__' | |
27 | )) |
|
28 | )) | |
28 |
|
29 | |||
29 |
|
30 | |||
30 | class UserSchema(Schema): |
|
31 | class UserSchema(Schema): | |
31 | """ |
|
32 | """ | |
32 | Marshmallow schema for a user |
|
33 | Marshmallow schema for a user | |
33 | """ |
|
34 | """ | |
34 | username = fields.Str() |
|
35 | username = fields.Str() | |
35 |
|
36 | |||
36 |
|
37 | |||
37 | class RhodecodeEventSchema(Schema): |
|
38 | class RhodecodeEventSchema(Schema): | |
38 | """ |
|
39 | """ | |
39 | Marshmallow schema for a rhodecode event |
|
40 | Marshmallow schema for a rhodecode event | |
40 | """ |
|
41 | """ | |
41 | utc_timestamp = fields.DateTime() |
|
42 | utc_timestamp = fields.DateTime() | |
42 | actor = fields.Nested(UserSchema) |
|
43 | actor = fields.Nested(UserSchema) | |
43 | actor_ip = fields.Str() |
|
44 | actor_ip = fields.Str() | |
44 | name = fields.Str() |
|
45 | name = fields.Str() | |
45 |
|
46 | |||
46 |
|
47 | |||
47 | class RhodecodeEvent(object): |
|
48 | class RhodecodeEvent(object): | |
48 | """ |
|
49 | """ | |
49 | Base event class for all Rhodecode events |
|
50 | Base event class for all Rhodecode events | |
50 | """ |
|
51 | """ | |
51 | MarshmallowSchema = RhodecodeEventSchema |
|
52 | MarshmallowSchema = RhodecodeEventSchema | |
52 |
|
53 | |||
53 | def __init__(self): |
|
54 | def __init__(self): | |
54 | self.request = get_current_request() |
|
55 | self.request = get_current_request() | |
55 | self.utc_timestamp = datetime.utcnow() |
|
56 | self.utc_timestamp = datetime.utcnow() | |
56 |
|
57 | |||
57 | @property |
|
58 | @property | |
58 | def actor(self): |
|
59 | def actor(self): | |
59 | if self.request: |
|
60 | if self.request: | |
60 | return self.request.user.get_instance() |
|
61 | return self.request.user.get_instance() | |
61 | return SYSTEM_USER |
|
62 | return SYSTEM_USER | |
62 |
|
63 | |||
63 | @property |
|
64 | @property | |
64 | def actor_ip(self): |
|
65 | def actor_ip(self): | |
65 | if self.request: |
|
66 | if self.request: | |
66 | return self.request.user.ip_addr |
|
67 | return self.request.user.ip_addr | |
67 | return '<no ip available>' |
|
68 | return '<no ip available>' | |
68 |
|
69 | |||
69 | def as_dict(self): |
|
70 | def as_dict(self): | |
70 | return self.MarshmallowSchema().dump(self).data No newline at end of file |
|
71 | return self.MarshmallowSchema().dump(self).data |
General Comments 0
You need to be logged in to leave comments.
Login now