| [13582] | 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2008-2009 Alexander von Bremen-Kuehne |
|---|
| [13585] | 4 | # Copyright (C) 2014 Ryan J Ollos <ryan.j.ollos@gmail.com> |
|---|
| [13582] | 5 | # All rights reserved. |
|---|
| 6 | # |
|---|
| 7 | # This software is licensed as described in the file COPYING, which |
|---|
| 8 | # you should have received as part of this distribution. |
|---|
| 9 | |
|---|
| [13583] | 10 | from trac.admin import IAdminPanelProvider |
|---|
| 11 | from trac.core import Component, implements |
|---|
| [16853] | 12 | from trac.env import IEnvironmentSetupParticipant |
|---|
| [13585] | 13 | from trac.util.translation import _ |
|---|
| 14 | from trac.web.chrome import ITemplateProvider, add_warning |
|---|
| [4918] | 15 | |
|---|
| [13583] | 16 | from tracusermanager.api import UserManager |
|---|
| [4918] | 17 | |
|---|
| [13583] | 18 | |
|---|
| [4918] | 19 | class TicketTeamDispatcherAdmin(Component): |
|---|
| [16854] | 20 | """Provides functions related to registration |
|---|
| [4918] | 21 | """ |
|---|
| [16853] | 22 | implements(IAdminPanelProvider, IEnvironmentSetupParticipant, |
|---|
| 23 | ITemplateProvider) |
|---|
| [4918] | 24 | |
|---|
| [16853] | 25 | # IEnvironmentSetupParticipant methods |
|---|
| 26 | |
|---|
| 27 | def environment_created(self): |
|---|
| 28 | self.upgrade_environment() |
|---|
| 29 | |
|---|
| 30 | def environment_needs_upgrade(self, db=None): |
|---|
| 31 | return 'ttd' not in self.config['ticket-custom'] |
|---|
| 32 | |
|---|
| 33 | def upgrade_environment(self, db=None): |
|---|
| 34 | self.config.set('ticket-custom', 'ttd', 'select') |
|---|
| 35 | self.config.set('ticket-custom', 'ttd.label', 'Team') |
|---|
| 36 | self.config.save() |
|---|
| 37 | |
|---|
| [13583] | 38 | # IAdminPanelProvider methods |
|---|
| [16853] | 39 | |
|---|
| [4918] | 40 | def get_admin_panels(self, req): |
|---|
| [16853] | 41 | if 'TICKET_ADMIN' in req.perm: |
|---|
| 42 | yield 'ticket', 'Ticket System', 'ttd', 'Team Dispatcher' |
|---|
| [13699] | 43 | |
|---|
| [4918] | 44 | def render_admin_panel(self, req, category, page, path_info): |
|---|
| [13583] | 45 | req.perm.require('TICKET_ADMIN') |
|---|
| [4918] | 46 | |
|---|
| [7157] | 47 | users = UserManager(self.env).get_active_users() |
|---|
| [13583] | 48 | caption = self.get_caption() |
|---|
| 49 | teams = self.get_teams() |
|---|
| [13699] | 50 | |
|---|
| [16853] | 51 | if req.method == 'POST': |
|---|
| 52 | action = req.args.get('action') |
|---|
| [7157] | 53 | if action == 'rename': |
|---|
| 54 | caption = req.args.get('caption') |
|---|
| [13583] | 55 | self.set_caption(caption) |
|---|
| [13585] | 56 | |
|---|
| 57 | elif action == 'addteam': |
|---|
| 58 | new_team = req.args.get('team') |
|---|
| 59 | if new_team: |
|---|
| 60 | if new_team not in teams: |
|---|
| 61 | teams.append(new_team) |
|---|
| 62 | self.set_teams(teams) |
|---|
| 63 | else: |
|---|
| 64 | add_warning(req, _('Team "%(team)s" already exists', |
|---|
| 65 | team=new_team)) |
|---|
| 66 | |
|---|
| 67 | elif action == 'addtoteam': |
|---|
| 68 | team = req.args.get('team') |
|---|
| 69 | username = req.args.get('subject') |
|---|
| 70 | for user in users: |
|---|
| 71 | if user.username == username: |
|---|
| 72 | user[team] = '1' |
|---|
| [7157] | 73 | user.save() |
|---|
| [4918] | 74 | |
|---|
| [13585] | 75 | elif action == 'remove': |
|---|
| [16854] | 76 | for item in req.args.getlist('sel'): |
|---|
| [13585] | 77 | if ':' in item: |
|---|
| 78 | username, team = item.split(':', 1) |
|---|
| 79 | for user in users: |
|---|
| 80 | if user.username == username: |
|---|
| 81 | user[team] = '0' |
|---|
| 82 | user.save() |
|---|
| 83 | else: |
|---|
| 84 | for user in users: |
|---|
| 85 | del user[item] |
|---|
| 86 | user.save() |
|---|
| 87 | teams.remove(item) |
|---|
| 88 | self.set_teams(teams) |
|---|
| 89 | |
|---|
| [13595] | 90 | elif action == 'notify': |
|---|
| 91 | self.set_notify_on('create', req.args.get('notify_on_create')) |
|---|
| 92 | self.set_notify_on('change', req.args.get('notify_on_change')) |
|---|
| 93 | self.set_notify_on('delete', req.args.get('notify_on_delete')) |
|---|
| 94 | |
|---|
| [16853] | 95 | req.redirect(req.href.admin('ticket/ttd')) |
|---|
| 96 | |
|---|
| [13587] | 97 | return 'team_dispatcher_admin.html', { |
|---|
| [13585] | 98 | 'teams': teams, |
|---|
| 99 | 'users': users, |
|---|
| [13595] | 100 | 'caption': caption, |
|---|
| 101 | 'notify_on_create': self.get_notify_on('create'), |
|---|
| 102 | 'notify_on_change': self.get_notify_on('change'), |
|---|
| 103 | 'notify_on_delete': self.get_notify_on('delete'), |
|---|
| [13583] | 104 | } |
|---|
| 105 | |
|---|
| 106 | # INavigationContributor methods |
|---|
| [16854] | 107 | |
|---|
| [4918] | 108 | def get_templates_dirs(self): |
|---|
| [13583] | 109 | from pkg_resources import resource_filename |
|---|
| [13578] | 110 | return [resource_filename(__name__, 'templates')] |
|---|
| [4918] | 111 | |
|---|
| 112 | def get_htdocs_dirs(self): |
|---|
| [13579] | 113 | return [] |
|---|
| [13583] | 114 | |
|---|
| 115 | # Internal methods |
|---|
| [16854] | 116 | |
|---|
| [13583] | 117 | def get_caption(self): |
|---|
| 118 | return self.config.get('ticket-custom', 'ttd.label') |
|---|
| 119 | |
|---|
| 120 | def set_caption(self, caption): |
|---|
| 121 | self.config.set('ticket-custom', 'ttd.label', caption) |
|---|
| 122 | self.config.save() |
|---|
| 123 | |
|---|
| [13595] | 124 | def get_notify_on(self, opt): |
|---|
| 125 | return self.config.getbool('team-dispatcher', 'notify_on_' + opt) |
|---|
| 126 | |
|---|
| 127 | def set_notify_on(self, opt, value): |
|---|
| 128 | value = True if value == 'on' else False |
|---|
| 129 | self.config.set('team-dispatcher', 'notify_on_' + opt, value) |
|---|
| 130 | self.config.save() |
|---|
| 131 | |
|---|
| [13583] | 132 | def get_teams(self): |
|---|
| 133 | return self.config.getlist('ticket-custom', 'ttd.options', sep='|') |
|---|
| 134 | |
|---|
| 135 | def set_teams(self, teams): |
|---|
| 136 | self.config.set('ticket-custom', 'ttd.options', '|'.join(teams)) |
|---|
| 137 | self.config.save() |
|---|