Ejemplo n.º 1
0
    def process_admin_request(self, req, cat, page, customfield):
        #assert req.perm.has_permission('TRAC_ADMIN')
        req.perm.assert_permission('TRAC_ADMIN')

        add_script(req, 'customfieldadmin/js/CustomFieldAdminPage_actions.js')

        def _customfield_from_req(self, req):
            cfdict = {
                'name':
                to_unicode(req.args.get('name')),
                'label':
                to_unicode(req.args.get('label')),
                'type':
                to_unicode(req.args.get('type')),
                'value':
                to_unicode(req.args.get('value')),
                'options': [
                    x.strip()
                    for x in to_unicode(req.args.get('options')).split("\n")
                ],
                'cols':
                to_unicode(req.args.get('cols')),
                'rows':
                to_unicode(req.args.get('rows')),
                'order':
                req.args.get('order', 0)
            }
            return cfdict

        cfapi = CustomFields(self.env)

        # Detail view?
        if customfield:
            exists = [
                True for cf in cfapi.get_custom_fields(self.env)
                if cf['name'] == customfield
            ]
            if not exists:
                raise TracError("Custom field %s does not exist." %
                                customfield)
            if req.method == 'POST':
                if req.args.get('save'):
                    cfdict = _customfield_from_req(self, req)
                    cfapi.update_custom_field(self.env, cfdict)
                    req.redirect(req.href.admin(cat, page))
                elif req.args.get('cancel'):
                    req.redirect(req.href.admin(cat, page))
            currentcf = cfapi.get_custom_fields(self.env,
                                                {'name': customfield})
            if currentcf.has_key('options'):
                optional_line = ''
                if currentcf.get('optional', False):
                    optional_line = "\n"
                currentcf['options'] = optional_line + "\n".join(
                    currentcf['options'])
            req.hdf['admin.customfield'] = currentcf
        else:
            if req.method == 'POST':
                # Add Custom Field
                if req.args.get('add') and req.args.get('name'):
                    cfdict = _customfield_from_req(self, req)
                    cfapi.update_custom_field(self.env, cfdict, create=True)
                    req.redirect(req.href.admin(cat, page))

                # Remove Custom Field
                elif req.args.get('remove') and req.args.get('sel'):
                    sel = req.args.get('sel')
                    sel = isinstance(sel, list) and sel or [sel]
                    if not sel:
                        raise TracError, 'No custom field selected'
                    for name in sel:
                        cfdict = {'name': name}
                        cfapi.delete_custom_field(self.env, cfdict)
                    req.redirect(req.href.admin(cat, page))

                elif req.args.get('apply'):
                    # Change order
                    order = dict([(key[6:], req.args.get(key))
                                  for key in req.args.keys()
                                  if key.startswith('order_')])
                    values = dict([(val, True) for val in order.values()])
                    if len(order) != len(values):
                        raise TracError, 'Order numbers must be unique.'
                    cf = cfapi.get_custom_fields(self.env)
                    for cur_cf in cf:
                        cur_cf['order'] = order[cur_cf['name']]
                        cfapi.update_custom_field(self.env, cur_cf)
                    req.redirect(req.href.admin(cat, page))

            hdf_list = []
            for item in cfapi.get_custom_fields(self.env):
                item['href'] = req.href.admin(cat, page, item['name'])
                hdf_list.append(item)
            req.hdf['admin.customfields'] = hdf_list

        return 'customfieldadmin.cs', None
