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