Blame generator/form.py

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