##// END OF EJS Templates
fix(imports): fixed circular import problem
super-admin -
r5341:115837d2 tip default
parent child Browse files
Show More
@@ -1,140 +1,141 b''
1 # Copyright (C) 2016-2023 RhodeCode GmbH
1 # Copyright (C) 2016-2023 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 import logging
19 import logging
20 import datetime
20 import datetime
21
21
22 from zope.cachedescriptors.property import Lazy as LazyProperty
22 from zope.cachedescriptors.property import Lazy as LazyProperty
23
23
24 from rhodecode.lib.pyramid_utils import get_current_request
24 from rhodecode.lib.pyramid_utils import get_current_request
25 from rhodecode.lib.auth import AuthUser
26 from rhodecode.lib.utils2 import AttributeDict
25 from rhodecode.lib.utils2 import AttributeDict
27
26
28
27
29 # this is a user object to be used for events caused by the system (eg. shell)
28 # this is a user object to be used for events caused by the system (eg. shell)
30 SYSTEM_USER = AttributeDict(dict(
29 SYSTEM_USER = AttributeDict(dict(
31 username='__SYSTEM__',
30 username='__SYSTEM__',
32 user_id='__SYSTEM_ID__'
31 user_id='__SYSTEM_ID__'
33 ))
32 ))
34
33
35 log = logging.getLogger(__name__)
34 log = logging.getLogger(__name__)
36
35
37
36
38 class RhodecodeEvent(object):
37 class RhodecodeEvent(object):
39 """
38 """
40 Base event class for all RhodeCode events
39 Base event class for all RhodeCode events
41 """
40 """
42 name = "RhodeCodeEvent"
41 name = "RhodeCodeEvent"
43 no_url_set = '<no server_url available>'
42 no_url_set = '<no server_url available>'
44
43
45 def __init__(self, request=None, actor=None):
44 def __init__(self, request=None, actor=None):
46 self._request = request
45 self._request = request
47 self._actor = actor
46 self._actor = actor
48 self.utc_timestamp = datetime.datetime.utcnow()
47 self.utc_timestamp = datetime.datetime.utcnow()
49
48
50 def __repr__(self):
49 def __repr__(self):
51 return '<{}:({})>'.format(self.__class__.__name__, self.name)
50 return '<{}:({})>'.format(self.__class__.__name__, self.name)
52
51
53 def get_request(self):
52 def get_request(self):
54 if self._request:
53 if self._request:
55 return self._request
54 return self._request
56 return get_current_request()
55 return get_current_request()
57
56
58 @LazyProperty
57 @LazyProperty
59 def request(self):
58 def request(self):
60 return self.get_request()
59 return self.get_request()
61
60
62 @property
61 @property
63 def auth_user(self):
62 def auth_user(self):
64 if not self.request:
63 if not self.request:
65 return
64 return
66
65
67 user = getattr(self.request, 'user', None)
66 user = getattr(self.request, 'user', None)
68 if user:
67 if user:
69 return user
68 return user
70
69
71 api_user = getattr(self.request, 'rpc_user', None)
70 api_user = getattr(self.request, 'rpc_user', None)
72 if api_user:
71 if api_user:
73 return api_user
72 return api_user
74
73
75 @property
74 @property
76 def actor(self):
75 def actor(self):
76 from rhodecode.lib.auth import AuthUser
77
77 # if an explicit actor is specified, use this
78 # if an explicit actor is specified, use this
78 if self._actor:
79 if self._actor:
79 return self._actor
80 return self._actor
80
81
81 auth_user = self.auth_user
82 auth_user = self.auth_user
82 log.debug('Got integration actor: %s', auth_user)
83 log.debug('Got integration actor: %s', auth_user)
83 if isinstance(auth_user, AuthUser):
84 if isinstance(auth_user, AuthUser):
84 instance = auth_user.get_instance()
85 instance = auth_user.get_instance()
85 # we can't find this DB user...
86 # we can't find this DB user...
86 if not instance:
87 if not instance:
87 return AttributeDict(dict(
88 return AttributeDict(dict(
88 username=auth_user.username,
89 username=auth_user.username,
89 user_id=auth_user.user_id,
90 user_id=auth_user.user_id,
90 ))
91 ))
91 elif auth_user:
92 elif auth_user:
92 return auth_user
93 return auth_user
93 return SYSTEM_USER
94 return SYSTEM_USER
94
95
95 @property
96 @property
96 def actor_ip(self):
97 def actor_ip(self):
97 auth_user = self.auth_user
98 auth_user = self.auth_user
98 if auth_user:
99 if auth_user:
99 return auth_user.ip_addr
100 return auth_user.ip_addr
100 return '<no ip available>'
101 return '<no ip available>'
101
102
102 @property
103 @property
103 def server_url(self):
104 def server_url(self):
104 if self.request:
105 if self.request:
105 try:
106 try:
106 return self.request.route_url('home')
107 return self.request.route_url('home')
107 except Exception:
108 except Exception:
108 log.exception('Failed to fetch URL for server')
109 log.exception('Failed to fetch URL for server')
109 return self.no_url_set
110 return self.no_url_set
110
111
111 return self.no_url_set
112 return self.no_url_set
112
113
113 def as_dict(self):
114 def as_dict(self):
114 data = {
115 data = {
115 'name': self.name,
116 'name': self.name,
116 'utc_timestamp': self.utc_timestamp,
117 'utc_timestamp': self.utc_timestamp,
117 'actor_ip': self.actor_ip,
118 'actor_ip': self.actor_ip,
118 'actor': {
119 'actor': {
119 'username': self.actor.username,
120 'username': self.actor.username,
120 'user_id': self.actor.user_id
121 'user_id': self.actor.user_id
121 },
122 },
122 'server_url': self.server_url
123 'server_url': self.server_url
123 }
124 }
124 return data
125 return data
125
126
126
127
127 class RhodeCodeIntegrationEvent(RhodecodeEvent):
128 class RhodeCodeIntegrationEvent(RhodecodeEvent):
128 """
129 """
129 Special subclass for Integration events
130 Special subclass for Integration events
130 """
131 """
131 description = ''
132 description = ''
132
133
133
134
134 class FtsBuild(RhodecodeEvent):
135 class FtsBuild(RhodecodeEvent):
135 """
136 """
136 This event will be triggered when FTS Build is triggered
137 This event will be triggered when FTS Build is triggered
137 """
138 """
138 name = 'fts-build'
139 name = 'fts-build'
139 display_name = 'Start FTS Build'
140 display_name = 'Start FTS Build'
140
141
General Comments 0
You need to be logged in to leave comments. Login now