Files @ 7e5f8c12a3fc
Branch filter:

Location: kallithea/rhodecode/controllers/admin/settings.py - annotation

Bradley M. Kuhn
First step in two-part process to rename directories to kallithea.
This first step is to change all references in the files where they refer
to the old directory name.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
389d02a5df52
a671db5bdd58
a671db5bdd58
a671db5bdd58
a671db5bdd58
6832ef664673
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
6832ef664673
1e757ac98988
a671db5bdd58
ffd45b185016
7e5f8c12a3fc
ffd45b185016
389d02a5df52
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
042d38683d42
042d38683d42
042d38683d42
042d38683d42
1e757ac98988
ffd45b185016
ffd45b185016
1e757ac98988
042d38683d42
7e5f8c12a3fc
7e5f8c12a3fc
7e5f8c12a3fc
7e5f8c12a3fc
7e5f8c12a3fc
7e5f8c12a3fc
7e5f8c12a3fc
7e5f8c12a3fc
a221706dab50
7e5f8c12a3fc
7e5f8c12a3fc
7e5f8c12a3fc
7e5f8c12a3fc
7e5f8c12a3fc
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
6832ef664673
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
08cf7741e5e1
7e536d1af60d
ffd45b185016
ffd45b185016
a801c4542f48
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
7e536d1af60d
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
7e536d1af60d
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
7e536d1af60d
1e757ac98988
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
91c442a489bb
91c442a489bb
91c442a489bb
ba7e24cd4786
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
1e757ac98988
91c442a489bb
91c442a489bb
7e536d1af60d
1e757ac98988
9bce679a3f49
9bce679a3f49
9bce679a3f49
985db7f7b9b2
985db7f7b9b2
985db7f7b9b2
985db7f7b9b2
7e536d1af60d
91c442a489bb
9bce679a3f49
8173b0cfe9bd
9bce679a3f49
7e536d1af60d
9bce679a3f49
8173b0cfe9bd
9bce679a3f49
9bce679a3f49
9bce679a3f49
8173b0cfe9bd
9bce679a3f49
7e536d1af60d
9bce679a3f49
8173b0cfe9bd
9bce679a3f49
9bce679a3f49
7e536d1af60d
9bce679a3f49
9bce679a3f49
7b0f803229be
7b0f803229be
7b0f803229be
7b0f803229be
7b0f803229be
8173b0cfe9bd
9bce679a3f49
7e536d1af60d
9bce679a3f49
7b0f803229be
7b0f803229be
7b0f803229be
7b0f803229be
7b0f803229be
7b0f803229be
8173b0cfe9bd
7676606f7622
7676606f7622
ffd45b185016
7676606f7622
7676606f7622
9bce679a3f49
9bce679a3f49
9bce679a3f49
7b0f803229be
7b0f803229be
7b0f803229be
7b0f803229be
7b0f803229be
7b0f803229be
8173b0cfe9bd
9bce679a3f49
9bce679a3f49
91c442a489bb
7e536d1af60d
9bce679a3f49
7e536d1af60d
7676606f7622
7676606f7622
7676606f7622
7676606f7622
7676606f7622
7676606f7622
91c442a489bb
91c442a489bb
a8f2d78d14ea
91c442a489bb
7e536d1af60d
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
7e5f8c12a3fc
7e5f8c12a3fc
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
9b4ba12ef8c3
9b4ba12ef8c3
9b4ba12ef8c3
ffd45b185016
ffd45b185016
ffd45b185016
9b4ba12ef8c3
9b4ba12ef8c3
9b4ba12ef8c3
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
b50348816a80
9b4ba12ef8c3
9b4ba12ef8c3
9b4ba12ef8c3
9b4ba12ef8c3
9b4ba12ef8c3
9b4ba12ef8c3
9b4ba12ef8c3
b50348816a80
9b4ba12ef8c3
9b4ba12ef8c3
9b4ba12ef8c3
9b4ba12ef8c3
9b4ba12ef8c3
9b4ba12ef8c3
9b4ba12ef8c3
b50348816a80
ffd45b185016
7e536d1af60d
ffd45b185016
ffd45b185016
1e757ac98988
b50348816a80
b50348816a80
b50348816a80
b50348816a80
ffd45b185016
ffd45b185016
b50348816a80
ffd45b185016
b50348816a80
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
4910b2607a29
ffd45b185016
ffd45b185016
4910b2607a29
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
070f32743632
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
7e536d1af60d
ffd45b185016
ffd45b185016
7e536d1af60d
7e5f8c12a3fc
7e5f8c12a3fc
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
0226b6d6b2b5
ffd45b185016
ffd45b185016
1e757ac98988
1e757ac98988
ffd45b185016
1e757ac98988
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
7e5f8c12a3fc
7e5f8c12a3fc
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
1e757ac98988
7e5f8c12a3fc
7e5f8c12a3fc
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
1e757ac98988
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
ffd45b185016
# -*- coding: utf-8 -*-
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
kallithea.controllers.admin.settings
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

