##// END OF EJS Templates
events: remove attribute from schema, redundant
dan -
r390:36aef1e6 default
parent child Browse files
Show More
@@ -1,70 +1,70 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 SYSTEM_USER = AttributeDict(dict(
25 SYSTEM_USER = AttributeDict(dict(
26 username='__SYSTEM__'
26 username='__SYSTEM__'
27 ))
27 ))
28
28
29
29
30 class UserSchema(Schema):
30 class UserSchema(Schema):
31 """
31 """
32 Marshmallow schema for a user
32 Marshmallow schema for a user
33 """
33 """
34 username = fields.Str()
34 username = fields.Str()
35
35
36
36
37 class RhodecodeEventSchema(Schema):
37 class RhodecodeEventSchema(Schema):
38 """
38 """
39 Marshmallow schema for a rhodecode event
39 Marshmallow schema for a rhodecode event
40 """
40 """
41 utc_timestamp = fields.DateTime()
41 utc_timestamp = fields.DateTime()
42 actor = fields.Nested(UserSchema)
42 actor = fields.Nested(UserSchema)
43 actor_ip = fields.Str()
43 actor_ip = fields.Str()
44 name = fields.Str(attribute='name')
44 name = fields.Str()
45
45
46
46
47 class RhodecodeEvent(object):
47 class RhodecodeEvent(object):
48 """
48 """
49 Base event class for all Rhodecode events
49 Base event class for all Rhodecode events
50 """
50 """
51 MarshmallowSchema = RhodecodeEventSchema
51 MarshmallowSchema = RhodecodeEventSchema
52
52
53 def __init__(self):
53 def __init__(self):
54 self.request = get_current_request()
54 self.request = get_current_request()
55 self.utc_timestamp = datetime.utcnow()
55 self.utc_timestamp = datetime.utcnow()
56
56
57 @property
57 @property
58 def actor(self):
58 def actor(self):
59 if self.request:
59 if self.request:
60 return self.request.user.get_instance()
60 return self.request.user.get_instance()
61 return SYSTEM_USER
61 return SYSTEM_USER
62
62
63 @property
63 @property
64 def actor_ip(self):
64 def actor_ip(self):
65 if self.request:
65 if self.request:
66 return self.request.user.ip_addr
66 return self.request.user.ip_addr
67 return '<no ip available>'
67 return '<no ip available>'
68
68
69 def as_dict(self):
69 def as_dict(self):
70 return self.MarshmallowSchema().dump(self).data No newline at end of file
70 return self.MarshmallowSchema().dump(self).data
General Comments 0
You need to be logged in to leave comments. Login now