Esempio n. 1
0
    def expand_macro(self, formatter, name, args):
        from trac.config import ConfigSection, Option
        section_filter = key_filter = ''
        args, kw = parse_args(args)
        if args:
            section_filter = args.pop(0).strip()
        if args:
            key_filter = args.pop(0).strip()

        def getdoc(option_or_section):
            doc = to_unicode(option_or_section.__doc__)
            if doc:
                doc = dgettext(option_or_section.doc_domain, doc)
            return doc

        registry = ConfigSection.get_registry(self.compmgr)
        sections = dict((name, getdoc(section))
                        for name, section in registry.iteritems()
                        if name.startswith(section_filter))

        registry = Option.get_registry(self.compmgr)
        options = {}
        for (section, key), option in registry.iteritems():
            if section.startswith(section_filter):
                options.setdefault(section, {})[key] = option
                sections.setdefault(section, '')

        def default_cell(option):
            default = option.default
            if default is True:
                default = 'true'
            elif default is False:
                default = 'false'
            elif default == 0:
                default = '0.0' if isinstance(default, float) else '0'
            elif default:
                default = ', '.join(to_unicode(val) for val in default) \
                          if isinstance(default, (list, tuple)) \
                          else to_unicode(default)
            else:
                return tag.td(_("(no default)"), class_='nodefault')
            return tag.td(tag.code(default), class_='default')

        return tag.div(class_='tracini')(
            (tag.h3(tag.code('[%s]' % section), id='%s-section' % section),
             format_to_html(self.env, formatter.context, section_doc),
             tag.table(class_='wiki')(tag.tbody(
                 tag.tr(
                     tag.td(tag.tt(option.name)),
                     tag.td(
                         format_to_oneliner(self.env, formatter.context,
                                            getdoc(option))),
                     default_cell(option))
                 for option in sorted(options.get(section, {}).itervalues(),
                                      key=lambda o: o.name)
                 if option.name.startswith(key_filter))))
            for section, section_doc in sorted(sections.iteritems()))
Esempio n. 2
0
    def expand_macro(self, formatter, name, args):
        from trac.config import ConfigSection, Option
        section_filter = key_filter = ''
        args, kw = parse_args(args)
        if args:
            section_filter = args.pop(0).strip()
        if args:
            key_filter = args.pop(0).strip()

        def getdoc(option_or_section):
            doc = to_unicode(option_or_section.__doc__)
            if doc:
                doc = dgettext(option_or_section.doc_domain, doc)
            return doc

        registry = ConfigSection.get_registry(self.compmgr)
        sections = dict((name, getdoc(section))
                        for name, section in registry.iteritems()
                        if name.startswith(section_filter))

        registry = Option.get_registry(self.compmgr)
        options = {}
        for (section, key), option in registry.iteritems():
            if section.startswith(section_filter):
                options.setdefault(section, {})[key] = option
                sections.setdefault(section, '')

        def default_cell(option):
            default = option.default
            if default is True:
                default = 'true'
            elif default is False:
                default = 'false'
            elif default == 0:
                default = '0.0' if isinstance(default, float) else '0'
            elif default:
                default = ', '.join(to_unicode(val) for val in default) \
                          if isinstance(default, (list, tuple)) \
                          else to_unicode(default)
            else:
                return tag.td(_("(no default)"), class_='nodefault')
            return tag.td(tag.code(default), class_='default')

        return tag.div(class_='tracini')(
            (tag.h3(tag.code('[%s]' % section), id='%s-section' % section),
             format_to_html(self.env, formatter.context, section_doc),
             tag.table(class_='wiki')(tag.tbody(
                 tag.tr(tag.td(tag.tt(option.name)),
                        tag.td(format_to_oneliner(
                            self.env, formatter.context, getdoc(option))),
                        default_cell(option))
                 for option in sorted(options.get(section, {}).itervalues(),
                                      key=lambda o: o.name)
                 if option.name.startswith(key_filter))))
            for section, section_doc in sorted(sections.iteritems()))
