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)
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)
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)
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)
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)
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)
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)
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)
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')
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')
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')
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
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]
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]
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'))
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
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')
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
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': ['']}
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]}
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]}
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]}
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
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 ''
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]
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')
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)
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
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
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 ''
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)
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
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))
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]
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]}
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)
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
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]
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]}
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]
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
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)
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)
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]
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)
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
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
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)
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]
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)
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)
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 ''
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)
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)
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 > 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
def from_request_data(self, field, request_data): return [ string_converter(field.attr.attr).to_type(d) for d in request_data ]