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 as errors: return htmlfill.render( render('admin/settings/settings.html'), defaults=errors.value, errors=errors.error_dict or {}, prefix_error=False, encoding="UTF-8", force_defaults=False) try: sett1 = Setting.create_or_update('title', form_result['title']) Session().add(sett1) sett2 = Setting.create_or_update('realm', form_result['realm']) Session().add(sett2) sett3 = Setting.create_or_update('ga_code', form_result['ga_code']) Session().add(sett3) sett4 = Setting.create_or_update('captcha_public_key', form_result['captcha_public_key']) Session().add(sett4) sett5 = Setting.create_or_update('captcha_private_key', form_result['captcha_private_key']) Session().add(sett5) Session().commit() set_app_settings(config) h.flash(_('Updated application settings'), category='success') except Exception: log.error(traceback.format_exc()) h.flash(_('Error occurred while updating ' 'application settings'), category='error') raise HTTPFound(location=url('admin_settings_global')) defaults = Setting.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)
def _set_settings(*kvtseq): session = Session() for kvt in kvtseq: assert len(kvt) in (2, 3) k = kvt[0] v = kvt[1] t = kvt[2] if len(kvt) == 3 else 'unicode' Setting.create_or_update(k, v, t) session.commit()
def _set_settings(*kvtseq): session = Session() for kvt in kvtseq: assert len(kvt) in (2, 3) k = kvt[0] v = kvt[1] t = kvt[2] if len(kvt) == 3 else 'unicode' Setting.create_or_update(k, v, t) session.commit()
def settings_visual(self): c.active = 'visual' if request.POST: application_form = ApplicationVisualisationForm()() try: form_result = application_form.to_python(dict(request.POST)) except formencode.Invalid as errors: return htmlfill.render( render('admin/settings/settings.html'), defaults=errors.value, errors=errors.error_dict or {}, prefix_error=False, encoding="UTF-8", force_defaults=False) try: settings = [ ('show_public_icon', 'show_public_icon', 'bool'), ('show_private_icon', 'show_private_icon', 'bool'), ('stylify_metalabels', 'stylify_metalabels', 'bool'), ('repository_fields', 'repository_fields', 'bool'), ('dashboard_items', 'dashboard_items', 'int'), ('admin_grid_items', 'admin_grid_items', 'int'), ('show_version', 'show_version', 'bool'), ('use_gravatar', 'use_gravatar', 'bool'), ('gravatar_url', 'gravatar_url', 'unicode'), ('clone_uri_tmpl', 'clone_uri_tmpl', 'unicode'), ('clone_ssh_tmpl', 'clone_ssh_tmpl', 'unicode'), ] for setting, form_key, type_ in settings: Setting.create_or_update(setting, form_result[form_key], type_) Session().commit() set_app_settings(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') raise HTTPFound(location=url('admin_settings_visual')) defaults = Setting.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)
def settings_visual(self): c.active = 'visual' if request.POST: application_form = ApplicationVisualisationForm()() try: form_result = application_form.to_python(dict(request.POST)) except formencode.Invalid as errors: return htmlfill.render( render('admin/settings/settings.html'), defaults=errors.value, errors=errors.error_dict or {}, prefix_error=False, encoding="UTF-8", force_defaults=False) try: settings = [ ('show_public_icon', 'show_public_icon', 'bool'), ('show_private_icon', 'show_private_icon', 'bool'), ('stylify_metatags', 'stylify_metatags', 'bool'), ('repository_fields', 'repository_fields', 'bool'), ('dashboard_items', 'dashboard_items', 'int'), ('admin_grid_items', 'admin_grid_items', 'int'), ('show_version', 'show_version', 'bool'), ('use_gravatar', 'use_gravatar', 'bool'), ('gravatar_url', 'gravatar_url', 'unicode'), ('clone_uri_tmpl', 'clone_uri_tmpl', 'unicode'), ] for setting, form_key, type_ in settings: Setting.create_or_update(setting, form_result[form_key], type_) Session().commit() set_app_settings(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') raise HTTPFound(location=url('admin_settings_visual')) defaults = Setting.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)
def update(self, id): _form = DefaultsForm()() try: form_result = _form.to_python(dict(request.POST)) for k, v in form_result.items(): setting = Setting.create_or_update(k, v) Session().commit() h.flash(_('Default settings updated successfully'), category='success') except formencode.Invalid as errors: defaults = errors.value return htmlfill.render( render('admin/defaults/defaults.html'), defaults=defaults, errors=errors.error_dict or {}, prefix_error=False, encoding="UTF-8", force_defaults=False) except Exception: log.error(traceback.format_exc()) h.flash(_('Error occurred during update of defaults'), category='error') raise HTTPFound(location=url('defaults'))
def update(self, id): _form = DefaultsForm()() try: form_result = _form.to_python(dict(request.POST)) for k, v in form_result.iteritems(): setting = Setting.create_or_update(k, v) Session().commit() h.flash(_('Default settings updated successfully'), category='success') except formencode.Invalid as errors: defaults = errors.value return htmlfill.render( render('admin/defaults/defaults.html'), defaults=defaults, errors=errors.error_dict or {}, prefix_error=False, encoding="UTF-8", force_defaults=False) except Exception: log.error(traceback.format_exc()) h.flash(_('Error occurred during update of defaults'), category='error') raise HTTPFound(location=url('defaults'))
def auth_settings(self): """POST create and store auth settings""" self.__load_defaults() _form = AuthSettingsForm(c.enabled_plugins)() log.debug("POST Result: %s" % formatted_json(dict(request.POST))) try: form_result = _form.to_python(dict(request.POST)) for k, v in form_result.items(): if k == 'auth_plugins': # we want to store it comma separated inside our settings v = ','.join(v) log.debug("%s = %s" % (k, str(v))) setting = Setting.create_or_update(k, v) Session().add(setting) Session().commit() h.flash(_('Auth settings updated successfully'), category='success') except formencode.Invalid, errors: log.error(traceback.format_exc()) e = errors.error_dict or {} return self.index( defaults=errors.value, errors=e, prefix_error=False)
def set_test_settings(): """Restore settings after test is over.""" # Save settings. settings_snapshot = [(s.app_settings_name, s.app_settings_value, s.app_settings_type) for s in Setting.query().all()] yield _set_settings # Restore settings. session = Session() keys = frozenset(k for (k, v, t) in settings_snapshot) for s in Setting.query().all(): if s.app_settings_name not in keys: session.delete(s) for k, v, t in settings_snapshot: if t == 'list' and hasattr(v, '__iter__'): v = ','.join(v) # Quirk: must format list value manually. Setting.create_or_update(k, v, t) session.commit()
def settings_global(self): c.active = 'global' if request.POST: application_form = ApplicationSettingsForm()() try: form_result = application_form.to_python(dict(request.POST)) except formencode.Invalid as errors: return htmlfill.render( render('admin/settings/settings.html'), defaults=errors.value, errors=errors.error_dict or {}, prefix_error=False, encoding="UTF-8", force_defaults=False) try: for setting in ( 'title', 'realm', 'ga_code', 'captcha_public_key', 'captcha_private_key', ): Setting.create_or_update(setting, form_result[setting]) Session().commit() set_app_settings(config) h.flash(_('Updated application settings'), category='success') except Exception: log.error(traceback.format_exc()) h.flash(_('Error occurred while updating ' 'application settings'), category='error') raise HTTPFound(location=url('admin_settings_global')) defaults = Setting.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)
def test_list_valued_setting_creation_requires_manual_value_formatting(): assert Setting.get_by_name(name) is None # Quirk: need manual formatting of list setting value. setting = Setting.create_or_update(name, 'spam,eggs', type='list') Session().flush() # must flush so we can delete it below try: assert setting.app_settings_value == ['spam', 'eggs'] finally: Session().delete(setting)
def set_test_settings(): """Restore settings after test is over.""" # Save settings. settings_snapshot = [ (s.app_settings_name, s.app_settings_value, s.app_settings_type) for s in Setting.query().all()] yield _set_settings # Restore settings. session = Session() keys = frozenset(k for (k, v, t) in settings_snapshot) for s in Setting.query().all(): if s.app_settings_name not in keys: session.delete(s) for k, v, t in settings_snapshot: if t == 'list' and hasattr(v, '__iter__'): v = ','.join(v) # Quirk: must format list value manually. Setting.create_or_update(k, v, t) session.commit()
def test_passing_list_setting_value_results_in_string_valued_setting(): assert Setting.get_by_name(name) is None setting = Setting.create_or_update(name, ['spam', 'eggs']) Session().flush() # must flush so we can delete it below try: assert Setting.get_by_name(name) is not None # Quirk: list value is stringified. assert Setting.get_by_name(name).app_settings_value \ == "['spam', 'eggs']" assert Setting.get_by_name(name).app_settings_type == 'unicode' finally: Session().delete(setting)
def auth_settings(self): """POST create and store auth settings""" self.__load_defaults() log.debug("POST Result: %s", formatted_json(dict(request.POST))) # First, parse only the plugin list (not the plugin settings). _auth_plugins_validator = AuthSettingsForm([]).fields['auth_plugins'] try: new_enabled_plugins = _auth_plugins_validator.to_python( request.POST.get('auth_plugins')) except formencode.Invalid: # User provided an invalid plugin list. Just fall back to # the list of currently enabled plugins. (We'll re-validate # and show an error message to the user, below.) pass else: # Hide plugins that the user has asked to be disabled, but # do not show plugins that the user has asked to be enabled # (yet), since that'll cause validation errors and/or wrong # settings being applied (e.g. checkboxes being cleared), # since the plugin settings will not be in the POST data. c.enabled_plugin_names = [ p for p in c.enabled_plugin_names if p in new_enabled_plugins ] # Next, parse everything including plugin settings. _form = AuthSettingsForm(c.enabled_plugin_names)() try: form_result = _form.to_python(dict(request.POST)) for k, v in form_result.items(): if k == 'auth_plugins': # we want to store it comma separated inside our settings v = ','.join(v) log.debug("%s = %s", k, str(v)) setting = Setting.create_or_update(k, v) Session().commit() h.flash(_('Auth settings updated successfully'), category='success') except formencode.Invalid as errors: log.error(traceback.format_exc()) e = errors.error_dict or {} return self.__render( defaults=errors.value, errors=e, ) except Exception: log.error(traceback.format_exc()) h.flash(_('error occurred during update of auth settings'), category='error') raise HTTPFound(location=url('auth_home'))
def test_list_valued_setting_update(): assert Setting.get_by_name(name) is None setting = Setting.create_or_update(name, 'spam', type='list') Session().flush() # must flush so we can delete it below try: assert setting.app_settings_value == ['spam'] # Assign back setting value. setting.app_settings_value = setting.app_settings_value # Quirk: value is stringified on write and listified on read. assert setting.app_settings_value == ["['spam']"] setting.app_settings_value = setting.app_settings_value assert setting.app_settings_value == ["[\"['spam']\"]"] finally: Session().delete(setting)
def auth_settings(self): """POST create and store auth settings""" self.__load_defaults() log.debug("POST Result: %s", formatted_json(dict(request.POST))) # First, parse only the plugin list (not the plugin settings). _auth_plugins_validator = AuthSettingsForm([]).fields['auth_plugins'] try: new_enabled_plugins = _auth_plugins_validator.to_python(request.POST.get('auth_plugins')) except formencode.Invalid: # User provided an invalid plugin list. Just fall back to # the list of currently enabled plugins. (We'll re-validate # and show an error message to the user, below.) pass else: # Hide plugins that the user has asked to be disabled, but # do not show plugins that the user has asked to be enabled # (yet), since that'll cause validation errors and/or wrong # settings being applied (e.g. checkboxes being cleared), # since the plugin settings will not be in the POST data. c.enabled_plugins = [ p for p in c.enabled_plugins if p in new_enabled_plugins ] # Next, parse everything including plugin settings. _form = AuthSettingsForm(c.enabled_plugins)() try: form_result = _form.to_python(dict(request.POST)) for k, v in form_result.items(): if k == 'auth_plugins': # we want to store it comma separated inside our settings v = ','.join(v) log.debug("%s = %s", k, str(v)) setting = Setting.create_or_update(k, v) Session().add(setting) Session().commit() h.flash(_('Auth settings updated successfully'), category='success') except formencode.Invalid as errors: log.error(traceback.format_exc()) e = errors.error_dict or {} return self.__render( defaults=errors.value, errors=e, ) except Exception: log.error(traceback.format_exc()) h.flash(_('error occurred during update of auth settings'), category='error') raise HTTPFound(location=url('auth_home'))
def update(self, id): """PUT /defaults/id: Update an existing item""" # Forms posted to this method should contain a hidden field: # <input type="hidden" name="_method" value="PUT" /> # Or using helpers: # h.form(url('default', id=ID), # method='put') # url('default', id=ID) _form = DefaultsForm()() try: form_result = _form.to_python(dict(request.POST)) for k, v in form_result.iteritems(): setting = Setting.create_or_update(k, v) Session().add(setting) Session().commit() h.flash(_('Default settings updated successfully'), category='success') except formencode.Invalid as errors: defaults = errors.value return htmlfill.render( render('admin/defaults/defaults.html'), defaults=defaults, errors=errors.error_dict or {}, prefix_error=False, encoding="UTF-8", force_defaults=False) except Exception: log.error(traceback.format_exc()) h.flash(_('Error occurred during update of defaults'), category='error') raise HTTPFound(location=url('defaults'))
def auth_settings(self): """POST create and store auth settings""" self.__load_defaults() _form = AuthSettingsForm(c.enabled_plugins)() log.debug("POST Result: %s" % formatted_json(dict(request.POST))) try: form_result = _form.to_python(dict(request.POST)) for k, v in form_result.items(): if k == 'auth_plugins': # we want to store it comma separated inside our settings v = ','.join(v) log.debug("%s = %s" % (k, str(v))) setting = Setting.create_or_update(k, v) Session().add(setting) Session().commit() h.flash(_('Auth settings updated successfully'), category='success') except formencode.Invalid, errors: log.error(traceback.format_exc()) e = errors.error_dict or {} return self.index(defaults=errors.value, errors=e, prefix_error=False)