Ejemplo n.º 2
0
    def render_admin_panel(self, req, cat, page, customfield):
        req.perm.require('TICKET_ADMIN')
        
        add_script(req, 'customfieldadmin/js/customfieldadmin.js')

        def _customfield_from_req(self, req):
            cfdict = {'name': req.args.get('name'),
                      'label': req.args.get('label'),
                      'type': req.args.get('type'),
                      'value': req.args.get('value'),
                      'options': [x.strip() for x in req.args.get('options').split("\n")],
                      'cols': req.args.get('cols'),
                      'rows': req.args.get('rows'),
                      'order': req.args.get('order', ''),
                      'format': req.args.get('format', '')}
            return cfdict
        
        cfapi = CustomFields(self.env)
        cfadmin = {} # Return values for template rendering
        
        # Detail view?
        if customfield:
            exists = [True for cf in cfapi.get_custom_fields() if cf['name'] == customfield]
            if not exists:
                raise TracError("Custom field %s does not exist." % customfield)
            if req.method == 'POST':
                if req.args.get('save'):
                    cfdict = _customfield_from_req(self, req) 
                    cfapi.update_custom_field(cfdict)
                    req.redirect(req.href.admin(cat, page))
                elif req.args.get('cancel'):
                    req.redirect(req.href.admin(cat, page))
            currentcf = cfapi.get_custom_fields({'name': customfield})
            if currentcf.has_key('options'):
                optional_line = ''
                if currentcf.get('optional', False):
                    optional_line = "\n\n"
                currentcf['options'] = optional_line + "\n".join(currentcf['options'])
            cfadmin['customfield'] = currentcf
            cfadmin['display'] = 'detail'
        else:
            if req.method == 'POST':
                # Add Custom Field
                if req.args.get('add') and req.args.get('name'):
                    cfdict = _customfield_from_req(self, req)
                    cfapi.update_custom_field(cfdict, create=True)
                    req.redirect(req.href.admin(cat, page))
                         
                # Remove Custom Field
                elif req.args.get('remove') and req.args.get('sel'):
                    sel = req.args.get('sel')
                    sel = isinstance(sel, list) and sel or [sel]
                    if not sel:
                        raise TracError, 'No custom field selected'
                    for name in sel:
                        cfdict =  {'name': name}
                        cfapi.delete_custom_field(cfdict)
                    req.redirect(req.href.admin(cat, page))

                elif req.args.get('apply'):
                    # Change order
                    order = dict([(key[6:], req.args.get(key)) for key
                                  in req.args.keys()
                                  if key.startswith('order_')])
                    values = dict([(val, True) for val in order.values()])
                    cf = cfapi.get_custom_fields()
                    for cur_cf in cf:
                        cur_cf['order'] = order[cur_cf['name']]
                        cfapi.update_custom_field(cur_cf)
                    req.redirect(req.href.admin(cat, page))

            cf_list = []
            for item in cfapi.get_custom_fields():
                item['href'] = req.href.admin(cat, page, item['name'])
                item['registry'] = ('ticket-custom', 
                                            item['name']) in Option.registry
                cf_list.append(item)
            cfadmin['customfields'] = cf_list
            cfadmin['display'] = 'list'

        return ('customfieldadmin.html', {'cfadmin': cfadmin})
Ejemplo n.º 3
0
    def process_admin_request(self, req, cat, page, customfield):
        #assert req.perm.has_permission('TRAC_ADMIN')
        req.perm.assert_permission('TRAC_ADMIN')
        
        add_script(req, 'customfieldadmin/js/CustomFieldAdminPage_actions.js')

        def _customfield_from_req(self, req):
            cfdict = {'name': to_unicode(req.args.get('name')),
                      'label': to_unicode(req.args.get('label')),
                      'type': to_unicode(req.args.get('type')),
                      'value': to_unicode(req.args.get('value')),
                      'options': [x.strip() for x in to_unicode(req.args.get('options')).split("\n")],
                      'cols': to_unicode(req.args.get('cols')),
                      'rows': to_unicode(req.args.get('rows')),
                      'order': req.args.get('order', 0)}
            return cfdict
        
        cfapi = CustomFields(self.env)
        
        # Detail view?
        if customfield:
            exists = [True for cf in cfapi.get_custom_fields(self.env) if cf['name'] == customfield]
            if not exists:
                raise TracError("Custom field %s does not exist." % customfield)
            if req.method == 'POST':
                if req.args.get('save'):
                    cfdict = _customfield_from_req(self, req) 
                    cfapi.update_custom_field(self.env, cfdict)
                    req.redirect(req.href.admin(cat, page))
                elif req.args.get('cancel'):
                    req.redirect(req.href.admin(cat, page))
            currentcf = cfapi.get_custom_fields(self.env, {'name': customfield})
            if currentcf.has_key('options'):
                optional_line = ''
                if currentcf.get('optional', False):
                    optional_line = "\n"
                currentcf['options'] = optional_line + "\n".join(currentcf['options'])
            req.hdf['admin.customfield'] = currentcf
        else:
            if req.method == 'POST':
                # Add Custom Field
                if req.args.get('add') and req.args.get('name'):
                    cfdict = _customfield_from_req(self, req)
                    cfapi.update_custom_field(self.env, cfdict, create=True)
                    req.redirect(req.href.admin(cat, page))
                         
                # Remove Custom Field
                elif req.args.get('remove') and req.args.get('sel'):
                    sel = req.args.get('sel')
                    sel = isinstance(sel, list) and sel or [sel]
                    if not sel:
                        raise TracError, 'No custom field selected'
                    for name in sel:
                        cfdict =  {'name': name}
                        cfapi.delete_custom_field(self.env, cfdict)
                    req.redirect(req.href.admin(cat, page))

                elif req.args.get('apply'):
                    # Change order
                    order = dict([(key[6:], req.args.get(key)) for key
                                  in req.args.keys()
                                  if key.startswith('order_')])
                    values = dict([(val, True) for val in order.values()])
                    if len(order) != len(values):
                        raise TracError, 'Order numbers must be unique.'
                    cf = cfapi.get_custom_fields(self.env)
                    for cur_cf in cf:
                        cur_cf['order'] = order[cur_cf['name']]
                        cfapi.update_custom_field(self.env, cur_cf)
                    req.redirect(req.href.admin(cat, page))

            hdf_list = []
            for item in cfapi.get_custom_fields(self.env):
                item['href'] = req.href.admin(cat, page, item['name'])
                hdf_list.append(item)
            req.hdf['admin.customfields'] = hdf_list

        return 'customfieldadmin.cs', None