Esempio n. 3
0
    def expand_macro(self, formatter, name, content):
        from trac.config import ConfigSection, Option
        section_filter = key_filter = ''
        args, kw = parse_args(content)
        if args:
            section_filter = args.pop(0).strip()
        if args:
            key_filter = args.pop(0).strip()

        def getdoc(option_or_section):
            doc = to_unicode(option_or_section.__doc__)
            if doc:
                doc = dgettext(option_or_section.doc_domain, doc)
            return doc

        registry = ConfigSection.get_registry(self.compmgr)
        sections = dict((name, getdoc(section))
                        for name, section in registry.iteritems()
                        if name.startswith(section_filter))

        registry = Option.get_registry(self.compmgr)
        options = {}
        for (section, key), option in registry.iteritems():
            if section.startswith(section_filter):
                options.setdefault(section, {})[key] = option
                sections.setdefault(section, '')

        def default_cell(option):
            default = option.default
            if default is not None and default != '':
                return tag.td(tag.code(option.dumps(default)),
                              class_='default')
            else:
                return tag.td(_("(no default)"), class_='nodefault')

        return tag.div(class_='tracini')(
            (tag.h3(tag.code('[%s]' % section), id='%s-section' % section),
             format_to_html(self.env, formatter.context, section_doc),
             tag.table(class_='wiki')(tag.tbody(
                 tag.tr(tag.td(tag.code(option.name)),
                        tag.td(
                            format_to_oneliner(self.env, formatter.context,
                                               getdoc(option))),
                        default_cell(option),
                        class_='odd' if idx % 2 else 'even')
                 for idx, option in enumerate(
                     sorted(options.get(section, {}).itervalues(),
                            key=lambda o: o.name))
                 if option.name.startswith(key_filter))))
            for section, section_doc in sorted(sections.iteritems()))
Esempio n. 4
0
    def expand_macro(self, formatter, name, content):
        from trac.config import ConfigSection, Option
        section_filter = key_filter = ''
        args, kw = parse_args(content)
        if args:
            section_filter = args.pop(0).strip()
        if args:
            key_filter = args.pop(0).strip()

        def getdoc(option_or_section):
            doc = to_unicode(option_or_section.__doc__)
            if doc:
                doc = dgettext(option_or_section.doc_domain, doc)
            return doc

        registry = ConfigSection.get_registry(self.compmgr)
        sections = dict((name, getdoc(section))
                        for name, section in registry.iteritems()
                        if name.startswith(section_filter))

        registry = Option.get_registry(self.compmgr)
        options = {}
        for (section, key), option in registry.iteritems():
            if section.startswith(section_filter):
                options.setdefault(section, {})[key] = option
                sections.setdefault(section, '')

        def default_cell(option):
            default = option.default
            if default is not None and default != '':
                return tag.td(tag.code(option.dumps(default)),
                              class_='default')
            else:
                return tag.td(_("(no default)"), class_='nodefault')

        return tag.div(class_='tracini')(
            (tag.h3(tag.code('[%s]' % section), id='%s-section' % section),
             format_to_html(self.env, formatter.context, section_doc),
             tag.table(class_='wiki')(tag.tbody(
                 tag.tr(tag.td(tag.tt(option.name)),
                        tag.td(format_to_oneliner(
                            self.env, formatter.context, getdoc(option))),
                        default_cell(option),
                        class_='odd' if idx % 2 else 'even')
                 for idx, option in
                    enumerate(sorted(options.get(section, {}).itervalues(),
                                     key=lambda o: o.name))
                 if option.name.startswith(key_filter))))
            for section, section_doc in sorted(sections.iteritems()))
