File: Form.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 (452 lines) | stat: -rw-r--r-- 14,786 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import re
import types

from Entry import *
from Module import *

FORM_TEMPLATE = """
<form action="%(action)s" method="%(method)s">
  %(content)s
  %(submit)s
  <input type="hidden" name="is_submit" value="1" />
</form>
"""

SUBMIT_BUTTON = """
<input type="submit" %(submit_props)s />
"""
DEFAULT_SUBMIT_VALUE= 'value="Submit"'

class WebComponent:
    def __init__ (self, id, cfg):
        self._id  = id
        self._cfg = cfg

    def _op_handler (self, uri, post):
        if post.get_val('is_submit'):
            tmp = self._op_apply_changes (uri, post)
            if tmp:
                return tmp
        return self._op_render()

    def _op_render (self):
        raise "Should have been overridden"

    def _apply_changes (self, uri, post):
        raise "Should have been overridden"

    def HandleRequest (self, uri, post):
        # Is this a form submit
        is_submit = post.get_val('is_submit')

        # Check the URL
        parts = uri.split('/')[2:]        
        if parts:
            ruri = '/' + '/'.join(parts)
        else:
            ruri = '/'

        if len(ruri) <= 1 and not is_submit:
            return self._op_render()

        return self._op_handler(ruri, post)

class Form:
    def __init__ (self, action, method='post', add_submit=True):
        self._action       = action
        self._method       = method
        self._add_submit   = add_submit
        
    def Render (self, content='', submit_props='' ):
        keys = {'submit':       '',
                'submit_props': submit_props,
                'content':      content,
                'action':       self._action,
                'method':       self._method}
                
        if self._add_submit:
            keys['submit'] = SUBMIT_BUTTON

        render = FORM_TEMPLATE
        while '%(' in render:
            render = render % keys
        return render


