Show More
@@ -1,561 +1,562 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 | """ |
|
22 | 22 | this is forms validation classes |
|
23 | 23 | http://formencode.org/module-formencode.validators.html |
|
24 | 24 | for list off all availible validators |
|
25 | 25 | |
|
26 | 26 | we can create our own validators |
|
27 | 27 | |
|
28 | 28 | The table below outlines the options which can be used in a schema in addition to the validators themselves |
|
29 | 29 | pre_validators [] These validators will be applied before the schema |
|
30 | 30 | chained_validators [] These validators will be applied after the schema |
|
31 | 31 | allow_extra_fields False If True, then it is not an error when keys that aren't associated with a validator are present |
|
32 | 32 | filter_extra_fields False If True, then keys that aren't associated with a validator are removed |
|
33 | 33 | if_key_missing NoDefault If this is given, then any keys that aren't available but are expected will be replaced with this value (and then validated). This does not override a present .if_missing attribute on validators. NoDefault is a special FormEncode class to mean that no default values has been specified and therefore missing keys shouldn't take a default value. |
|
34 | 34 | ignore_key_missing False If True, then missing keys will be missing in the result, if the validator doesn't have .if_missing on it already |
|
35 | 35 | |
|
36 | 36 | |
|
37 | 37 | <name> = formencode.validators.<name of validator> |
|
38 | 38 | <name> must equal form name |
|
39 | 39 | list=[1,2,3,4,5] |
|
40 | 40 | for SELECT use formencode.All(OneOf(list), Int()) |
|
41 | 41 | |
|
42 | 42 | """ |
|
43 | 43 | |
|
44 | 44 | import logging |
|
45 | 45 | |
|
46 | 46 | import formencode |
|
47 | 47 | from formencode import All, Pipe |
|
48 | 48 | |
|
49 | 49 | from pylons.i18n.translation import _ |
|
50 | 50 | |
|
51 | 51 | from rhodecode import BACKENDS |
|
52 | 52 | from rhodecode.model import validators as v |
|
53 | 53 | |
|
54 | 54 | log = logging.getLogger(__name__) |
|
55 | 55 | |
|
56 | 56 | |
|
57 | 57 | def LoginForm(): |
|
58 | 58 | class _LoginForm(formencode.Schema): |
|
59 | 59 | allow_extra_fields = True |
|
60 | 60 | filter_extra_fields = True |
|
61 | 61 | username = v.UnicodeString( |
|
62 | 62 | strip=True, |
|
63 | 63 | min=1, |
|
64 | 64 | not_empty=True, |
|
65 | 65 | messages={ |
|
66 | 66 | 'empty': _(u'Please enter a login'), |
|
67 | 67 | 'tooShort': _(u'Enter a value %(min)i characters long or more') |
|
68 | 68 | } |
|
69 | 69 | ) |
|
70 | 70 | |
|
71 | 71 | password = v.UnicodeString( |
|
72 | 72 | strip=False, |
|
73 | 73 | min=3, |
|
74 | 74 | not_empty=True, |
|
75 | 75 | messages={ |
|
76 | 76 | 'empty': _(u'Please enter a password'), |
|
77 | 77 | 'tooShort': _(u'Enter %(min)i characters or more')} |
|
78 | 78 | ) |
|
79 | 79 | |
|
80 | 80 | remember = v.StringBoolean(if_missing=False) |
|
81 | 81 | |
|
82 | 82 | chained_validators = [v.ValidAuth()] |
|
83 | 83 | return _LoginForm |
|
84 | 84 | |
|
85 | 85 | |
|
86 | 86 | def PasswordChangeForm(username): |
|
87 | 87 | class _PasswordChangeForm(formencode.Schema): |
|
88 | 88 | allow_extra_fields = True |
|
89 | 89 | filter_extra_fields = True |
|
90 | 90 | |
|
91 | 91 | current_password = v.ValidOldPassword(username)(not_empty=True) |
|
92 | 92 | new_password = All(v.ValidPassword(), v.UnicodeString(strip=False, min=6)) |
|
93 | 93 | new_password_confirmation = All(v.ValidPassword(), v.UnicodeString(strip=False, min=6)) |
|
94 | 94 | |
|
95 | 95 | chained_validators = [v.ValidPasswordsMatch('new_password', |
|
96 | 96 | 'new_password_confirmation')] |
|
97 | 97 | return _PasswordChangeForm |
|
98 | 98 | |
|
99 | 99 | |
|
100 | 100 | def UserForm(edit=False, available_languages=[], old_data={}): |
|
101 | 101 | class _UserForm(formencode.Schema): |
|
102 | 102 | allow_extra_fields = True |
|
103 | 103 | filter_extra_fields = True |
|
104 | 104 | username = All(v.UnicodeString(strip=True, min=1, not_empty=True), |
|
105 | 105 | v.ValidUsername(edit, old_data)) |
|
106 | 106 | if edit: |
|
107 | 107 | new_password = All( |
|
108 | 108 | v.ValidPassword(), |
|
109 | 109 | v.UnicodeString(strip=False, min=6, not_empty=False) |
|
110 | 110 | ) |
|
111 | 111 | password_confirmation = All( |
|
112 | 112 | v.ValidPassword(), |
|
113 | 113 | v.UnicodeString(strip=False, min=6, not_empty=False), |
|
114 | 114 | ) |
|
115 | 115 | admin = v.StringBoolean(if_missing=False) |
|
116 | 116 | else: |
|
117 | 117 | password = All( |
|
118 | 118 | v.ValidPassword(), |
|
119 | 119 | v.UnicodeString(strip=False, min=6, not_empty=True) |
|
120 | 120 | ) |
|
121 | 121 | password_confirmation = All( |
|
122 | 122 | v.ValidPassword(), |
|
123 | 123 | v.UnicodeString(strip=False, min=6, not_empty=False) |
|
124 | 124 | ) |
|
125 | 125 | |
|
126 | 126 | password_change = v.StringBoolean(if_missing=False) |
|
127 | 127 | create_repo_group = v.StringBoolean(if_missing=False) |
|
128 | 128 | |
|
129 | 129 | active = v.StringBoolean(if_missing=False) |
|
130 | 130 | firstname = v.UnicodeString(strip=True, min=1, not_empty=False) |
|
131 | 131 | lastname = v.UnicodeString(strip=True, min=1, not_empty=False) |
|
132 | 132 | email = All(v.Email(not_empty=True), v.UniqSystemEmail(old_data)) |
|
133 | 133 | extern_name = v.UnicodeString(strip=True) |
|
134 | 134 | extern_type = v.UnicodeString(strip=True) |
|
135 | 135 | language = v.OneOf(available_languages, hideList=False, |
|
136 | 136 | testValueList=True, if_missing=None) |
|
137 | 137 | chained_validators = [v.ValidPasswordsMatch()] |
|
138 | 138 | return _UserForm |
|
139 | 139 | |
|
140 | 140 | |
|
141 | 141 | def UserGroupForm(edit=False, old_data=None, available_members=None, |
|
142 | 142 | allow_disabled=False): |
|
143 | 143 | old_data = old_data or {} |
|
144 | 144 | available_members = available_members or [] |
|
145 | 145 | |
|
146 | 146 | class _UserGroupForm(formencode.Schema): |
|
147 | 147 | allow_extra_fields = True |
|
148 | 148 | filter_extra_fields = True |
|
149 | 149 | |
|
150 | 150 | users_group_name = All( |
|
151 | 151 | v.UnicodeString(strip=True, min=1, not_empty=True), |
|
152 | 152 | v.ValidUserGroup(edit, old_data) |
|
153 | 153 | ) |
|
154 | 154 | user_group_description = v.UnicodeString(strip=True, min=1, |
|
155 | 155 | not_empty=False) |
|
156 | 156 | |
|
157 | 157 | users_group_active = v.StringBoolean(if_missing=False) |
|
158 | 158 | |
|
159 | 159 | if edit: |
|
160 | 160 | users_group_members = v.OneOf( |
|
161 | 161 | available_members, hideList=False, testValueList=True, |
|
162 | 162 | if_missing=None, not_empty=False |
|
163 | 163 | ) |
|
164 | 164 | # this is user group owner |
|
165 | 165 | user = All( |
|
166 | 166 | v.UnicodeString(not_empty=True), |
|
167 | 167 | v.ValidRepoUser(allow_disabled)) |
|
168 | 168 | return _UserGroupForm |
|
169 | 169 | |
|
170 | 170 | |
|
171 | 171 | def RepoGroupForm(edit=False, old_data=None, available_groups=None, |
|
172 | 172 | can_create_in_root=False, allow_disabled=False): |
|
173 | 173 | old_data = old_data or {} |
|
174 | 174 | available_groups = available_groups or [] |
|
175 | 175 | |
|
176 | 176 | class _RepoGroupForm(formencode.Schema): |
|
177 | 177 | allow_extra_fields = True |
|
178 | 178 | filter_extra_fields = False |
|
179 | 179 | |
|
180 | 180 | group_name = All(v.UnicodeString(strip=True, min=1, not_empty=True), |
|
181 | 181 | v.SlugifyName(),) |
|
182 | 182 | group_description = v.UnicodeString(strip=True, min=1, |
|
183 | 183 | not_empty=False) |
|
184 | 184 | group_copy_permissions = v.StringBoolean(if_missing=False) |
|
185 | 185 | |
|
186 | 186 | group_parent_id = v.OneOf(available_groups, hideList=False, |
|
187 | 187 | testValueList=True, not_empty=True) |
|
188 | 188 | enable_locking = v.StringBoolean(if_missing=False) |
|
189 | 189 | chained_validators = [ |
|
190 | 190 | v.ValidRepoGroup(edit, old_data, can_create_in_root)] |
|
191 | 191 | |
|
192 | 192 | if edit: |
|
193 | 193 | # this is repo group owner |
|
194 | 194 | user = All( |
|
195 | 195 | v.UnicodeString(not_empty=True), |
|
196 | 196 | v.ValidRepoUser(allow_disabled)) |
|
197 | 197 | |
|
198 | 198 | return _RepoGroupForm |
|
199 | 199 | |
|
200 | 200 | |
|
201 | 201 | def RegisterForm(edit=False, old_data={}): |
|
202 | 202 | class _RegisterForm(formencode.Schema): |
|
203 | 203 | allow_extra_fields = True |
|
204 | 204 | filter_extra_fields = True |
|
205 | 205 | username = All( |
|
206 | 206 | v.ValidUsername(edit, old_data), |
|
207 | 207 | v.UnicodeString(strip=True, min=1, not_empty=True) |
|
208 | 208 | ) |
|
209 | 209 | password = All( |
|
210 | 210 | v.ValidPassword(), |
|
211 | 211 | v.UnicodeString(strip=False, min=6, not_empty=True) |
|
212 | 212 | ) |
|
213 | 213 | password_confirmation = All( |
|
214 | 214 | v.ValidPassword(), |
|
215 | 215 | v.UnicodeString(strip=False, min=6, not_empty=True) |
|
216 | 216 | ) |
|
217 | 217 | active = v.StringBoolean(if_missing=False) |
|
218 | 218 | firstname = v.UnicodeString(strip=True, min=1, not_empty=False) |
|
219 | 219 | lastname = v.UnicodeString(strip=True, min=1, not_empty=False) |
|
220 | 220 | email = All(v.Email(not_empty=True), v.UniqSystemEmail(old_data)) |
|
221 | 221 | |
|
222 | 222 | chained_validators = [v.ValidPasswordsMatch()] |
|
223 | 223 | |
|
224 | 224 | return _RegisterForm |
|
225 | 225 | |
|
226 | 226 | |
|
227 | 227 | def PasswordResetForm(): |
|
228 | 228 | class _PasswordResetForm(formencode.Schema): |
|
229 | 229 | allow_extra_fields = True |
|
230 | 230 | filter_extra_fields = True |
|
231 | 231 | email = All(v.ValidSystemEmail(), v.Email(not_empty=True)) |
|
232 | 232 | return _PasswordResetForm |
|
233 | 233 | |
|
234 | 234 | |
|
235 | 235 | def RepoForm(edit=False, old_data=None, repo_groups=None, landing_revs=None, |
|
236 | 236 | allow_disabled=False): |
|
237 | 237 | old_data = old_data or {} |
|
238 | 238 | repo_groups = repo_groups or [] |
|
239 | 239 | landing_revs = landing_revs or [] |
|
240 | 240 | supported_backends = BACKENDS.keys() |
|
241 | 241 | |
|
242 | 242 | class _RepoForm(formencode.Schema): |
|
243 | 243 | allow_extra_fields = True |
|
244 | 244 | filter_extra_fields = False |
|
245 | 245 | repo_name = All(v.UnicodeString(strip=True, min=1, not_empty=True), |
|
246 | 246 | v.SlugifyName()) |
|
247 | 247 | repo_group = All(v.CanWriteGroup(old_data), |
|
248 | 248 | v.OneOf(repo_groups, hideList=True)) |
|
249 | 249 | repo_type = v.OneOf(supported_backends, required=False, |
|
250 | 250 | if_missing=old_data.get('repo_type')) |
|
251 | 251 | repo_description = v.UnicodeString(strip=True, min=1, not_empty=False) |
|
252 | 252 | repo_private = v.StringBoolean(if_missing=False) |
|
253 | 253 | repo_landing_rev = v.OneOf(landing_revs, hideList=True) |
|
254 | 254 | repo_copy_permissions = v.StringBoolean(if_missing=False) |
|
255 | 255 | clone_uri = All(v.UnicodeString(strip=True, min=1, not_empty=False)) |
|
256 | 256 | |
|
257 | 257 | repo_enable_statistics = v.StringBoolean(if_missing=False) |
|
258 | 258 | repo_enable_downloads = v.StringBoolean(if_missing=False) |
|
259 | 259 | repo_enable_locking = v.StringBoolean(if_missing=False) |
|
260 | 260 | |
|
261 | 261 | if edit: |
|
262 | 262 | # this is repo owner |
|
263 | 263 | user = All( |
|
264 | 264 | v.UnicodeString(not_empty=True), |
|
265 | 265 | v.ValidRepoUser(allow_disabled)) |
|
266 | 266 | clone_uri_change = v.UnicodeString( |
|
267 | 267 | not_empty=False, if_missing=v.Missing) |
|
268 | 268 | |
|
269 | 269 | chained_validators = [v.ValidCloneUri(), |
|
270 | 270 | v.ValidRepoName(edit, old_data)] |
|
271 | 271 | return _RepoForm |
|
272 | 272 | |
|
273 | 273 | |
|
274 | 274 | def RepoPermsForm(): |
|
275 | 275 | class _RepoPermsForm(formencode.Schema): |
|
276 | 276 | allow_extra_fields = True |
|
277 | 277 | filter_extra_fields = False |
|
278 | 278 | chained_validators = [v.ValidPerms(type_='repo')] |
|
279 | 279 | return _RepoPermsForm |
|
280 | 280 | |
|
281 | 281 | |
|
282 | 282 | def RepoGroupPermsForm(valid_recursive_choices): |
|
283 | 283 | class _RepoGroupPermsForm(formencode.Schema): |
|
284 | 284 | allow_extra_fields = True |
|
285 | 285 | filter_extra_fields = False |
|
286 | 286 | recursive = v.OneOf(valid_recursive_choices) |
|
287 | 287 | chained_validators = [v.ValidPerms(type_='repo_group')] |
|
288 | 288 | return _RepoGroupPermsForm |
|
289 | 289 | |
|
290 | 290 | |
|
291 | 291 | def UserGroupPermsForm(): |
|
292 | 292 | class _UserPermsForm(formencode.Schema): |
|
293 | 293 | allow_extra_fields = True |
|
294 | 294 | filter_extra_fields = False |
|
295 | 295 | chained_validators = [v.ValidPerms(type_='user_group')] |
|
296 | 296 | return _UserPermsForm |
|
297 | 297 | |
|
298 | 298 | |
|
299 | 299 | def RepoFieldForm(): |
|
300 | 300 | class _RepoFieldForm(formencode.Schema): |
|
301 | 301 | filter_extra_fields = True |
|
302 | 302 | allow_extra_fields = True |
|
303 | 303 | |
|
304 | 304 | new_field_key = All(v.FieldKey(), |
|
305 | 305 | v.UnicodeString(strip=True, min=3, not_empty=True)) |
|
306 | 306 | new_field_value = v.UnicodeString(not_empty=False, if_missing=u'') |
|
307 | 307 | new_field_type = v.OneOf(['str', 'unicode', 'list', 'tuple'], |
|
308 | 308 | if_missing='str') |
|
309 | 309 | new_field_label = v.UnicodeString(not_empty=False) |
|
310 | 310 | new_field_desc = v.UnicodeString(not_empty=False) |
|
311 | 311 | |
|
312 | 312 | return _RepoFieldForm |
|
313 | 313 | |
|
314 | 314 | |
|
315 | 315 | def RepoForkForm(edit=False, old_data={}, supported_backends=BACKENDS.keys(), |
|
316 | 316 | repo_groups=[], landing_revs=[]): |
|
317 | 317 | class _RepoForkForm(formencode.Schema): |
|
318 | 318 | allow_extra_fields = True |
|
319 | 319 | filter_extra_fields = False |
|
320 | 320 | repo_name = All(v.UnicodeString(strip=True, min=1, not_empty=True), |
|
321 | 321 | v.SlugifyName()) |
|
322 | 322 | repo_group = All(v.CanWriteGroup(), |
|
323 | 323 | v.OneOf(repo_groups, hideList=True)) |
|
324 | 324 | repo_type = All(v.ValidForkType(old_data), v.OneOf(supported_backends)) |
|
325 | 325 | description = v.UnicodeString(strip=True, min=1, not_empty=True) |
|
326 | 326 | private = v.StringBoolean(if_missing=False) |
|
327 | 327 | copy_permissions = v.StringBoolean(if_missing=False) |
|
328 | 328 | fork_parent_id = v.UnicodeString() |
|
329 | 329 | chained_validators = [v.ValidForkName(edit, old_data)] |
|
330 | 330 | landing_rev = v.OneOf(landing_revs, hideList=True) |
|
331 | 331 | |
|
332 | 332 | return _RepoForkForm |
|
333 | 333 | |
|
334 | 334 | |
|
335 | 335 | def ApplicationSettingsForm(): |
|
336 | 336 | class _ApplicationSettingsForm(formencode.Schema): |
|
337 | 337 | allow_extra_fields = True |
|
338 | 338 | filter_extra_fields = False |
|
339 | 339 | rhodecode_title = v.UnicodeString(strip=True, max=40, not_empty=False) |
|
340 | 340 | rhodecode_realm = v.UnicodeString(strip=True, min=1, not_empty=True) |
|
341 | 341 | rhodecode_pre_code = v.UnicodeString(strip=True, min=1, not_empty=False) |
|
342 | 342 | rhodecode_post_code = v.UnicodeString(strip=True, min=1, not_empty=False) |
|
343 | 343 | rhodecode_captcha_public_key = v.UnicodeString(strip=True, min=1, not_empty=False) |
|
344 | 344 | rhodecode_captcha_private_key = v.UnicodeString(strip=True, min=1, not_empty=False) |
|
345 | 345 | |
|
346 | 346 | return _ApplicationSettingsForm |
|
347 | 347 | |
|
348 | 348 | |
|
349 | 349 | def ApplicationVisualisationForm(): |
|
350 | 350 | class _ApplicationVisualisationForm(formencode.Schema): |
|
351 | 351 | allow_extra_fields = True |
|
352 | 352 | filter_extra_fields = False |
|
353 | 353 | rhodecode_show_public_icon = v.StringBoolean(if_missing=False) |
|
354 | 354 | rhodecode_show_private_icon = v.StringBoolean(if_missing=False) |
|
355 | 355 | rhodecode_stylify_metatags = v.StringBoolean(if_missing=False) |
|
356 | 356 | |
|
357 | 357 | rhodecode_repository_fields = v.StringBoolean(if_missing=False) |
|
358 | 358 | rhodecode_lightweight_journal = v.StringBoolean(if_missing=False) |
|
359 | 359 | rhodecode_dashboard_items = v.Int(min=5, not_empty=True) |
|
360 | 360 | rhodecode_admin_grid_items = v.Int(min=5, not_empty=True) |
|
361 | 361 | rhodecode_show_version = v.StringBoolean(if_missing=False) |
|
362 | 362 | rhodecode_use_gravatar = v.StringBoolean(if_missing=False) |
|
363 | 363 | rhodecode_markup_renderer = v.OneOf(['markdown', 'rst']) |
|
364 | 364 | rhodecode_gravatar_url = v.UnicodeString(min=3) |
|
365 | 365 | rhodecode_clone_uri_tmpl = v.UnicodeString(min=3) |
|
366 | 366 | rhodecode_support_url = v.UnicodeString() |
|
367 | 367 | rhodecode_show_revision_number = v.StringBoolean(if_missing=False) |
|
368 | 368 | rhodecode_show_sha_length = v.Int(min=4, not_empty=True) |
|
369 | 369 | |
|
370 | 370 | return _ApplicationVisualisationForm |
|
371 | 371 | |
|
372 | 372 | |
|
373 | 373 | class _BaseVcsSettingsForm(formencode.Schema): |
|
374 | 374 | allow_extra_fields = True |
|
375 | 375 | filter_extra_fields = False |
|
376 | 376 | hooks_changegroup_repo_size = v.StringBoolean(if_missing=False) |
|
377 | 377 | hooks_changegroup_push_logger = v.StringBoolean(if_missing=False) |
|
378 | 378 | hooks_outgoing_pull_logger = v.StringBoolean(if_missing=False) |
|
379 | 379 | |
|
380 | 380 | extensions_largefiles = v.StringBoolean(if_missing=False) |
|
381 | 381 | phases_publish = v.StringBoolean(if_missing=False) |
|
382 | 382 | |
|
383 | 383 | rhodecode_pr_merge_enabled = v.StringBoolean(if_missing=False) |
|
384 | 384 | rhodecode_use_outdated_comments = v.StringBoolean(if_missing=False) |
|
385 | rhodecode_hg_use_rebase_for_merging = v.StringBoolean(if_missing=False) | |
|
385 | 386 | |
|
386 | 387 | |
|
387 | 388 | def ApplicationUiSettingsForm(): |
|
388 | 389 | class _ApplicationUiSettingsForm(_BaseVcsSettingsForm): |
|
389 | 390 | web_push_ssl = v.StringBoolean(if_missing=False) |
|
390 | 391 | paths_root_path = All( |
|
391 | 392 | v.ValidPath(), |
|
392 | 393 | v.UnicodeString(strip=True, min=1, not_empty=True) |
|
393 | 394 | ) |
|
394 | 395 | extensions_hgsubversion = v.StringBoolean(if_missing=False) |
|
395 | 396 | extensions_hggit = v.StringBoolean(if_missing=False) |
|
396 | 397 | new_svn_branch = v.ValidSvnPattern(section='vcs_svn_branch') |
|
397 | 398 | new_svn_tag = v.ValidSvnPattern(section='vcs_svn_tag') |
|
398 | 399 | |
|
399 | 400 | return _ApplicationUiSettingsForm |
|
400 | 401 | |
|
401 | 402 | |
|
402 | 403 | def RepoVcsSettingsForm(repo_name): |
|
403 | 404 | class _RepoVcsSettingsForm(_BaseVcsSettingsForm): |
|
404 | 405 | inherit_global_settings = v.StringBoolean(if_missing=False) |
|
405 | 406 | new_svn_branch = v.ValidSvnPattern( |
|
406 | 407 | section='vcs_svn_branch', repo_name=repo_name) |
|
407 | 408 | new_svn_tag = v.ValidSvnPattern( |
|
408 | 409 | section='vcs_svn_tag', repo_name=repo_name) |
|
409 | 410 | |
|
410 | 411 | return _RepoVcsSettingsForm |
|
411 | 412 | |
|
412 | 413 | |
|
413 | 414 | def LabsSettingsForm(): |
|
414 | 415 | class _LabSettingsForm(formencode.Schema): |
|
415 | 416 | allow_extra_fields = True |
|
416 | 417 | filter_extra_fields = False |
|
417 | 418 | |
|
418 | 419 | rhodecode_hg_use_rebase_for_merging = v.StringBoolean(if_missing=False) |
|
419 | 420 | rhodecode_proxy_subversion_http_requests = v.StringBoolean( |
|
420 | 421 | if_missing=False) |
|
421 | 422 | rhodecode_subversion_http_server_url = v.UnicodeString( |
|
422 | 423 | strip=True, if_missing=None) |
|
423 | 424 | |
|
424 | 425 | return _LabSettingsForm |
|
425 | 426 | |
|
426 | 427 | |
|
427 | 428 | def ApplicationPermissionsForm(register_choices, extern_activate_choices): |
|
428 | 429 | class _DefaultPermissionsForm(formencode.Schema): |
|
429 | 430 | allow_extra_fields = True |
|
430 | 431 | filter_extra_fields = True |
|
431 | 432 | |
|
432 | 433 | anonymous = v.StringBoolean(if_missing=False) |
|
433 | 434 | default_register = v.OneOf(register_choices) |
|
434 | 435 | default_register_message = v.UnicodeString() |
|
435 | 436 | default_extern_activate = v.OneOf(extern_activate_choices) |
|
436 | 437 | |
|
437 | 438 | return _DefaultPermissionsForm |
|
438 | 439 | |
|
439 | 440 | |
|
440 | 441 | def ObjectPermissionsForm(repo_perms_choices, group_perms_choices, |
|
441 | 442 | user_group_perms_choices): |
|
442 | 443 | class _ObjectPermissionsForm(formencode.Schema): |
|
443 | 444 | allow_extra_fields = True |
|
444 | 445 | filter_extra_fields = True |
|
445 | 446 | overwrite_default_repo = v.StringBoolean(if_missing=False) |
|
446 | 447 | overwrite_default_group = v.StringBoolean(if_missing=False) |
|
447 | 448 | overwrite_default_user_group = v.StringBoolean(if_missing=False) |
|
448 | 449 | default_repo_perm = v.OneOf(repo_perms_choices) |
|
449 | 450 | default_group_perm = v.OneOf(group_perms_choices) |
|
450 | 451 | default_user_group_perm = v.OneOf(user_group_perms_choices) |
|
451 | 452 | |
|
452 | 453 | return _ObjectPermissionsForm |
|
453 | 454 | |
|
454 | 455 | |
|
455 | 456 | def UserPermissionsForm(create_choices, create_on_write_choices, |
|
456 | 457 | repo_group_create_choices, user_group_create_choices, |
|
457 | 458 | fork_choices, inherit_default_permissions_choices): |
|
458 | 459 | class _DefaultPermissionsForm(formencode.Schema): |
|
459 | 460 | allow_extra_fields = True |
|
460 | 461 | filter_extra_fields = True |
|
461 | 462 | |
|
462 | 463 | anonymous = v.StringBoolean(if_missing=False) |
|
463 | 464 | |
|
464 | 465 | default_repo_create = v.OneOf(create_choices) |
|
465 | 466 | default_repo_create_on_write = v.OneOf(create_on_write_choices) |
|
466 | 467 | default_user_group_create = v.OneOf(user_group_create_choices) |
|
467 | 468 | default_repo_group_create = v.OneOf(repo_group_create_choices) |
|
468 | 469 | default_fork_create = v.OneOf(fork_choices) |
|
469 | 470 | default_inherit_default_permissions = v.OneOf(inherit_default_permissions_choices) |
|
470 | 471 | |
|
471 | 472 | return _DefaultPermissionsForm |
|
472 | 473 | |
|
473 | 474 | |
|
474 | 475 | def UserIndividualPermissionsForm(): |
|
475 | 476 | class _DefaultPermissionsForm(formencode.Schema): |
|
476 | 477 | allow_extra_fields = True |
|
477 | 478 | filter_extra_fields = True |
|
478 | 479 | |
|
479 | 480 | inherit_default_permissions = v.StringBoolean(if_missing=False) |
|
480 | 481 | |
|
481 | 482 | return _DefaultPermissionsForm |
|
482 | 483 | |
|
483 | 484 | |
|
484 | 485 | def DefaultsForm(edit=False, old_data={}, supported_backends=BACKENDS.keys()): |
|
485 | 486 | class _DefaultsForm(formencode.Schema): |
|
486 | 487 | allow_extra_fields = True |
|
487 | 488 | filter_extra_fields = True |
|
488 | 489 | default_repo_type = v.OneOf(supported_backends) |
|
489 | 490 | default_repo_private = v.StringBoolean(if_missing=False) |
|
490 | 491 | default_repo_enable_statistics = v.StringBoolean(if_missing=False) |
|
491 | 492 | default_repo_enable_downloads = v.StringBoolean(if_missing=False) |
|
492 | 493 | default_repo_enable_locking = v.StringBoolean(if_missing=False) |
|
493 | 494 | |
|
494 | 495 | return _DefaultsForm |
|
495 | 496 | |
|
496 | 497 | |
|
497 | 498 | def AuthSettingsForm(): |
|
498 | 499 | class _AuthSettingsForm(formencode.Schema): |
|
499 | 500 | allow_extra_fields = True |
|
500 | 501 | filter_extra_fields = True |
|
501 | 502 | auth_plugins = All(v.ValidAuthPlugins(), |
|
502 | 503 | v.UniqueListFromString()(not_empty=True)) |
|
503 | 504 | |
|
504 | 505 | return _AuthSettingsForm |
|
505 | 506 | |
|
506 | 507 | |
|
507 | 508 | def UserExtraEmailForm(): |
|
508 | 509 | class _UserExtraEmailForm(formencode.Schema): |
|
509 | 510 | email = All(v.UniqSystemEmail(), v.Email(not_empty=True)) |
|
510 | 511 | return _UserExtraEmailForm |
|
511 | 512 | |
|
512 | 513 | |
|
513 | 514 | def UserExtraIpForm(): |
|
514 | 515 | class _UserExtraIpForm(formencode.Schema): |
|
515 | 516 | ip = v.ValidIp()(not_empty=True) |
|
516 | 517 | return _UserExtraIpForm |
|
517 | 518 | |
|
518 | 519 | |
|
519 | 520 | def PullRequestForm(repo_id): |
|
520 | 521 | class _PullRequestForm(formencode.Schema): |
|
521 | 522 | allow_extra_fields = True |
|
522 | 523 | filter_extra_fields = True |
|
523 | 524 | |
|
524 | 525 | user = v.UnicodeString(strip=True, required=True) |
|
525 | 526 | source_repo = v.UnicodeString(strip=True, required=True) |
|
526 | 527 | source_ref = v.UnicodeString(strip=True, required=True) |
|
527 | 528 | target_repo = v.UnicodeString(strip=True, required=True) |
|
528 | 529 | target_ref = v.UnicodeString(strip=True, required=True) |
|
529 | 530 | revisions = All(#v.NotReviewedRevisions(repo_id)(), |
|
530 | 531 | v.UniqueList()(not_empty=True)) |
|
531 | 532 | review_members = v.UniqueList(convert=int)(not_empty=True) |
|
532 | 533 | |
|
533 | 534 | pullrequest_title = v.UnicodeString(strip=True, required=True) |
|
534 | 535 | pullrequest_desc = v.UnicodeString(strip=True, required=False) |
|
535 | 536 | |
|
536 | 537 | return _PullRequestForm |
|
537 | 538 | |
|
538 | 539 | |
|
539 | 540 | def GistForm(lifetime_options, acl_level_options): |
|
540 | 541 | class _GistForm(formencode.Schema): |
|
541 | 542 | |
|
542 | 543 | gistid = All(v.UniqGistId(), v.UnicodeString(strip=True, min=3, not_empty=False, if_missing=None)) |
|
543 | 544 | filename = All(v.BasePath()(), |
|
544 | 545 | v.UnicodeString(strip=True, required=False)) |
|
545 | 546 | description = v.UnicodeString(required=False, if_missing=u'') |
|
546 | 547 | lifetime = v.OneOf(lifetime_options) |
|
547 | 548 | mimetype = v.UnicodeString(required=False, if_missing=None) |
|
548 | 549 | content = v.UnicodeString(required=True, not_empty=True) |
|
549 | 550 | public = v.UnicodeString(required=False, if_missing=u'') |
|
550 | 551 | private = v.UnicodeString(required=False, if_missing=u'') |
|
551 | 552 | acl_level = v.OneOf(acl_level_options) |
|
552 | 553 | |
|
553 | 554 | return _GistForm |
|
554 | 555 | |
|
555 | 556 | |
|
556 | 557 | def IssueTrackerPatternsForm(): |
|
557 | 558 | class _IssueTrackerPatternsForm(formencode.Schema): |
|
558 | 559 | allow_extra_fields = True |
|
559 | 560 | filter_extra_fields = False |
|
560 | 561 | chained_validators = [v.ValidPattern()] |
|
561 | 562 | return _IssueTrackerPatternsForm |
@@ -1,696 +1,698 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 | 26 | from rhodecode.lib import caches |
|
27 | 27 | from rhodecode.lib.caching_query import FromCache |
|
28 | 28 | from rhodecode.lib.utils2 import ( |
|
29 | 29 | Optional, AttributeDict, safe_str, remove_prefix, str2bool) |
|
30 | 30 | from rhodecode.model import BaseModel |
|
31 | 31 | from rhodecode.model.db import ( |
|
32 | 32 | RepoRhodeCodeUi, RepoRhodeCodeSetting, RhodeCodeUi, RhodeCodeSetting) |
|
33 | 33 | from rhodecode.model.meta import Session |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | log = logging.getLogger(__name__) |
|
37 | 37 | |
|
38 | 38 | |
|
39 | 39 | UiSetting = namedtuple( |
|
40 | 40 | 'UiSetting', ['section', 'key', 'value', 'active']) |
|
41 | 41 | |
|
42 | 42 | SOCIAL_PLUGINS_LIST = ['github', 'bitbucket', 'twitter', 'google'] |
|
43 | 43 | |
|
44 | 44 | |
|
45 | 45 | class SettingNotFound(Exception): |
|
46 | 46 | def __init__(self): |
|
47 | 47 | super(SettingNotFound, self).__init__('Setting is not found') |
|
48 | 48 | |
|
49 | 49 | |
|
50 | 50 | class SettingsModel(BaseModel): |
|
51 | 51 | BUILTIN_HOOKS = ( |
|
52 | 52 | RhodeCodeUi.HOOK_REPO_SIZE, RhodeCodeUi.HOOK_PUSH, |
|
53 | 53 | RhodeCodeUi.HOOK_PRE_PUSH, RhodeCodeUi.HOOK_PULL, |
|
54 | 54 | RhodeCodeUi.HOOK_PRE_PULL) |
|
55 | 55 | HOOKS_SECTION = 'hooks' |
|
56 | 56 | |
|
57 | 57 | def __init__(self, sa=None, repo=None): |
|
58 | 58 | self.repo = repo |
|
59 | 59 | self.UiDbModel = RepoRhodeCodeUi if repo else RhodeCodeUi |
|
60 | 60 | self.SettingsDbModel = ( |
|
61 | 61 | RepoRhodeCodeSetting if repo else RhodeCodeSetting) |
|
62 | 62 | super(SettingsModel, self).__init__(sa) |
|
63 | 63 | |
|
64 | 64 | def get_ui_by_key(self, key): |
|
65 | 65 | q = self.UiDbModel.query() |
|
66 | 66 | q = q.filter(self.UiDbModel.ui_key == key) |
|
67 | 67 | q = self._filter_by_repo(RepoRhodeCodeUi, q) |
|
68 | 68 | return q.scalar() |
|
69 | 69 | |
|
70 | 70 | def get_ui_by_section(self, section): |
|
71 | 71 | q = self.UiDbModel.query() |
|
72 | 72 | q = q.filter(self.UiDbModel.ui_section == section) |
|
73 | 73 | q = self._filter_by_repo(RepoRhodeCodeUi, q) |
|
74 | 74 | return q.all() |
|
75 | 75 | |
|
76 | 76 | def get_ui_by_section_and_key(self, section, key): |
|
77 | 77 | q = self.UiDbModel.query() |
|
78 | 78 | q = q.filter(self.UiDbModel.ui_section == section) |
|
79 | 79 | q = q.filter(self.UiDbModel.ui_key == key) |
|
80 | 80 | q = self._filter_by_repo(RepoRhodeCodeUi, q) |
|
81 | 81 | return q.scalar() |
|
82 | 82 | |
|
83 | 83 | def get_ui(self, section=None, key=None): |
|
84 | 84 | q = self.UiDbModel.query() |
|
85 | 85 | q = self._filter_by_repo(RepoRhodeCodeUi, q) |
|
86 | 86 | |
|
87 | 87 | if section: |
|
88 | 88 | q = q.filter(self.UiDbModel.ui_section == section) |
|
89 | 89 | if key: |
|
90 | 90 | q = q.filter(self.UiDbModel.ui_key == key) |
|
91 | 91 | |
|
92 | 92 | # TODO: mikhail: add caching |
|
93 | 93 | result = [ |
|
94 | 94 | UiSetting( |
|
95 | 95 | section=safe_str(r.ui_section), key=safe_str(r.ui_key), |
|
96 | 96 | value=safe_str(r.ui_value), active=r.ui_active |
|
97 | 97 | ) |
|
98 | 98 | for r in q.all() |
|
99 | 99 | ] |
|
100 | 100 | return result |
|
101 | 101 | |
|
102 | 102 | def get_builtin_hooks(self): |
|
103 | 103 | q = self.UiDbModel.query() |
|
104 | 104 | q = q.filter(self.UiDbModel.ui_key.in_(self.BUILTIN_HOOKS)) |
|
105 | 105 | return self._get_hooks(q) |
|
106 | 106 | |
|
107 | 107 | def get_custom_hooks(self): |
|
108 | 108 | q = self.UiDbModel.query() |
|
109 | 109 | q = q.filter(~self.UiDbModel.ui_key.in_(self.BUILTIN_HOOKS)) |
|
110 | 110 | return self._get_hooks(q) |
|
111 | 111 | |
|
112 | 112 | def create_ui_section_value(self, section, val, key=None, active=True): |
|
113 | 113 | new_ui = self.UiDbModel() |
|
114 | 114 | new_ui.ui_section = section |
|
115 | 115 | new_ui.ui_value = val |
|
116 | 116 | new_ui.ui_active = active |
|
117 | 117 | |
|
118 | 118 | if self.repo: |
|
119 | 119 | repo = self._get_repo(self.repo) |
|
120 | 120 | repository_id = repo.repo_id |
|
121 | 121 | new_ui.repository_id = repository_id |
|
122 | 122 | |
|
123 | 123 | if not key: |
|
124 | 124 | # keys are unique so they need appended info |
|
125 | 125 | if self.repo: |
|
126 | 126 | key = hashlib.sha1( |
|
127 | 127 | '{}{}{}'.format(section, val, repository_id)).hexdigest() |
|
128 | 128 | else: |
|
129 | 129 | key = hashlib.sha1('{}{}'.format(section, val)).hexdigest() |
|
130 | 130 | |
|
131 | 131 | new_ui.ui_key = key |
|
132 | 132 | |
|
133 | 133 | Session().add(new_ui) |
|
134 | 134 | return new_ui |
|
135 | 135 | |
|
136 | 136 | def create_or_update_hook(self, key, value): |
|
137 | 137 | ui = ( |
|
138 | 138 | self.get_ui_by_section_and_key(self.HOOKS_SECTION, key) or |
|
139 | 139 | self.UiDbModel()) |
|
140 | 140 | ui.ui_section = self.HOOKS_SECTION |
|
141 | 141 | ui.ui_active = True |
|
142 | 142 | ui.ui_key = key |
|
143 | 143 | ui.ui_value = value |
|
144 | 144 | |
|
145 | 145 | if self.repo: |
|
146 | 146 | repo = self._get_repo(self.repo) |
|
147 | 147 | repository_id = repo.repo_id |
|
148 | 148 | ui.repository_id = repository_id |
|
149 | 149 | |
|
150 | 150 | Session().add(ui) |
|
151 | 151 | return ui |
|
152 | 152 | |
|
153 | 153 | def delete_ui(self, id_): |
|
154 | 154 | ui = self.UiDbModel.get(id_) |
|
155 | 155 | if not ui: |
|
156 | 156 | raise SettingNotFound() |
|
157 | 157 | Session().delete(ui) |
|
158 | 158 | |
|
159 | 159 | def get_setting_by_name(self, name): |
|
160 | 160 | q = self._get_settings_query() |
|
161 | 161 | q = q.filter(self.SettingsDbModel.app_settings_name == name) |
|
162 | 162 | return q.scalar() |
|
163 | 163 | |
|
164 | 164 | def create_or_update_setting( |
|
165 | 165 | self, name, val=Optional(''), type_=Optional('unicode')): |
|
166 | 166 | """ |
|
167 | 167 | Creates or updates RhodeCode setting. If updates is triggered it will |
|
168 | 168 | only update parameters that are explicityl set Optional instance will |
|
169 | 169 | be skipped |
|
170 | 170 | |
|
171 | 171 | :param name: |
|
172 | 172 | :param val: |
|
173 | 173 | :param type_: |
|
174 | 174 | :return: |
|
175 | 175 | """ |
|
176 | 176 | |
|
177 | 177 | res = self.get_setting_by_name(name) |
|
178 | 178 | repo = self._get_repo(self.repo) if self.repo else None |
|
179 | 179 | |
|
180 | 180 | if not res: |
|
181 | 181 | val = Optional.extract(val) |
|
182 | 182 | type_ = Optional.extract(type_) |
|
183 | 183 | |
|
184 | 184 | args = ( |
|
185 | 185 | (repo.repo_id, name, val, type_) |
|
186 | 186 | if repo else (name, val, type_)) |
|
187 | 187 | res = self.SettingsDbModel(*args) |
|
188 | 188 | |
|
189 | 189 | else: |
|
190 | 190 | if self.repo: |
|
191 | 191 | res.repository_id = repo.repo_id |
|
192 | 192 | |
|
193 | 193 | res.app_settings_name = name |
|
194 | 194 | if not isinstance(type_, Optional): |
|
195 | 195 | # update if set |
|
196 | 196 | res.app_settings_type = type_ |
|
197 | 197 | if not isinstance(val, Optional): |
|
198 | 198 | # update if set |
|
199 | 199 | res.app_settings_value = val |
|
200 | 200 | |
|
201 | 201 | Session.add(res) |
|
202 | 202 | return res |
|
203 | 203 | |
|
204 | 204 | def invalidate_settings_cache(self): |
|
205 | 205 | namespace = 'rhodecode_settings' |
|
206 | 206 | cache_manager = caches.get_cache_manager('sql_cache_short', namespace) |
|
207 | 207 | caches.clear_cache_manager(cache_manager) |
|
208 | 208 | |
|
209 | 209 | def get_all_settings(self, cache=False): |
|
210 | 210 | def _compute(): |
|
211 | 211 | q = self._get_settings_query() |
|
212 | 212 | if not q: |
|
213 | 213 | raise Exception('Could not get application settings !') |
|
214 | 214 | |
|
215 | 215 | settings = { |
|
216 | 216 | 'rhodecode_' + result.app_settings_name: result.app_settings_value |
|
217 | 217 | for result in q |
|
218 | 218 | } |
|
219 | 219 | return settings |
|
220 | 220 | |
|
221 | 221 | if cache: |
|
222 | 222 | log.debug('Fetching app settings using cache') |
|
223 | 223 | repo = self._get_repo(self.repo) if self.repo else None |
|
224 | 224 | namespace = 'rhodecode_settings' |
|
225 | 225 | cache_manager = caches.get_cache_manager( |
|
226 | 226 | 'sql_cache_short', namespace) |
|
227 | 227 | _cache_key = ( |
|
228 | 228 | "get_repo_{}_settings".format(repo.repo_id) |
|
229 | 229 | if repo else "get_app_settings") |
|
230 | 230 | |
|
231 | 231 | return cache_manager.get(_cache_key, createfunc=_compute) |
|
232 | 232 | |
|
233 | 233 | else: |
|
234 | 234 | return _compute() |
|
235 | 235 | |
|
236 | 236 | def get_auth_settings(self): |
|
237 | 237 | q = self._get_settings_query() |
|
238 | 238 | q = q.filter( |
|
239 | 239 | self.SettingsDbModel.app_settings_name.startswith('auth_')) |
|
240 | 240 | rows = q.all() |
|
241 | 241 | auth_settings = { |
|
242 | 242 | row.app_settings_name: row.app_settings_value for row in rows} |
|
243 | 243 | return auth_settings |
|
244 | 244 | |
|
245 | 245 | def get_auth_plugins(self): |
|
246 | 246 | auth_plugins = self.get_setting_by_name("auth_plugins") |
|
247 | 247 | return auth_plugins.app_settings_value |
|
248 | 248 | |
|
249 | 249 | def get_default_repo_settings(self, strip_prefix=False): |
|
250 | 250 | q = self._get_settings_query() |
|
251 | 251 | q = q.filter( |
|
252 | 252 | self.SettingsDbModel.app_settings_name.startswith('default_')) |
|
253 | 253 | rows = q.all() |
|
254 | 254 | |
|
255 | 255 | result = {} |
|
256 | 256 | for row in rows: |
|
257 | 257 | key = row.app_settings_name |
|
258 | 258 | if strip_prefix: |
|
259 | 259 | key = remove_prefix(key, prefix='default_') |
|
260 | 260 | result.update({key: row.app_settings_value}) |
|
261 | 261 | return result |
|
262 | 262 | |
|
263 | 263 | def get_repo(self): |
|
264 | 264 | repo = self._get_repo(self.repo) |
|
265 | 265 | if not repo: |
|
266 | 266 | raise Exception( |
|
267 | 267 | 'Repository {} cannot be found'.format(self.repo)) |
|
268 | 268 | return repo |
|
269 | 269 | |
|
270 | 270 | def _filter_by_repo(self, model, query): |
|
271 | 271 | if self.repo: |
|
272 | 272 | repo = self.get_repo() |
|
273 | 273 | query = query.filter(model.repository_id == repo.repo_id) |
|
274 | 274 | return query |
|
275 | 275 | |
|
276 | 276 | def _get_hooks(self, query): |
|
277 | 277 | query = query.filter(self.UiDbModel.ui_section == self.HOOKS_SECTION) |
|
278 | 278 | query = self._filter_by_repo(RepoRhodeCodeUi, query) |
|
279 | 279 | return query.all() |
|
280 | 280 | |
|
281 | 281 | def _get_settings_query(self): |
|
282 | 282 | q = self.SettingsDbModel.query() |
|
283 | 283 | return self._filter_by_repo(RepoRhodeCodeSetting, q) |
|
284 | 284 | |
|
285 | 285 | def list_enabled_social_plugins(self, settings): |
|
286 | 286 | enabled = [] |
|
287 | 287 | for plug in SOCIAL_PLUGINS_LIST: |
|
288 | 288 | if str2bool(settings.get('rhodecode_auth_{}_enabled'.format(plug) |
|
289 | 289 | )): |
|
290 | 290 | enabled.append(plug) |
|
291 | 291 | return enabled |
|
292 | 292 | |
|
293 | 293 | |
|
294 | 294 | def assert_repo_settings(func): |
|
295 | 295 | @wraps(func) |
|
296 | 296 | def _wrapper(self, *args, **kwargs): |
|
297 | 297 | if not self.repo_settings: |
|
298 | 298 | raise Exception('Repository is not specified') |
|
299 | 299 | return func(self, *args, **kwargs) |
|
300 | 300 | return _wrapper |
|
301 | 301 | |
|
302 | 302 | |
|
303 | 303 | class IssueTrackerSettingsModel(object): |
|
304 | 304 | INHERIT_SETTINGS = 'inherit_issue_tracker_settings' |
|
305 | 305 | SETTINGS_PREFIX = 'issuetracker_' |
|
306 | 306 | |
|
307 | 307 | def __init__(self, sa=None, repo=None): |
|
308 | 308 | self.global_settings = SettingsModel(sa=sa) |
|
309 | 309 | self.repo_settings = SettingsModel(sa=sa, repo=repo) if repo else None |
|
310 | 310 | |
|
311 | 311 | @property |
|
312 | 312 | def inherit_global_settings(self): |
|
313 | 313 | if not self.repo_settings: |
|
314 | 314 | return True |
|
315 | 315 | setting = self.repo_settings.get_setting_by_name(self.INHERIT_SETTINGS) |
|
316 | 316 | return setting.app_settings_value if setting else True |
|
317 | 317 | |
|
318 | 318 | @inherit_global_settings.setter |
|
319 | 319 | def inherit_global_settings(self, value): |
|
320 | 320 | if self.repo_settings: |
|
321 | 321 | settings = self.repo_settings.create_or_update_setting( |
|
322 | 322 | self.INHERIT_SETTINGS, value, type_='bool') |
|
323 | 323 | Session().add(settings) |
|
324 | 324 | |
|
325 | 325 | def _get_keyname(self, key, uid, prefix=''): |
|
326 | 326 | return '{0}{1}{2}_{3}'.format( |
|
327 | 327 | prefix, self.SETTINGS_PREFIX, key, uid) |
|
328 | 328 | |
|
329 | 329 | def _make_dict_for_settings(self, qs): |
|
330 | 330 | prefix_match = self._get_keyname('pat', '', 'rhodecode_') |
|
331 | 331 | |
|
332 | 332 | issuetracker_entries = {} |
|
333 | 333 | # create keys |
|
334 | 334 | for k, v in qs.items(): |
|
335 | 335 | if k.startswith(prefix_match): |
|
336 | 336 | uid = k[len(prefix_match):] |
|
337 | 337 | issuetracker_entries[uid] = None |
|
338 | 338 | |
|
339 | 339 | # populate |
|
340 | 340 | for uid in issuetracker_entries: |
|
341 | 341 | issuetracker_entries[uid] = AttributeDict({ |
|
342 | 342 | 'pat': qs.get(self._get_keyname('pat', uid, 'rhodecode_')), |
|
343 | 343 | 'url': qs.get(self._get_keyname('url', uid, 'rhodecode_')), |
|
344 | 344 | 'pref': qs.get(self._get_keyname('pref', uid, 'rhodecode_')), |
|
345 | 345 | 'desc': qs.get(self._get_keyname('desc', uid, 'rhodecode_')), |
|
346 | 346 | }) |
|
347 | 347 | return issuetracker_entries |
|
348 | 348 | |
|
349 | 349 | def get_global_settings(self, cache=False): |
|
350 | 350 | """ |
|
351 | 351 | Returns list of global issue tracker settings |
|
352 | 352 | """ |
|
353 | 353 | defaults = self.global_settings.get_all_settings(cache=cache) |
|
354 | 354 | settings = self._make_dict_for_settings(defaults) |
|
355 | 355 | return settings |
|
356 | 356 | |
|
357 | 357 | def get_repo_settings(self, cache=False): |
|
358 | 358 | """ |
|
359 | 359 | Returns list of issue tracker settings per repository |
|
360 | 360 | """ |
|
361 | 361 | if not self.repo_settings: |
|
362 | 362 | raise Exception('Repository is not specified') |
|
363 | 363 | all_settings = self.repo_settings.get_all_settings(cache=cache) |
|
364 | 364 | settings = self._make_dict_for_settings(all_settings) |
|
365 | 365 | return settings |
|
366 | 366 | |
|
367 | 367 | def get_settings(self, cache=False): |
|
368 | 368 | if self.inherit_global_settings: |
|
369 | 369 | return self.get_global_settings(cache=cache) |
|
370 | 370 | else: |
|
371 | 371 | return self.get_repo_settings(cache=cache) |
|
372 | 372 | |
|
373 | 373 | def delete_entries(self, uid): |
|
374 | 374 | if self.repo_settings: |
|
375 | 375 | all_patterns = self.get_repo_settings() |
|
376 | 376 | settings_model = self.repo_settings |
|
377 | 377 | else: |
|
378 | 378 | all_patterns = self.get_global_settings() |
|
379 | 379 | settings_model = self.global_settings |
|
380 | 380 | entries = all_patterns.get(uid) |
|
381 | 381 | |
|
382 | 382 | for del_key in entries: |
|
383 | 383 | setting_name = self._get_keyname(del_key, uid) |
|
384 | 384 | entry = settings_model.get_setting_by_name(setting_name) |
|
385 | 385 | if entry: |
|
386 | 386 | Session().delete(entry) |
|
387 | 387 | |
|
388 | 388 | Session().commit() |
|
389 | 389 | |
|
390 | 390 | def create_or_update_setting( |
|
391 | 391 | self, name, val=Optional(''), type_=Optional('unicode')): |
|
392 | 392 | if self.repo_settings: |
|
393 | 393 | setting = self.repo_settings.create_or_update_setting( |
|
394 | 394 | name, val, type_) |
|
395 | 395 | else: |
|
396 | 396 | setting = self.global_settings.create_or_update_setting( |
|
397 | 397 | name, val, type_) |
|
398 | 398 | return setting |
|
399 | 399 | |
|
400 | 400 | |
|
401 | 401 | class VcsSettingsModel(object): |
|
402 | 402 | |
|
403 | 403 | INHERIT_SETTINGS = 'inherit_vcs_settings' |
|
404 | GENERAL_SETTINGS = ('use_outdated_comments', 'pr_merge_enabled') | |
|
404 | GENERAL_SETTINGS = ( | |
|
405 | 'use_outdated_comments', 'pr_merge_enabled', | |
|
406 | 'hg_use_rebase_for_merging') | |
|
405 | 407 | HOOKS_SETTINGS = ( |
|
406 | 408 | ('hooks', 'changegroup.repo_size'), |
|
407 | 409 | ('hooks', 'changegroup.push_logger'), |
|
408 | 410 | ('hooks', 'outgoing.pull_logger')) |
|
409 | 411 | HG_SETTINGS = ( |
|
410 | 412 | ('extensions', 'largefiles'), ('phases', 'publish')) |
|
411 | 413 | GLOBAL_HG_SETTINGS = HG_SETTINGS + (('extensions', 'hgsubversion'), ) |
|
412 | 414 | SVN_BRANCH_SECTION = 'vcs_svn_branch' |
|
413 | 415 | SVN_TAG_SECTION = 'vcs_svn_tag' |
|
414 | 416 | SSL_SETTING = ('web', 'push_ssl') |
|
415 | 417 | PATH_SETTING = ('paths', '/') |
|
416 | 418 | |
|
417 | 419 | def __init__(self, sa=None, repo=None): |
|
418 | 420 | self.global_settings = SettingsModel(sa=sa) |
|
419 | 421 | self.repo_settings = SettingsModel(sa=sa, repo=repo) if repo else None |
|
420 | 422 | self._ui_settings = self.HG_SETTINGS + self.HOOKS_SETTINGS |
|
421 | 423 | self._svn_sections = (self.SVN_BRANCH_SECTION, self.SVN_TAG_SECTION) |
|
422 | 424 | |
|
423 | 425 | @property |
|
424 | 426 | @assert_repo_settings |
|
425 | 427 | def inherit_global_settings(self): |
|
426 | 428 | setting = self.repo_settings.get_setting_by_name(self.INHERIT_SETTINGS) |
|
427 | 429 | return setting.app_settings_value if setting else True |
|
428 | 430 | |
|
429 | 431 | @inherit_global_settings.setter |
|
430 | 432 | @assert_repo_settings |
|
431 | 433 | def inherit_global_settings(self, value): |
|
432 | 434 | self.repo_settings.create_or_update_setting( |
|
433 | 435 | self.INHERIT_SETTINGS, value, type_='bool') |
|
434 | 436 | |
|
435 | 437 | def get_global_svn_branch_patterns(self): |
|
436 | 438 | return self.global_settings.get_ui_by_section(self.SVN_BRANCH_SECTION) |
|
437 | 439 | |
|
438 | 440 | @assert_repo_settings |
|
439 | 441 | def get_repo_svn_branch_patterns(self): |
|
440 | 442 | return self.repo_settings.get_ui_by_section(self.SVN_BRANCH_SECTION) |
|
441 | 443 | |
|
442 | 444 | def get_global_svn_tag_patterns(self): |
|
443 | 445 | return self.global_settings.get_ui_by_section(self.SVN_TAG_SECTION) |
|
444 | 446 | |
|
445 | 447 | @assert_repo_settings |
|
446 | 448 | def get_repo_svn_tag_patterns(self): |
|
447 | 449 | return self.repo_settings.get_ui_by_section(self.SVN_TAG_SECTION) |
|
448 | 450 | |
|
449 | 451 | def get_global_settings(self): |
|
450 | 452 | return self._collect_all_settings(global_=True) |
|
451 | 453 | |
|
452 | 454 | @assert_repo_settings |
|
453 | 455 | def get_repo_settings(self): |
|
454 | 456 | return self._collect_all_settings(global_=False) |
|
455 | 457 | |
|
456 | 458 | @assert_repo_settings |
|
457 | 459 | def create_or_update_repo_settings( |
|
458 | 460 | self, data, inherit_global_settings=False): |
|
459 | 461 | from rhodecode.model.scm import ScmModel |
|
460 | 462 | |
|
461 | 463 | self.inherit_global_settings = inherit_global_settings |
|
462 | 464 | |
|
463 | 465 | repo = self.repo_settings.get_repo() |
|
464 | 466 | if not inherit_global_settings: |
|
465 | 467 | if repo.repo_type == 'svn': |
|
466 | 468 | self.create_repo_svn_settings(data) |
|
467 | 469 | else: |
|
468 | 470 | self.create_or_update_repo_hook_settings(data) |
|
469 | 471 | self.create_or_update_repo_pr_settings(data) |
|
470 | 472 | |
|
471 | 473 | if repo.repo_type == 'hg': |
|
472 | 474 | self.create_or_update_repo_hg_settings(data) |
|
473 | 475 | |
|
474 | 476 | ScmModel().mark_for_invalidation(repo.repo_name, delete=True) |
|
475 | 477 | |
|
476 | 478 | @assert_repo_settings |
|
477 | 479 | def create_or_update_repo_hook_settings(self, data): |
|
478 | 480 | for section, key in self.HOOKS_SETTINGS: |
|
479 | 481 | data_key = self._get_form_ui_key(section, key) |
|
480 | 482 | if data_key not in data: |
|
481 | 483 | raise ValueError( |
|
482 | 484 | 'The given data does not contain {} key'.format(data_key)) |
|
483 | 485 | |
|
484 | 486 | active = data.get(data_key) |
|
485 | 487 | repo_setting = self.repo_settings.get_ui_by_section_and_key( |
|
486 | 488 | section, key) |
|
487 | 489 | if not repo_setting: |
|
488 | 490 | global_setting = self.global_settings.\ |
|
489 | 491 | get_ui_by_section_and_key(section, key) |
|
490 | 492 | self.repo_settings.create_ui_section_value( |
|
491 | 493 | section, global_setting.ui_value, key=key, active=active) |
|
492 | 494 | else: |
|
493 | 495 | repo_setting.ui_active = active |
|
494 | 496 | Session().add(repo_setting) |
|
495 | 497 | |
|
496 | 498 | def update_global_hook_settings(self, data): |
|
497 | 499 | for section, key in self.HOOKS_SETTINGS: |
|
498 | 500 | data_key = self._get_form_ui_key(section, key) |
|
499 | 501 | if data_key not in data: |
|
500 | 502 | raise ValueError( |
|
501 | 503 | 'The given data does not contain {} key'.format(data_key)) |
|
502 | 504 | active = data.get(data_key) |
|
503 | 505 | repo_setting = self.global_settings.get_ui_by_section_and_key( |
|
504 | 506 | section, key) |
|
505 | 507 | repo_setting.ui_active = active |
|
506 | 508 | Session().add(repo_setting) |
|
507 | 509 | |
|
508 | 510 | @assert_repo_settings |
|
509 | 511 | def create_or_update_repo_pr_settings(self, data): |
|
510 | 512 | return self._create_or_update_general_settings( |
|
511 | 513 | self.repo_settings, data) |
|
512 | 514 | |
|
513 | 515 | def create_or_update_global_pr_settings(self, data): |
|
514 | 516 | return self._create_or_update_general_settings( |
|
515 | 517 | self.global_settings, data) |
|
516 | 518 | |
|
517 | 519 | @assert_repo_settings |
|
518 | 520 | def create_repo_svn_settings(self, data): |
|
519 | 521 | return self._create_svn_settings(self.repo_settings, data) |
|
520 | 522 | |
|
521 | 523 | def create_global_svn_settings(self, data): |
|
522 | 524 | return self._create_svn_settings(self.global_settings, data) |
|
523 | 525 | |
|
524 | 526 | @assert_repo_settings |
|
525 | 527 | def create_or_update_repo_hg_settings(self, data): |
|
526 | 528 | largefiles, phases = self.HG_SETTINGS |
|
527 | 529 | largefiles_key, phases_key = self._get_hg_settings( |
|
528 | 530 | self.HG_SETTINGS, data) |
|
529 | 531 | self._create_or_update_ui( |
|
530 | 532 | self.repo_settings, *largefiles, value='', |
|
531 | 533 | active=data[largefiles_key]) |
|
532 | 534 | self._create_or_update_ui( |
|
533 | 535 | self.repo_settings, *phases, value=safe_str(data[phases_key])) |
|
534 | 536 | |
|
535 | 537 | def create_or_update_global_hg_settings(self, data): |
|
536 | 538 | largefiles, phases, subversion = self.GLOBAL_HG_SETTINGS |
|
537 | 539 | largefiles_key, phases_key, subversion_key = self._get_hg_settings( |
|
538 | 540 | self.GLOBAL_HG_SETTINGS, data) |
|
539 | 541 | self._create_or_update_ui( |
|
540 | 542 | self.global_settings, *largefiles, value='', |
|
541 | 543 | active=data[largefiles_key]) |
|
542 | 544 | self._create_or_update_ui( |
|
543 | 545 | self.global_settings, *phases, value=safe_str(data[phases_key])) |
|
544 | 546 | self._create_or_update_ui( |
|
545 | 547 | self.global_settings, *subversion, active=data[subversion_key]) |
|
546 | 548 | |
|
547 | 549 | def update_global_ssl_setting(self, value): |
|
548 | 550 | self._create_or_update_ui( |
|
549 | 551 | self.global_settings, *self.SSL_SETTING, value=value) |
|
550 | 552 | |
|
551 | 553 | def update_global_path_setting(self, value): |
|
552 | 554 | self._create_or_update_ui( |
|
553 | 555 | self.global_settings, *self.PATH_SETTING, value=value) |
|
554 | 556 | |
|
555 | 557 | @assert_repo_settings |
|
556 | 558 | def delete_repo_svn_pattern(self, id_): |
|
557 | 559 | self.repo_settings.delete_ui(id_) |
|
558 | 560 | |
|
559 | 561 | def delete_global_svn_pattern(self, id_): |
|
560 | 562 | self.global_settings.delete_ui(id_) |
|
561 | 563 | |
|
562 | 564 | @assert_repo_settings |
|
563 | 565 | def get_repo_ui_settings(self, section=None, key=None): |
|
564 | 566 | global_uis = self.global_settings.get_ui(section, key) |
|
565 | 567 | repo_uis = self.repo_settings.get_ui(section, key) |
|
566 | 568 | filtered_repo_uis = self._filter_ui_settings(repo_uis) |
|
567 | 569 | filtered_repo_uis_keys = [ |
|
568 | 570 | (s.section, s.key) for s in filtered_repo_uis] |
|
569 | 571 | |
|
570 | 572 | def _is_global_ui_filtered(ui): |
|
571 | 573 | return ( |
|
572 | 574 | (ui.section, ui.key) in filtered_repo_uis_keys |
|
573 | 575 | or ui.section in self._svn_sections) |
|
574 | 576 | |
|
575 | 577 | filtered_global_uis = [ |
|
576 | 578 | ui for ui in global_uis if not _is_global_ui_filtered(ui)] |
|
577 | 579 | |
|
578 | 580 | return filtered_global_uis + filtered_repo_uis |
|
579 | 581 | |
|
580 | 582 | def get_global_ui_settings(self, section=None, key=None): |
|
581 | 583 | return self.global_settings.get_ui(section, key) |
|
582 | 584 | |
|
583 | 585 | def get_ui_settings(self, section=None, key=None): |
|
584 | 586 | if not self.repo_settings or self.inherit_global_settings: |
|
585 | 587 | return self.get_global_ui_settings(section, key) |
|
586 | 588 | else: |
|
587 | 589 | return self.get_repo_ui_settings(section, key) |
|
588 | 590 | |
|
589 | 591 | def get_svn_patterns(self, section=None): |
|
590 | 592 | if not self.repo_settings: |
|
591 | 593 | return self.get_global_ui_settings(section) |
|
592 | 594 | else: |
|
593 | 595 | return self.get_repo_ui_settings(section) |
|
594 | 596 | |
|
595 | 597 | @assert_repo_settings |
|
596 | 598 | def get_repo_general_settings(self): |
|
597 | 599 | global_settings = self.global_settings.get_all_settings() |
|
598 | 600 | repo_settings = self.repo_settings.get_all_settings() |
|
599 | 601 | filtered_repo_settings = self._filter_general_settings(repo_settings) |
|
600 | 602 | global_settings.update(filtered_repo_settings) |
|
601 | 603 | return global_settings |
|
602 | 604 | |
|
603 | 605 | def get_global_general_settings(self): |
|
604 | 606 | return self.global_settings.get_all_settings() |
|
605 | 607 | |
|
606 | 608 | def get_general_settings(self): |
|
607 | 609 | if not self.repo_settings or self.inherit_global_settings: |
|
608 | 610 | return self.get_global_general_settings() |
|
609 | 611 | else: |
|
610 | 612 | return self.get_repo_general_settings() |
|
611 | 613 | |
|
612 | 614 | def get_repos_location(self): |
|
613 | 615 | return self.global_settings.get_ui_by_key('/').ui_value |
|
614 | 616 | |
|
615 | 617 | def _filter_ui_settings(self, settings): |
|
616 | 618 | filtered_settings = [ |
|
617 | 619 | s for s in settings if self._should_keep_setting(s)] |
|
618 | 620 | return filtered_settings |
|
619 | 621 | |
|
620 | 622 | def _should_keep_setting(self, setting): |
|
621 | 623 | keep = ( |
|
622 | 624 | (setting.section, setting.key) in self._ui_settings or |
|
623 | 625 | setting.section in self._svn_sections) |
|
624 | 626 | return keep |
|
625 | 627 | |
|
626 | 628 | def _filter_general_settings(self, settings): |
|
627 | 629 | keys = ['rhodecode_{}'.format(key) for key in self.GENERAL_SETTINGS] |
|
628 | 630 | return { |
|
629 | 631 | k: settings[k] |
|
630 | 632 | for k in settings if k in keys} |
|
631 | 633 | |
|
632 | 634 | def _collect_all_settings(self, global_=False): |
|
633 | 635 | settings = self.global_settings if global_ else self.repo_settings |
|
634 | 636 | result = {} |
|
635 | 637 | |
|
636 | 638 | for section, key in self._ui_settings: |
|
637 | 639 | ui = settings.get_ui_by_section_and_key(section, key) |
|
638 | 640 | result_key = self._get_form_ui_key(section, key) |
|
639 | 641 | if ui: |
|
640 | 642 | if section in ('hooks', 'extensions'): |
|
641 | 643 | result[result_key] = ui.ui_active |
|
642 | 644 | else: |
|
643 | 645 | result[result_key] = ui.ui_value |
|
644 | 646 | |
|
645 | 647 | for name in self.GENERAL_SETTINGS: |
|
646 | 648 | setting = settings.get_setting_by_name(name) |
|
647 | 649 | if setting: |
|
648 | 650 | result_key = 'rhodecode_{}'.format(name) |
|
649 | 651 | result[result_key] = setting.app_settings_value |
|
650 | 652 | |
|
651 | 653 | return result |
|
652 | 654 | |
|
653 | 655 | def _get_form_ui_key(self, section, key): |
|
654 | 656 | return '{section}_{key}'.format( |
|
655 | 657 | section=section, key=key.replace('.', '_')) |
|
656 | 658 | |
|
657 | 659 | def _create_or_update_ui( |
|
658 | 660 | self, settings, section, key, value=None, active=None): |
|
659 | 661 | ui = settings.get_ui_by_section_and_key(section, key) |
|
660 | 662 | if not ui: |
|
661 | 663 | active = True if active is None else active |
|
662 | 664 | settings.create_ui_section_value( |
|
663 | 665 | section, value, key=key, active=active) |
|
664 | 666 | else: |
|
665 | 667 | if active is not None: |
|
666 | 668 | ui.ui_active = active |
|
667 | 669 | if value is not None: |
|
668 | 670 | ui.ui_value = value |
|
669 | 671 | Session().add(ui) |
|
670 | 672 | |
|
671 | 673 | def _create_svn_settings(self, settings, data): |
|
672 | 674 | svn_settings = { |
|
673 | 675 | 'new_svn_branch': self.SVN_BRANCH_SECTION, |
|
674 | 676 | 'new_svn_tag': self.SVN_TAG_SECTION |
|
675 | 677 | } |
|
676 | 678 | for key in svn_settings: |
|
677 | 679 | if data.get(key): |
|
678 | 680 | settings.create_ui_section_value(svn_settings[key], data[key]) |
|
679 | 681 | |
|
680 | 682 | def _create_or_update_general_settings(self, settings, data): |
|
681 | 683 | for name in self.GENERAL_SETTINGS: |
|
682 | 684 | data_key = 'rhodecode_{}'.format(name) |
|
683 | 685 | if data_key not in data: |
|
684 | 686 | raise ValueError( |
|
685 | 687 | 'The given data does not contain {} key'.format(data_key)) |
|
686 | 688 | setting = settings.create_or_update_setting( |
|
687 | 689 | name, data[data_key], 'bool') |
|
688 | 690 | Session().add(setting) |
|
689 | 691 | |
|
690 | 692 | def _get_hg_settings(self, settings, data): |
|
691 | 693 | data_keys = [self._get_form_ui_key(*s) for s in settings] |
|
692 | 694 | for data_key in data_keys: |
|
693 | 695 | if data_key not in data: |
|
694 | 696 | raise ValueError( |
|
695 | 697 | 'The given data does not contain {} key'.format(data_key)) |
|
696 | 698 | return data_keys |
General Comments 0
You need to be logged in to leave comments.
Login now