Esempio n. 5
0
    def expand_macro(self, formatter, name, args):
        from trac.config import ConfigSection, Option
        section_filter = key_filter = ''
        args, kw = parse_args(args)
        if args:
            section_filter = args.pop(0).strip()
        if args:
            key_filter = args.pop(0).strip()

        registry = ConfigSection.get_registry(self.compmgr)
        sections = dict(
            (name, dgettext(section.doc_domain, to_unicode(section.__doc__)))
            for name, section in registry.iteritems()
            if name.startswith(section_filter))

        registry = Option.get_registry(self.compmgr)
        options = {}
        for (section, key), option in registry.iteritems():
            if section.startswith(section_filter):
                options.setdefault(section, {})[key] = option
                sections.setdefault(section, '')

        return tag.div(class_='tracini')(
            (tag.h3(tag.code('[%s]' % section), id='%s-section' % section),
             format_to_html(self.env, formatter.context, section_doc),
             tag.table(class_='wiki')(tag.tbody(
                 tag.tr(
                     tag.td(tag.tt(option.name)),
                     tag.td(
                         format_to_oneliner(
                             self.env, formatter.context,
                             dgettext(option.doc_domain,
                                      to_unicode(option.__doc__)))),
                     tag.td(
                         tag.code(option.default or 'false') if option.default
                         or option.default is False else _("(no default)"),
                         class_='default' if option.default
                         or option.default is False else 'nodefault'))
                 for option in sorted(options.get(section, {}).itervalues(),
                                      key=lambda o: o.name)
                 if option.name.startswith(key_filter))))
            for section, section_doc in sorted(sections.iteritems()))
Esempio n. 6
0
    def expand_macro(self, formatter, name, args):
        from trac.config import ConfigSection, Option
        section_filter = key_filter = ''
        args, kw = parse_args(args)
        if args:
            section_filter = args.pop(0).strip()
        if args:
            key_filter = args.pop(0).strip()

        registry = ConfigSection.get_registry(self.compmgr)
        sections = dict((name, dgettext(section.doc_domain,
                                        to_unicode(section.__doc__)))
                        for name, section in registry.iteritems()
                        if name.startswith(section_filter))

        registry = Option.get_registry(self.compmgr)
        options = {}
        for (section, key), option in registry.iteritems():
            if section.startswith(section_filter):
                options.setdefault(section, {})[key] = option
                sections.setdefault(section, '')

        return tag.div(class_='tracini')(
            (tag.h3(tag.code('[%s]' % section), id='%s-section' % section),
             format_to_html(self.env, formatter.context, section_doc),
             tag.table(class_='wiki')(tag.tbody(
                 tag.tr(tag.td(tag.tt(option.name)),
                        tag.td(format_to_oneliner(
                            self.env, formatter.context,
                            dgettext(option.doc_domain,
                                     to_unicode(option.__doc__)))),
                        tag.td(tag.code(option.default or 'false')
                                   if option.default or option.default is False
                                   else _("(no default)"),
                               class_='default' if option.default or 
                                                   option.default is False 
                                                else 'nodefault'))
                 for option in sorted(options.get(section, {}).itervalues(),
                                      key=lambda o: o.name)
                 if option.name.startswith(key_filter))))
            for section, section_doc in sorted(sections.iteritems()))