class FormHelper (WebComponent):
    options_wrap_num = 1

    def __init__ (self, id, cfg):
        WebComponent.__init__ (self, id, cfg)

        self.errors      = {}
        self.submit_url  = None
        
    def set_submit_url (self, url):
        self.submit_url = url

    def Indent (self, content):
        return '<div class="indented">%s</div>' %(content)
        
    def Dialog (self, txt, type_='information'):
        return '<div class="dialog-%s">%s</div>' % (type_, txt)

    def HiddenInput (self, key, value):
        return '<input type="hidden" value="%s" id="%s" name="%s" />' % (value, key, key)

    def InstanceEntry (self, cfg_key, tipe, **kwargs):
        # Instance an Entry
        entry = Entry (cfg_key, tipe, self._cfg, **kwargs)
        txt = str(entry)

        # Check whether there is a related error
        if cfg_key in self.errors.keys():
            msg, val = self.errors[cfg_key]
            if val:
                txt += '<div class="error"><b>%s</b>: %s</div>' % (val, msg)
            else:
                txt += '<div class="error">%s</div>' % (msg)
        return txt

    def Label(self, title, for_id):
        txt = '<label for="%s">%s</label>' % (for_id, title)
        return txt

    def InstanceTab (self, entries):
        # HTML
        txt = '<dl class="tab" id="tab_%s">' % (self._id)
        num = 0
        for title, content in entries:
            txt += '<dt num="%d">%s</dt>\n' % (num, title)
            txt += '<dd>%s</dd>\n' % (content)
            num += 1
        txt += '</dl>'

        # Javascript
        txt += '''
        <script type="text/javascript">
          var settings = {
             autoheight: true,
             alwaysOpen: true,
             animated:   false
          };

          open_tab = get_cookie('open_tab');
          if (open_tab) {
            settings['active'] = parseInt(open_tab);
          } 

          jQuery("#tab_%s").Accordion(settings).change(
            function (event, newHeader, oldHeader) { 
              document.cookie = "open_tab=" + newHeader.attr("num");
          });
        </script>
        ''' % (self._id)
        return txt

    def AddTableEntry (self, table, title, cfg_key, extra_cols=None):
        # Get the entry
        txt = self.InstanceEntry (cfg_key, 'text')

        # Add to the table
        label = self.Label(title, cfg_key);
        tup = (label, txt)
        if extra_cols:
            tup += extra_cols
        table += tup

    def InstanceButton (self, name, **kwargs):
        extra = ""
        for karg in kwargs:
            extra += '%s="%s" '%(karg, kwargs[karg])
        return '<input type="button" value="%s" %s/>' % (name, extra)

    def InstanceImage (self, name, alt, **kwargs):
        extra = ""
        for karg in kwargs:
            extra += '%s="%s" '%(karg, kwargs[karg])
        return '<img src="/static/images/%s" alt="%s" %s/>' % (name, alt, extra)

    def _get_auto_wrap_id (self):
        return "options_wrap_%d" % (FormHelper.options_wrap_num)

    def InstanceOptions (self, cfg_key, options, *args, **kwargs):
        value = self._cfg.get_val (cfg_key)
        if value != None:
            ops = EntryOptions (cfg_key, options, selected=value, *args, **kwargs)
        else:
            ops = EntryOptions (cfg_key, options, *args, **kwargs)

        # Auto wrap
        auto_wrap_id = self._get_auto_wrap_id()
        FormHelper.options_wrap_num += 1

        ops = '<div id="%s" name="%s">%s</div>'%(auto_wrap_id, auto_wrap_id, ops)
        return (ops, value, auto_wrap_id)

    def AddTableOptions (self, table, title, cfg_key, options, *args, **kwargs):
        entry, value, wrap = self.InstanceOptions (cfg_key, options, *args, **kwargs)

        label = self.Label(title, cfg_key)
        table += (label, entry)

        return value

    def AddTableOptions_Ajax (self, table, title, cfg_key, options, *args, **kwargs):
        wrap_id = self._get_auto_wrap_id()
        js = "options_changed('/ajax/update','%s','%s');" % (cfg_key, wrap_id)
        kwargs['onChange'] = js

        return self.AddTableOptions (table, title, cfg_key, options, *args, **kwargs)

    def AddPropOptions_Ajax (self, table, title, cfg_key, options, comment, *args, **kwargs):
        wrap_id = self._get_auto_wrap_id()
        js = "options_changed('/ajax/update','%s','%s');" % (cfg_key, wrap_id)
        kwargs['onChange'] = js

        return self.AddPropOptions (table, title, cfg_key, options, comment, *args, **kwargs)

    def AddPropOptions_Reload (self, table, title, cfg_key, options, comment, **kwargs):
        assert (self.submit_url)

        # The Table entry itself
        auto_wrap_id = self._get_auto_wrap_id()
        js = "options_changed('/ajax/update','%s','%s');" % (cfg_key, auto_wrap_id)
        kwargs['onChange'] = js
        name = self.AddPropOptions (table, title, cfg_key, options, comment, **kwargs)
        
        # If there was no cfg value, pick the first
        if not name:
            name = options[0][0]
        
        # Render active option
        if name:
            try:
                # Inherit the errors, if any
                kwargs['errors'] = self.errors
                props_widget = module_obj_factory (name, self._cfg, cfg_key, 
                                                   self.submit_url, **kwargs)
                render = props_widget._op_render()
            except IOError:
                render = "Couldn't load the properties module: %s" % (name)
        else:
            render = ''
        
        return render

    def AddTableOptions_Reload (self, table, title, cfg_key, options, **kwargs):
        assert (self.submit_url)
        print "DEPRECATED: AddTableOptions_Reload"

        # The Table entry itself
        auto_wrap_id = self._get_auto_wrap_id()
        js = "options_changed('/ajax/update','%s','%s');" % (cfg_key, auto_wrap_id)
        name = self.AddTableOptions (table, title, cfg_key, options, onChange=js)
        
        # If there was no cfg value, pick the first
        if not name:
            name = options[0][0]
        
        # Render active option
        if name:
            try:
                # Inherit the errors, if any
                kwargs['errors'] = self.errors
                props_widget = module_obj_factory (name, self._cfg, cfg_key, 
                                                   self.submit_url, **kwargs)
                render = props_widget._op_render()
            except IOError:
                render = "Couldn't load the properties module: %s" % (name)
        else:
            render = ''
        
        return render

    def InstanceCheckbox (self, cfg_key, default=None):
        try:
            tmp = self._cfg[cfg_key].value.lower()
            if tmp in ["on", "1", "true"]: 
                value = '1'
            else:
                value = '0'
        except:
            value = None

        if value == '1':
            entry = Entry (cfg_key, 'checkbox', checked=value)
        elif value == '0':
            entry = Entry (cfg_key, 'checkbox')
        else:
            if default == True:
                entry = Entry (cfg_key, 'checkbox', checked='1')
            elif default == False:
                entry = Entry (cfg_key, 'checkbox')
            else:
                entry = Entry (cfg_key, 'checkbox')
        return entry

    def AddTableCheckbox (self, table, title, cfg_key, default=None):
        entry = self.InstanceCheckbox (cfg_key, default)
        label = self.Label(title, cfg_key);
        table += (label, entry)

    # Errors
    #

    def _error_add (self, key, wrong_val, msg):
        self.errors[key] = (msg, str(wrong_val))
        
    def has_errors (self):
        return len(self.errors) > 0

    # Applying changes
    #
    
    def Validate_NotEmpty (self, post, cfg_key, error_msg):
        try:
            cfg_val = self._cfg[cfg_key].value
        except:
            cfg_val = None

        if not cfg_val and \
           not post.get_val(cfg_key):
            self._error_add (cfg_key, '', error_msg)
    
    def ValidateChange_SingleKey (self, key, post, validation):
        for regex, tmp in validation:
            pass_cfg = False

            if type(tmp) == types.FunctionType:
                validation_func = tmp

            elif type(tmp) == types.TupleType:
                validation_func = tmp[0]
                for k in tmp[1:]:
                    if k == 'cfg':
                        pass_cfg = True
                    else:
                        print "UNKNOWN validation option:", k

            p = re.compile (regex)
            if p.match (key):
                value = post.get_val(key)
                if not value:
                    continue
                try:
                    if pass_cfg:
                        tmp = validation_func (value, self._cfg)
                    else:
                        tmp = validation_func (value)
                    post[key] = [tmp]
                except ValueError, error:
                    self._error_add (key, value, error)

    def _ValidateChanges (self, post, validation):
        for post_entry in post:
            self.ValidateChange_SingleKey (post_entry, post, validation)

    def ApplyCheckbox (self, post, cfg_key):
        if cfg_key in self.errors:
            return

        if cfg_key in post:
            value = post.pop(cfg_key)
            if value.lower() in ['on', '1']:
                self._cfg[cfg_key] = '1'
            else:
                self._cfg[cfg_key] = '0'
        else:
            self._cfg[cfg_key] = "0"

    def ApplyChanges (self, checkboxes, post, validation=None):
        # Validate changes
        if validation:
            self._ValidateChanges (post, validation)
        
        # Apply checkboxes
        for key in checkboxes:
            self.ApplyCheckbox (post, key)

        # Apply text entries
        for confkey in post:
            if not '!' in confkey:
                continue

            if confkey in self.errors:
                continue
            if not confkey in checkboxes:
                value = post[confkey][0]
                if not value:
                    del (self._cfg[confkey])
                else:
                    self._cfg[confkey] = value
        
    def ApplyChangesPrefix (self, prefix, checkboxes, post, validation=None):
        checkboxes_pre = ["%s!%s"%(prefix, x) for x in checkboxes]
        return self.ApplyChanges (checkboxes_pre, post, validation)

    def ApplyChanges_OptionModule (self, cfg_key, uri, post):
        # Read the option entry value
        name = self._cfg.get_val(cfg_key)
        if not name: return

        # Instance module and apply the changes
        module = module_obj_factory (name, self._cfg, cfg_key, self.submit_url)
        module._op_apply_changes (uri, post)

        # Include module errors
        for error in module.errors:
            self.errors[error] = module.errors[error]

        # Clean up properties
        props = module.__class__.PROPERTIES

        to_be_deleted = []
        for entry in self._cfg[cfg_key]:
            prop_cfg = "%s!%s" % (cfg_key, entry)
            if entry not in props:
                to_be_deleted.append (prop_cfg)

        for entry in to_be_deleted:
            del(self._cfg[entry])

    def AddProp (self, table, title, cfg_key, entry, comment=None):
        label = self.Label (title, cfg_key);
        table += (label, entry, comment)

    def AddPropEntry (self, table, title, cfg_key, comment=None, **kwargs):
        entry = self.InstanceEntry (cfg_key, 'text', **kwargs)
        self.AddProp (table, title, cfg_key, entry, comment)

    def AddPropCheck (self, table, title, cfg_key, default, comment=None):
        entry = self.InstanceCheckbox (cfg_key, default)
        self.AddProp (table, title, cfg_key, entry, comment)

    def AddPropOptions (self, table, title, cfg_key, options, comment=None, **kwargs):
        entry, v, w = self.InstanceOptions (cfg_key, options, **kwargs)
        self.AddProp (table, title, cfg_key, entry, comment)
        return v


class TableProps:
    def __init__ (self):
        self._content = []

    def __add__ (self, (title, content, comment)):
        self._content.append((title, content, comment))

    def _render_entry (self, (title, content, comment)):
        txt  = '<table class="tableprop">'
        txt += '  <tr><th class="title">%s</th><td>%s</td></tr>' % (title, content)
        txt += '  <tr><td colspan="2"><div class="comment">%s</div></td></tr>' % (comment)
        txt += '</table>'
        return txt

    def __str__ (self):
        tmp = []
        for entry in self._content:
            tmp.append (self._render_entry(entry))

        txt = "<hr />".join(tmp)
        return '<div class="tableprop_block">%s</div>' % (txt)