def post_page(self): body_parts = self.request.body.split(b'\r\n--') for part in body_parts: field_part = re.search(b'name="(?P<name>.+)"\r\n\r\n(?P<value>.*)', part) file_part = re.search( b'name="(?P<name>.+)"; filename="(?P<filename>.+)"\r\n' b'Content-Type: application/octet-stream\r\n\r\n(?P<value>.*)', part) if field_part: val = field_part.group('value') name = any_to_unicode(field_part.group('name')) if isinstance(FIELDS[name], list) and all( val != any_to_bytes(x) for x in FIELDS[name]): self.doc.put('BAD') elif not isinstance(FIELDS[name], list) and any_to_bytes( FIELDS[name]) != val: self.doc.put('BAD') elif file_part: val = file_part.group('value') name = any_to_unicode(file_part.group('name')) filename = file_part.group('filename') for file in FILES[name]: if any_to_bytes( file['filename']) == filename and any_to_bytes( file['body']) != val: self.doc.put('BAD') elif re.search(b'name=', part): self.doc.put('BAD')
def post_page(self): errors_count = 0 body_parts = self.request.body.split(b'\r\n--') for part in body_parts: field_part = re.search(b'name="(?P<name>.+)"\r\n\r\n(?P<value>.*)', part) file_part = re.search(b'name="(?P<name>.+)"; filename="(?P<filename>.+)"\r\n' b'Content-Type: application/octet-stream\r\n\r\n(?P<value>.*)', part) if field_part: val = field_part.group('value') name = any_to_unicode(field_part.group('name')) if isinstance(FIELDS[name], list) and all(val != any_to_bytes(x) for x in FIELDS[name]): errors_count += 1 elif not isinstance(FIELDS[name], list) and any_to_bytes(FIELDS[name]) != val: errors_count += 1 elif file_part: val = file_part.group('value') name = any_to_unicode(file_part.group('name')) filename = file_part.group('filename') for file in FILES[name]: if any_to_bytes(file['filename']) == filename and any_to_bytes(file['body']) != val: errors_count += 1 elif re.search(b'name=', part): errors_count += 1 self.json.put({'errors_count': errors_count})
def dict_to_xml(dict_value, element_name): element = etree.Element(element_name) if not isinstance(dict_value, dict): element.text = any_to_unicode(dict_value) return element for k, v in dict_value.items(): element.append(dict_to_xml(v, k)) return element
def test_any_to_unicode(self): self.assertEqual(any_to_unicode(5), '5') self.assertEqual(any_to_unicode(None), 'None') self.assertEqual(any_to_unicode('тест'), 'тест') self.assertEqual(any_to_unicode('тест'.encode('utf-8')), 'тест')
def test_any_to_unicode(self): self.assertEqual(any_to_unicode(5), u'5') self.assertEqual(any_to_unicode(None), u'None') self.assertEqual(any_to_unicode(u'тест'), u'тест') self.assertEqual(any_to_unicode(u'тест'.encode('utf-8')), u'тест')