| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2011 Itamar Ostricher <itamarost@gmail.com> |
|---|
| 4 | # Copyright (C) 2011-2013 Steffen Hoffmann <hoff.st@web.de> |
|---|
| 5 | # |
|---|
| 6 | # This software is licensed as described in the file COPYING, which |
|---|
| 7 | # you should have received as part of this distribution. |
|---|
| 8 | # |
|---|
| 9 | |
|---|
| 10 | from trac.admin import IAdminPanelProvider |
|---|
| 11 | from trac.core import Component, implements |
|---|
| 12 | from trac.web.chrome import Chrome, add_warning |
|---|
| 13 | |
|---|
| 14 | from tractags.api import TagSystem, _ |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | class TagChangeAdminPanel(Component): |
|---|
| 18 | """[opt] Admin web-UI providing administrative tag system actions.""" |
|---|
| 19 | |
|---|
| 20 | implements(IAdminPanelProvider) |
|---|
| 21 | |
|---|
| 22 | # AdminPanelProvider methods |
|---|
| 23 | def get_admin_panels(self, req): |
|---|
| 24 | if 'TAGS_ADMIN' in req.perm: |
|---|
| 25 | yield 'tags', _('Tag System'), 'replace', _('Replace') |
|---|
| 26 | |
|---|
| 27 | def render_admin_panel(self, req, cat, page, version): |
|---|
| 28 | req.perm.require('TAGS_ADMIN') |
|---|
| 29 | |
|---|
| 30 | tag_system = TagSystem(self.env) |
|---|
| 31 | all_realms = tag_system.get_taggable_realms(req.perm) |
|---|
| 32 | # Check request for enabled filters, or use default. |
|---|
| 33 | if not [r for r in all_realms if r in req.args]: |
|---|
| 34 | for realm in all_realms: |
|---|
| 35 | req.args[realm] = 'on' |
|---|
| 36 | checked_realms = [r for r in all_realms if r in req.args] |
|---|
| 37 | data = dict(checked_realms=checked_realms, |
|---|
| 38 | tag_realms=list(dict(name=realm, |
|---|
| 39 | checked=realm in checked_realms) |
|---|
| 40 | for realm in all_realms)) |
|---|
| 41 | |
|---|
| 42 | if req.method == 'POST': |
|---|
| 43 | # Replace Tag |
|---|
| 44 | allow_delete = req.args.get('allow_delete') |
|---|
| 45 | new_tag = req.args.get('tag_new_name').strip() |
|---|
| 46 | new_tag = not new_tag == u'' and new_tag or None |
|---|
| 47 | if not (allow_delete or new_tag): |
|---|
| 48 | add_warning(req, _("Selected current tag(s) and either " |
|---|
| 49 | "new tag or delete approval are required")) |
|---|
| 50 | else: |
|---|
| 51 | comment = req.args.get('comment', u'') |
|---|
| 52 | old_tags = req.args.getlist('tag_name') |
|---|
| 53 | if old_tags: |
|---|
| 54 | tag_system.replace_tag(req, old_tags, new_tag, comment, |
|---|
| 55 | allow_delete, filter=checked_realms) |
|---|
| 56 | data['selected'] = new_tag |
|---|
| 57 | req.redirect(req.href.admin('tags', 'replace')) |
|---|
| 58 | |
|---|
| 59 | query = ' or '.join('realm:%s' % r for r in checked_realms) |
|---|
| 60 | all_tags = sorted(tag_system.get_all_tags(req, query)) |
|---|
| 61 | data['tags'] = all_tags |
|---|
| 62 | chrome = Chrome(self.env) |
|---|
| 63 | chrome.add_textarea_grips(req) |
|---|
| 64 | if hasattr(chrome, 'jenv'): |
|---|
| 65 | return 'admin_tag_change.html', data, None |
|---|
| 66 | else: |
|---|
| 67 | return 'admin_tag_change.html', data |
|---|