Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 148 lines (118 sloc) 5.759 kb
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
# -*- coding: utf-8 -*-
#
# Cherokee-admin
#
# Authors:
# Alvaro Lopez Ortega <alvaro@alobbs.com>
#
# Copyright (C) 2010 Alvaro Lopez Ortega
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

import CTK
import Handler

from util import *
from consts import *

URL_APPLY = '/plugin/redir/apply'

NOTE_SHOW = N_("Defines whether the redirection will be seen by the client.")
NOTE_REGEX = N_('Regular expression. Check out the <a target="_blank" href="http://perldoc.perl.org/perlre.html">Reference</a>.')
NOTE_SUBSTITUTION = N_("Target address. It can use Regular Expression substitution sub-strings.")
NOTE_EMPTY = N_("At least one Regular Expression should be defined.")

HELPS = [('modules_handlers_redir', N_("Redirections")),
         ('http://perldoc.perl.org/perlre.html', N_("Regular Expressions"))]


def commit():
    new_show = CTK.post.pop('tmp!new_show')
    new_regex = CTK.post.pop('tmp!new_regex')
    new_subst = CTK.post.pop('tmp!new_subst')
    key = CTK.post.pop('key')

    # New
    if new_regex or new_subst:
        next = CTK.cfg.get_next_entry_prefix (key)
        CTK.cfg['%s!show'%(next)] = new_show
        if new_regex:
            CTK.cfg['%s!regex'%(next)] = new_regex
        if new_subst:
            CTK.cfg['%s!substring'%(next)] = new_subst
        return CTK.cfg_reply_ajax_ok()

    # Modification
    return CTK.cfg_apply_post()


class Plugin_redir (Handler.PluginHandler):
    class Content (CTK.Box):
        def __init__ (self, refresh, key):
            CTK.Box.__init__ (self)

            CTK.cfg.normalize (key)
            keys = CTK.cfg.keys (key)
            keys.sort(lambda x,y: cmp(int(x), int(y)))

            self += CTK.RawHTML ("<h3>%s</h3>" % (_('Rule list')))
            if not keys:
                self += CTK.Indenter (CTK.Notice ('information', CTK.RawHTML(_(NOTE_EMPTY))))
            else:
                # table = CTK.Table()
                table = CTK.SortableList (lambda arg: CTK.SortableList__reorder_generic (arg, key), self.id)
                table += [CTK.RawHTML(x) for x in ('', _('Type'), _('Regular Expression'), _('Substitution'))]
                table.set_header(1)

                for k in keys:
                    show = CTK.ComboCfg('%s!%s!show' %(key, k), trans_options(REDIR_SHOW))
                    regex = CTK.TextCfg ('%s!%s!regex' %(key, k))
                    subst = CTK.TextCfg ('%s!%s!substring'%(key, k))

                    remove = None
                    if len(keys) >= 2:
                        remove = CTK.ImageStock('del')
                        remove.bind('click', CTK.JS.Ajax (URL_APPLY, data={'%s!%s'%(key, k): ''},
                                                          complete = refresh.JS_to_refresh()))

                    table += [None, show, regex, subst, remove]

                    table[-1].props['id'] = k
                    table[-1][1].props['class'] = 'dragHandle'

                submit = CTK.Submitter (URL_APPLY)
                submit += table
                self += CTK.Indenter (submit)

    class AddNew_Button (CTK.Box):
        class Content (CTK.Container):
            def __init__ (self, key):
                CTK.Container.__init__ (self)

                table = CTK.PropsTable()
                table.Add (_('Show'), CTK.ComboCfg('tmp!new_show', trans_options(REDIR_SHOW), {'class': 'noauto'}), _(NOTE_SHOW))
                table.Add (_('Regular Expression'), CTK.TextCfg('tmp!new_regex', False, {'class': 'noauto'}), _(NOTE_REGEX))
                table.Add (_('Substitution'), CTK.TextCfg('tmp!new_subst', False, {'class': 'noauto'}), _(NOTE_SUBSTITUTION))

                submit = CTK.Submitter(URL_APPLY)
                submit += CTK.Hidden ('key', '%s!rewrite'%(key))
                submit += table
                self += submit

        def __init__ (self, key):
            CTK.Box.__init__ (self, {'class': 'mime-button'})

            # Add New
            dialog = CTK.Dialog ({'title': _('Add New Regular Expression'), 'width': 540})
            dialog.AddButton (_('Add'), dialog.JS_to_trigger('submit'))
            dialog.AddButton (_('Cancel'), "close")
            dialog += self.Content (key)

            button = CTK.Button(_('Add New RegEx'))
            button.bind ('click', dialog.JS_to_show())
            dialog.bind ('submit_success', dialog.JS_to_close())
            dialog.bind ('submit_success', self.JS_to_trigger('submit_success'));

            self += button
            self += dialog

    def __init__ (self, key, **kwargs):
        kwargs['show_document_root'] = False
        Handler.PluginHandler.__init__ (self, key, **kwargs)
        Handler.PluginHandler.AddCommon (self)

        # List
        refresh = CTK.Refreshable ({'id': 'plugin_redir'})
        refresh.register (lambda: self.Content(refresh, '%s!rewrite'%(key)).Render())
        self += refresh

        # New
        button = self.AddNew_Button (key)
        button.bind ('submit_success', refresh.JS_to_refresh ())
        self += CTK.Indenter (button)


CTK.publish ('^%s'%(URL_APPLY), commit, method="POST")
Something went wrong with that request. Please try again.