Show More
@@ -1,826 +1,826 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2010-2017 RhodeCode GmbH |
|
3 | # Copyright (C) 2010-2017 RhodeCode GmbH | |
4 | # |
|
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify |
|
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
7 | # (only), as published by the Free Software Foundation. |
|
7 | # (only), as published by the Free Software Foundation. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License |
|
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | # |
|
16 | # | |
17 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | |||
21 | import os |
|
21 | import os | |
22 | import hashlib |
|
22 | import hashlib | |
23 | import logging |
|
23 | import logging | |
24 | from collections import namedtuple |
|
24 | from collections import namedtuple | |
25 | from functools import wraps |
|
25 | from functools import wraps | |
26 | import bleach |
|
26 | import bleach | |
27 |
|
27 | |||
28 | from rhodecode.lib import caches |
|
28 | from rhodecode.lib import caches | |
29 | from rhodecode.lib.utils2 import ( |
|
29 | from rhodecode.lib.utils2 import ( | |
30 | Optional, AttributeDict, safe_str, remove_prefix, str2bool) |
|
30 | Optional, AttributeDict, safe_str, remove_prefix, str2bool) | |
31 | from rhodecode.lib.vcs.backends import base |
|
31 | from rhodecode.lib.vcs.backends import base | |
32 | from rhodecode.model import BaseModel |
|
32 | from rhodecode.model import BaseModel | |
33 | from rhodecode.model.db import ( |
|
33 | from rhodecode.model.db import ( | |
34 | RepoRhodeCodeUi, RepoRhodeCodeSetting, RhodeCodeUi, RhodeCodeSetting) |
|
34 | RepoRhodeCodeUi, RepoRhodeCodeSetting, RhodeCodeUi, RhodeCodeSetting) | |
35 | from rhodecode.model.meta import Session |
|
35 | from rhodecode.model.meta import Session | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | log = logging.getLogger(__name__) |
|
38 | log = logging.getLogger(__name__) | |
39 |
|
39 | |||
40 |
|
40 | |||
41 | UiSetting = namedtuple( |
|
41 | UiSetting = namedtuple( | |
42 | 'UiSetting', ['section', 'key', 'value', 'active']) |
|
42 | 'UiSetting', ['section', 'key', 'value', 'active']) | |
43 |
|
43 | |||
44 | SOCIAL_PLUGINS_LIST = ['github', 'bitbucket', 'twitter', 'google'] |
|
44 | SOCIAL_PLUGINS_LIST = ['github', 'bitbucket', 'twitter', 'google'] | |
45 |
|
45 | |||
46 |
|
46 | |||
47 | class SettingNotFound(Exception): |
|
47 | class SettingNotFound(Exception): | |
48 | def __init__(self, setting_id): |
|
48 | def __init__(self, setting_id): | |
49 | msg = 'Setting `{}` is not found'.format(setting_id) |
|
49 | msg = 'Setting `{}` is not found'.format(setting_id) | |
50 | super(SettingNotFound, self).__init__(msg) |
|
50 | super(SettingNotFound, self).__init__(msg) | |
51 |
|
51 | |||
52 |
|
52 | |||
53 | class SettingsModel(BaseModel): |
|
53 | class SettingsModel(BaseModel): | |
54 | BUILTIN_HOOKS = ( |
|
54 | BUILTIN_HOOKS = ( | |
55 | RhodeCodeUi.HOOK_REPO_SIZE, RhodeCodeUi.HOOK_PUSH, |
|
55 | RhodeCodeUi.HOOK_REPO_SIZE, RhodeCodeUi.HOOK_PUSH, | |
56 | RhodeCodeUi.HOOK_PRE_PUSH, RhodeCodeUi.HOOK_PRETX_PUSH, |
|
56 | RhodeCodeUi.HOOK_PRE_PUSH, RhodeCodeUi.HOOK_PRETX_PUSH, | |
57 | RhodeCodeUi.HOOK_PULL, RhodeCodeUi.HOOK_PRE_PULL, |
|
57 | RhodeCodeUi.HOOK_PULL, RhodeCodeUi.HOOK_PRE_PULL, | |
58 | RhodeCodeUi.HOOK_PUSH_KEY,) |
|
58 | RhodeCodeUi.HOOK_PUSH_KEY,) | |
59 | HOOKS_SECTION = 'hooks' |
|
59 | HOOKS_SECTION = 'hooks' | |
60 |
|
60 | |||
61 | def __init__(self, sa=None, repo=None): |
|
61 | def __init__(self, sa=None, repo=None): | |
62 | self.repo = repo |
|
62 | self.repo = repo | |
63 | self.UiDbModel = RepoRhodeCodeUi if repo else RhodeCodeUi |
|
63 | self.UiDbModel = RepoRhodeCodeUi if repo else RhodeCodeUi | |
64 | self.SettingsDbModel = ( |
|
64 | self.SettingsDbModel = ( | |
65 | RepoRhodeCodeSetting if repo else RhodeCodeSetting) |
|
65 | RepoRhodeCodeSetting if repo else RhodeCodeSetting) | |
66 | super(SettingsModel, self).__init__(sa) |
|
66 | super(SettingsModel, self).__init__(sa) | |
67 |
|
67 | |||
68 | def get_ui_by_key(self, key): |
|
68 | def get_ui_by_key(self, key): | |
69 | q = self.UiDbModel.query() |
|
69 | q = self.UiDbModel.query() | |
70 | q = q.filter(self.UiDbModel.ui_key == key) |
|
70 | q = q.filter(self.UiDbModel.ui_key == key) | |
71 | q = self._filter_by_repo(RepoRhodeCodeUi, q) |
|
71 | q = self._filter_by_repo(RepoRhodeCodeUi, q) | |
72 | return q.scalar() |
|
72 | return q.scalar() | |
73 |
|
73 | |||
74 | def get_ui_by_section(self, section): |
|
74 | def get_ui_by_section(self, section): | |
75 | q = self.UiDbModel.query() |
|
75 | q = self.UiDbModel.query() | |
76 | q = q.filter(self.UiDbModel.ui_section == section) |
|
76 | q = q.filter(self.UiDbModel.ui_section == section) | |
77 | q = self._filter_by_repo(RepoRhodeCodeUi, q) |
|
77 | q = self._filter_by_repo(RepoRhodeCodeUi, q) | |
78 | return q.all() |
|
78 | return q.all() | |
79 |
|
79 | |||
80 | def get_ui_by_section_and_key(self, section, key): |
|
80 | def get_ui_by_section_and_key(self, section, key): | |
81 | q = self.UiDbModel.query() |
|
81 | q = self.UiDbModel.query() | |
82 | q = q.filter(self.UiDbModel.ui_section == section) |
|
82 | q = q.filter(self.UiDbModel.ui_section == section) | |
83 | q = q.filter(self.UiDbModel.ui_key == key) |
|
83 | q = q.filter(self.UiDbModel.ui_key == key) | |
84 | q = self._filter_by_repo(RepoRhodeCodeUi, q) |
|
84 | q = self._filter_by_repo(RepoRhodeCodeUi, q) | |
85 | return q.scalar() |
|
85 | return q.scalar() | |
86 |
|
86 | |||
87 | def get_ui(self, section=None, key=None): |
|
87 | def get_ui(self, section=None, key=None): | |
88 | q = self.UiDbModel.query() |
|
88 | q = self.UiDbModel.query() | |
89 | q = self._filter_by_repo(RepoRhodeCodeUi, q) |
|
89 | q = self._filter_by_repo(RepoRhodeCodeUi, q) | |
90 |
|
90 | |||
91 | if section: |
|
91 | if section: | |
92 | q = q.filter(self.UiDbModel.ui_section == section) |
|
92 | q = q.filter(self.UiDbModel.ui_section == section) | |
93 | if key: |
|
93 | if key: | |
94 | q = q.filter(self.UiDbModel.ui_key == key) |
|
94 | q = q.filter(self.UiDbModel.ui_key == key) | |
95 |
|
95 | |||
96 | # TODO: mikhail: add caching |
|
96 | # TODO: mikhail: add caching | |
97 | result = [ |
|
97 | result = [ | |
98 | UiSetting( |
|
98 | UiSetting( | |
99 | section=safe_str(r.ui_section), key=safe_str(r.ui_key), |
|
99 | section=safe_str(r.ui_section), key=safe_str(r.ui_key), | |
100 | value=safe_str(r.ui_value), active=r.ui_active |
|
100 | value=safe_str(r.ui_value), active=r.ui_active | |
101 | ) |
|
101 | ) | |
102 | for r in q.all() |
|
102 | for r in q.all() | |
103 | ] |
|
103 | ] | |
104 | return result |
|
104 | return result | |
105 |
|
105 | |||
106 | def get_builtin_hooks(self): |
|
106 | def get_builtin_hooks(self): | |
107 | q = self.UiDbModel.query() |
|
107 | q = self.UiDbModel.query() | |
108 | q = q.filter(self.UiDbModel.ui_key.in_(self.BUILTIN_HOOKS)) |
|
108 | q = q.filter(self.UiDbModel.ui_key.in_(self.BUILTIN_HOOKS)) | |
109 | return self._get_hooks(q) |
|
109 | return self._get_hooks(q) | |
110 |
|
110 | |||
111 | def get_custom_hooks(self): |
|
111 | def get_custom_hooks(self): | |
112 | q = self.UiDbModel.query() |
|
112 | q = self.UiDbModel.query() | |
113 | q = q.filter(~self.UiDbModel.ui_key.in_(self.BUILTIN_HOOKS)) |
|
113 | q = q.filter(~self.UiDbModel.ui_key.in_(self.BUILTIN_HOOKS)) | |
114 | return self._get_hooks(q) |
|
114 | return self._get_hooks(q) | |
115 |
|
115 | |||
116 | def create_ui_section_value(self, section, val, key=None, active=True): |
|
116 | def create_ui_section_value(self, section, val, key=None, active=True): | |
117 | new_ui = self.UiDbModel() |
|
117 | new_ui = self.UiDbModel() | |
118 | new_ui.ui_section = section |
|
118 | new_ui.ui_section = section | |
119 | new_ui.ui_value = val |
|
119 | new_ui.ui_value = val | |
120 | new_ui.ui_active = active |
|
120 | new_ui.ui_active = active | |
121 |
|
121 | |||
122 | if self.repo: |
|
122 | if self.repo: | |
123 | repo = self._get_repo(self.repo) |
|
123 | repo = self._get_repo(self.repo) | |
124 | repository_id = repo.repo_id |
|
124 | repository_id = repo.repo_id | |
125 | new_ui.repository_id = repository_id |
|
125 | new_ui.repository_id = repository_id | |
126 |
|
126 | |||
127 | if not key: |
|
127 | if not key: | |
128 | # keys are unique so they need appended info |
|
128 | # keys are unique so they need appended info | |
129 | if self.repo: |
|
129 | if self.repo: | |
130 | key = hashlib.sha1( |
|
130 | key = hashlib.sha1( | |
131 | '{}{}{}'.format(section, val, repository_id)).hexdigest() |
|
131 | '{}{}{}'.format(section, val, repository_id)).hexdigest() | |
132 | else: |
|
132 | else: | |
133 | key = hashlib.sha1('{}{}'.format(section, val)).hexdigest() |
|
133 | key = hashlib.sha1('{}{}'.format(section, val)).hexdigest() | |
134 |
|
134 | |||
135 | new_ui.ui_key = key |
|
135 | new_ui.ui_key = key | |
136 |
|
136 | |||
137 | Session().add(new_ui) |
|
137 | Session().add(new_ui) | |
138 | return new_ui |
|
138 | return new_ui | |
139 |
|
139 | |||
140 | def create_or_update_hook(self, key, value): |
|
140 | def create_or_update_hook(self, key, value): | |
141 | ui = ( |
|
141 | ui = ( | |
142 | self.get_ui_by_section_and_key(self.HOOKS_SECTION, key) or |
|
142 | self.get_ui_by_section_and_key(self.HOOKS_SECTION, key) or | |
143 | self.UiDbModel()) |
|
143 | self.UiDbModel()) | |
144 | ui.ui_section = self.HOOKS_SECTION |
|
144 | ui.ui_section = self.HOOKS_SECTION | |
145 | ui.ui_active = True |
|
145 | ui.ui_active = True | |
146 | ui.ui_key = key |
|
146 | ui.ui_key = key | |
147 | ui.ui_value = value |
|
147 | ui.ui_value = value | |
148 |
|
148 | |||
149 | if self.repo: |
|
149 | if self.repo: | |
150 | repo = self._get_repo(self.repo) |
|
150 | repo = self._get_repo(self.repo) | |
151 | repository_id = repo.repo_id |
|
151 | repository_id = repo.repo_id | |
152 | ui.repository_id = repository_id |
|
152 | ui.repository_id = repository_id | |
153 |
|
153 | |||
154 | Session().add(ui) |
|
154 | Session().add(ui) | |
155 | return ui |
|
155 | return ui | |
156 |
|
156 | |||
157 | def delete_ui(self, id_): |
|
157 | def delete_ui(self, id_): | |
158 | ui = self.UiDbModel.get(id_) |
|
158 | ui = self.UiDbModel.get(id_) | |
159 | if not ui: |
|
159 | if not ui: | |
160 | raise SettingNotFound(id_) |
|
160 | raise SettingNotFound(id_) | |
161 | Session().delete(ui) |
|
161 | Session().delete(ui) | |
162 |
|
162 | |||
163 | def get_setting_by_name(self, name): |
|
163 | def get_setting_by_name(self, name): | |
164 | q = self._get_settings_query() |
|
164 | q = self._get_settings_query() | |
165 | q = q.filter(self.SettingsDbModel.app_settings_name == name) |
|
165 | q = q.filter(self.SettingsDbModel.app_settings_name == name) | |
166 | return q.scalar() |
|
166 | return q.scalar() | |
167 |
|
167 | |||
168 | def create_or_update_setting( |
|
168 | def create_or_update_setting( | |
169 | self, name, val=Optional(''), type_=Optional('unicode')): |
|
169 | self, name, val=Optional(''), type_=Optional('unicode')): | |
170 | """ |
|
170 | """ | |
171 | Creates or updates RhodeCode setting. If updates is triggered it will |
|
171 | Creates or updates RhodeCode setting. If updates is triggered it will | |
172 | only update parameters that are explicityl set Optional instance will |
|
172 | only update parameters that are explicityl set Optional instance will | |
173 | be skipped |
|
173 | be skipped | |
174 |
|
174 | |||
175 | :param name: |
|
175 | :param name: | |
176 | :param val: |
|
176 | :param val: | |
177 | :param type_: |
|
177 | :param type_: | |
178 | :return: |
|
178 | :return: | |
179 | """ |
|
179 | """ | |
180 |
|
180 | |||
181 | res = self.get_setting_by_name(name) |
|
181 | res = self.get_setting_by_name(name) | |
182 | repo = self._get_repo(self.repo) if self.repo else None |
|
182 | repo = self._get_repo(self.repo) if self.repo else None | |
183 |
|
183 | |||
184 | if not res: |
|
184 | if not res: | |
185 | val = Optional.extract(val) |
|
185 | val = Optional.extract(val) | |
186 | type_ = Optional.extract(type_) |
|
186 | type_ = Optional.extract(type_) | |
187 |
|
187 | |||
188 | args = ( |
|
188 | args = ( | |
189 | (repo.repo_id, name, val, type_) |
|
189 | (repo.repo_id, name, val, type_) | |
190 | if repo else (name, val, type_)) |
|
190 | if repo else (name, val, type_)) | |
191 | res = self.SettingsDbModel(*args) |
|
191 | res = self.SettingsDbModel(*args) | |
192 |
|
192 | |||
193 | else: |
|
193 | else: | |
194 | if self.repo: |
|
194 | if self.repo: | |
195 | res.repository_id = repo.repo_id |
|
195 | res.repository_id = repo.repo_id | |
196 |
|
196 | |||
197 | res.app_settings_name = name |
|
197 | res.app_settings_name = name | |
198 | if not isinstance(type_, Optional): |
|
198 | if not isinstance(type_, Optional): | |
199 | # update if set |
|
199 | # update if set | |
200 | res.app_settings_type = type_ |
|
200 | res.app_settings_type = type_ | |
201 | if not isinstance(val, Optional): |
|
201 | if not isinstance(val, Optional): | |
202 | # update if set |
|
202 | # update if set | |
203 | res.app_settings_value = val |
|
203 | res.app_settings_value = val | |
204 |
|
204 | |||
205 | Session().add(res) |
|
205 | Session().add(res) | |
206 | return res |
|
206 | return res | |
207 |
|
207 | |||
208 | def invalidate_settings_cache(self): |
|
208 | def invalidate_settings_cache(self): | |
209 | namespace = 'rhodecode_settings' |
|
209 | namespace = 'rhodecode_settings' | |
210 | cache_manager = caches.get_cache_manager('sql_cache_short', namespace) |
|
210 | cache_manager = caches.get_cache_manager('sql_cache_short', namespace) | |
211 | caches.clear_cache_manager(cache_manager) |
|
211 | caches.clear_cache_manager(cache_manager) | |
212 |
|
212 | |||
213 | def get_all_settings(self, cache=False): |
|
213 | def get_all_settings(self, cache=False): | |
214 |
|
214 | |||
215 | def _compute(): |
|
215 | def _compute(): | |
216 | q = self._get_settings_query() |
|
216 | q = self._get_settings_query() | |
217 | if not q: |
|
217 | if not q: | |
218 | raise Exception('Could not get application settings !') |
|
218 | raise Exception('Could not get application settings !') | |
219 |
|
219 | |||
220 | settings = { |
|
220 | settings = { | |
221 | 'rhodecode_' + result.app_settings_name: result.app_settings_value |
|
221 | 'rhodecode_' + result.app_settings_name: result.app_settings_value | |
222 | for result in q |
|
222 | for result in q | |
223 | } |
|
223 | } | |
224 | return settings |
|
224 | return settings | |
225 |
|
225 | |||
226 | if cache: |
|
226 | if cache: | |
227 | log.debug('Fetching app settings using cache') |
|
227 | log.debug('Fetching app settings using cache') | |
228 | repo = self._get_repo(self.repo) if self.repo else None |
|
228 | repo = self._get_repo(self.repo) if self.repo else None | |
229 | namespace = 'rhodecode_settings' |
|
229 | namespace = 'rhodecode_settings' | |
230 | cache_manager = caches.get_cache_manager( |
|
230 | cache_manager = caches.get_cache_manager( | |
231 | 'sql_cache_short', namespace) |
|
231 | 'sql_cache_short', namespace) | |
232 | _cache_key = ( |
|
232 | _cache_key = ( | |
233 | "get_repo_{}_settings".format(repo.repo_id) |
|
233 | "get_repo_{}_settings".format(repo.repo_id) | |
234 | if repo else "get_app_settings") |
|
234 | if repo else "get_app_settings") | |
235 |
|
235 | |||
236 | return cache_manager.get(_cache_key, createfunc=_compute) |
|
236 | return cache_manager.get(_cache_key, createfunc=_compute) | |
237 |
|
237 | |||
238 | else: |
|
238 | else: | |
239 | return _compute() |
|
239 | return _compute() | |
240 |
|
240 | |||
241 | def get_auth_settings(self): |
|
241 | def get_auth_settings(self): | |
242 | q = self._get_settings_query() |
|
242 | q = self._get_settings_query() | |
243 | q = q.filter( |
|
243 | q = q.filter( | |
244 | self.SettingsDbModel.app_settings_name.startswith('auth_')) |
|
244 | self.SettingsDbModel.app_settings_name.startswith('auth_')) | |
245 | rows = q.all() |
|
245 | rows = q.all() | |
246 | auth_settings = { |
|
246 | auth_settings = { | |
247 | row.app_settings_name: row.app_settings_value for row in rows} |
|
247 | row.app_settings_name: row.app_settings_value for row in rows} | |
248 | return auth_settings |
|
248 | return auth_settings | |
249 |
|
249 | |||
250 | def get_auth_plugins(self): |
|
250 | def get_auth_plugins(self): | |
251 | auth_plugins = self.get_setting_by_name("auth_plugins") |
|
251 | auth_plugins = self.get_setting_by_name("auth_plugins") | |
252 | return auth_plugins.app_settings_value |
|
252 | return auth_plugins.app_settings_value | |
253 |
|
253 | |||
254 | def get_default_repo_settings(self, strip_prefix=False): |
|
254 | def get_default_repo_settings(self, strip_prefix=False): | |
255 | q = self._get_settings_query() |
|
255 | q = self._get_settings_query() | |
256 | q = q.filter( |
|
256 | q = q.filter( | |
257 | self.SettingsDbModel.app_settings_name.startswith('default_')) |
|
257 | self.SettingsDbModel.app_settings_name.startswith('default_')) | |
258 | rows = q.all() |
|
258 | rows = q.all() | |
259 |
|
259 | |||
260 | result = {} |
|
260 | result = {} | |
261 | for row in rows: |
|
261 | for row in rows: | |
262 | key = row.app_settings_name |
|
262 | key = row.app_settings_name | |
263 | if strip_prefix: |
|
263 | if strip_prefix: | |
264 | key = remove_prefix(key, prefix='default_') |
|
264 | key = remove_prefix(key, prefix='default_') | |
265 | result.update({key: row.app_settings_value}) |
|
265 | result.update({key: row.app_settings_value}) | |
266 | return result |
|
266 | return result | |
267 |
|
267 | |||
268 | def get_repo(self): |
|
268 | def get_repo(self): | |
269 | repo = self._get_repo(self.repo) |
|
269 | repo = self._get_repo(self.repo) | |
270 | if not repo: |
|
270 | if not repo: | |
271 | raise Exception( |
|
271 | raise Exception( | |
272 | 'Repository `{}` cannot be found inside the database'.format( |
|
272 | 'Repository `{}` cannot be found inside the database'.format( | |
273 | self.repo)) |
|
273 | self.repo)) | |
274 | return repo |
|
274 | return repo | |
275 |
|
275 | |||
276 | def _filter_by_repo(self, model, query): |
|
276 | def _filter_by_repo(self, model, query): | |
277 | if self.repo: |
|
277 | if self.repo: | |
278 | repo = self.get_repo() |
|
278 | repo = self.get_repo() | |
279 | query = query.filter(model.repository_id == repo.repo_id) |
|
279 | query = query.filter(model.repository_id == repo.repo_id) | |
280 | return query |
|
280 | return query | |
281 |
|
281 | |||
282 | def _get_hooks(self, query): |
|
282 | def _get_hooks(self, query): | |
283 | query = query.filter(self.UiDbModel.ui_section == self.HOOKS_SECTION) |
|
283 | query = query.filter(self.UiDbModel.ui_section == self.HOOKS_SECTION) | |
284 | query = self._filter_by_repo(RepoRhodeCodeUi, query) |
|
284 | query = self._filter_by_repo(RepoRhodeCodeUi, query) | |
285 | return query.all() |
|
285 | return query.all() | |
286 |
|
286 | |||
287 | def _get_settings_query(self): |
|
287 | def _get_settings_query(self): | |
288 | q = self.SettingsDbModel.query() |
|
288 | q = self.SettingsDbModel.query() | |
289 | return self._filter_by_repo(RepoRhodeCodeSetting, q) |
|
289 | return self._filter_by_repo(RepoRhodeCodeSetting, q) | |
290 |
|
290 | |||
291 | def list_enabled_social_plugins(self, settings): |
|
291 | def list_enabled_social_plugins(self, settings): | |
292 | enabled = [] |
|
292 | enabled = [] | |
293 | for plug in SOCIAL_PLUGINS_LIST: |
|
293 | for plug in SOCIAL_PLUGINS_LIST: | |
294 | if str2bool(settings.get('rhodecode_auth_{}_enabled'.format(plug) |
|
294 | if str2bool(settings.get('rhodecode_auth_{}_enabled'.format(plug) | |
295 | )): |
|
295 | )): | |
296 | enabled.append(plug) |
|
296 | enabled.append(plug) | |
297 | return enabled |
|
297 | return enabled | |
298 |
|
298 | |||
299 |
|
299 | |||
300 | def assert_repo_settings(func): |
|
300 | def assert_repo_settings(func): | |
301 | @wraps(func) |
|
301 | @wraps(func) | |
302 | def _wrapper(self, *args, **kwargs): |
|
302 | def _wrapper(self, *args, **kwargs): | |
303 | if not self.repo_settings: |
|
303 | if not self.repo_settings: | |
304 | raise Exception('Repository is not specified') |
|
304 | raise Exception('Repository is not specified') | |
305 | return func(self, *args, **kwargs) |
|
305 | return func(self, *args, **kwargs) | |
306 | return _wrapper |
|
306 | return _wrapper | |
307 |
|
307 | |||
308 |
|
308 | |||
309 | class IssueTrackerSettingsModel(object): |
|
309 | class IssueTrackerSettingsModel(object): | |
310 | INHERIT_SETTINGS = 'inherit_issue_tracker_settings' |
|
310 | INHERIT_SETTINGS = 'inherit_issue_tracker_settings' | |
311 | SETTINGS_PREFIX = 'issuetracker_' |
|
311 | SETTINGS_PREFIX = 'issuetracker_' | |
312 |
|
312 | |||
313 | def __init__(self, sa=None, repo=None): |
|
313 | def __init__(self, sa=None, repo=None): | |
314 | self.global_settings = SettingsModel(sa=sa) |
|
314 | self.global_settings = SettingsModel(sa=sa) | |
315 | self.repo_settings = SettingsModel(sa=sa, repo=repo) if repo else None |
|
315 | self.repo_settings = SettingsModel(sa=sa, repo=repo) if repo else None | |
316 |
|
316 | |||
317 | @property |
|
317 | @property | |
318 | def inherit_global_settings(self): |
|
318 | def inherit_global_settings(self): | |
319 | if not self.repo_settings: |
|
319 | if not self.repo_settings: | |
320 | return True |
|
320 | return True | |
321 | setting = self.repo_settings.get_setting_by_name(self.INHERIT_SETTINGS) |
|
321 | setting = self.repo_settings.get_setting_by_name(self.INHERIT_SETTINGS) | |
322 | return setting.app_settings_value if setting else True |
|
322 | return setting.app_settings_value if setting else True | |
323 |
|
323 | |||
324 | @inherit_global_settings.setter |
|
324 | @inherit_global_settings.setter | |
325 | def inherit_global_settings(self, value): |
|
325 | def inherit_global_settings(self, value): | |
326 | if self.repo_settings: |
|
326 | if self.repo_settings: | |
327 | settings = self.repo_settings.create_or_update_setting( |
|
327 | settings = self.repo_settings.create_or_update_setting( | |
328 | self.INHERIT_SETTINGS, value, type_='bool') |
|
328 | self.INHERIT_SETTINGS, value, type_='bool') | |
329 | Session().add(settings) |
|
329 | Session().add(settings) | |
330 |
|
330 | |||
331 | def _get_keyname(self, key, uid, prefix=''): |
|
331 | def _get_keyname(self, key, uid, prefix=''): | |
332 | return '{0}{1}{2}_{3}'.format( |
|
332 | return '{0}{1}{2}_{3}'.format( | |
333 | prefix, self.SETTINGS_PREFIX, key, uid) |
|
333 | prefix, self.SETTINGS_PREFIX, key, uid) | |
334 |
|
334 | |||
335 | def _make_dict_for_settings(self, qs): |
|
335 | def _make_dict_for_settings(self, qs): | |
336 | prefix_match = self._get_keyname('pat', '', 'rhodecode_') |
|
336 | prefix_match = self._get_keyname('pat', '', 'rhodecode_') | |
337 |
|
337 | |||
338 | issuetracker_entries = {} |
|
338 | issuetracker_entries = {} | |
339 | # create keys |
|
339 | # create keys | |
340 | for k, v in qs.items(): |
|
340 | for k, v in qs.items(): | |
341 | if k.startswith(prefix_match): |
|
341 | if k.startswith(prefix_match): | |
342 | uid = k[len(prefix_match):] |
|
342 | uid = k[len(prefix_match):] | |
343 | issuetracker_entries[uid] = None |
|
343 | issuetracker_entries[uid] = None | |
344 |
|
344 | |||
345 | # populate |
|
345 | # populate | |
346 | for uid in issuetracker_entries: |
|
346 | for uid in issuetracker_entries: | |
347 | issuetracker_entries[uid] = AttributeDict({ |
|
347 | issuetracker_entries[uid] = AttributeDict({ | |
348 | 'pat': qs.get( |
|
348 | 'pat': qs.get( | |
349 | self._get_keyname('pat', uid, 'rhodecode_')), |
|
349 | self._get_keyname('pat', uid, 'rhodecode_')), | |
350 | 'url': bleach.clean( |
|
350 | 'url': bleach.clean( | |
351 | qs.get(self._get_keyname('url', uid, 'rhodecode_')) or ''), |
|
351 | qs.get(self._get_keyname('url', uid, 'rhodecode_')) or ''), | |
352 |
'pref': |
|
352 | 'pref': bleach.clean( | |
353 | self._get_keyname('pref', uid, 'rhodecode_')), |
|
353 | qs.get(self._get_keyname('pref', uid, 'rhodecode_')) or ''), | |
354 | 'desc': qs.get( |
|
354 | 'desc': qs.get( | |
355 | self._get_keyname('desc', uid, 'rhodecode_')), |
|
355 | self._get_keyname('desc', uid, 'rhodecode_')), | |
356 | }) |
|
356 | }) | |
357 | return issuetracker_entries |
|
357 | return issuetracker_entries | |
358 |
|
358 | |||
359 | def get_global_settings(self, cache=False): |
|
359 | def get_global_settings(self, cache=False): | |
360 | """ |
|
360 | """ | |
361 | Returns list of global issue tracker settings |
|
361 | Returns list of global issue tracker settings | |
362 | """ |
|
362 | """ | |
363 | defaults = self.global_settings.get_all_settings(cache=cache) |
|
363 | defaults = self.global_settings.get_all_settings(cache=cache) | |
364 | settings = self._make_dict_for_settings(defaults) |
|
364 | settings = self._make_dict_for_settings(defaults) | |
365 | return settings |
|
365 | return settings | |
366 |
|
366 | |||
367 | def get_repo_settings(self, cache=False): |
|
367 | def get_repo_settings(self, cache=False): | |
368 | """ |
|
368 | """ | |
369 | Returns list of issue tracker settings per repository |
|
369 | Returns list of issue tracker settings per repository | |
370 | """ |
|
370 | """ | |
371 | if not self.repo_settings: |
|
371 | if not self.repo_settings: | |
372 | raise Exception('Repository is not specified') |
|
372 | raise Exception('Repository is not specified') | |
373 | all_settings = self.repo_settings.get_all_settings(cache=cache) |
|
373 | all_settings = self.repo_settings.get_all_settings(cache=cache) | |
374 | settings = self._make_dict_for_settings(all_settings) |
|
374 | settings = self._make_dict_for_settings(all_settings) | |
375 | return settings |
|
375 | return settings | |
376 |
|
376 | |||
377 | def get_settings(self, cache=False): |
|
377 | def get_settings(self, cache=False): | |
378 | if self.inherit_global_settings: |
|
378 | if self.inherit_global_settings: | |
379 | return self.get_global_settings(cache=cache) |
|
379 | return self.get_global_settings(cache=cache) | |
380 | else: |
|
380 | else: | |
381 | return self.get_repo_settings(cache=cache) |
|
381 | return self.get_repo_settings(cache=cache) | |
382 |
|
382 | |||
383 | def delete_entries(self, uid): |
|
383 | def delete_entries(self, uid): | |
384 | if self.repo_settings: |
|
384 | if self.repo_settings: | |
385 | all_patterns = self.get_repo_settings() |
|
385 | all_patterns = self.get_repo_settings() | |
386 | settings_model = self.repo_settings |
|
386 | settings_model = self.repo_settings | |
387 | else: |
|
387 | else: | |
388 | all_patterns = self.get_global_settings() |
|
388 | all_patterns = self.get_global_settings() | |
389 | settings_model = self.global_settings |
|
389 | settings_model = self.global_settings | |
390 | entries = all_patterns.get(uid, []) |
|
390 | entries = all_patterns.get(uid, []) | |
391 |
|
391 | |||
392 | for del_key in entries: |
|
392 | for del_key in entries: | |
393 | setting_name = self._get_keyname(del_key, uid) |
|
393 | setting_name = self._get_keyname(del_key, uid) | |
394 | entry = settings_model.get_setting_by_name(setting_name) |
|
394 | entry = settings_model.get_setting_by_name(setting_name) | |
395 | if entry: |
|
395 | if entry: | |
396 | Session().delete(entry) |
|
396 | Session().delete(entry) | |
397 |
|
397 | |||
398 | Session().commit() |
|
398 | Session().commit() | |
399 |
|
399 | |||
400 | def create_or_update_setting( |
|
400 | def create_or_update_setting( | |
401 | self, name, val=Optional(''), type_=Optional('unicode')): |
|
401 | self, name, val=Optional(''), type_=Optional('unicode')): | |
402 | if self.repo_settings: |
|
402 | if self.repo_settings: | |
403 | setting = self.repo_settings.create_or_update_setting( |
|
403 | setting = self.repo_settings.create_or_update_setting( | |
404 | name, val, type_) |
|
404 | name, val, type_) | |
405 | else: |
|
405 | else: | |
406 | setting = self.global_settings.create_or_update_setting( |
|
406 | setting = self.global_settings.create_or_update_setting( | |
407 | name, val, type_) |
|
407 | name, val, type_) | |
408 | return setting |
|
408 | return setting | |
409 |
|
409 | |||
410 |
|
410 | |||
411 | class VcsSettingsModel(object): |
|
411 | class VcsSettingsModel(object): | |
412 |
|
412 | |||
413 | INHERIT_SETTINGS = 'inherit_vcs_settings' |
|
413 | INHERIT_SETTINGS = 'inherit_vcs_settings' | |
414 | GENERAL_SETTINGS = ( |
|
414 | GENERAL_SETTINGS = ( | |
415 | 'use_outdated_comments', |
|
415 | 'use_outdated_comments', | |
416 | 'pr_merge_enabled', |
|
416 | 'pr_merge_enabled', | |
417 | 'hg_use_rebase_for_merging', |
|
417 | 'hg_use_rebase_for_merging', | |
418 | 'hg_close_branch_before_merging', |
|
418 | 'hg_close_branch_before_merging', | |
419 | 'git_use_rebase_for_merging', |
|
419 | 'git_use_rebase_for_merging', | |
420 | 'git_close_branch_before_merging') |
|
420 | 'git_close_branch_before_merging') | |
421 |
|
421 | |||
422 | HOOKS_SETTINGS = ( |
|
422 | HOOKS_SETTINGS = ( | |
423 | ('hooks', 'changegroup.repo_size'), |
|
423 | ('hooks', 'changegroup.repo_size'), | |
424 | ('hooks', 'changegroup.push_logger'), |
|
424 | ('hooks', 'changegroup.push_logger'), | |
425 | ('hooks', 'outgoing.pull_logger'),) |
|
425 | ('hooks', 'outgoing.pull_logger'),) | |
426 | HG_SETTINGS = ( |
|
426 | HG_SETTINGS = ( | |
427 | ('extensions', 'largefiles'), |
|
427 | ('extensions', 'largefiles'), | |
428 | ('phases', 'publish'), |
|
428 | ('phases', 'publish'), | |
429 | ('extensions', 'evolve'),) |
|
429 | ('extensions', 'evolve'),) | |
430 | GIT_SETTINGS = ( |
|
430 | GIT_SETTINGS = ( | |
431 | ('vcs_git_lfs', 'enabled'),) |
|
431 | ('vcs_git_lfs', 'enabled'),) | |
432 | GLOBAL_HG_SETTINGS = ( |
|
432 | GLOBAL_HG_SETTINGS = ( | |
433 | ('extensions', 'largefiles'), |
|
433 | ('extensions', 'largefiles'), | |
434 | ('largefiles', 'usercache'), |
|
434 | ('largefiles', 'usercache'), | |
435 | ('phases', 'publish'), |
|
435 | ('phases', 'publish'), | |
436 | ('extensions', 'hgsubversion'), |
|
436 | ('extensions', 'hgsubversion'), | |
437 | ('extensions', 'evolve'),) |
|
437 | ('extensions', 'evolve'),) | |
438 | GLOBAL_GIT_SETTINGS = ( |
|
438 | GLOBAL_GIT_SETTINGS = ( | |
439 | ('vcs_git_lfs', 'enabled'), |
|
439 | ('vcs_git_lfs', 'enabled'), | |
440 | ('vcs_git_lfs', 'store_location')) |
|
440 | ('vcs_git_lfs', 'store_location')) | |
441 | GLOBAL_SVN_SETTINGS = ( |
|
441 | GLOBAL_SVN_SETTINGS = ( | |
442 | ('vcs_svn_proxy', 'http_requests_enabled'), |
|
442 | ('vcs_svn_proxy', 'http_requests_enabled'), | |
443 | ('vcs_svn_proxy', 'http_server_url')) |
|
443 | ('vcs_svn_proxy', 'http_server_url')) | |
444 |
|
444 | |||
445 | SVN_BRANCH_SECTION = 'vcs_svn_branch' |
|
445 | SVN_BRANCH_SECTION = 'vcs_svn_branch' | |
446 | SVN_TAG_SECTION = 'vcs_svn_tag' |
|
446 | SVN_TAG_SECTION = 'vcs_svn_tag' | |
447 | SSL_SETTING = ('web', 'push_ssl') |
|
447 | SSL_SETTING = ('web', 'push_ssl') | |
448 | PATH_SETTING = ('paths', '/') |
|
448 | PATH_SETTING = ('paths', '/') | |
449 |
|
449 | |||
450 | def __init__(self, sa=None, repo=None): |
|
450 | def __init__(self, sa=None, repo=None): | |
451 | self.global_settings = SettingsModel(sa=sa) |
|
451 | self.global_settings = SettingsModel(sa=sa) | |
452 | self.repo_settings = SettingsModel(sa=sa, repo=repo) if repo else None |
|
452 | self.repo_settings = SettingsModel(sa=sa, repo=repo) if repo else None | |
453 | self._ui_settings = ( |
|
453 | self._ui_settings = ( | |
454 | self.HG_SETTINGS + self.GIT_SETTINGS + self.HOOKS_SETTINGS) |
|
454 | self.HG_SETTINGS + self.GIT_SETTINGS + self.HOOKS_SETTINGS) | |
455 | self._svn_sections = (self.SVN_BRANCH_SECTION, self.SVN_TAG_SECTION) |
|
455 | self._svn_sections = (self.SVN_BRANCH_SECTION, self.SVN_TAG_SECTION) | |
456 |
|
456 | |||
457 | @property |
|
457 | @property | |
458 | @assert_repo_settings |
|
458 | @assert_repo_settings | |
459 | def inherit_global_settings(self): |
|
459 | def inherit_global_settings(self): | |
460 | setting = self.repo_settings.get_setting_by_name(self.INHERIT_SETTINGS) |
|
460 | setting = self.repo_settings.get_setting_by_name(self.INHERIT_SETTINGS) | |
461 | return setting.app_settings_value if setting else True |
|
461 | return setting.app_settings_value if setting else True | |
462 |
|
462 | |||
463 | @inherit_global_settings.setter |
|
463 | @inherit_global_settings.setter | |
464 | @assert_repo_settings |
|
464 | @assert_repo_settings | |
465 | def inherit_global_settings(self, value): |
|
465 | def inherit_global_settings(self, value): | |
466 | self.repo_settings.create_or_update_setting( |
|
466 | self.repo_settings.create_or_update_setting( | |
467 | self.INHERIT_SETTINGS, value, type_='bool') |
|
467 | self.INHERIT_SETTINGS, value, type_='bool') | |
468 |
|
468 | |||
469 | def get_global_svn_branch_patterns(self): |
|
469 | def get_global_svn_branch_patterns(self): | |
470 | return self.global_settings.get_ui_by_section(self.SVN_BRANCH_SECTION) |
|
470 | return self.global_settings.get_ui_by_section(self.SVN_BRANCH_SECTION) | |
471 |
|
471 | |||
472 | @assert_repo_settings |
|
472 | @assert_repo_settings | |
473 | def get_repo_svn_branch_patterns(self): |
|
473 | def get_repo_svn_branch_patterns(self): | |
474 | return self.repo_settings.get_ui_by_section(self.SVN_BRANCH_SECTION) |
|
474 | return self.repo_settings.get_ui_by_section(self.SVN_BRANCH_SECTION) | |
475 |
|
475 | |||
476 | def get_global_svn_tag_patterns(self): |
|
476 | def get_global_svn_tag_patterns(self): | |
477 | return self.global_settings.get_ui_by_section(self.SVN_TAG_SECTION) |
|
477 | return self.global_settings.get_ui_by_section(self.SVN_TAG_SECTION) | |
478 |
|
478 | |||
479 | @assert_repo_settings |
|
479 | @assert_repo_settings | |
480 | def get_repo_svn_tag_patterns(self): |
|
480 | def get_repo_svn_tag_patterns(self): | |
481 | return self.repo_settings.get_ui_by_section(self.SVN_TAG_SECTION) |
|
481 | return self.repo_settings.get_ui_by_section(self.SVN_TAG_SECTION) | |
482 |
|
482 | |||
483 | def get_global_settings(self): |
|
483 | def get_global_settings(self): | |
484 | return self._collect_all_settings(global_=True) |
|
484 | return self._collect_all_settings(global_=True) | |
485 |
|
485 | |||
486 | @assert_repo_settings |
|
486 | @assert_repo_settings | |
487 | def get_repo_settings(self): |
|
487 | def get_repo_settings(self): | |
488 | return self._collect_all_settings(global_=False) |
|
488 | return self._collect_all_settings(global_=False) | |
489 |
|
489 | |||
490 | @assert_repo_settings |
|
490 | @assert_repo_settings | |
491 | def create_or_update_repo_settings( |
|
491 | def create_or_update_repo_settings( | |
492 | self, data, inherit_global_settings=False): |
|
492 | self, data, inherit_global_settings=False): | |
493 | from rhodecode.model.scm import ScmModel |
|
493 | from rhodecode.model.scm import ScmModel | |
494 |
|
494 | |||
495 | self.inherit_global_settings = inherit_global_settings |
|
495 | self.inherit_global_settings = inherit_global_settings | |
496 |
|
496 | |||
497 | repo = self.repo_settings.get_repo() |
|
497 | repo = self.repo_settings.get_repo() | |
498 | if not inherit_global_settings: |
|
498 | if not inherit_global_settings: | |
499 | if repo.repo_type == 'svn': |
|
499 | if repo.repo_type == 'svn': | |
500 | self.create_repo_svn_settings(data) |
|
500 | self.create_repo_svn_settings(data) | |
501 | else: |
|
501 | else: | |
502 | self.create_or_update_repo_hook_settings(data) |
|
502 | self.create_or_update_repo_hook_settings(data) | |
503 | self.create_or_update_repo_pr_settings(data) |
|
503 | self.create_or_update_repo_pr_settings(data) | |
504 |
|
504 | |||
505 | if repo.repo_type == 'hg': |
|
505 | if repo.repo_type == 'hg': | |
506 | self.create_or_update_repo_hg_settings(data) |
|
506 | self.create_or_update_repo_hg_settings(data) | |
507 |
|
507 | |||
508 | if repo.repo_type == 'git': |
|
508 | if repo.repo_type == 'git': | |
509 | self.create_or_update_repo_git_settings(data) |
|
509 | self.create_or_update_repo_git_settings(data) | |
510 |
|
510 | |||
511 | ScmModel().mark_for_invalidation(repo.repo_name, delete=True) |
|
511 | ScmModel().mark_for_invalidation(repo.repo_name, delete=True) | |
512 |
|
512 | |||
513 | @assert_repo_settings |
|
513 | @assert_repo_settings | |
514 | def create_or_update_repo_hook_settings(self, data): |
|
514 | def create_or_update_repo_hook_settings(self, data): | |
515 | for section, key in self.HOOKS_SETTINGS: |
|
515 | for section, key in self.HOOKS_SETTINGS: | |
516 | data_key = self._get_form_ui_key(section, key) |
|
516 | data_key = self._get_form_ui_key(section, key) | |
517 | if data_key not in data: |
|
517 | if data_key not in data: | |
518 | raise ValueError( |
|
518 | raise ValueError( | |
519 | 'The given data does not contain {} key'.format(data_key)) |
|
519 | 'The given data does not contain {} key'.format(data_key)) | |
520 |
|
520 | |||
521 | active = data.get(data_key) |
|
521 | active = data.get(data_key) | |
522 | repo_setting = self.repo_settings.get_ui_by_section_and_key( |
|
522 | repo_setting = self.repo_settings.get_ui_by_section_and_key( | |
523 | section, key) |
|
523 | section, key) | |
524 | if not repo_setting: |
|
524 | if not repo_setting: | |
525 | global_setting = self.global_settings.\ |
|
525 | global_setting = self.global_settings.\ | |
526 | get_ui_by_section_and_key(section, key) |
|
526 | get_ui_by_section_and_key(section, key) | |
527 | self.repo_settings.create_ui_section_value( |
|
527 | self.repo_settings.create_ui_section_value( | |
528 | section, global_setting.ui_value, key=key, active=active) |
|
528 | section, global_setting.ui_value, key=key, active=active) | |
529 | else: |
|
529 | else: | |
530 | repo_setting.ui_active = active |
|
530 | repo_setting.ui_active = active | |
531 | Session().add(repo_setting) |
|
531 | Session().add(repo_setting) | |
532 |
|
532 | |||
533 | def update_global_hook_settings(self, data): |
|
533 | def update_global_hook_settings(self, data): | |
534 | for section, key in self.HOOKS_SETTINGS: |
|
534 | for section, key in self.HOOKS_SETTINGS: | |
535 | data_key = self._get_form_ui_key(section, key) |
|
535 | data_key = self._get_form_ui_key(section, key) | |
536 | if data_key not in data: |
|
536 | if data_key not in data: | |
537 | raise ValueError( |
|
537 | raise ValueError( | |
538 | 'The given data does not contain {} key'.format(data_key)) |
|
538 | 'The given data does not contain {} key'.format(data_key)) | |
539 | active = data.get(data_key) |
|
539 | active = data.get(data_key) | |
540 | repo_setting = self.global_settings.get_ui_by_section_and_key( |
|
540 | repo_setting = self.global_settings.get_ui_by_section_and_key( | |
541 | section, key) |
|
541 | section, key) | |
542 | repo_setting.ui_active = active |
|
542 | repo_setting.ui_active = active | |
543 | Session().add(repo_setting) |
|
543 | Session().add(repo_setting) | |
544 |
|
544 | |||
545 | @assert_repo_settings |
|
545 | @assert_repo_settings | |
546 | def create_or_update_repo_pr_settings(self, data): |
|
546 | def create_or_update_repo_pr_settings(self, data): | |
547 | return self._create_or_update_general_settings( |
|
547 | return self._create_or_update_general_settings( | |
548 | self.repo_settings, data) |
|
548 | self.repo_settings, data) | |
549 |
|
549 | |||
550 | def create_or_update_global_pr_settings(self, data): |
|
550 | def create_or_update_global_pr_settings(self, data): | |
551 | return self._create_or_update_general_settings( |
|
551 | return self._create_or_update_general_settings( | |
552 | self.global_settings, data) |
|
552 | self.global_settings, data) | |
553 |
|
553 | |||
554 | @assert_repo_settings |
|
554 | @assert_repo_settings | |
555 | def create_repo_svn_settings(self, data): |
|
555 | def create_repo_svn_settings(self, data): | |
556 | return self._create_svn_settings(self.repo_settings, data) |
|
556 | return self._create_svn_settings(self.repo_settings, data) | |
557 |
|
557 | |||
558 | @assert_repo_settings |
|
558 | @assert_repo_settings | |
559 | def create_or_update_repo_hg_settings(self, data): |
|
559 | def create_or_update_repo_hg_settings(self, data): | |
560 | largefiles, phases, evolve = \ |
|
560 | largefiles, phases, evolve = \ | |
561 | self.HG_SETTINGS |
|
561 | self.HG_SETTINGS | |
562 | largefiles_key, phases_key, evolve_key = \ |
|
562 | largefiles_key, phases_key, evolve_key = \ | |
563 | self._get_settings_keys(self.HG_SETTINGS, data) |
|
563 | self._get_settings_keys(self.HG_SETTINGS, data) | |
564 |
|
564 | |||
565 | self._create_or_update_ui( |
|
565 | self._create_or_update_ui( | |
566 | self.repo_settings, *largefiles, value='', |
|
566 | self.repo_settings, *largefiles, value='', | |
567 | active=data[largefiles_key]) |
|
567 | active=data[largefiles_key]) | |
568 | self._create_or_update_ui( |
|
568 | self._create_or_update_ui( | |
569 | self.repo_settings, *evolve, value='', |
|
569 | self.repo_settings, *evolve, value='', | |
570 | active=data[evolve_key]) |
|
570 | active=data[evolve_key]) | |
571 | self._create_or_update_ui( |
|
571 | self._create_or_update_ui( | |
572 | self.repo_settings, *phases, value=safe_str(data[phases_key])) |
|
572 | self.repo_settings, *phases, value=safe_str(data[phases_key])) | |
573 |
|
573 | |||
574 | def create_or_update_global_hg_settings(self, data): |
|
574 | def create_or_update_global_hg_settings(self, data): | |
575 | largefiles, largefiles_store, phases, hgsubversion, evolve \ |
|
575 | largefiles, largefiles_store, phases, hgsubversion, evolve \ | |
576 | = self.GLOBAL_HG_SETTINGS |
|
576 | = self.GLOBAL_HG_SETTINGS | |
577 | largefiles_key, largefiles_store_key, phases_key, subversion_key, evolve_key \ |
|
577 | largefiles_key, largefiles_store_key, phases_key, subversion_key, evolve_key \ | |
578 | = self._get_settings_keys(self.GLOBAL_HG_SETTINGS, data) |
|
578 | = self._get_settings_keys(self.GLOBAL_HG_SETTINGS, data) | |
579 |
|
579 | |||
580 | self._create_or_update_ui( |
|
580 | self._create_or_update_ui( | |
581 | self.global_settings, *largefiles, value='', |
|
581 | self.global_settings, *largefiles, value='', | |
582 | active=data[largefiles_key]) |
|
582 | active=data[largefiles_key]) | |
583 | self._create_or_update_ui( |
|
583 | self._create_or_update_ui( | |
584 | self.global_settings, *largefiles_store, |
|
584 | self.global_settings, *largefiles_store, | |
585 | value=data[largefiles_store_key]) |
|
585 | value=data[largefiles_store_key]) | |
586 | self._create_or_update_ui( |
|
586 | self._create_or_update_ui( | |
587 | self.global_settings, *phases, value=safe_str(data[phases_key])) |
|
587 | self.global_settings, *phases, value=safe_str(data[phases_key])) | |
588 | self._create_or_update_ui( |
|
588 | self._create_or_update_ui( | |
589 | self.global_settings, *hgsubversion, active=data[subversion_key]) |
|
589 | self.global_settings, *hgsubversion, active=data[subversion_key]) | |
590 | self._create_or_update_ui( |
|
590 | self._create_or_update_ui( | |
591 | self.global_settings, *evolve, value='', |
|
591 | self.global_settings, *evolve, value='', | |
592 | active=data[evolve_key]) |
|
592 | active=data[evolve_key]) | |
593 |
|
593 | |||
594 | def create_or_update_repo_git_settings(self, data): |
|
594 | def create_or_update_repo_git_settings(self, data): | |
595 | # NOTE(marcink): # comma make unpack work properly |
|
595 | # NOTE(marcink): # comma make unpack work properly | |
596 | lfs_enabled, \ |
|
596 | lfs_enabled, \ | |
597 | = self.GIT_SETTINGS |
|
597 | = self.GIT_SETTINGS | |
598 |
|
598 | |||
599 | lfs_enabled_key, \ |
|
599 | lfs_enabled_key, \ | |
600 | = self._get_settings_keys(self.GIT_SETTINGS, data) |
|
600 | = self._get_settings_keys(self.GIT_SETTINGS, data) | |
601 |
|
601 | |||
602 | self._create_or_update_ui( |
|
602 | self._create_or_update_ui( | |
603 | self.repo_settings, *lfs_enabled, value=data[lfs_enabled_key], |
|
603 | self.repo_settings, *lfs_enabled, value=data[lfs_enabled_key], | |
604 | active=data[lfs_enabled_key]) |
|
604 | active=data[lfs_enabled_key]) | |
605 |
|
605 | |||
606 | def create_or_update_global_git_settings(self, data): |
|
606 | def create_or_update_global_git_settings(self, data): | |
607 | lfs_enabled, lfs_store_location \ |
|
607 | lfs_enabled, lfs_store_location \ | |
608 | = self.GLOBAL_GIT_SETTINGS |
|
608 | = self.GLOBAL_GIT_SETTINGS | |
609 | lfs_enabled_key, lfs_store_location_key \ |
|
609 | lfs_enabled_key, lfs_store_location_key \ | |
610 | = self._get_settings_keys(self.GLOBAL_GIT_SETTINGS, data) |
|
610 | = self._get_settings_keys(self.GLOBAL_GIT_SETTINGS, data) | |
611 |
|
611 | |||
612 | self._create_or_update_ui( |
|
612 | self._create_or_update_ui( | |
613 | self.global_settings, *lfs_enabled, value=data[lfs_enabled_key], |
|
613 | self.global_settings, *lfs_enabled, value=data[lfs_enabled_key], | |
614 | active=data[lfs_enabled_key]) |
|
614 | active=data[lfs_enabled_key]) | |
615 | self._create_or_update_ui( |
|
615 | self._create_or_update_ui( | |
616 | self.global_settings, *lfs_store_location, |
|
616 | self.global_settings, *lfs_store_location, | |
617 | value=data[lfs_store_location_key]) |
|
617 | value=data[lfs_store_location_key]) | |
618 |
|
618 | |||
619 | def create_or_update_global_svn_settings(self, data): |
|
619 | def create_or_update_global_svn_settings(self, data): | |
620 | # branch/tags patterns |
|
620 | # branch/tags patterns | |
621 | self._create_svn_settings(self.global_settings, data) |
|
621 | self._create_svn_settings(self.global_settings, data) | |
622 |
|
622 | |||
623 | http_requests_enabled, http_server_url = self.GLOBAL_SVN_SETTINGS |
|
623 | http_requests_enabled, http_server_url = self.GLOBAL_SVN_SETTINGS | |
624 | http_requests_enabled_key, http_server_url_key = self._get_settings_keys( |
|
624 | http_requests_enabled_key, http_server_url_key = self._get_settings_keys( | |
625 | self.GLOBAL_SVN_SETTINGS, data) |
|
625 | self.GLOBAL_SVN_SETTINGS, data) | |
626 |
|
626 | |||
627 | self._create_or_update_ui( |
|
627 | self._create_or_update_ui( | |
628 | self.global_settings, *http_requests_enabled, |
|
628 | self.global_settings, *http_requests_enabled, | |
629 | value=safe_str(data[http_requests_enabled_key])) |
|
629 | value=safe_str(data[http_requests_enabled_key])) | |
630 | self._create_or_update_ui( |
|
630 | self._create_or_update_ui( | |
631 | self.global_settings, *http_server_url, |
|
631 | self.global_settings, *http_server_url, | |
632 | value=data[http_server_url_key]) |
|
632 | value=data[http_server_url_key]) | |
633 |
|
633 | |||
634 | def update_global_ssl_setting(self, value): |
|
634 | def update_global_ssl_setting(self, value): | |
635 | self._create_or_update_ui( |
|
635 | self._create_or_update_ui( | |
636 | self.global_settings, *self.SSL_SETTING, value=value) |
|
636 | self.global_settings, *self.SSL_SETTING, value=value) | |
637 |
|
637 | |||
638 | def update_global_path_setting(self, value): |
|
638 | def update_global_path_setting(self, value): | |
639 | self._create_or_update_ui( |
|
639 | self._create_or_update_ui( | |
640 | self.global_settings, *self.PATH_SETTING, value=value) |
|
640 | self.global_settings, *self.PATH_SETTING, value=value) | |
641 |
|
641 | |||
642 | @assert_repo_settings |
|
642 | @assert_repo_settings | |
643 | def delete_repo_svn_pattern(self, id_): |
|
643 | def delete_repo_svn_pattern(self, id_): | |
644 | ui = self.repo_settings.UiDbModel.get(id_) |
|
644 | ui = self.repo_settings.UiDbModel.get(id_) | |
645 | if ui and ui.repository.repo_name == self.repo_settings.repo: |
|
645 | if ui and ui.repository.repo_name == self.repo_settings.repo: | |
646 | # only delete if it's the same repo as initialized settings |
|
646 | # only delete if it's the same repo as initialized settings | |
647 | self.repo_settings.delete_ui(id_) |
|
647 | self.repo_settings.delete_ui(id_) | |
648 | else: |
|
648 | else: | |
649 | # raise error as if we wouldn't find this option |
|
649 | # raise error as if we wouldn't find this option | |
650 | self.repo_settings.delete_ui(-1) |
|
650 | self.repo_settings.delete_ui(-1) | |
651 |
|
651 | |||
652 | def delete_global_svn_pattern(self, id_): |
|
652 | def delete_global_svn_pattern(self, id_): | |
653 | self.global_settings.delete_ui(id_) |
|
653 | self.global_settings.delete_ui(id_) | |
654 |
|
654 | |||
655 | @assert_repo_settings |
|
655 | @assert_repo_settings | |
656 | def get_repo_ui_settings(self, section=None, key=None): |
|
656 | def get_repo_ui_settings(self, section=None, key=None): | |
657 | global_uis = self.global_settings.get_ui(section, key) |
|
657 | global_uis = self.global_settings.get_ui(section, key) | |
658 | repo_uis = self.repo_settings.get_ui(section, key) |
|
658 | repo_uis = self.repo_settings.get_ui(section, key) | |
659 | filtered_repo_uis = self._filter_ui_settings(repo_uis) |
|
659 | filtered_repo_uis = self._filter_ui_settings(repo_uis) | |
660 | filtered_repo_uis_keys = [ |
|
660 | filtered_repo_uis_keys = [ | |
661 | (s.section, s.key) for s in filtered_repo_uis] |
|
661 | (s.section, s.key) for s in filtered_repo_uis] | |
662 |
|
662 | |||
663 | def _is_global_ui_filtered(ui): |
|
663 | def _is_global_ui_filtered(ui): | |
664 | return ( |
|
664 | return ( | |
665 | (ui.section, ui.key) in filtered_repo_uis_keys |
|
665 | (ui.section, ui.key) in filtered_repo_uis_keys | |
666 | or ui.section in self._svn_sections) |
|
666 | or ui.section in self._svn_sections) | |
667 |
|
667 | |||
668 | filtered_global_uis = [ |
|
668 | filtered_global_uis = [ | |
669 | ui for ui in global_uis if not _is_global_ui_filtered(ui)] |
|
669 | ui for ui in global_uis if not _is_global_ui_filtered(ui)] | |
670 |
|
670 | |||
671 | return filtered_global_uis + filtered_repo_uis |
|
671 | return filtered_global_uis + filtered_repo_uis | |
672 |
|
672 | |||
673 | def get_global_ui_settings(self, section=None, key=None): |
|
673 | def get_global_ui_settings(self, section=None, key=None): | |
674 | return self.global_settings.get_ui(section, key) |
|
674 | return self.global_settings.get_ui(section, key) | |
675 |
|
675 | |||
676 | def get_ui_settings_as_config_obj(self, section=None, key=None): |
|
676 | def get_ui_settings_as_config_obj(self, section=None, key=None): | |
677 | config = base.Config() |
|
677 | config = base.Config() | |
678 |
|
678 | |||
679 | ui_settings = self.get_ui_settings(section=section, key=key) |
|
679 | ui_settings = self.get_ui_settings(section=section, key=key) | |
680 |
|
680 | |||
681 | for entry in ui_settings: |
|
681 | for entry in ui_settings: | |
682 | config.set(entry.section, entry.key, entry.value) |
|
682 | config.set(entry.section, entry.key, entry.value) | |
683 |
|
683 | |||
684 | return config |
|
684 | return config | |
685 |
|
685 | |||
686 | def get_ui_settings(self, section=None, key=None): |
|
686 | def get_ui_settings(self, section=None, key=None): | |
687 | if not self.repo_settings or self.inherit_global_settings: |
|
687 | if not self.repo_settings or self.inherit_global_settings: | |
688 | return self.get_global_ui_settings(section, key) |
|
688 | return self.get_global_ui_settings(section, key) | |
689 | else: |
|
689 | else: | |
690 | return self.get_repo_ui_settings(section, key) |
|
690 | return self.get_repo_ui_settings(section, key) | |
691 |
|
691 | |||
692 | def get_svn_patterns(self, section=None): |
|
692 | def get_svn_patterns(self, section=None): | |
693 | if not self.repo_settings: |
|
693 | if not self.repo_settings: | |
694 | return self.get_global_ui_settings(section) |
|
694 | return self.get_global_ui_settings(section) | |
695 | else: |
|
695 | else: | |
696 | return self.get_repo_ui_settings(section) |
|
696 | return self.get_repo_ui_settings(section) | |
697 |
|
697 | |||
698 | @assert_repo_settings |
|
698 | @assert_repo_settings | |
699 | def get_repo_general_settings(self): |
|
699 | def get_repo_general_settings(self): | |
700 | global_settings = self.global_settings.get_all_settings() |
|
700 | global_settings = self.global_settings.get_all_settings() | |
701 | repo_settings = self.repo_settings.get_all_settings() |
|
701 | repo_settings = self.repo_settings.get_all_settings() | |
702 | filtered_repo_settings = self._filter_general_settings(repo_settings) |
|
702 | filtered_repo_settings = self._filter_general_settings(repo_settings) | |
703 | global_settings.update(filtered_repo_settings) |
|
703 | global_settings.update(filtered_repo_settings) | |
704 | return global_settings |
|
704 | return global_settings | |
705 |
|
705 | |||
706 | def get_global_general_settings(self): |
|
706 | def get_global_general_settings(self): | |
707 | return self.global_settings.get_all_settings() |
|
707 | return self.global_settings.get_all_settings() | |
708 |
|
708 | |||
709 | def get_general_settings(self): |
|
709 | def get_general_settings(self): | |
710 | if not self.repo_settings or self.inherit_global_settings: |
|
710 | if not self.repo_settings or self.inherit_global_settings: | |
711 | return self.get_global_general_settings() |
|
711 | return self.get_global_general_settings() | |
712 | else: |
|
712 | else: | |
713 | return self.get_repo_general_settings() |
|
713 | return self.get_repo_general_settings() | |
714 |
|
714 | |||
715 | def get_repos_location(self): |
|
715 | def get_repos_location(self): | |
716 | return self.global_settings.get_ui_by_key('/').ui_value |
|
716 | return self.global_settings.get_ui_by_key('/').ui_value | |
717 |
|
717 | |||
718 | def _filter_ui_settings(self, settings): |
|
718 | def _filter_ui_settings(self, settings): | |
719 | filtered_settings = [ |
|
719 | filtered_settings = [ | |
720 | s for s in settings if self._should_keep_setting(s)] |
|
720 | s for s in settings if self._should_keep_setting(s)] | |
721 | return filtered_settings |
|
721 | return filtered_settings | |
722 |
|
722 | |||
723 | def _should_keep_setting(self, setting): |
|
723 | def _should_keep_setting(self, setting): | |
724 | keep = ( |
|
724 | keep = ( | |
725 | (setting.section, setting.key) in self._ui_settings or |
|
725 | (setting.section, setting.key) in self._ui_settings or | |
726 | setting.section in self._svn_sections) |
|
726 | setting.section in self._svn_sections) | |
727 | return keep |
|
727 | return keep | |
728 |
|
728 | |||
729 | def _filter_general_settings(self, settings): |
|
729 | def _filter_general_settings(self, settings): | |
730 | keys = ['rhodecode_{}'.format(key) for key in self.GENERAL_SETTINGS] |
|
730 | keys = ['rhodecode_{}'.format(key) for key in self.GENERAL_SETTINGS] | |
731 | return { |
|
731 | return { | |
732 | k: settings[k] |
|
732 | k: settings[k] | |
733 | for k in settings if k in keys} |
|
733 | for k in settings if k in keys} | |
734 |
|
734 | |||
735 | def _collect_all_settings(self, global_=False): |
|
735 | def _collect_all_settings(self, global_=False): | |
736 | settings = self.global_settings if global_ else self.repo_settings |
|
736 | settings = self.global_settings if global_ else self.repo_settings | |
737 | result = {} |
|
737 | result = {} | |
738 |
|
738 | |||
739 | for section, key in self._ui_settings: |
|
739 | for section, key in self._ui_settings: | |
740 | ui = settings.get_ui_by_section_and_key(section, key) |
|
740 | ui = settings.get_ui_by_section_and_key(section, key) | |
741 | result_key = self._get_form_ui_key(section, key) |
|
741 | result_key = self._get_form_ui_key(section, key) | |
742 |
|
742 | |||
743 | if ui: |
|
743 | if ui: | |
744 | if section in ('hooks', 'extensions'): |
|
744 | if section in ('hooks', 'extensions'): | |
745 | result[result_key] = ui.ui_active |
|
745 | result[result_key] = ui.ui_active | |
746 | elif result_key in ['vcs_git_lfs_enabled']: |
|
746 | elif result_key in ['vcs_git_lfs_enabled']: | |
747 | result[result_key] = ui.ui_active |
|
747 | result[result_key] = ui.ui_active | |
748 | else: |
|
748 | else: | |
749 | result[result_key] = ui.ui_value |
|
749 | result[result_key] = ui.ui_value | |
750 |
|
750 | |||
751 | for name in self.GENERAL_SETTINGS: |
|
751 | for name in self.GENERAL_SETTINGS: | |
752 | setting = settings.get_setting_by_name(name) |
|
752 | setting = settings.get_setting_by_name(name) | |
753 | if setting: |
|
753 | if setting: | |
754 | result_key = 'rhodecode_{}'.format(name) |
|
754 | result_key = 'rhodecode_{}'.format(name) | |
755 | result[result_key] = setting.app_settings_value |
|
755 | result[result_key] = setting.app_settings_value | |
756 |
|
756 | |||
757 | return result |
|
757 | return result | |
758 |
|
758 | |||
759 | def _get_form_ui_key(self, section, key): |
|
759 | def _get_form_ui_key(self, section, key): | |
760 | return '{section}_{key}'.format( |
|
760 | return '{section}_{key}'.format( | |
761 | section=section, key=key.replace('.', '_')) |
|
761 | section=section, key=key.replace('.', '_')) | |
762 |
|
762 | |||
763 | def _create_or_update_ui( |
|
763 | def _create_or_update_ui( | |
764 | self, settings, section, key, value=None, active=None): |
|
764 | self, settings, section, key, value=None, active=None): | |
765 | ui = settings.get_ui_by_section_and_key(section, key) |
|
765 | ui = settings.get_ui_by_section_and_key(section, key) | |
766 | if not ui: |
|
766 | if not ui: | |
767 | active = True if active is None else active |
|
767 | active = True if active is None else active | |
768 | settings.create_ui_section_value( |
|
768 | settings.create_ui_section_value( | |
769 | section, value, key=key, active=active) |
|
769 | section, value, key=key, active=active) | |
770 | else: |
|
770 | else: | |
771 | if active is not None: |
|
771 | if active is not None: | |
772 | ui.ui_active = active |
|
772 | ui.ui_active = active | |
773 | if value is not None: |
|
773 | if value is not None: | |
774 | ui.ui_value = value |
|
774 | ui.ui_value = value | |
775 | Session().add(ui) |
|
775 | Session().add(ui) | |
776 |
|
776 | |||
777 | def _create_svn_settings(self, settings, data): |
|
777 | def _create_svn_settings(self, settings, data): | |
778 | svn_settings = { |
|
778 | svn_settings = { | |
779 | 'new_svn_branch': self.SVN_BRANCH_SECTION, |
|
779 | 'new_svn_branch': self.SVN_BRANCH_SECTION, | |
780 | 'new_svn_tag': self.SVN_TAG_SECTION |
|
780 | 'new_svn_tag': self.SVN_TAG_SECTION | |
781 | } |
|
781 | } | |
782 | for key in svn_settings: |
|
782 | for key in svn_settings: | |
783 | if data.get(key): |
|
783 | if data.get(key): | |
784 | settings.create_ui_section_value(svn_settings[key], data[key]) |
|
784 | settings.create_ui_section_value(svn_settings[key], data[key]) | |
785 |
|
785 | |||
786 | def _create_or_update_general_settings(self, settings, data): |
|
786 | def _create_or_update_general_settings(self, settings, data): | |
787 | for name in self.GENERAL_SETTINGS: |
|
787 | for name in self.GENERAL_SETTINGS: | |
788 | data_key = 'rhodecode_{}'.format(name) |
|
788 | data_key = 'rhodecode_{}'.format(name) | |
789 | if data_key not in data: |
|
789 | if data_key not in data: | |
790 | raise ValueError( |
|
790 | raise ValueError( | |
791 | 'The given data does not contain {} key'.format(data_key)) |
|
791 | 'The given data does not contain {} key'.format(data_key)) | |
792 | setting = settings.create_or_update_setting( |
|
792 | setting = settings.create_or_update_setting( | |
793 | name, data[data_key], 'bool') |
|
793 | name, data[data_key], 'bool') | |
794 | Session().add(setting) |
|
794 | Session().add(setting) | |
795 |
|
795 | |||
796 | def _get_settings_keys(self, settings, data): |
|
796 | def _get_settings_keys(self, settings, data): | |
797 | data_keys = [self._get_form_ui_key(*s) for s in settings] |
|
797 | data_keys = [self._get_form_ui_key(*s) for s in settings] | |
798 | for data_key in data_keys: |
|
798 | for data_key in data_keys: | |
799 | if data_key not in data: |
|
799 | if data_key not in data: | |
800 | raise ValueError( |
|
800 | raise ValueError( | |
801 | 'The given data does not contain {} key'.format(data_key)) |
|
801 | 'The given data does not contain {} key'.format(data_key)) | |
802 | return data_keys |
|
802 | return data_keys | |
803 |
|
803 | |||
804 | def create_largeobjects_dirs_if_needed(self, repo_store_path): |
|
804 | def create_largeobjects_dirs_if_needed(self, repo_store_path): | |
805 | """ |
|
805 | """ | |
806 | This is subscribed to the `pyramid.events.ApplicationCreated` event. It |
|
806 | This is subscribed to the `pyramid.events.ApplicationCreated` event. It | |
807 | does a repository scan if enabled in the settings. |
|
807 | does a repository scan if enabled in the settings. | |
808 | """ |
|
808 | """ | |
809 |
|
809 | |||
810 | from rhodecode.lib.vcs.backends.hg import largefiles_store |
|
810 | from rhodecode.lib.vcs.backends.hg import largefiles_store | |
811 | from rhodecode.lib.vcs.backends.git import lfs_store |
|
811 | from rhodecode.lib.vcs.backends.git import lfs_store | |
812 |
|
812 | |||
813 | paths = [ |
|
813 | paths = [ | |
814 | largefiles_store(repo_store_path), |
|
814 | largefiles_store(repo_store_path), | |
815 | lfs_store(repo_store_path)] |
|
815 | lfs_store(repo_store_path)] | |
816 |
|
816 | |||
817 | for path in paths: |
|
817 | for path in paths: | |
818 | if os.path.isdir(path): |
|
818 | if os.path.isdir(path): | |
819 | continue |
|
819 | continue | |
820 | if os.path.isfile(path): |
|
820 | if os.path.isfile(path): | |
821 | continue |
|
821 | continue | |
822 | # not a file nor dir, we try to create it |
|
822 | # not a file nor dir, we try to create it | |
823 | try: |
|
823 | try: | |
824 | os.makedirs(path) |
|
824 | os.makedirs(path) | |
825 | except Exception: |
|
825 | except Exception: | |
826 | log.warning('Failed to create largefiles dir:%s', path) |
|
826 | log.warning('Failed to create largefiles dir:%s', path) |
General Comments 0
You need to be logged in to leave comments.
Login now