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