##// END OF EJS Templates
events: fix wrongly returned author data.
marcink -
r1466:c2d5cf61 default
parent child Browse files
Show More
@@ -1,98 +1,100 b''
1 1 # Copyright (C) 2016-2017 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 import logging
19 19
20 20 from datetime import datetime
21 21 from pyramid.threadlocal import get_current_request
22 22 from rhodecode.lib.utils2 import AttributeDict
23 23
24 24
25 25 # this is a user object to be used for events caused by the system (eg. shell)
26 26 SYSTEM_USER = AttributeDict(dict(
27 27 username='__SYSTEM__'
28 28 ))
29 29
30 30 log = logging.getLogger(__name__)
31 31
32 32
33 33 class RhodecodeEvent(object):
34 34 """
35 35 Base event class for all Rhodecode events
36 36 """
37 37 name = "RhodeCodeEvent"
38 38
39 39 def __init__(self):
40 40 self.request = get_current_request()
41 41 self.utc_timestamp = datetime.utcnow()
42 42
43 43 @property
44 44 def auth_user(self):
45 45 if not self.request:
46 46 return
47 47
48 48 user = getattr(self.request, 'user', None)
49 49 if user:
50 50 return user
51 51
52 52 api_user = getattr(self.request, 'rpc_user', None)
53 53 if api_user:
54 54 return api_user
55 55
56 56 @property
57 57 def actor(self):
58 58 auth_user = self.auth_user
59
59 60 if auth_user:
60 61 instance = auth_user.get_instance()
61 62 if not instance:
62 63 return AttributeDict(dict(
63 64 username=auth_user.username
64 65 ))
66 return instance
65 67
66 68 return SYSTEM_USER
67 69
68 70 @property
69 71 def actor_ip(self):
70 72 auth_user = self.auth_user
71 73 if auth_user:
72 74 return auth_user.ip_addr
73 75 return '<no ip available>'
74 76
75 77 @property
76 78 def server_url(self):
77 79 default = '<no server_url available>'
78 80 if self.request:
79 81 from rhodecode.lib import helpers as h
80 82 try:
81 83 return h.url('home', qualified=True)
82 84 except Exception:
83 85 log.exception('Failed to fetch URL for server')
84 86 return default
85 87
86 88 return default
87 89
88 90 def as_dict(self):
89 91 data = {
90 92 'name': self.name,
91 93 'utc_timestamp': self.utc_timestamp,
92 94 'actor_ip': self.actor_ip,
93 95 'actor': {
94 96 'username': self.actor.username
95 97 },
96 98 'server_url': self.server_url
97 99 }
98 100 return data
General Comments 0
You need to be logged in to leave comments. Login now