Exemplo n.º 1
0
    def test_sequencesequenceinteger_string_conversion(self):
        type = schemaish.Sequence(schemaish.Sequence(schemaish.Integer()))
        value = [[1,2,3],[4,5,6]]
        expected = '1,2,3\n4,5,6'
        actual = string_converter(type).from_type(value)
        self.assertEquals(actual,expected)

        value, expected = expected, value
        actual = string_converter(type).to_type(value)
        self.assertEquals(actual,expected)
Exemplo n.º 2
0
    def test_float_string_conversion(self):
        type = schemaish.Float()
        value = 1.28
        expected = '1.28'
        actual = string_converter(type).from_type(value)
        self.assertEquals(actual,expected)

        value, expected = expected, value
        actual = string_converter(type).to_type(value)
        self.assertEquals(actual,expected)
Exemplo n.º 3
0
    def test_sequenceboolean_string_conversion(self):
        type = schemaish.Sequence(schemaish.Boolean())
        value = [True, False, True, True]
        expected = 'True,False,True,True'
        actual = string_converter(type).from_type(value)
        self.assertEquals(actual, expected)

        value, expected = expected, value
        actual = string_converter(type).to_type(value)
        self.assertEquals(actual, expected)
Exemplo n.º 4
0
    def test_float_string_conversion(self):
        type = schemaish.Float()
        value = 1.28
        expected = '1.28'
        actual = string_converter(type).from_type(value)
        self.assertEquals(actual, expected)

        value, expected = expected, value
        actual = string_converter(type).to_type(value)
        self.assertEquals(actual, expected)
Exemplo n.º 5
0
    def test_sequencesequenceinteger_string_conversion(self):
        type = schemaish.Sequence(schemaish.Sequence(schemaish.Integer()))
        value = [[1, 2, 3], [4, 5, 6]]
        expected = '1,2,3\n4,5,6'
        actual = string_converter(type).from_type(value)
        self.assertEquals(actual, expected)

        value, expected = expected, value
        actual = string_converter(type).to_type(value)
        self.assertEquals(actual, expected)
Exemplo n.º 6
0
    def test_sequenceboolean_string_conversion(self):
        type = schemaish.Sequence(schemaish.Boolean())
        value = [True,False,True,True]
        expected = 'True,False,True,True'
        actual = string_converter(type).from_type(value)
        self.assertEquals(actual,expected)

        value, expected = expected, value
        actual = string_converter(type).to_type(value)
        self.assertEquals(actual,expected)
Exemplo n.º 7
0
    def test_sequencetupleintegerstring_string_conversion(self):
        type = schemaish.Sequence(schemaish.Tuple((schemaish.Integer(),schemaish.String())))
        value = [(1,'1'),(2,'2')]
        expected = '1,1\n2,2'
    
        actual = string_converter(type).from_type(value)
        self.assertEquals(actual,expected)

        value, expected = expected, value
        actual = string_converter(type).to_type(value)
        self.assertEquals(actual,expected)
Exemplo n.º 8
0
    def test_sequencetupleintegerstring_string_conversion(self):
        type = schemaish.Sequence(
            schemaish.Tuple((schemaish.Integer(), schemaish.String())))
        value = [(1, '1'), (2, '2')]
        expected = '1,1\n2,2'

        actual = string_converter(type).from_type(value)
        self.assertEquals(actual, expected)

        value, expected = expected, value
        actual = string_converter(type).to_type(value)
        self.assertEquals(actual, expected)
Exemplo n.º 9
0
    def test_date_string_conversion(self):
        type = schemaish.Date()
        value = date(1966,12,18)
        expected = '1966-12-18'
        actual = string_converter(type).from_type(value)
        self.assertEquals(actual,expected)

        value, expected = expected, value
        actual = string_converter(type).to_type(value)
        self.assertEquals(actual,expected)
        self.assertRaises(ConvertError, string_converter(type).to_type, 'nonsense')
        self.assertRaises(ConvertError, string_converter(type).to_type, '1990-1-1andabit')
