def _process_read(self, req, page_param): ticket_type = req.args.get('type') ticket_types = [{ 'name': type.name, 'value': type.value, 'selected': (type.name == ticket_type) } for type in ticket.Type.select(self.env)] # if type isn't selected, it uses the first element. if ticket_type == None: ticket_type = (ticket_types[0])['name'] template_api = TicketTemplate() ticket_template = template_api.get_template_field( self.env, ticket_type) # check enable customfield customfields_api = CustomFields(self.env) customfields = customfields_api.get_custom_fields(self.env) enable_list = ticket_template['enablefields'].split(',') for cf in customfields: cf['enable'] = False for field in enable_list: if (field.strip() == cf['name']): cf['enable'] = True break page_param['types'] = ticket_types page_param['template'] = ticket_template['template'] page_param['enablefields'] = ticket_template['enablefields'] page_param['customfields'] = customfields
def _process_read(self, req, page_param): ticket_type = req.args.get('type') ticket_types = [{ 'name' : type.name, 'value' : type.value, 'selected' : (type.name == ticket_type) } for type in ticket.Type.select(self.env)] # if type isn't selected, it uses the first element. if ticket_type == None: ticket_type = (ticket_types[0])['name'] template_api = TicketTemplate() ticket_template = template_api.get_template_field(self.env, ticket_type) # check enable customfield customfields_api = CustomFields(self.env) customfields = customfields_api.get_custom_fields(self.env) enable_list = ticket_template['enablefields'].split(',') for cf in customfields: cf['enable'] = False for field in enable_list: if (field.strip() == cf['name']): cf['enable'] = True break page_param['types'] = ticket_types page_param['template'] = ticket_template['template'] page_param['enablefields'] = ticket_template['enablefields'] page_param['customfields'] = customfields
class CustomFieldApiTestCase(unittest.TestCase): def setUp(self): self.env = EnvironmentStub() self.cf_api = CustomFields(self.env) def tearDown(self): if hasattr(self.env, "destroy_db"): self.env.destroy_db() del self.env def test_systeminfo(self): try: from trac.loader import get_plugin_info # From ~0.12, Trac handles plugins and versions - no need to test return except ImportError: self.assertTrue( ("CustomFieldAdmin", __import__("customfieldadmin", ["__version__"]).__version__) in self.env.systeminfo ) def test_create(self): for f in ["one", "two", "three"]: cfield = {"name": f, "type": "text"} self.cf_api.create_custom_field(cfield) self.assertEquals( self.cf_api.get_custom_fields(), [ {"name": u"one", "format": "plain", "value": "", "label": u"One", "type": u"text", "order": 1}, {"name": u"two", "format": "plain", "value": "", "label": u"Two", "type": u"text", "order": 2}, {"name": u"three", "format": "plain", "value": "", "label": u"Three", "type": u"text", "order": 3}, ], ) def test_update(self): cfield = {"name": "foo", "type": "text"} self.cf_api.create_custom_field(cfield) self.assertEquals( cfield, {"name": u"foo", "format": "plain", "value": "", "label": u"Foo", "type": u"text", "order": 1} ) cfield["label"] = "Answer" cfield["value"] = "42" self.cf_api.update_custom_field(cfield=cfield) self.assertEquals( cfield, {"name": u"foo", "format": "plain", "value": "42", "label": u"Answer", "type": u"text", "order": 1} ) def test_update_non_existing(self): try: self.cf_api.update_custom_field(cfield={"name": "no_field"}) self.fail("Huh. Missing exception!") except Exception, e: self.assertTrue("'no_field'" in e.message) self.assertTrue("does not exist" in e.message)
def _process_read(self, req): add_script(req, 'ticketext/jquery.js') add_script(req, 'ticketext/ticketext.js') footer = req.hdf['project.footer'] + '\n<script type="text/javascript">'\ + 'TicketTemplate.initialize(\'' + req.base_path + '\');'\ + '</script>\n' req.hdf['project.footer'] = Markup(footer) ticket_type = req.args.get('type') ticket_types = [{ 'name': type.name, 'value': type.value, 'selected': (type.name == ticket_type) } for type in ticket.Type.select(self.env)] # if type isn't selected, it uses the first element. if ticket_type == None: ticket_type = (ticket_types[0])['name'] template_api = TicketTemplate(self.env) ticket_template = template_api.get_template_field(ticket_type) # check enable customfield customfields_api = CustomFields(self.env) customfields = customfields_api.get_custom_fields(self.env) enable_list = ticket_template['enablefields'].split(',') for cf in customfields: cf['enable'] = False for field in enable_list: if field.strip() == unicode(cf['name'], 'utf-8'): cf['enable'] = True break req.hdf['ticketext.template'] = { 'types': ticket_types, 'template': ticket_template['template'], 'enablefields': ticket_template['enablefields'], 'customfields': customfields }
def _process_read(self, req): add_script(req, 'ticketext/jquery.js') add_script(req, 'ticketext/ticketext.js') footer = req.hdf['project.footer'] + '\n<script type="text/javascript">'\ + 'TicketTemplate.initialize(\'' + req.base_path + '\');'\ + '</script>\n' req.hdf['project.footer'] = Markup(footer) ticket_type = req.args.get('type') ticket_types = [{ 'name' : type.name, 'value' : type.value, 'selected' : (type.name == ticket_type) } for type in ticket.Type.select(self.env)] # if type isn't selected, it uses the first element. if ticket_type == None: ticket_type = (ticket_types[0])['name'] template_api = TicketTemplate(self.env) ticket_template = template_api.get_template_field(ticket_type) # check enable customfield customfields_api = CustomFields(self.env) customfields = customfields_api.get_custom_fields(self.env) enable_list = ticket_template['enablefields'].split(',') for cf in customfields: cf['enable'] = False for field in enable_list: if field.strip() == unicode(cf['name'], 'utf-8'): cf['enable'] = True break req.hdf['ticketext.template'] = { 'types' : ticket_types, 'template' : ticket_template['template'], 'enablefields' : ticket_template['enablefields'], 'customfields' : customfields }
class CustomFieldApiTestCase(unittest.TestCase): def setUp(self): self.env = EnvironmentStub() self.cf_api = CustomFields(self.env) def tearDown(self): if hasattr(self.env, 'destroy_db'): self.env.destroy_db() del self.env def test_systeminfo(self): try: from trac.loader import get_plugin_info # From ~0.12, Trac handles plugins and versions - no need to test return except ImportError: self.assertTrue(('CustomFieldAdmin', __import__('customfieldadmin', ['__version__']).__version__) \ in self.env.systeminfo) def test_create(self): for f in ['one', 'two', 'three']: cfield = {'name': f, 'type': 'text'} self.cf_api.create_custom_field(cfield) self.assertEquals(self.cf_api.get_custom_fields(), [{'name': u'one', 'format': 'plain', 'value': '', 'label': u'One', 'type': u'text', 'order': 1}, {'name': u'two', 'format': 'plain', 'value': '', 'label': u'Two', 'type': u'text', 'order': 2}, {'name': u'three', 'format': 'plain', 'value': '', 'label': u'Three', 'type': u'text', 'order': 3}]) def test_update(self): cfield = {'name': 'foo', 'type': 'text'} self.cf_api.create_custom_field(cfield) self.assertEquals(cfield, {'name': u'foo', 'format': 'plain', 'value': '', 'label': u'Foo', 'type': u'text', 'order': 1}) cfield['label'] = 'Answer' cfield['value'] = '42' self.cf_api.update_custom_field(cfield=cfield) self.assertEquals(cfield, {'name': u'foo', 'format': 'plain', 'value': '42', 'label': u'Answer', 'type': u'text', 'order': 1}) def test_update_textarea(self): cfield = {'name': 'foo', 'type': 'textarea'} self.cf_api.create_custom_field(cfield) self.assertEquals(cfield, {'name': u'foo', 'format': 'plain', 'value': '', 'label': u'Foo', 'type': u'textarea', 'order': 1, 'cols': 60, 'rows': 5}) cfield['cols'] = 42 cfield['rows'] = 3 self.cf_api.update_custom_field(cfield=cfield) self.assertEquals(cfield, {'name': u'foo', 'format': 'plain', 'value': '', 'label': u'Foo', 'type': u'textarea', 'order': 1, 'cols': 42, 'rows': 3}) def test_update_non_existing(self): try: self.cf_api.update_custom_field(cfield={'name': 'no_field'}) self.fail("Huh. Missing exception!") except Exception, e: self.assertTrue("'no_field'" in e.message) self.assertTrue('does not exist' in e.message)
def render_admin_panel(self, req, cat, page, customfield): req.perm('admin', 'ticket/customfields').require('TICKET_ADMIN') add_script(req, 'customfieldadmin/js/customfieldadmin.js') def _customfield_from_req(self, req): cfield = {'name': req.args.get('name','').encode('utf-8'), 'label': req.args.get('label','').encode('utf-8'), 'type': req.args.get('type','').encode('utf-8'), 'value': req.args.get('value','').encode('utf-8'), 'options': [x.strip().encode('utf-8') for x in \ req.args.get('options','').split("\n")], 'cols': req.args.get('cols','').encode('utf-8'), 'rows': req.args.get('rows','').encode('utf-8'), 'order': req.args.get('order', '').encode('utf-8'), 'format': req.args.get('format', '').encode('utf-8')} return cfield cf_api = CustomFields(self.env) cf_admin = {} # Return values for template rendering # Detail view? if customfield: cfield = None for a_cfield in cf_api.get_custom_fields(): if a_cfield['name'] == customfield: cfield = a_cfield break if not cfield: raise TracError( _("Custom field %(name)s does not exist.", name=customfield)) if req.method == 'POST': if req.args.get('save'): cfield.update(_customfield_from_req(self, req)) cf_api.update_custom_field(cfield) req.redirect(req.href.admin(cat, page)) elif req.args.get('cancel'): req.redirect(req.href.admin(cat, page)) if cfield.has_key('options'): optional_line = '' if cfield.get('optional', False): optional_line = "\n\n" cfield['options'] = optional_line + "\n".join( cfield['options']) cf_admin['cfield'] = cfield cf_admin['cf_display'] = 'detail' else: if req.method == 'POST': # Add Custom Field if req.args.get('add') and req.args.get('name'): cfield = _customfield_from_req(self, req) cf_api.update_custom_field(cfield, 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: cfield = {'name': name} cf_api.delete_custom_field(cfield) 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_')]) cfields = cf_api.get_custom_fields() for current_cfield in cfields: new_order = order.get(current_cfield['name'], 0) if new_order: current_cfield['order'] = new_order cf_api.update_custom_field(current_cfield) req.redirect(req.href.admin(cat, page)) cfields = [] orders_in_use = [] for item in cf_api.get_custom_fields(): item['href'] = req.href.admin(cat, page, item['name']) item['registry'] = ('ticket-custom', item['name']) in Option.registry cfields.append(item) orders_in_use.append(int(item.get('order'))) cf_admin['cfields'] = cfields cf_admin['cf_display'] = 'list' if sorted(orders_in_use) != range(1, len(cfields) + 1): add_warning(req, _("Custom Fields are not correctly sorted. " \ "This may affect appearance when viewing tickets.")) return ('customfieldadmin.html', cf_admin)
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): cfield = { "name": req.args.get("name", "").encode("utf-8"), "label": req.args.get("label", "").encode("utf-8"), "type": req.args.get("type", "").encode("utf-8"), "value": req.args.get("value", "").encode("utf-8"), "options": [x.strip().encode("utf-8") for x in req.args.get("options", "").split("\n")], "cols": req.args.get("cols", "").encode("utf-8"), "rows": req.args.get("rows", "").encode("utf-8"), "order": req.args.get("order", "").encode("utf-8"), "format": req.args.get("format", "").encode("utf-8"), } return cfield cf_api = CustomFields(self.env) cf_admin = {} # Return values for template rendering # Detail view? if customfield: cfield = None for a_cfield in cf_api.get_custom_fields(): if a_cfield["name"] == customfield: cfield = a_cfield break if not cfield: raise TracError(_("Custom field %(name)s does not exist.", name=customfield)) if req.method == "POST": if req.args.get("save"): cfield.update(_customfield_from_req(self, req)) cf_api.update_custom_field(cfield) req.redirect(req.href.admin(cat, page)) elif req.args.get("cancel"): req.redirect(req.href.admin(cat, page)) if cfield.has_key("options"): optional_line = "" if cfield.get("optional", False): optional_line = "\n\n" cfield["options"] = optional_line + "\n".join(cfield["options"]) cf_admin["cfield"] = cfield cf_admin["cf_display"] = "detail" else: if req.method == "POST": # Add Custom Field if req.args.get("add") and req.args.get("name"): cfield = _customfield_from_req(self, req) cf_api.update_custom_field(cfield, 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: cfield = {"name": name} cf_api.delete_custom_field(cfield) 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_")]) cfields = cf_api.get_custom_fields() for current_cfield in cfields: current_cfield["order"] = order[current_cfield["name"]] cf_api.update_custom_field(current_cfield) req.redirect(req.href.admin(cat, page)) cfields = [] orders_in_use = [] for item in cf_api.get_custom_fields(): item["href"] = req.href.admin(cat, page, item["name"]) item["registry"] = ("ticket-custom", item["name"]) in Option.registry cfields.append(item) orders_in_use.append(int(item.get("order"))) cf_admin["cfields"] = cfields cf_admin["cf_display"] = "list" if sorted(orders_in_use) != range(1, len(cfields) + 1): add_warning( req, _("Custom Fields are not correctly sorted. " "This may affect appearance when viewing tickets."), ) return ("customfieldadmin.html", cf_admin)