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