Exemplo n.º 10
0
    def test_date_string_conversion(self):
        type = schemaish.Date()
        value = date(1966, 12, 18)
        expected = '1966-12-18'
        actual = string_converter(type).from_type(value)
        self.assertEquals(actual, expected)

        value, expected = expected, value
        actual = string_converter(type).to_type(value)
        self.assertEquals(actual, expected)
        self.assertRaises(ConvertError,
                          string_converter(type).to_type, 'nonsense')
        self.assertRaises(ConvertError,
                          string_converter(type).to_type, '1990-1-1andabit')
Exemplo n.º 11
0
 def test_file_string_converstion(self):
     # Just for my sanity.
     FileType = schemaish.type.File
     # Reference type.
     type = schemaish.File()
     # Check that it can't be converted without a file-like.
     self.assertRaises(ValueError, string_converter(type).from_type,
                       FileType(None, None, None))
     # Check that the file-likes content are returned.
     self.assertTrue(string_converter(type).from_type(FileType(StringIO('foo'), None, None)) == 'foo')
     # Check that a string is converted to a file.
     file = string_converter(type).to_type('foo')
     self.assertTrue(file.mimetype == 'text/plain')
     self.assertTrue(file.filename == 'content.txt')
     self.assertTrue(file.file.read() == 'foo')
Exemplo n.º 12
0
 def from_request_data(self, field, request_data):
     if request_data[0] == '':
         return self.empty
     if string_converter(field.attr).to_type(
             request_data[0]) == self.checked_value:
         return self.checked_value
     return self.unchecked_value
Exemplo n.º 13
0
 def to_request_data(self, field, data):
     """
     Iterate over the data, converting each one
     """
     if data is None: 
         return []
     return [string_converter(field.attr.attr).from_type(d) for d in data]
Exemplo n.º 14
0
 def pre_render(self, schema_type, data):
     """
     Iterate over the data, converting each one
     """
     if data is None: 
         return []
     return [string_converter(schema_type.attr).from_type(d) for d in data]
Exemplo n.º 15
0
 def test_tuple_unicode(self):
     schema = schemaish.Tuple([schemaish.String(), schemaish.String()])
     converter = string_converter(schema)
     self.assertEquals(converter.from_type(('£1'.decode('utf-8'), u'foo')),
                       '£1,foo'.decode('utf-8'))
     self.assertEquals(converter.to_type('£1,foo'.decode('utf-8')),
                       ('£1'.decode('utf-8'), u'foo'))
Exemplo n.º 16
0
 def to_request_data(self, field, data):
     """
     Iterate over the data, converting each one
     """
     if data is None:
         return []
     return [string_converter(field.attr.attr).from_type(d) for d in data]
Exemplo n.º 17
0
    def from_request_data(self, field, request_data):
        """
        Iterating to convert back to the source data
        """

        result = filter(None, [string_converter(field.attr.attr).to_type(d) for d in request_data])
        return result
Exemplo n.º 18
0
 def from_request_data(self, field, data):
     print 'in from ', data
     params = urllib.urlencode({
         'privatekey':
         self.privatekey,
         'remoteip':
         self.remoteip,
         'challenge':
         data['recaptcha_challenge_field'][0],
         'response':
         data['recaptcha_response_field'][0],
     })
     request = urllib2.Request(url="http://%s/verify" % self.VERIFY_SERVER,
                               data=params,
                               headers={
                                   "Content-type":
                                   "application/x-www-form-urlencoded",
                                   "User-agent": self.USER_AGENT
                               })
     response = urllib2.urlopen(request)
     return_values = response.read().splitlines()
     response.close()
     return_code = return_values[0]
     if (return_code == "true"):
         return string_converter(field.attr).to_type('True')
     else:
         raise ConvertError('reCAPTCHA failed')
Exemplo n.º 19
0
 def get_options(self, field):
     """
     Return all of the options for the widget
     """
     options = []
     for value, label in self.options:
         options.append( (string_converter(field.attr).from_type(value),label) )
     return options
Exemplo n.º 20
0
 def pre_render(self, schema_type, data):
     """
     populate the other choice if needed
     """
     string_data = string_converter(schema_type).from_type(data)
     if string_data in [value for value, label in self.options]:
         return {'select': ['...'], 'other': [string_data]}
     return {'select': [string_data], 'other': ['']}
