Esempio n. 1
0
class IntegerValidator(StringBaseValidator):
    property_names = StringBaseValidator.property_names +\
                     ['start', 'end']

    start = fields.IntegerField(
        'start',
        title='Start',
        description=(
            "The integer entered by the user must be larger than or equal to "
            "this value. If left empty, there is no minimum."),
        default="",
        required=0)

    end = fields.IntegerField(
        'end',
        title='End',
        description=(
            "The integer entered by the user must be smaller than this "
            "value. If left empty, there is no maximum."),
        default="",
        required=0)

    message_names = StringBaseValidator.message_names +\
                    ['not_integer', 'integer_out_of_range']

    not_integer = 'You did not enter an integer.'
    integer_out_of_range = 'The integer you entered was out of range.'

    def validate(self, field, key, REQUEST):
        value = StringBaseValidator.validate(self, field, key, REQUEST)
        # we need to add this check again
        if value == "" and not field.get_value('required'):
            return value

        value = normalizeFullWidthNumber(value)

        try:
            if value.find(' ') > 0:
                value = value.replace(' ', '')
            value = int(value)
        except ValueError:
            self.raise_error('not_integer', field)

        start = field.get_value('start')
        end = field.get_value('end')
        if start != "" and value < start:
            self.raise_error('integer_out_of_range', field)
        if end != "" and value >= end:
            self.raise_error('integer_out_of_range', field)
        return value
Esempio n. 2
0
def create_settings_form():
    """Create settings form for ZMIForm.
    """
    form = BasicForm('manage_settings')

    title = fields.StringField('title',
                               title="Title",
                               required=False,
                               default="")
    row_length = fields.IntegerField(
        'row_length',
        title='Number of groups in row (in order tab)',
        required=True,
        default=4)
    name = fields.StringField('name',
                              title="Form name",
                              required=False,
                              default="")
    action = fields.StringField('action',
                                title='Form action',
                                required=False,
                                default="")
    method = fields.ListField('method',
                              title='Form method',
                              items=[('POST', 'POST'), ('GET', 'GET')],
                              required=True,
                              size=1,
                              default='POST')
    enctype = fields.ListField('enctype',
                               title='Form enctype',
                               items=[('No enctype', ""),
                                      ('application/x-www-form-urlencoded',
                                       'application/x-www-form-urlencoded'),
                                      ('multipart/form-data',
                                       'multipart/form-data')],
                               required=False,
                               size=1,
                               default=None)

    encoding = fields.StringField('encoding',
                                  title='Encoding of pages the form is in',
                                  default="UTF-8",
                                  required=True)

    stored_encoding = fields.StringField('stored_encoding',
                                         title='Encoding of form properties',
                                         default='ISO-8859-1',
                                         required=True)
    unicode_mode = fields.CheckBoxField('unicode_mode',
                                        title='Form properties are unicode',
                                        default=False,
                                        required=True)

    form.add_fields([
        title, row_length, name, action, method, enctype, encoding,
        stored_encoding, unicode_mode
    ])
    return form
Esempio n. 3
0
class StringValidator(StringBaseValidator):
    property_names = StringBaseValidator.property_names +\
                     ['unicode', 'max_length', 'truncate']

    unicode = fields.CheckBoxField(
        'unicode',
        title='Unicode',
        description=(
            "Checked if the field delivers a unicode string instead of an "
            "8-bit string."),
        default=0)

    max_length = fields.IntegerField(
        'max_length',
        title='Maximum length',
        description=(
            "The maximum amount of characters that can be entered in this "
            "field. If set to 0 or is left empty, there is no maximum. "
            "Note that this is server side validation."),
        default="",
        required=0)

    truncate = fields.CheckBoxField(
        'truncate',
        title='Truncate',
        description=
        ("If checked, truncate the field if it receives more input than is "
         "allowed. The normal behavior in this case is to raise a validation "
         "error, but the text can be silently truncated instead."),
        default=0)

    message_names = StringBaseValidator.message_names +\
                    ['too_long']

    too_long = 'Too much input was given.'

    def validate(self, field, key, REQUEST):
        value = StringBaseValidator.validate(self, field, key, REQUEST)
        if field.get_value('unicode'):
            # use acquisition to get encoding of form
            value = unicode(value, field.get_form_encoding())

        max_length = field.get_value('max_length') or 0
        truncate = field.get_value('truncate')

        if max_length > 0 and len(value) > max_length:
            if truncate:
                value = value[:max_length]
            else:
                self.raise_error('too_long', field)
        return value
Esempio n. 4
0
class LinesValidator(StringBaseValidator):
  property_names = StringBaseValidator.property_names +\
                    ['unicode', 'max_lines', 'max_linelength', 'max_length']

  unicode = fields.CheckBoxField('unicode',
                                  title='Unicode',
                                  description=(
      "Checked if the field delivers a unicode string instead of an "
      "8-bit string."),
                                  default=0)

  max_lines = fields.IntegerField('max_lines',
                                  title='Maximum lines',
                                  description=(
      "The maximum amount of lines a user can enter. If set to 0, "
      "or is left empty, there is no maximum."),
                                  default="",
                                  required=0)

  max_linelength = fields.IntegerField('max_linelength',
                                        title="Maximum length of line",
                                        description=(
      "The maximum length of a line. If set to 0 or is left empty, there "
      "is no maximum."),
                                        default="",
                                        required=0)

  max_length = fields.IntegerField('max_length',
                                    title="Maximum length (in characters)",
                                    description=(
      "The maximum total length in characters that the user may enter. "
      "If set to 0 or is left empty, there is no maximum."),
                                    default="",
                                    required=0)

  message_names = StringBaseValidator.message_names +\
                  ['too_many_lines', 'line_too_long', 'too_long']

  too_many_lines = 'You entered too many lines.'
  line_too_long = 'A line was too long.'
  too_long = 'You entered too many characters.'

  def validate(self, field, key, REQUEST):
    value = StringBaseValidator.validate(self, field, key, REQUEST)
    # Added as a patch for hidden values
    if isinstance(value, (list, tuple)):
      value = '\n'.join(value)
    # we need to add this check again
    if value == "" and not field.get_value('required'):
      return []
    if field.get_value('unicode'):
        value = unicode(value, field.get_form_encoding())
    # check whether the entire input is too long
    max_length = field.get_value('max_length') or 0
    if max_length and len(value) > max_length:
      self.raise_error('too_long', field)
    # split input into separate lines
    lines = value.replace('\r\n', '\n').split('\n')

    # check whether we have too many lines
    max_lines = field.get_value('max_lines') or 0
    if max_lines and len(lines) > max_lines:
      self.raise_error('too_many_lines', field)

    # strip extraneous data from lines and check whether each line is
    # short enough
    max_linelength = field.get_value('max_linelength') or 0
    result = []
    whitespace_preserve = field.get_value('whitespace_preserve')
    for line in lines:
      if not whitespace_preserve:
        line = line.strip()
      if max_linelength and len(line) > max_linelength:
        self.raise_error('line_too_long', field)
      result.append(line)

    return result