def clean_xml_file(xml_file): from django.conf import settings from tcms.core.contrib.xml2dict.xml2dict import XML2Dict xml_file = xml_file.replace('\n', '') xml_file = xml_file.replace('&testopia_', '&') xml_file = xml_file.encode("utf8") try: xml = XML2Dict() xml_data = xml.fromstring(xml_file) root_element = xml_data.get('testopia', None) if root_element is None: raise ValueError('Invalid XML document.') if not root_element.get('version', None) != settings.TESTOPIA_XML_VERSION: raise case_elements = root_element.get('testcase', None) if case_elements is not None: if isinstance(case_elements, list): return map(process_case, case_elements) elif isinstance(case_elements, dict): return map(process_case, (case_elements, )) else: raise else: raise except Exception: raise
def clean(self, data, initial=None): """ Check the file content type is XML or not """ f = super(CasePlanXMLField, self).clean(data, initial) if f is None: return None elif not data and initial: return initial if not data.content_type == 'text/xml': raise forms.ValidationError(self.error_messages['invalid_file']) # We need to get a file object for PIL. We might have a path or we # might have to read the data into memory. if hasattr(data, 'temporary_file_path'): xml_file = data.temporary_file_path() else: if hasattr(data, 'read'): xml_file = data.read() else: xml_file = data['content'] # Replace line breaks for XML interpret xml_file = xml_file.replace('\n', '') xml_file = xml_file.replace('&testopia_', '&') # Insert clean code here try: xml = XML2Dict() self.xml_data = xml.fromstring(xml_file) if not self.xml_data.get('testopia'): raise forms.ValidationError( self.error_messages['root_element_is_needed']) if not self.xml_data['testopia'].get( 'version') != settings.TESTOPIA_XML_VERSION: raise forms.ValidationError( self.error_messages['xml_version_is_incorrect']) if not self.xml_data['testopia'].get('testcase'): raise forms.ValidationError( self.error_messages['test_case_element_is_needed']) new_case_from_xml = [] if isinstance(self.xml_data['testopia']['testcase'], list): for case in self.xml_data['testopia']['testcase']: new_case_from_xml.append(self.process_case(case)) elif isinstance(self.xml_data['testopia']['testcase'], dict): new_case_from_xml.append( self.process_case(self.xml_data['testopia']['testcase'])) else: raise forms.ValidationError( self.error_messages['test_case_element_is_needed']) except Exception, error: raise forms.ValidationError( '%s: %s' % (self.error_messages['interpret_error'], error))
def setUp(self): self.xml2dict = XML2Dict() self.user = User.objects.create(username='******', email='*****@*****.**') result = Priority.objects.get_or_create(value='P1') self.priority_p1, self.priority_p1_created = result result = TestCaseStatus.objects.get_or_create(name='CONFIRMED') self.status_confirmed, self.status_confirmed_created = result result = TestCaseStatus.objects.get_or_create(name='PROPOSED') self.status_proposed, self.status_proposed_created = result
def setUpTestData(cls): cls.xml2dict = XML2Dict() cls.user = UserFactory(username='******', email='*****@*****.**') cls.priority_p1, created = Priority.objects.get_or_create(value='P1') cls.status_confirmed, created = TestCaseStatus.objects.get_or_create(name='CONFIRMED') cls.status_proposed, created = TestCaseStatus.objects.get_or_create(name='PROPOSED')