Exemplo n.º 21
0
 def to_request_data(self, field, data):
     """
     Extract both the input and confirm fields
     """
     if data is None or (data == self.empty and self.roundtrip_empty is True):
         return self.none_value_as_request_data(field)
     string_data = string_converter(field.attr).from_type(data)
     return {'input': [string_data], 'confirm': [string_data]}
Exemplo n.º 22
0
 def to_request_data(self, field, data):
     """
     Extract both the input and confirm fields
     """
     string_data = string_converter(field.attr).from_type(data)
     if string_data is None:
         return {'input': [''], 'confirm': ['']}
     return {'input': [string_data], 'confirm': [string_data]}
Exemplo n.º 23
0
 def pre_render(self, schema_type, data):
     """
     Extract both the password and confirm fields
     """
     string_data = string_converter(schema_type).from_type(data)
     if string_data is None:
         return {'password': [''], 'confirm': ['']}
     return {'password': [string_data], 'confirm': [string_data]}
Exemplo n.º 24
0
 def get_none_option_value(self, field):
     """
     Get the default option (the 'unselected' option)
     """
     none_option =  string_converter(field.attr).from_type(self.none_option[0])
     if none_option is self.empty:
         return ''
     return none_option
Exemplo n.º 25
0
 def checked(self, field):
     """
     For each value, convert it and check to see if it matches the input data
     """
     if field.value and string_converter(field.attr).to_type(field.value[0]) == self.checked_value:
         return ' checked="checked"'
     else:
         return ''
Exemplo n.º 26
0
 def to_request_data(self, field, data):
     """
     We're using the converter options to allow processing sequence data
     using the csv module
     """
     string_data = string_converter(field.attr).from_type(data, converter_options=self.converter_options)
     if string_data is None:
         return ['']
     return [string_data]
Exemplo n.º 27
0
 def test_file_string_converstion(self):
     # Just for my sanity.
     FileType = schemaish.type.File
     # Reference type.
     type = schemaish.File()
     # Check that it can't be converted without a file-like.
     self.assertRaises(ValueError,
                       string_converter(type).from_type,
                       FileType(None, None, None))
     # Check that the file-likes content are returned.
     self.assertTrue(
         string_converter(type).from_type(
             FileType(StringIO('foo'), None, None)) == 'foo')
     # Check that a string is converted to a file.
     file = string_converter(type).to_type('foo')
     self.assertTrue(file.mimetype == 'text/plain')
     self.assertTrue(file.filename == 'content.txt')
     self.assertTrue(file.file.read() == 'foo')
Exemplo n.º 28
0
 def convert(self, schema_type, request_data):
     """
     If the request data exists, then we treat this as True
     """
     if len(request_data) == 0:
         out_string = 'False'
     else:
         out_string = 'True'
     return string_converter(schema_type).to_type(out_string)
Exemplo n.º 29
0
 def get_none_option_value(self, field):
     """
     Get the default option (the 'unselected' option)
     """
     none_option = string_converter(field.attr).from_type(
         self.none_option[0])
     if none_option is self.empty:
         return ''
     return none_option
Exemplo n.º 30
0
    def from_request_data(self, field, request_data):
        """
        Iterating to convert back to the source data
        """

        result = filter(None, [
            string_converter(field.attr.attr).to_type(d) for d in request_data
        ])
        return result
Exemplo n.º 31
0
 def checked(self, field):
     """
     For each value, convert it and check to see if it matches the input data
     """
     if field.value and field.value[0] and string_converter(
             field.attr).to_type(field.value[0]) == self.checked_value:
         return ' checked="checked"'
     else:
         return ''
Exemplo n.º 32
0
 def from_request_data(self, field, request_data):
     """
     after the form has been submitted, the request data is converted into
     to the schema type.
     """
     string_data = request_data[0]
     if string_data == '':
         return self.empty
     return string_converter(field.attr).to_type(string_data, converter_options=self.converter_options)
Exemplo n.º 33
0
 def get_options(self, field):
     """
     Return all of the options for the widget
     """
     options = []
     for value, label in self.options:
         options.append(
             (string_converter(field.attr).from_type(value), label))
     return options
