示例#1
0
 def process(self, context, boundTo, data):
     """Password needs to look at two passwords in the data,
     """
     typed = self.original
     pw1 = data[0]
     args = context.locate(inevow.IRequest).args
     binding = context.locate(iformless.IBinding)
     pw2 = args.get("%s____2" % binding.name, [''])[0]
     if typed.strip:
         pw1 = pw1.strip()
         pw2 = pw2.strip()
     if pw1 != pw2:
         raise formless.InputError("Passwords do not match. Please reenter.")
     elif pw1 == '':
         if typed.required:
             raise formless.InputError(typed.requiredFailMessage)
         else:
             return typed.null
     val = data[0]
     if typed.unicode:
         try:
             val = val.decode(getPOSTCharset(context), 'replace')
         except LookupError:
             val = val.decode('utf-8', 'replace')
     try:
         return typed.coerce(val, boundTo)
     except TypeError:
         warnings.warn('Typed.coerce takes two values now, the value to coerce and the configurable in whose context the coerce is taking place. %s %s' % (typed.__class__, typed))
         return typed.coerce(data[0])
示例#2
0
    def process(self, context, boundTo, data):

        typed = self.original
        bind = context.locate(iformless.IBinding)

        # TOTAL HACK: this comes from outer space
        fields = context.locate(inevow.IRequest).fields
        try:
            field = fields[bind.name]
        except KeyError:
            return ''
        
        def hasContent(field):
            """Test if the uploaded file has any content by looking for a single byte.
            """
            file = field.file
            pos = file.tell()
            file.seek(0)
            ch = file.read(1)
            file.seek(pos)
            return ch != ''
        
        # Testing for required'ness is a bit of a hack (not my fault!) ...
        # The upload is only considered missing if both the file name and content
        # are empty. That allows for files with content called ' ' and empty files
        # with a sensible name.

        # field might be a list, if multiple files were uploaded with the same
        # name.
        if isinstance(field, list):
            fieldList = field
        else:
            fieldList = [field]

        for maybeEmptyField in fieldList:
            if maybeEmptyField.filename.strip() or hasContent(maybeEmptyField):
                break
        else:
            if typed.required:
                raise formless.InputError(typed.requiredFailMessage)
            else:
                return typed.null
            
        return field
示例#3
0
 def process(self, context, boundTo, data):
     """data is a list of strings at this point
     """
     typed = self.original
     val = data[0]
     if typed.unicode:
         try:
             val = val.decode(getPOSTCharset(context), 'replace')
         except LookupError:
             val = val.decode('utf-8', 'replace')
     if typed.strip:
         val = val.strip()
     if val == '' or val is None:
         if typed.required:
             raise formless.InputError(typed.requiredFailMessage)
         else:
             return typed.null
     try:
         return typed.coerce(val, boundTo)
     except TypeError, e:
         warnings.warn('Typed.coerce takes two values now, the value to coerce and the configurable in whose context the coerce is taking place. %s %s' % (typed.__class__, typed))
         return typed.coerce(val)
示例#4
0
 def coerce(self, data):
     one, two = data
     if (one, two) != (6, 9):
         raise formless.InputError(
             "What do you get when you multiply six by nine?")