Esempio n. 7
0
    def expand_macro(self, formatter, name, content):
        from trac.config import ConfigSection, Option

        args, kw = parse_args(content)
        filters = {}
        for name, index in (('section', 0), ('option', 1)):
            pattern = kw.get(name, '').strip()
            if pattern:
                filters[name] = fnmatch.translate(pattern)
                continue
            prefix = args[index].strip() if index < len(args) else ''
            if prefix:
                filters[name] = re.escape(prefix)
        has_option_filter = 'option' in filters
        for name in ('section', 'option'):
            filters[name] = re.compile(filters[name], re.IGNORECASE).match \
                            if name in filters \
                            else lambda v: True
        section_filter = filters['section']
        option_filter = filters['option']

        section_registry = ConfigSection.get_registry(self.compmgr)
        option_registry = Option.get_registry(self.compmgr)
        options = {}
        for (section, key), option in option_registry.iteritems():
            if section_filter(section) and option_filter(key):
                options.setdefault(section, {})[key] = option
        if not has_option_filter:
            for section in section_registry:
                if section_filter(section):
                    options.setdefault(section, {})
        for section in options:
            options[section] = sorted(options[section].itervalues(),
                                      key=lambda option: option.name)
        sections = [(section, section_registry[section].doc
                              if section in section_registry else '')
                    for section in sorted(options)]

        def default_cell(option):
            default = option.default
            if default is not None and default != '':
                return tag.td(tag.code(option.dumps(default)),
                              class_='default')
            else:
                return tag.td(_("(no default)"), class_='nodefault')

        def options_table(section, options):
            if options:
                return tag.table(class_='wiki')(
                    tag.tbody(
                        tag.tr(
                            tag.td(tag.a(tag.code(option.name),
                                         class_='tracini-option',
                                         href='#%s-%s-option' %
                                              (section, option.name))),
                            tag.td(format_to_html(self.env, formatter.context,
                                                  option.doc)),
                            default_cell(option),
                            id='%s-%s-option' % (section, option.name),
                            class_='odd' if idx % 2 else 'even')
                     for idx, option in enumerate(options)))

        return tag.div(class_='tracini')(
            (tag.h3(tag.code('[%s]' % section), id='%s-section' % section),
             format_to_html(self.env, formatter.context, section_doc),
             options_table(section, options.get(section)))
            for section, section_doc in sections)
Esempio n. 8
0
    def render_admin_panel(self, req, cat, page, path_info):
        req.perm.require('TRAC_ADMIN')

        if path_info == None:
            ext = ""
        else:
            ext = '/' + path_info

        #
        # Gather section names for section drop down field
        #
        all_section_names = []
        for section_name in self.config.sections():
            if section_name == 'components':
                continue
            all_section_names.append(section_name)

        # Check whether section exists and if it's not existing then check whether
        # its name is a valid section name.
        if (path_info is not None) and (path_info not in ('', '/', '_all_sections')) \
           and (path_info not in all_section_names):
            if path_info == 'components':
                add_warning(
                    req,
                    _('The section "components" can\'t be edited with the ini editor.'
                      ))
                req.redirect(req.href.admin(cat, page))
                return None
            elif self.valid_section_name_chars_regexp.match(path_info) is None:
                add_warning(req,
                            _('The section name %s is invalid.') % path_info)
                req.redirect(req.href.admin(cat, page))
                return None

            # Add current section if it's not already in the list. This happens if
            # the section is essentially empty (i.e. newly created with no non-default
            # option values and no option from the option registry).
            all_section_names.append(path_info)

        registry = ConfigSection.get_registry(self.compmgr)
        descriptions = {}
        for section_name, section in registry.items():
            if section_name == 'components':
                continue
            doc = section.__doc__
            if not section_name in all_section_names:
                all_section_names.append(section_name)
            if doc:
                descriptions[section_name] = dgettext(section.doc_domain, doc)

        all_section_names.sort()

        sections = {}

        #
        # Check security manager
        #
        manager = None
        try:
            manager = self.security_manager
        except Exception, detail:  # "except ... as ..." is only available since Python 2.6
            if req.method != 'POST':
                # only add this warning once
                add_warning(
                    req,
                    _('Security manager could not be initated. %s') %
                    unicode(detail))