Exemplo n.º 34
0
 def to_request_data(self, field, data):
     """
     Extract both the input and confirm fields
     """
     if data is None or (data == self.empty
                         and self.roundtrip_empty is True):
         return self.none_value_as_request_data(field)
     string_data = string_converter(field.attr).from_type(data)
     return {'input': [string_data], 'confirm': [string_data]}
Exemplo n.º 35
0
 def test_tuple_noneifying(self):
     schema = schemaish.Tuple([schemaish.Integer(), schemaish.String()])
     converter = string_converter(schema)
     self.assertEquals(converter.from_type((None, None)), ',')
     self.assertEquals(converter.from_type((None, '')), ',')
     self.assertEquals(converter.from_type((None, 'foo')), ',foo')
     self.assertEquals(converter.from_type((1, None)), '1,')
     self.assertEquals(converter.to_type(','), (None, None))
     self.assertEquals(converter.to_type(',foo'), (None, 'foo'))
     self.assertEquals(converter.to_type('1,'), (1, None))
Exemplo n.º 36
0
 def pre_render(self, schema_type, data):
     """
     We're using the converter options to allow processing sequence data
     using the csv module
     """
     string_data = string_converter(schema_type).from_type(data, \
         converter_options=self.converter_options)
     if string_data is None:
         return ['']
     return [string_data]
Exemplo n.º 37
0
 def to_request_data(self, field, data):
     """
     populate the other choice if needed
     """
     string_data = string_converter(field.attr).from_type(data)
     if string_data is None:
         return {'select': [self.get_none_option_value(field)], 'other': ['']}
     if string_data in [value for value, label in self.options]:
         return {'select': [string_data], 'other': ['']}
     return {'select': [self.other_option[0]], 'other': [string_data]}
Exemplo n.º 38
0
    def from_request_data(self, field, request_data):
        """
        If we don't have a choice, set a blank value
        """
        string_data = request_data[0]

        if string_data == '':
            return self.empty

        return string_converter(field.attr).to_type(string_data)
Exemplo n.º 39
0
 def from_request_data(self, field, request_data):
     L = []
     for string_data in request_data:
         if self.strip is True:
             string_data = string_data.strip()
         if not string_data:
             continue
         value = string_converter(field.attr.attr).to_type(string_data, converter_options=self.converter_options)
         L.append(value)
     return L
Exemplo n.º 40
0
 def pre_render(self, schema_type, data):
     """
     Before the widget is rendered, the data is converted to a string
     format.If the data is None then we return an empty string. The sequence
     is request data representation.
     """
     string_data = string_converter(schema_type).from_type(data)
     if string_data is None:
         return ['']
     return [string_data]
Exemplo n.º 41
0
    def from_request_data(self, field, request_data):
        """
        If we don't have a choice, set a blank value
        """
        string_data = request_data[0]

        if string_data == '':
            return self.empty

        return string_converter(field.attr).to_type(string_data)
Exemplo n.º 42
0
 def to_request_data(self, field, data):
     """
     populate the other choice if needed
     """
     string_data = string_converter(field.attr).from_type(data)
     if string_data is None:
         return self.none_value_as_request_data(field)
     if string_data in [value for value, label in self.options]:
         return {'select': [string_data], 'other': ['']}
     return {'select': [self.other_option[0]], 'other': [string_data]}
Exemplo n.º 43
0
 def to_request_data(self, field, data):
     """
     Before the widget is rendered, the data is converted to a string
     format.If the data is None then we return an empty string. The sequence
     is request data representation.
     """
     if data is None:
         return ['']
     string_data = string_converter(field.attr).from_type(data, converter_options=self.converter_options)
     return [string_data]
Exemplo n.º 44
0
 def from_request_data(self, field, request_data):
     """
     after the form has been submitted, the request data is converted into
     to the schema type.
     """
     if request_data == self.none_value_as_request_data(field):
         data = self.empty
     else:
         data = string_converter(field.attr).to_type(request_data[0], converter_options=self.converter_options)
     return data
