source: tagsplugin/trunk/tractags/admin.py @ 17050

Last change on this file since 17050 was 17050, checked in by Ryan J Ollos, 6 years ago

TracTags 0.10dev: Restore compatibility with Trac < 1.3.2

This revises incorrect changes in [17046:17047].

Refs #13316.

File size: 2.7 KB
Line 
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
10from pkg_resources import parse_version
11
12from trac import __version__
13from trac.admin import IAdminPanelProvider
14from trac.core import Component, implements
15from trac.web.chrome import Chrome, add_warning
16
17from tractags.api import TagSystem, _
18
19
20class TagChangeAdminPanel(Component):
21    """[opt] Admin web-UI providing administrative tag system actions."""
22
23    implements(IAdminPanelProvider)
24
25    # AdminPanelProvider methods
26    def get_admin_panels(self, req):
27        if 'TAGS_ADMIN' in req.perm:
28            yield 'tags', _('Tag System'), 'replace', _('Replace')
29
30    def render_admin_panel(self, req, cat, page, version):
31        req.perm.require('TAGS_ADMIN')
32
33        tag_system = TagSystem(self.env)
34        all_realms = tag_system.get_taggable_realms(req.perm)
35        # Check request for enabled filters, or use default.
36        if not [r for r in all_realms if r in req.args]:
37            for realm in all_realms:
38                req.args[realm] = 'on'
39        checked_realms = [r for r in all_realms if r in req.args]
40        data = dict(checked_realms=checked_realms,
41                    tag_realms=list(dict(name=realm,
42                                         checked=realm in checked_realms)
43                                    for realm in all_realms))
44
45        if req.method == 'POST':
46            # Replace Tag
47            allow_delete = req.args.get('allow_delete')
48            new_tag = req.args.get('tag_new_name').strip()
49            new_tag = not new_tag == u'' and new_tag or None
50            if not (allow_delete or new_tag):
51                add_warning(req, _("Selected current tag(s) and either "
52                                   "new tag or delete approval are required"))
53            else:
54                comment = req.args.get('comment', u'')
55                old_tags = req.args.getlist('tag_name')
56                if old_tags:
57                    tag_system.replace_tag(req, old_tags, new_tag, comment,
58                                           allow_delete, filter=checked_realms)
59                data['selected'] = new_tag
60            req.redirect(req.href.admin('tags', 'replace'))
61
62        query = ' or '.join('realm:%s' % r for r in checked_realms)
63        all_tags = sorted(tag_system.get_all_tags(req, query))
64        data['tags'] = all_tags
65        chrome = Chrome(self.env)
66        chrome.add_textarea_grips(req)
67        if hasattr(chrome, 'jenv'):
68            return 'admin_tag_change.html', data, None
69        else:
70            return 'admin_tag_change.html', data
Note: See TracBrowser for help on using the repository browser.