class Form():
FORM_BEGIN = '<form method="POST">\n'
FORM_END = '</form>\n'
GROUP_BEGIN = '<fieldset>\n'
GROUP_END = '</fieldset>\n'
TITLE_BEGIN = '<legend>'
TITLE_END = '</legend>\n'
FIELD_BEGIN = '<div>'
FIELD_END = '</div>\n'
NAME_BEGIN = '<span>'
NAME_END = '</span>\n'
VALUE_BEGIN = '<div>'
VALUE_END = '</div>\n'
BEGIN = FORM_BEGIN + GROUP_BEGIN
END = GROUP_END + FORM_END
def __init__(self, request):
self.request = request
self.answer = self.request.answer
self.content = ''
def title(self, title):
return self.TITLE_BEGIN \
+ self.answer.te(title) \
+ self.TITLE_END
def name(self, name):
return self.NAME_BEGIN \
+ self.answer.te(name) \
+ self.NAME_END
def value_raw(self, raw):
return self.VALUE_BEGIN \
+ raw \
+ self.VALUE_END
def input(self, name, type, value = None):
type = self.answer.e(type)
name = self.answer.e(name)
value = self.answer.encodedfield(name) if value is None else self.answer.e(value)
content = '<input'
if type: content += ' type="' + type + '"'
if name: content += ' name="' + name + '"'
if value: content += ' value="' + value + '"'
content += ' />'
return content
def checkbox(self, name, value = None):
name = self.answer.e(name)
value = bool(self.answer.fields.get(name) if value is None else value)
content = '<input type="checkbox" '
if name: content += ' name="' + name + '"'
if value: content += ' checked'
content += ' />'
return content
def select(self, name, variants, value = None):
name = self.answer.e(name)
value = self.answer.encodedfield(name) if value is None else self.answer.e(value)
content = '<select'
if name: content += ' name="' + name + '"'
content += '>\n'
for k, v in variants.items():
kk = self.answer.e(k)
if kk:
content += '<option value="' + kk + '"'
if kk == value: content += 'selected'
content += '>'
content += self.answer.te(v)
content += '</option>\n'
content += '</select>\n'
return content
def textarea(self, name, value = None):
name = self.answer.e(name)
value = self.answer.encodedfield(name) if value is None else self.answer.e(value)
content = '<textarea'
if name: content += ' name="' + name + '"'
content += '>\n'
content += value
content += '</textarea>'
return content
def field_raw(self, name, raw):
content = self.FIELD_BEGIN;
content += self.name(name)
content += self.value_raw(raw)
content += self.FIELD_END;
return content
def add_field_raw(self, name, raw):
self.content += self.field_raw(name, raw)
def add_hidden(self, name, value):
self.content += self.input(name, 'hidden', '' if value is None else value)
def add_input(self, title, *args, **kvargs):
self.add_field_raw(title, self.input(*args, **kvargs))
def add_checkbox(self, title, *args, **kvargs):
self.add_field_raw(title, self.checkbox(*args, **kvargs))
def add_select(self, title, *args, **kvargs):
self.add_field_raw(title, self.select(*args, **kvargs))
def add_textarea(self, title, *args, **kvargs):
self.add_field_raw(title, self.textarea(*args, **kvargs))
def add_submit(self, title = 'Submit'):
self.add_field_raw('', self.input('', 'submit', self.answer.t(title)))
def begin(self, title, action):
self.content += self.BEGIN
if title: self.content += self.title(title)
if action: self.add_hidden('action', action)
def end(self):
self.content += self.END