root/cherokee/trunk/admin/Entry.py

Revision 2321, 2.5 kB (checked in by aperez, 1 month ago)

--

Line 
1 class Entry:
2     def __init__ (self, name, type, cfg=None, *args, **kwargs):
3         self._name   = name
4         self._type   = type
5         self._args   = args
6         self._kwargs = kwargs
7
8         if cfg:
9             self._init_value (cfg)
10
11         if not 'size' in kwargs:
12             self._kwargs['size'] = 40
13
14         # Entries with req=True will be checked against
15         # check_all_or_none before autosubmissions
16         str_class = ''
17         if 'req' in kwargs and kwargs['req'] == True:
18             str_class += 'required '
19             del kwargs['req']
20
21         if 'noautosubmit' in kwargs and kwargs['noautosubmit'] == True:
22             str_class += 'noautosubmit '
23             del kwargs['noautosubmit']
24
25         if str_class:
26             self._kwargs['class'] = str_class
27
28     def _init_value (self, cfg):
29         try:
30             value = cfg[self._name].value
31             self._kwargs['value'] = value
32         except:
33             pass
34
35     def __str__ (self):
36         suffix = ""
37         if self._type == "checkbox":
38             if "quiet" in self._kwargs and self._kwargs["quiet"]:
39                 del(self._kwargs["quiet"])
40             else:
41                 suffix = " Enabled"
42
43         error = '<div id="error_%s"></div>' % (self._name)
44         props = 'id="%s" name="%s" type="%s"' % (self._name, self._name, self._type)
45
46         for prop in self._kwargs:
47             props += ' %s="%s"' % (prop, self._kwargs[prop])
48
49         return "<input %s />"%(props) + suffix + error
50
51
52
53 class EntryOptions:
54     def __init__ (self, name, options, *args, **kwargs):
55         self._name     = name
56         self._opts     = options
57         self._args     = args
58         self._kwargs   = kwargs
59         self._selected = None
60
61         if 'noautosubmit' in kwargs and kwargs['noautosubmit'] == True:
62             self._kwargs['class'] = 'noautosubmit '
63             del kwargs['noautosubmit']
64
65     def __str__ (self):
66         props = 'id="%s" name="%s"' % (self._name, self._name)
67
68         for prop in self._kwargs:
69             if prop == "selected":
70                 self._selected = self._kwargs[prop]
71             else:
72                 props += ' %s="%s"' % (prop, self._kwargs[prop])
73
74         txt = '<select %s>\n' % (props)
75         for option in self._opts:
76             name, label = option
77             if self._selected == name:
78                 txt += '\t<option value="%s" selected>%s</option>\n' % (name, label)
79             else:
80                 txt += '\t<option value="%s">%s</option>\n' % (name, label)
81
82         txt += '</select>\n'
83
84         return txt
Note: See TracBrowser for help on using the browser.