Exemplo n.º 45
0
    def test_boolean_string_conversion(self):
        type = schemaish.Boolean()
        value = True
        expected = 'True'
        actual = string_converter(type).from_type(value)
        self.assertEquals(actual,expected)

        value, expected = expected, value
        actual = string_converter(type).to_type(value)
        self.assertEquals(actual,expected)

        value = False
        expected = 'False'
        actual = string_converter(type).from_type(value)
        self.assertEquals(actual,expected)

        value, expected = expected, value
        actual = string_converter(type).to_type(value)
        self.assertEquals(actual,expected)
Exemplo n.º 46
0
 def test_tuple_noneifying(self):
     schema = schemaish.Tuple([schemaish.Integer(), schemaish.String()])
     converter = string_converter(schema)
     self.assertEquals(converter.from_type((None, None)), ',')
     self.assertEquals(converter.from_type((None, '')), ',')
     self.assertEquals(converter.from_type((None, 'foo')), ',foo')
     self.assertEquals(converter.from_type((1, None)), '1,')
     self.assertEquals(converter.to_type(','), (None, None))
     self.assertEquals(converter.to_type(',foo'), (None, 'foo'))
     self.assertEquals(converter.to_type('1,'), (1, None))
Exemplo n.º 47
0
 def from_request_data(self, field, request_data):
     """
     Default to stripping whitespace
     """
     string_data = request_data[0]
     if self.strip is True:
         string_data = string_data.strip()
     if string_data == '':
         return self.empty
     return string_converter(field.attr).to_type(string_data, converter_options=self.converter_options)
Exemplo n.º 48
0
 def to_request_data(self, field, data):
     """
     Before the widget is rendered, the data is converted to a string
     format.If the data is None then we return an empty string. The sequence
     is request data representation.
     """
     if data is None or (data == self.empty and self.roundtrip_empty is True):
         return self.none_value_as_request_data(field)
     string_data = string_converter(field.attr).from_type(data, converter_options=self.converter_options)
     return [string_data]
Exemplo n.º 49
0
    def test_boolean_string_conversion(self):
        type = schemaish.Boolean()
        value = True
        expected = 'True'
        actual = string_converter(type).from_type(value)
        self.assertEquals(actual, expected)

        value, expected = expected, value
        actual = string_converter(type).to_type(value)
        self.assertEquals(actual, expected)

        value = False
        expected = 'False'
        actual = string_converter(type).from_type(value)
        self.assertEquals(actual, expected)

        value, expected = expected, value
        actual = string_converter(type).to_type(value)
        self.assertEquals(actual, expected)
Exemplo n.º 50
0
 def from_request_data(self, field, request_data):
     L = []
     for string_data in request_data:
         if self.strip is True:
             string_data = string_data.strip()
         if not string_data:
             continue
         value = string_converter(field.attr.attr).to_type(
             string_data, converter_options=self.converter_options)
         L.append(value)
     return L
Exemplo n.º 51
0
 def from_request_data(self, field, request_data):
     """
     after the form has been submitted, the request data is converted into
     to the schema type.
     """
     if request_data == self.none_value_as_request_data(field):
         data = self.empty
     else:
         data = string_converter(field.attr).to_type(
             request_data[0], converter_options=self.converter_options)
     return data
Exemplo n.º 52
0
    def test_integer_to_string_conversion(self):
        type = schemaish.Integer()

        value_expected = [
            (0, '0'),
            (1, '1'),
            (1L, '1'),
        ]
        for value, expected in value_expected:
            actual = string_converter(type).from_type(value)
            self.assertEquals(actual, expected)

        value_expected = [
            ('0', 0),
            ('1', 1),
            ('20', 20),
        ]
        for value, expected in value_expected:
            actual = string_converter(type).to_type(value)
            self.assertEquals(actual, expected)
Exemplo n.º 53
0
 def to_request_data(self, field, data):
     """
     Before the widget is rendered, the data is converted to a string
     format.If the data is None then we return an empty string. The sequence
     is request data representation.
     """
     if data is None or (data == self.empty
                         and self.roundtrip_empty is True):
         return self.none_value_as_request_data(field)
     string_data = string_converter(field.attr).from_type(
         data, converter_options=self.converter_options)
     return [string_data]