Esempio n. 9
0
class TracIniAdminPanel(Component):
    """ An editor panel for trac.ini. """

    implements(IAdminPanelProvider, ITemplateProvider)

    valid_section_name_chars = Option(
        'ini-editor',
        'valid-section-name-chars',
        '^[a-zA-Z0-9\\-_\\:]+$',
        doc="""Defines the valid characters for a section name or option name in 
      `trac.ini`. Must be a valid regular expression. You only need to change 
      these if you have plugins that use some strange section or option names.
      """,
        doc_domain="inieditorpanel")

    valid_option_name_chars = Option(
        'ini-editor',
        'valid-option-name-chars',
        '^[a-zA-Z0-9\\-_\\:.]+$',
        doc="""Defines the valid characters for a section name or option name in 
      `trac.ini`. Must be a valid regular expression. You only need to change 
      these if you have plugins that use some strange section or option names.
      """,
        doc_domain="inieditorpanel")

    security_manager = ExtensionOption(
        'ini-editor',
        'security-manager',
        IOptionSecurityManager,
        'IniEditorEmptySecurityManager',
        doc="""Defines the security manager that specifies whether the user has 
      access to certain options.
      """,
        doc_domain="inieditorpanel")

    # See "IniEditorBasicSecurityManager" for why we use a pipe char here.
    password_options = ListOption(
        'ini-editor',
        'password-options',
        doc="""Defines option fields (as `section-name|option-name`) that 
      represent passwords. Password input fields are used for these fields.
      Note the fields specified here are taken additionally to some predefined 
      fields provided by the ini editor.
      """,
        doc_domain="inieditorpanel")

    ini_section = ConfigSection(
        'ini-editor',
        """This section is used to handle configurations used by
      TracIniAdminPanel plugin.""",
        doc_domain='inieditorpanel')

    DEFAULT_PASSWORD_OPTIONS = {'notification|smtp_password': True}

    def __init__(self):
        """Set up translation domain"""
        locale_dir = resource_filename(__name__, 'locale')
        add_domain(self.env.path, locale_dir)

        self.valid_section_name_chars_regexp = re.compile(
            self.valid_section_name_chars)
        self.valid_option_name_chars_regexp = re.compile(
            self.valid_option_name_chars)

        self.password_options_set = copy.deepcopy(
            self.DEFAULT_PASSWORD_OPTIONS)
        for option in self.password_options:
            self.password_options_set[option] = True

    #
    # IAdminPanelProvider methods
    #

    def get_admin_panels(self, req):
        if 'TRAC_ADMIN' in req.perm:
            yield ('general', dgettext('messages',
                                       'General'), 'trac_ini_editor',
                   _('trac.ini Editor'))

    def render_admin_panel(self, req, cat, page, path_info):
        req.perm.require('TRAC_ADMIN')

        if path_info == None:
            ext = ""
        else:
            ext = '/' + path_info

        #
        # Gather section names for section drop down field
        #
        all_section_names = []
        for section_name in self.config.sections():
            if section_name == 'components':
                continue
            all_section_names.append(section_name)

        # Check whether section exists and if it's not existing then check whether
        # its name is a valid section name.
        if (path_info is not None) and (path_info not in ('', '/', '_all_sections')) \
           and (path_info not in all_section_names):
            if path_info == 'components':
                add_warning(
                    req,
                    _('The section "components" can\'t be edited with the ini editor.'
                      ))
                req.redirect(req.href.admin(cat, page))
                return None
            elif self.valid_section_name_chars_regexp.match(path_info) is None:
                add_warning(req,
                            _('The section name %s is invalid.') % path_info)
                req.redirect(req.href.admin(cat, page))
                return None

            # Add current section if it's not already in the list. This happens if
            # the section is essentially empty (i.e. newly created with no non-default
            # option values and no option from the option registry).
            all_section_names.append(path_info)

        registry = ConfigSection.get_registry(self.compmgr)
        descriptions = {}
        for section_name, section in registry.items():
            if section_name == 'components':
                continue
            doc = section.__doc__
            if not section_name in all_section_names:
                all_section_names.append(section_name)
            if doc:
                descriptions[section_name] = dgettext(section.doc_domain, doc)

        all_section_names.sort()

        sections = {}

        #
        # Check security manager
        #
        manager = None
        try:
            manager = self.security_manager
        except Exception, detail:  # "except ... as ..." is only available since Python 2.6
            if req.method != 'POST':
                # only add this warning once
                add_warning(
                    req,
                    _('Security manager could not be initated. %s') %
                    unicode(detail))

        if manager is None:
            #
            # Security manager is not available
            #
            if req.method == 'POST':
                req.redirect(req.href.admin(cat, page) + ext)
                return None

        elif req.method == 'POST' and 'change-section' in req.args:
            #
            # Changing the section
            #
            req.redirect(
                req.href.admin(cat, page) + '/' + req.args['change-section'])
            return None

        elif req.method == 'POST' and 'new-section-name' in req.args:
            #
            # Create new section (essentially simply changing the section)
            #
            section_name = req.args['new-section-name'].strip()

            if section_name == '':
                add_warning(req, _('The section name was empty.'))
                req.redirect(req.href.admin(cat, page) + ext)
            elif section_name == 'components':
                add_warning(
                    req,
                    _('The section "components" can\'t be edited with the ini editor.'
                      ))
                req.redirect(req.href.admin(cat, page))
            elif self.valid_section_name_chars_regexp.match(
                    section_name) is None:
                add_warning(
                    req,
                    _('The section name %s is invalid.') % section_name)
                req.redirect(req.href.admin(cat, page) + ext)
            else:
                if section_name not in all_section_names:
                    add_notice(
                        req,
                        _('Section %s has been created. Note that you need to add at least one option to store it permanently.'
                          ) % section_name)
                else:
                    add_warning(req, _('The section already exists.'))
                req.redirect(req.href.admin(cat, page) + '/' + section_name)

            return None

        elif path_info is not None and path_info not in ('', '/'):
            #
            # Display and possibly modify section (if one is selected)
            #
            default_values = self.config.defaults()

            # Gather option values
            # NOTE: This needs to be done regardless whether we have POST data just to
            #   be on the safe site.
            if path_info == '_all_sections':
                # All sections
                custom_options = self._get_session_custom_options(req)
                # Only show sections with any data
                for section_name in all_section_names:
                    sections[section_name] = self._read_section_config(
                        req, section_name, default_values, custom_options)
            else:
                # Only a single section
                # Note: At this point path_info has already been verified to contain a
                #   valid section name (see check above).
                sections[path_info] = self._read_section_config(
                    req, path_info, default_values)

            #
            # Handle POST data
            #
            if req.method == 'POST':
                # Overwrite option values with POST values so that they don't get lost
                for key, value in req.args.items():
                    if not key.startswith(
                            'inieditor_value##'):  # skip unrelated args
                        continue

                    name = key[len('inieditor_value##'):].split('##')
                    section_name = name[0].strip()
                    option_name = name[1].strip()

                    if section_name == 'components':
                        continue

                    if option_name == 'dummy':
                        if section_name not in sections:
                            sections[section_name] = {}
                        continue

                    section = sections.get(section_name, None)
                    if section:
                        option = section.get(option_name, None)
                        if option:
                            self._set_option_value(req, section_name,
                                                   option_name, option, value)
                        else:
                            # option not available; was propably newly added
                            section[
                                option_name] = self._create_new_field_instance(
                                    req, section_name, option_name,
                                    default_values.get(section_name, None),
                                    value)
                    else:
                        # newly created section (not yet stored)
                        sections[section_name] = {
                            option_name:
                            self._create_new_field_instance(
                                req, section_name, option_name, None, value)
                        }

                # Check which options use their default values
                # NOTE: Must be done after assigning field value from the previous step
                #   to ensure that the default value has been initialized.
                if 'inieditor_default' in req.args:
                    default_using_options = req.args.get('inieditor_default')
                    if default_using_options is None or len(
                            default_using_options) == 0:
                        # if no checkbox was selected make this explicitly a list (just for safety)
                        default_using_options = []
                    elif type(default_using_options).__name__ != 'list':
                        # if there's only one checkbox it's just a string
                        default_using_options = [
                            unicode(default_using_options)
                        ]

                    for default_using_option in default_using_options:
                        name = default_using_option.split('##')
                        section_name = name[0].strip()
                        option_name = name[1].strip()
                        section = sections.get(section_name, None)
                        if section:
                            option = section.get(option_name, None)
                            if option:
                                if option['access'] == ACCESS_MODIFIABLE:
                                    option['value'] = option['default_value']
                            else:
                                # option not available; was propably newly added
                                section[
                                    option_name] = self._create_new_field_instance(
                                        req, section_name, option_name,
                                        default_values.get(section_name, None))
                        else:
                            # newly created section (not yet stored)
                            sections[section_name] = {
                                option_name:
                                self._create_new_field_instance(
                                    req, section_name, option_name, None)
                            }

                #
                # Identify submit type
                # NOTE: Using "cur_focused_field" is a hack to support hitting the
                #  return key even for the new-options field. Without this hitting
                #  return would always associated to the apply button.
                #
                submit_type = None
                cur_focused_field = req.args.get('inieditor_cur_focused_field',
                                                 '')
                if cur_focused_field.startswith('option-value-'):
                    submit_type = 'apply-' + cur_focused_field[
                        len('option-value-'):]
                elif cur_focused_field.startswith('new-options-'):
                    submit_type = 'addnewoptions-' + cur_focused_field[
                        len('new-options-'):]
                else:
                    for key in req.args:
                        if not key.startswith('inieditor-submit-'):
                            continue

                        submit_type = key[len('inieditor-submit-'):]
                        break

                if submit_type.startswith('apply'):  # apply changes
                    if submit_type.startswith('apply-'):
                        # apply only one section
                        section_name = submit_type[len('apply-'):].strip()
                        if self._apply_section_changes(req, section_name,
                                                       sections[section_name]):
                            add_notice(
                                req,
                                _('Changes for section %s have been applied.')
                                % section_name)
                            self.config.save()
                        else:
                            add_warning(req,
                                        _('No changes have been applied.'))
                    else:
                        # apply all sections
                        changes_applied = False
                        for section_name, options in sections.items():
                            if self._apply_section_changes(
                                    req, section_name, options):
                                changes_applied = True

                        if changes_applied:
                            add_notice(req, _('Changes have been applied.'))
                            self.config.save()
                        else:
                            add_warning(req,
                                        _('No changes have been applied.'))

                elif submit_type.startswith('discard'):
                    if submit_type.startswith('discard-'):
                        # discard only one section
                        section_name = submit_type[len('discard-'):].strip()
                        self._discard_section_changes(req, section_name,
                                                      sections[section_name])
                        add_notice(
                            req,
                            _('Your changes for section %s have been discarded.'
                              ) % section_name)
                    else:
                        # discard all sections
                        for section_name, options in sections.items():
                            self._discard_section_changes(
                                req, section_name, options)
                        add_notice(req, _('All changes have been discarded.'))

                elif submit_type.startswith('addnewoptions-'):
                    section_name = submit_type[len('addnewoptions-'):].strip()
                    section = sections[section_name]
                    new_option_names = req.args['new-options-' +
                                                section_name].split(',')
                    section_default_values = default_values.get(
                        section_name, None)

                    field_added = False
                    for new_option_name in new_option_names:
                        new_option_name = new_option_name.strip()
                        if new_option_name in section:
                            continue  # field already exists

                        if self.valid_option_name_chars_regexp.match(
                                new_option_name) is None:
                            add_warning(
                                req,
                                _('The option name %s is invalid.') %
                                new_option_name)
                            continue

                        new_option = self._create_new_field_instance(
                            req, section_name, new_option_name,
                            section_default_values)
                        if new_option['access'] != ACCESS_MODIFIABLE:
                            add_warning(
                                req,
                                _('The new option %s could not be added due to security restrictions.'
                                  ) % new_option_name)
                            continue

                        self._add_session_custom_option(
                            req, section_name, new_option_name)
                        field_added = True

                    if field_added:
                        add_notice(
                            req,
                            _('The new fields have been added to section %s.')
                            % section_name)
                    else:
                        add_warning(req, _('No new fields have been added.'))

                req.redirect(req.href.admin(cat, page) + ext)
                return None

        # Split sections dict for faster template rendering
        modifiable_options = {}
        readonly_options = {}
        hidden_options = {}
        for section_name, options in sections.items():
            sect_modifiable = {}
            sect_readonly = {}
            sect_hidden = {}
            for option_name, option in options.items():
                if option['access'] == ACCESS_MODIFIABLE:
                    sect_modifiable[option_name] = option
                elif option['access'] == ACCESS_READONLY:
                    sect_readonly[option_name] = option
                else:
                    sect_hidden[option_name] = option

            modifiable_options[section_name] = sect_modifiable
            readonly_options[section_name] = sect_readonly
            hidden_options[section_name] = sect_hidden

        registry = ConfigSection.get_registry(self.compmgr)
        descriptions = {}
        for name, section in registry.items():
            doc = section.__doc__
            if doc:
                descriptions[name] = dgettext(section.doc_domain, doc)

        data = {
            'all_section_names': all_section_names,
            'sections': sections,
            'descriptions': descriptions,
            'modifiable_options': modifiable_options,
            'readonly_options': readonly_options,
            'hidden_options': hidden_options
        }

        section_counters = {}
        settings_stored_values = {}
        for section_name, section in sections.iteritems():
            escaped = section_name.replace(':', '_')
            section_counters[escaped] = {'option_count': len(section)}
            settings_stored_values[escaped] = dict(
                (name, option['stored_value']) for name, option in
                modifiable_options[section_name].iteritems()
                if option['type'] != 'password')

        add_script_data(
            req, {
                'section_count':
                len(sections),
                'section_names':
                sorted(section_counters),
                'section_counters':
                section_counters,
                'settings_stored_values':
                settings_stored_values,
                'info_format':
                _("Modified: %(mod)d | Defaults: %(def)d | Options "
                  "count: %(opt)d"),
            })

        add_stylesheet(req, 'inieditorpanel/main.css')
        add_script(req, 'inieditorpanel/editor.js')
        return 'admin_tracini.html', data
