File: ModuleErrorRedir.py

package info (click to toggle)
cherokee 0.7.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,808 kB
  • ctags: 6,577
  • sloc: ansic: 45,071; python: 9,628; sh: 9,468; makefile: 1,639; xml: 61; perl: 32
file content (56 lines) | stat: -rw-r--r-- 1,798 bytes parent folder | download
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
import validations

from Form import *
from Table import *
from Module import *
from consts import *

DATA_VALIDATION = [
    ("new_error_url", validations.is_url_or_path)
]


class ModuleErrorRedir (Module, FormHelper):
    PROPERTIES = [x[0] for x in ERROR_CODES]

    def __init__ (self, cfg, prefix, submit_url):
        Module.__init__ (self, 'error_redir', cfg, prefix, submit_url)
        FormHelper.__init__ (self, 'error_redir', cfg)

    def _op_render (self):
        txt = ''
        
        # Render error list
        errors = self._cfg[self._prefix]
        if errors and errors.has_child():
            txt += '<h3>Configured error codes</h3>'
            table = Table(3,1)
            table += ('Error Code', 'URL', '')
            for error in errors:
                js = "post_del_key('/ajax/update', '%s!%s');" % (self._prefix, error)
                link_del = self.InstanceImage ("bin.png", "Delete", border="0", onClick=js)
                table += (error, self._cfg.get_val('%s!%s'%(self._prefix,error)), link_del)
            txt += str(table)

        # New error
        txt += '<h3>Add error codes</h3>'
        table = Table(3,1)
        table += ('Error', 'URL', '')

        options = EntryOptions ('new_error_code', ERROR_CODES)
        entry = self.InstanceEntry('new_error_url', 'text', size=30)
        table += (options, entry, SUBMIT_BUTTON)

        txt += str(table)
        return txt

    def _op_apply_changes (self, uri, post):
        self.ValidateChange_SingleKey ('new_error_url', post, DATA_VALIDATION)
        if self.has_errors():
            return

        new_error = post.pop('new_error_code')
        new_url   = post.pop('new_error_url')

        if new_error and new_url:
            self._cfg['%s!%s'%(self._prefix, new_error)] = new_url