Exemplo n.º 54
0
 def test_time_string_conversion(self):
     schema = schemaish.Time()
     converter = string_converter(schema)
     tz = SimpleTZInfo(90)
     tests = [(time(1), '01:00:00'), (time(1, 2), '01:02:00'),
              (time(1, 2, 3), '01:02:03'),
              (time(1, 2, 3, 4), '01:02:03.000004'),
              (time(1, 0, 0, 0, tz), '01:00:00+01:30'),
              (time(1, 2, 0, 0, tz), '01:02:00+01:30'),
              (time(1, 2, 3, 0, tz), '01:02:03+01:30'),
              (time(1, 2, 3, 4, tz), '01:02:03.000004+01:30')]
     for t, s in tests:
         self.assertEquals(converter.from_type(t), s)
         self.assertEquals(converter.to_type(s), t)
Exemplo n.º 55
0
 def test_datetime_string_conversion(self):
     schema = schemaish.DateTime()
     converter = string_converter(schema)
     tz = SimpleTZInfo(90)
     tests = [
         (datetime(2001, 2, 3, 4, 5, 6), '2001-02-03T04:05:06'),
         (datetime(2001, 2, 3), '2001-02-03T00:00:00'),
         (datetime(2001, 2, 3, 4, 5, 6, 7), '2001-02-03T04:05:06.000007'),
         (datetime(2001, 2, 3, 4, 5, 6,
                   tzinfo=tz), '2001-02-03T04:05:06+01:30'),
     ]
     for t, s in tests:
         self.assertEquals(converter.from_type(t), s)
         self.assertEquals(converter.to_type(s), t)
Exemplo n.º 56
0
 def checked(self, option, field):
     """
     For each value, convert it and check to see if it matches the input data
     """
     if field.value is None:
         return ''
     cvs = []
     for v in field.value:
         try:
             cvs.append(string_converter(field.attr.attr).to_type(v))
         except ConvertError:
             continue
     if option[0] in cvs:
         return ' checked="checked"'
     else:
         return ''
Exemplo n.º 57
0
 def from_request_data(self, field, request_data):
     """
     Check the input and confirm match (when stripped)
     """
     if request_data == self.none_value_as_request_data(field):
         return self.empty
     input = request_data['input'][0]
     confirm = request_data['confirm'][0]
     if self.strip is True:
         input = input.strip()
         confirm = confirm.strip()
     if input != confirm:
         raise ConvertError(self.mismatch_message)
     if input == self.none_value:
         return self.empty
     return string_converter(field.attr).to_type(input)
Exemplo n.º 58
0
 def from_request_data(self, field, request_data):
     """
     Check to see if we need to use the 'other' value
     """
     select = request_data['select'][0]
     other = request_data['other'][0]
     if select == self.other_option[0]:
         value = other
     else:
         if other != '':
             raise ConvertError(
                 'You entered text in the box but had not selected "%s" in the drop down. We have now selected it for you. please check and resubmit'
                 % self.other_option[1])
         value = select
     if value == '':
         return self.empty
     return string_converter(field.attr).to_type(value)
Exemplo n.º 59
0
    def to_request_data(self, field, data):
        """
        Before the widget is rendered, the data is converted to a string
        format.If the data is None then we return an empty string. The sequence
        is request data representation.
        """
        if data is None:
            return ['']
        string_data = string_converter(field.attr).from_type(
            data, converter_options=self.converter_options)
        # Do the html entity conversion from here, as it seems structure is broken.
        # it does not convert the & from html entities, so for example both > and >
        # will flat out as &gt; in the textarea, which means that < > characters and
        # possibly also content between them will be stripped when a correctly saved
        # document is opened.
        #
        # XXX May be a recently introduced bug in chameleon? OR the usage of structure
        # in the formish widget was broken since the beginning?

        string_data = cgi.escape(string_data)

        result = [string_data]
        return result
Exemplo n.º 60
0
 def from_request_data(self, field, request_data):
     return [
         string_converter(field.attr.attr).to_type(d) for d in request_data
     ]