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
|
# -*- coding: utf-8 -*-
"""
Trac WebAdmin plugin for administration of custom fields.
License: BSD
(c) 2005-2007 ::: www.CodeResort.com - BV Network AS (simon-code@bvnetwork.no)
(c) 2007 ::: www.Optaros.com (.....)
"""
from trac.core import *
from trac.web.chrome import ITemplateProvider, add_stylesheet, add_script
from webadmin.web_ui import IAdminPageProvider
from api import CustomFields
from trac.util.text import to_unicode
class CustomFieldAdminPage(Component):
implements(ITemplateProvider, IAdminPageProvider)
# IAdminPageProvider methods
def get_admin_pages(self, req):
if req.perm.has_permission('TRAC_ADMIN'):
yield ('ticket', 'Ticket System', 'customfields', 'Custom Fields')
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
# ITemplateProvider methods
def get_templates_dirs(self):
from pkg_resources import resource_filename
return [resource_filename(__name__, 'templates')]
def get_htdocs_dirs(self):
from pkg_resources import resource_filename
return [('customfieldadmin', resource_filename(__name__, 'htdocs'))]
|