Esempio n. 10
0
    def render_admin_panel(self, req, cat, page, path_info):
        req.perm.require("TRAC_ADMIN")

        if path_info == None:
            ext = ""
        else:
            ext = "/" + path_info

        #
        # Gather section names for section drop down field
        #
        all_section_names = []
        for section_name in self.config.sections():
            if section_name == "components":
                continue
            all_section_names.append(section_name)

        # Check whether section exists and if it's not existing then check whether
        # its name is a valid section name.
        if (
            (path_info is not None)
            and (path_info not in ("", "/", "_all_sections"))
            and (path_info not in all_section_names)
        ):
            if path_info == "components":
                add_warning(req, _('The section "components" can\'t be edited with the ini editor.'))
                req.redirect(req.href.admin(cat, page))
                return None
            elif self.valid_section_name_chars_regexp.match(path_info) is None:
                add_warning(req, _("The section name %s is invalid.") % path_info)
                req.redirect(req.href.admin(cat, page))
                return None

            # Add current section if it's not already in the list. This happens if
            # the section is essentially empty (i.e. newly created with no non-default
            # option values and no option from the option registry).
            all_section_names.append(path_info)

        registry = ConfigSection.get_registry(self.compmgr)
        descriptions = {}
        for section_name, section in registry.items():
            if section_name == "components":
                continue
            doc = section.__doc__
            if not section_name in all_section_names:
                all_section_names.append(section_name)
            if doc:
                descriptions[section_name] = dgettext(section.doc_domain, doc)

        all_section_names.sort()

        sections = {}

        #
        # Check security manager
        #
        manager = None
        try:
            manager = self.security_manager
        except Exception, detail:  # "except ... as ..." is only available since Python 2.6
            if req.method != "POST":
                # only add this warning once
                add_warning(req, _("Security manager could not be initated. %s") % unicode(detail))