| 1 | # Copyright (C) 2009 Mat Booth <mat@matbooth.co.uk> |
|---|
| 2 | # All rights reserved. |
|---|
| 3 | # |
|---|
| 4 | # This software is licensed as described in the file COPYING, which |
|---|
| 5 | # you should have received as part of this distribution. |
|---|
| 6 | |
|---|
| 7 | import re |
|---|
| 8 | |
|---|
| 9 | from trac.core import * |
|---|
| 10 | from trac.perm import PermissionSystem |
|---|
| 11 | from trac.resource import ResourceNotFound |
|---|
| 12 | from trac.ticket.admin import TicketAdminPanel |
|---|
| 13 | from trac.util.datefmt import parse_date, get_date_format_hint, get_datetime_format_hint |
|---|
| 14 | from trac.util.translation import _ |
|---|
| 15 | from trac.web.chrome import add_script |
|---|
| 16 | |
|---|
| 17 | from multiproduct import model |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | class ProductAdminPanel(TicketAdminPanel): |
|---|
| 21 | """Provides an admin panel for Products.""" |
|---|
| 22 | |
|---|
| 23 | _type = 'products' |
|---|
| 24 | _label = ('Product', 'Products') |
|---|
| 25 | |
|---|
| 26 | # TicketAdminPanel methods |
|---|
| 27 | |
|---|
| 28 | def _render_admin_panel(self, req, cat, page, product): |
|---|
| 29 | # Detail view? |
|---|
| 30 | if product: |
|---|
| 31 | prod = model.Product(self.env, product) |
|---|
| 32 | if req.method == 'POST': |
|---|
| 33 | if req.args.get('save'): |
|---|
| 34 | prod.name = req.args.get('name') |
|---|
| 35 | prod.owner = req.args.get('owner') |
|---|
| 36 | prod.description = req.args.get('description') |
|---|
| 37 | prod.update() |
|---|
| 38 | req.redirect(req.href.admin(cat, page)) |
|---|
| 39 | elif req.args.get('cancel'): |
|---|
| 40 | req.redirect(req.href.admin(cat, page)) |
|---|
| 41 | |
|---|
| 42 | add_script(req, 'common/js/wikitoolbar.js') |
|---|
| 43 | data = {'view': 'detail', 'field': prod} |
|---|
| 44 | |
|---|
| 45 | else: |
|---|
| 46 | if req.method == 'POST': |
|---|
| 47 | # Add Product |
|---|
| 48 | if req.args.get('add') and req.args.get('name'): |
|---|
| 49 | name = req.args.get('name') |
|---|
| 50 | try: |
|---|
| 51 | model.Product(self.env, name=name) |
|---|
| 52 | except ResourceNotFound: |
|---|
| 53 | prod = model.Product(self.env) |
|---|
| 54 | prod.name = name |
|---|
| 55 | if req.args.get('owner'): |
|---|
| 56 | prod.owner = req.args.get('owner') |
|---|
| 57 | prod.insert() |
|---|
| 58 | req.redirect(req.href.admin(cat, page)) |
|---|
| 59 | else: |
|---|
| 60 | raise TracError(_('Product %s already exists.') % name) |
|---|
| 61 | |
|---|
| 62 | # Remove products |
|---|
| 63 | elif req.args.get('remove'): |
|---|
| 64 | sel = req.args.get('sel') |
|---|
| 65 | if not sel: |
|---|
| 66 | raise TracError(_('No product selected')) |
|---|
| 67 | if not isinstance(sel, list): |
|---|
| 68 | sel = [sel] |
|---|
| 69 | db = self.env.get_db_cnx() |
|---|
| 70 | for name in sel: |
|---|
| 71 | prod = model.Product(self.env, name, db=db) |
|---|
| 72 | prod.delete(db=db) |
|---|
| 73 | db.commit() |
|---|
| 74 | req.redirect(req.href.admin(cat, page)) |
|---|
| 75 | |
|---|
| 76 | # Set default product |
|---|
| 77 | elif req.args.get('apply'): |
|---|
| 78 | if req.args.get('default'): |
|---|
| 79 | name = req.args.get('default') |
|---|
| 80 | self.log.info('Setting default product to %s', name) |
|---|
| 81 | self.config.set('ticket', 'default_product', name) |
|---|
| 82 | self.config.save() |
|---|
| 83 | req.redirect(req.href.admin(cat, page)) |
|---|
| 84 | |
|---|
| 85 | default = self.config.get('ticket', 'default_product') |
|---|
| 86 | data = {'view': 'list', |
|---|
| 87 | 'products': list(model.Product.select(self.env)), |
|---|
| 88 | 'default': default} |
|---|
| 89 | |
|---|
| 90 | if self.config.getbool('ticket', 'restrict_owner'): |
|---|
| 91 | perm = PermissionSystem(self.env) |
|---|
| 92 | def valid_owner(username): |
|---|
| 93 | return perm.get_user_permissions(username).get('TICKET_MODIFY') |
|---|
| 94 | data['owners'] = [username for username, name, email |
|---|
| 95 | in self.env.get_known_users() |
|---|
| 96 | if valid_owner(username)] |
|---|
| 97 | data['owners'].insert(0, '') |
|---|
| 98 | data['owners'].sort() |
|---|
| 99 | else: |
|---|
| 100 | data['owners'] = None |
|---|
| 101 | |
|---|
| 102 | data['label_singular'] = self._label[0] |
|---|
| 103 | data['label_plural'] = self._label[1] |
|---|
| 104 | return 'admin_products.html', data |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | class ProductComponentAdminPanel(TicketAdminPanel): |
|---|
| 108 | """Provides an admin panel for Product Components.""" |
|---|
| 109 | |
|---|
| 110 | _type = 'productcomponents' |
|---|
| 111 | _label = ('Product Component', 'Product Components') |
|---|
| 112 | |
|---|
| 113 | # TicketAdminPanel methods |
|---|
| 114 | |
|---|
| 115 | def _render_admin_panel(self, req, cat, page, productcomponent): |
|---|
| 116 | # Look for pattern <product>/<productcomponent> in url |
|---|
| 117 | match = None |
|---|
| 118 | if productcomponent: |
|---|
| 119 | match = re.match('([^/]+)/(.*)$', productcomponent) |
|---|
| 120 | |
|---|
| 121 | # Detail view? |
|---|
| 122 | if match: |
|---|
| 123 | prodcomp = model.ProductComponent(self.env, match.group(2), match.group(1)) |
|---|
| 124 | if req.method == 'POST': |
|---|
| 125 | if req.args.get('save'): |
|---|
| 126 | prodcomp.name = req.args.get('name') |
|---|
| 127 | prodcomp.owner = req.args.get('owner') |
|---|
| 128 | prodcomp.description = req.args.get('description') |
|---|
| 129 | prodcomp.update() |
|---|
| 130 | req.redirect(req.href.admin(cat, page, match.group(1))) |
|---|
| 131 | elif req.args.get('cancel'): |
|---|
| 132 | req.redirect(req.href.admin(cat, page, match.group(1))) |
|---|
| 133 | |
|---|
| 134 | add_script(req, 'common/js/wikitoolbar.js') |
|---|
| 135 | data = {'view': 'detail', 'field': prodcomp} |
|---|
| 136 | |
|---|
| 137 | else: |
|---|
| 138 | if req.method == 'POST': |
|---|
| 139 | # Add product component |
|---|
| 140 | if req.args.get('add') and req.args.get('name') and req.args.get('parent'): |
|---|
| 141 | name = req.args.get('name') |
|---|
| 142 | parent = req.args.get('parent') |
|---|
| 143 | try: |
|---|
| 144 | model.ProductComponent(self.env, name=name, parent=parent) |
|---|
| 145 | except ResourceNotFound: |
|---|
| 146 | prodcomp = model.ProductComponent(self.env) |
|---|
| 147 | prodcomp.name = name |
|---|
| 148 | prodcomp.parent = parent |
|---|
| 149 | if req.args.get('owner'): |
|---|
| 150 | prodcomp.owner = req.args.get('owner') |
|---|
| 151 | prodcomp.insert() |
|---|
| 152 | req.redirect(req.href.admin(cat, page, prodcomp.parent)) |
|---|
| 153 | else: |
|---|
| 154 | raise TracError(_('Product component %s already exists.') % name) |
|---|
| 155 | |
|---|
| 156 | # Remove product components |
|---|
| 157 | elif req.args.get('remove'): |
|---|
| 158 | sel = req.args.get('sel') |
|---|
| 159 | parent = req.args.get('parent') |
|---|
| 160 | if not sel: |
|---|
| 161 | raise TracError(_('No product component selected')) |
|---|
| 162 | if not isinstance(sel, list): |
|---|
| 163 | sel = [sel] |
|---|
| 164 | db = self.env.get_db_cnx() |
|---|
| 165 | for name in sel: |
|---|
| 166 | prodcomp = model.ProductComponent(self.env, name, parent, db=db) |
|---|
| 167 | prodcomp.delete(db=db) |
|---|
| 168 | db.commit() |
|---|
| 169 | req.redirect(req.href.admin(cat, page, parent)) |
|---|
| 170 | |
|---|
| 171 | # Change selected parent product |
|---|
| 172 | elif req.args.get('parent'): |
|---|
| 173 | req.redirect(req.href.admin(cat, page, req.args.get('parent'))) |
|---|
| 174 | |
|---|
| 175 | products = list(model.Product.select(self.env)) |
|---|
| 176 | if productcomponent: |
|---|
| 177 | parent = productcomponent # Catches redirects |
|---|
| 178 | else: |
|---|
| 179 | parent = products[0].name or None # Just use the first in the list as default |
|---|
| 180 | |
|---|
| 181 | data = { |
|---|
| 182 | 'view': 'list', |
|---|
| 183 | 'products': products, |
|---|
| 184 | 'productcomponents': list(model.ProductComponent.select(self.env, parent=parent)), |
|---|
| 185 | 'parent': parent, |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | data['label_singular'] = self._label[0] |
|---|
| 189 | data['label_plural'] = self._label[1] |
|---|
| 190 | return 'admin_productcomponents.html', data |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | class ProductVersionAdminPanel(TicketAdminPanel): |
|---|
| 194 | """Provides an admin panel for Product Versions.""" |
|---|
| 195 | |
|---|
| 196 | _type = 'productversions' |
|---|
| 197 | _label = ('Product Version', 'Product Versions') |
|---|
| 198 | |
|---|
| 199 | # TicketAdminPanel methods |
|---|
| 200 | |
|---|
| 201 | def _render_admin_panel(self, req, cat, page, productversion): |
|---|
| 202 | # Look for pattern <product>/<productversion> in url |
|---|
| 203 | match = None |
|---|
| 204 | if productversion: |
|---|
| 205 | match = re.match('([^/]+)/(.*)$', productversion) |
|---|
| 206 | |
|---|
| 207 | # Detail view? |
|---|
| 208 | if match: |
|---|
| 209 | prodver = model.ProductVersion(self.env, match.group(2), match.group(1)) |
|---|
| 210 | if req.method == 'POST': |
|---|
| 211 | if req.args.get('save'): |
|---|
| 212 | prodver.name = req.args.get('name') |
|---|
| 213 | if req.args.get('time'): |
|---|
| 214 | prodver.time = parse_date(req.args.get('time'), req.tz) |
|---|
| 215 | else: |
|---|
| 216 | prodver.time = None # unset |
|---|
| 217 | prodver.description = req.args.get('description') |
|---|
| 218 | prodver.update() |
|---|
| 219 | req.redirect(req.href.admin(cat, page, match.group(1))) |
|---|
| 220 | elif req.args.get('cancel'): |
|---|
| 221 | req.redirect(req.href.admin(cat, page, match.group(1))) |
|---|
| 222 | |
|---|
| 223 | add_script(req, 'common/js/wikitoolbar.js') |
|---|
| 224 | data = {'view': 'detail', 'field': prodver} |
|---|
| 225 | |
|---|
| 226 | else: |
|---|
| 227 | if req.method == 'POST': |
|---|
| 228 | # Add product version |
|---|
| 229 | if req.args.get('add') and req.args.get('name') and req.args.get('parent'): |
|---|
| 230 | name = req.args.get('name') |
|---|
| 231 | parent = req.args.get('parent') |
|---|
| 232 | try: |
|---|
| 233 | model.ProductVersion(self.env, name=name, parent=parent) |
|---|
| 234 | except ResourceNotFound: |
|---|
| 235 | prodver = model.ProductVersion(self.env) |
|---|
| 236 | prodver.name = name |
|---|
| 237 | prodver.parent = parent |
|---|
| 238 | if req.args.get('time'): |
|---|
| 239 | prodver.time = parse_date(req.args.get('time'), req.tz) |
|---|
| 240 | prodver.insert() |
|---|
| 241 | req.redirect(req.href.admin(cat, page, prodver.parent)) |
|---|
| 242 | else: |
|---|
| 243 | raise TracError(_('Product version %s already exists.') % name) |
|---|
| 244 | |
|---|
| 245 | # Remove product versions |
|---|
| 246 | elif req.args.get('remove'): |
|---|
| 247 | sel = req.args.get('sel') |
|---|
| 248 | parent = req.args.get('parent') |
|---|
| 249 | if not sel: |
|---|
| 250 | raise TracError(_('No product version selected')) |
|---|
| 251 | if not isinstance(sel, list): |
|---|
| 252 | sel = [sel] |
|---|
| 253 | db = self.env.get_db_cnx() |
|---|
| 254 | for name in sel: |
|---|
| 255 | prodver = model.ProductVersion(self.env, name, parent, db=db) |
|---|
| 256 | prodver.delete(db=db) |
|---|
| 257 | db.commit() |
|---|
| 258 | req.redirect(req.href.admin(cat, page, parent)) |
|---|
| 259 | |
|---|
| 260 | # Change selected parent product |
|---|
| 261 | elif req.args.get('parent'): |
|---|
| 262 | req.redirect(req.href.admin(cat, page, req.args.get('parent'))) |
|---|
| 263 | |
|---|
| 264 | products = list(model.Product.select(self.env)) |
|---|
| 265 | if productversion: |
|---|
| 266 | parent = productversion # Catches redirects |
|---|
| 267 | else: |
|---|
| 268 | parent = products[0].name or None # Just use the first in the list as default |
|---|
| 269 | |
|---|
| 270 | data = { |
|---|
| 271 | 'view': 'list', |
|---|
| 272 | 'products': products, |
|---|
| 273 | 'productversions': list(model.ProductVersion.select(self.env, parent=parent)), |
|---|
| 274 | 'parent': parent, |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | data['datetime_hint'] = get_datetime_format_hint() |
|---|
| 278 | data['label_singular'] = self._label[0] |
|---|
| 279 | data['label_plural'] = self._label[1] |
|---|
| 280 | return 'admin_productversions.html', data |
|---|