settings controller for rhodecode admin

:created_on: Jul 14, 2010
:author: marcink
:copyright: (c) 2013 RhodeCode GmbH.
:license: GPLv3, see LICENSE for more details.
"""

import time
import logging
import traceback
import formencode

from formencode import htmlfill
from pylons import request, tmpl_context as c, url, config
from pylons.controllers.util import redirect
from pylons.i18n.translation import _

from kallithea.lib import helpers as h
from kallithea.lib.auth import LoginRequired, HasPermissionAllDecorator
from kallithea.lib.base import BaseController, render
from kallithea.lib.celerylib import tasks, run_task
from kallithea.lib.exceptions import HgsubversionImportError
from kallithea.lib.utils import repo2db_mapper, set_rhodecode_config
from kallithea.model.db import RhodeCodeUi, Repository, RhodeCodeSetting
from kallithea.model.forms import ApplicationSettingsForm, \
    ApplicationUiSettingsForm, ApplicationVisualisationForm
from kallithea.model.scm import ScmModel
from kallithea.model.notification import EmailNotificationModel
from kallithea.model.meta import Session
from kallithea.lib.utils2 import str2bool, safe_unicode, safe_str
from kallithea.lib.compat import json
log = logging.getLogger(__name__)


class SettingsController(BaseController):
    """REST Controller styled on the Atom Publishing Protocol"""
    # To properly map this controller, ensure your config/routing.py
    # file has a resource setup:
    #     map.resource('setting', 'settings', controller='admin/settings',
    #         path_prefix='/admin', name_prefix='admin_')

    @LoginRequired()
    def __before__(self):
        super(SettingsController, self).__before__()

    def _get_hg_ui_settings(self):
        ret = RhodeCodeUi.query().all()

        if not ret:
            raise Exception('Could not get application ui settings !')
        settings = {}
        for each in ret:
            k = each.ui_key
            v = each.ui_value
            if k == '/':
                k = 'root_path'

            if k == 'push_ssl':
                v = str2bool(v)

            if k.find('.') != -1:
                k = k.replace('.', '_')

            if each.ui_section in ['hooks', 'extensions']:
                v = each.ui_active

            settings[each.ui_section + '_' + k] = v
        return settings

    @HasPermissionAllDecorator('hg.admin')
    def settings_vcs(self):
        """GET /admin/settings: All items in the collection"""
        # url('admin_settings')
        c.active = 'vcs'
        if request.POST:
            application_form = ApplicationUiSettingsForm()()
            try:
                form_result = application_form.to_python(dict(request.POST))
            except formencode.Invalid, errors:
                return htmlfill.render(
                     render('admin/settings/settings.html'),
                     defaults=errors.value,
                     errors=errors.error_dict or {},
                     prefix_error=False,
                     encoding="UTF-8"
                )

            try:
                sett = RhodeCodeUi.get_by_key('push_ssl')
                sett.ui_value = form_result['web_push_ssl']
                Session().add(sett)
                if c.visual.allow_repo_location_change:
                    sett = RhodeCodeUi.get_by_key('/')
                    sett.ui_value = form_result['paths_root_path']
                    Session().add(sett)

                #HOOKS
                sett = RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_UPDATE)
                sett.ui_active = form_result['hooks_changegroup_update']
                Session().add(sett)

                sett = RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_REPO_SIZE)
                sett.ui_active = form_result['hooks_changegroup_repo_size']
                Session().add(sett)

                sett = RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_PUSH)
                sett.ui_active = form_result['hooks_changegroup_push_logger']
                Session().add(sett)

                sett = RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_PULL)
                sett.ui_active = form_result['hooks_outgoing_pull_logger']

                Session().add(sett)

                ## EXTENSIONS
                sett = RhodeCodeUi.get_by_key('largefiles')
                if not sett:
                    #make one if it's not there !
                    sett = RhodeCodeUi()
                    sett.ui_key = 'largefiles'
                    sett.ui_section = 'extensions'
                sett.ui_active = form_result['extensions_largefiles']
                Session().add(sett)

                sett = RhodeCodeUi.get_by_key('hgsubversion')
                if not sett:
                    #make one if it's not there !
                    sett = RhodeCodeUi()
                    sett.ui_key = 'hgsubversion'
                    sett.ui_section = 'extensions'

                sett.ui_active = form_result['extensions_hgsubversion']
                if sett.ui_active:
                    try:
                        import hgsubversion  # pragma: no cover
                    except ImportError:
                        raise HgsubversionImportError
                Session().add(sett)

#                sett = RhodeCodeUi.get_by_key('hggit')
#                if not sett:
#                    #make one if it's not there !
#                    sett = RhodeCodeUi()
#                    sett.ui_key = 'hggit'
#                    sett.ui_section = 'extensions'
#
#                sett.ui_active = form_result['extensions_hggit']
#                Session().add(sett)

                Session().commit()

                h.flash(_('Updated VCS settings'), category='success')

            except HgsubversionImportError:
                log.error(traceback.format_exc())
                h.flash(_('Unable to activate hgsubversion support. '
                          'The "hgsubversion" library is missing'),
                        category='error')

            except Exception:
                log.error(traceback.format_exc())
                h.flash(_('Error occurred during updating '
                          'application settings'), category='error')

        defaults = RhodeCodeSetting.get_app_settings()
        defaults.update(self._get_hg_ui_settings())

        return htmlfill.render(
            render('admin/settings/settings.html'),
            defaults=defaults,
            encoding="UTF-8",
            force_defaults=False)

    @HasPermissionAllDecorator('hg.admin')
    def settings_mapping(self):
        """GET /admin/settings/mapping: All items in the collection"""
        # url('admin_settings_mapping')
        c.active = 'mapping'
        if request.POST:
            rm_obsolete = request.POST.get('destroy', False)
            install_git_hooks = request.POST.get('hooks', False)
            invalidate_cache = request.POST.get('invalidate', False)
            log.debug('rescanning repo location with destroy obsolete=%s and '
                      'install git hooks=%s' % (rm_obsolete,install_git_hooks))

            if invalidate_cache:
                log.debug('invalidating all repositories cache')
                for repo in Repository.get_all():
                    ScmModel().mark_for_invalidation(repo.repo_name, delete=True)

            filesystem_repos = ScmModel().repo_scan()
            added, removed = repo2db_mapper(filesystem_repos, rm_obsolete,
                                            install_git_hook=install_git_hooks)
            _repr = lambda l: ', '.join(map(safe_unicode, l)) or '-'
            h.flash(_('Repositories successfully '
                      'rescanned added: %s ; removed: %s') %
                    (_repr(added), _repr(removed)),
                    category='success')
            return redirect(url('admin_settings_mapping'))

        defaults = RhodeCodeSetting.get_app_settings()
        defaults.update(self._get_hg_ui_settings())

        return htmlfill.render(
            render('admin/settings/settings.html'),
            defaults=defaults,
            encoding="UTF-8",
            force_defaults=False)

    @HasPermissionAllDecorator('hg.admin')
    def settings_global(self):
        """GET /admin/settings/global: All items in the collection"""
        # url('admin_settings_global')
        c.active = 'global'
        if request.POST:
            application_form = ApplicationSettingsForm()()
            try:
                form_result = application_form.to_python(dict(request.POST))
            except formencode.Invalid, errors:
                return htmlfill.render(
                    render('admin/settings/settings.html'),
                    defaults=errors.value,
                    errors=errors.error_dict or {},
                    prefix_error=False,
                    encoding="UTF-8")

            try:
                sett1 = RhodeCodeSetting.create_or_update('title',
                                            form_result['rhodecode_title'])
                Session().add(sett1)

                sett2 = RhodeCodeSetting.create_or_update('realm',
                                            form_result['rhodecode_realm'])
                Session().add(sett2)

                sett3 = RhodeCodeSetting.create_or_update('ga_code',
                                            form_result['rhodecode_ga_code'])
                Session().add(sett3)

                sett4 = RhodeCodeSetting.create_or_update('captcha_public_key',
                                    form_result['rhodecode_captcha_public_key'])
                Session().add(sett4)

                sett5 = RhodeCodeSetting.create_or_update('captcha_private_key',
                                    form_result['rhodecode_captcha_private_key'])
                Session().add(sett5)

                Session().commit()
                set_rhodecode_config(config)
                h.flash(_('Updated application settings'), category='success')

            except Exception:
                log.error(traceback.format_exc())
                h.flash(_('Error occurred during updating '
                          'application settings'),
                          category='error')

            return redirect(url('admin_settings_global'))

        defaults = RhodeCodeSetting.get_app_settings()
        defaults.update(self._get_hg_ui_settings())

        return htmlfill.render(
            render('admin/settings/settings.html'),
            defaults=defaults,
            encoding="UTF-8",
            force_defaults=False)

    @HasPermissionAllDecorator('hg.admin')
    def settings_visual(self):
        """GET /admin/settings/visual: All items in the collection"""
        # url('admin_settings_visual')
        c.active = 'visual'
        if request.POST:
            application_form = ApplicationVisualisationForm()()
            try:
                form_result = application_form.to_python(dict(request.POST))
            except formencode.Invalid, errors:
                return htmlfill.render(
                    render('admin/settings/settings.html'),
                    defaults=errors.value,
                    errors=errors.error_dict or {},
                    prefix_error=False,
                    encoding="UTF-8"
                )

            try:
                settings = [
                    ('show_public_icon', 'rhodecode_show_public_icon', 'bool'),
                    ('show_private_icon', 'rhodecode_show_private_icon', 'bool'),
                    ('stylify_metatags', 'rhodecode_stylify_metatags', 'bool'),
                    ('repository_fields', 'rhodecode_repository_fields', 'bool'),
                    ('dashboard_items', 'rhodecode_dashboard_items', 'int'),
                    ('admin_grid_items', 'rhodecode_admin_grid_items', 'int'),
                    ('show_version', 'rhodecode_show_version', 'bool'),
                    ('use_gravatar', 'rhodecode_use_gravatar', 'bool'),
                    ('gravatar_url', 'rhodecode_gravatar_url', 'unicode'),
                    ('clone_uri_tmpl', 'rhodecode_clone_uri_tmpl', 'unicode'),
                ]
                for setting, form_key, type_ in settings:
                    sett = RhodeCodeSetting.create_or_update(setting,
                                        form_result[form_key], type_)
                    Session().add(sett)

                Session().commit()
                set_rhodecode_config(config)
                h.flash(_('Updated visualisation settings'),
                        category='success')

            except Exception:
                log.error(traceback.format_exc())
                h.flash(_('Error occurred during updating '
                          'visualisation settings'),
                        category='error')

            return redirect(url('admin_settings_visual'))

        defaults = RhodeCodeSetting.get_app_settings()
        defaults.update(self._get_hg_ui_settings())

        return htmlfill.render(
            render('admin/settings/settings.html'),
            defaults=defaults,
            encoding="UTF-8",
            force_defaults=False)

    @HasPermissionAllDecorator('hg.admin')
    def settings_email(self):
        """GET /admin/settings/email: All items in the collection"""
        # url('admin_settings_email')
        c.active = 'email'
        if request.POST:
            test_email = request.POST.get('test_email')
            test_email_subj = 'RhodeCode test email'
            test_email_body = ('RhodeCode Email test, '
                               'RhodeCode version: %s' % c.rhodecode_version)
            if not test_email:
                h.flash(_('Please enter email address'), category='error')
                return redirect(url('admin_settings_email'))

            test_email_html_body = EmailNotificationModel()\
                .get_email_tmpl(EmailNotificationModel.TYPE_DEFAULT,
                                body=test_email_body)

            recipients = [test_email] if test_email else None

            run_task(tasks.send_email, recipients, test_email_subj,
                     test_email_body, test_email_html_body)

            h.flash(_('Send email task created'), category='success')
            return redirect(url('admin_settings_email'))

        defaults = RhodeCodeSetting.get_app_settings()
        defaults.update(self._get_hg_ui_settings())

        import kallithea
        c.rhodecode_ini = kallithea.CONFIG

        return htmlfill.render(
            render('admin/settings/settings.html'),
            defaults=defaults,
            encoding="UTF-8",
            force_defaults=False)

    @HasPermissionAllDecorator('hg.admin')
    def settings_hooks(self):
        """GET /admin/settings/hooks: All items in the collection"""
        # url('admin_settings_hooks')
        c.active = 'hooks'
        if request.POST:
            if c.visual.allow_custom_hooks_settings:
                ui_key = request.POST.get('new_hook_ui_key')
                ui_value = request.POST.get('new_hook_ui_value')

                hook_id = request.POST.get('hook_id')

                try:
                    if ui_value and ui_key:
                        RhodeCodeUi.create_or_update_hook(ui_key, ui_value)
                        h.flash(_('Added new hook'), category='success')
                    elif hook_id:
                        RhodeCodeUi.delete(hook_id)
                        Session().commit()

                    # check for edits
                    update = False
                    _d = request.POST.dict_of_lists()
                    for k, v in zip(_d.get('hook_ui_key', []),
                                    _d.get('hook_ui_value_new', [])):
                        RhodeCodeUi.create_or_update_hook(k, v)
                        update = True

                    if update:
                        h.flash(_('Updated hooks'), category='success')
                    Session().commit()
                except Exception:
                    log.error(traceback.format_exc())
                    h.flash(_('Error occurred during hook creation'),
                            category='error')

                return redirect(url('admin_settings_hooks'))

        defaults = RhodeCodeSetting.get_app_settings()
        defaults.update(self._get_hg_ui_settings())

        c.hooks = RhodeCodeUi.get_builtin_hooks()
        c.custom_hooks = RhodeCodeUi.get_custom_hooks()

        return htmlfill.render(
            render('admin/settings/settings.html'),
            defaults=defaults,
            encoding="UTF-8",
            force_defaults=False)

    @HasPermissionAllDecorator('hg.admin')
    def settings_search(self):
        """GET /admin/settings/search: All items in the collection"""
        # url('admin_settings_search')
        c.active = 'search'
        if request.POST:
            repo_location = self._get_hg_ui_settings()['paths_root_path']
            full_index = request.POST.get('full_index', False)
            run_task(tasks.whoosh_index, repo_location, full_index)
            h.flash(_('Whoosh reindex task scheduled'), category='success')
            return redirect(url('admin_settings_search'))

        defaults = RhodeCodeSetting.get_app_settings()
        defaults.update(self._get_hg_ui_settings())

        return htmlfill.render(
            render('admin/settings/settings.html'),
            defaults=defaults,
            encoding="UTF-8",
            force_defaults=False)

    @HasPermissionAllDecorator('hg.admin')
    def settings_system(self):
        """GET /admin/settings/system: All items in the collection"""
        # url('admin_settings_system')
        c.active = 'system'

        defaults = RhodeCodeSetting.get_app_settings()
        defaults.update(self._get_hg_ui_settings())

        import kallithea
        c.rhodecode_ini = kallithea.CONFIG
        c.rhodecode_update_url = defaults.get('rhodecode_update_url')
        server_info = RhodeCodeSetting.get_server_info()
        for key, val in server_info.iteritems():
            setattr(c, key, val)

        return htmlfill.render(
            render('admin/settings/settings.html'),
            defaults=defaults,
            encoding="UTF-8",
            force_defaults=False)

    @HasPermissionAllDecorator('hg.admin')
    def settings_system_update(self):
        """GET /admin/settings/system/updates: All items in the collection"""
        # url('admin_settings_system_update')
        import json
        import urllib2
        from kallithea.lib.verlib import NormalizedVersion
        from kallithea import __version__

        defaults = RhodeCodeSetting.get_app_settings()
        defaults.update(self._get_hg_ui_settings())
        _update_url = defaults.get('rhodecode_update_url', '')

        _err = lambda s: '<div style="color:#ff8888; padding:4px 0px">%s</div>' % (s)
        try:
            import kallithea
            ver = kallithea.__version__
            log.debug('Checking for upgrade on `%s` server' % _update_url)
            opener = urllib2.build_opener()
            opener.addheaders = [('User-agent', 'RhodeCode-SCM/%s' % ver)]
            response = opener.open(_update_url)
            response_data = response.read()
            data = json.loads(response_data)
        except urllib2.URLError, e:
            log.error(traceback.format_exc())
            return _err('Failed to contact upgrade server: %r' % e)
        except ValueError, e:
            log.error(traceback.format_exc())
            return _err('Bad data sent from update server')

        latest = data['versions'][0]

        c.update_url = _update_url
        c.latest_data = latest
        c.latest_ver = latest['version']
        c.cur_ver = __version__
        c.should_upgrade = False

        if NormalizedVersion(c.latest_ver) > NormalizedVersion(c.cur_ver):
            c.should_upgrade = True
        c.important_notices = latest['general']

        return render('admin/settings/settings_system_update.html'),