def remove_element(request): element_id = request.POST['id'] # sub_element = SchemaElement.objects.get(pk=element_id) element_list = SchemaElement.objects(children=element_id) if len(element_list) == 0: raise ValueError("No SchemaElement found") elif len(element_list) > 1: raise ValueError("More than one SchemaElement found") # Removing the element from the data structure schema_element = element_list[0] schema_element_to_pull = SchemaElement.objects.get(pk=element_id) schema_element.update(pull__children=schema_element_to_pull) schema_element.reload() update_branch_xpath(schema_element) # Deleting the branch from the database delete_branch_from_db(element_id) children_number = len(schema_element.children) # TODO Move it to parser function # FIXME Sequence elem it might not work if len(schema_element.children) == 0: elem_iter = SchemaElement() if schema_element.tag == 'element': elem_iter.tag = 'elem-iter' elif schema_element.tag == 'choice': elem_iter.tag = 'choice-iter' elif schema_element.tag == 'sequence': elem_iter.tag = 'sequence-iter' elem_iter.save() schema_element.update(add_to_set__children=[elem_iter]) schema_element.reload() response = { 'code': 0, 'html': "" } if children_number > schema_element.options['min']: return HttpResponse(json.dumps(response), status=HTTP_200_OK) else: # len(schema_element.children) == schema_element.options['min'] if schema_element.options['min'] != 0: response['code'] = 1 else: # schema_element.options['min'] == 0 renderer = ListRenderer(schema_element, request) html_form = renderer.render(True) response['code'] = 2 response['html'] = html_form return HttpResponse(json.dumps(response))
def remove_element(request): element_id = request.POST['id'] # sub_element = SchemaElement.objects.get(pk=element_id) element_list = SchemaElement.objects(children=element_id) if len(element_list) == 0: raise ValueError("No SchemaElement found") elif len(element_list) > 1: raise ValueError("More than one SchemaElement found") # Removing the element from the data structure schema_element = element_list[0] schema_element.update(pull__children=element_id) schema_element.reload() update_branch_xpath(schema_element) # Deleting the branch from the database delete_branch_from_db(element_id) children_number = len(schema_element.children) # TODO Move it to parser function # FIXME Sequence elem it might not work if len(schema_element.children) == 0: elem_iter = SchemaElement() if schema_element.tag == 'element': elem_iter.tag = 'elem-iter' elif schema_element.tag == 'choice': elem_iter.tag = 'choice-iter' elif schema_element.tag == 'sequence': elem_iter.tag = 'sequence-iter' elem_iter.save() schema_element.update(add_to_set__children=[elem_iter]) schema_element.reload() response = { 'code': 0, 'html': "" } if children_number > schema_element.options['min']: return HttpResponse(json.dumps(response), status=HTTP_200_OK) else: # len(schema_element.children) == schema_element.options['min'] if schema_element.options['min'] != 0: response['code'] = 1 else: # schema_element.options['min'] == 0 renderer = ListRenderer(schema_element, request) html_form = renderer.render(True) response['code'] = 2 response['html'] = html_form return HttpResponse(json.dumps(response))
def test_xsd_data_is_schema_element(self): xsd_data = SchemaElement() xsd_data.tag = "test" renderer = AbstractListRenderer(xsd_data) self.assertEqual(xsd_data, renderer.data)
def test_template_list_not_set(self): xsd_data = SchemaElement() xsd_data.tag = "test" renderer = AbstractListRenderer(xsd_data) # Loose comparison is enough for this test self.assertEqual(len(renderer.templates), 7)
def create_mock_db_input(value='', placeholder='', title=''): input_element = SchemaElement() input_element.tag = "input" input_element.value = value input_element.options = {'title': title, 'placeholder': placeholder} input_element.pk = 'mock' return input_element
def create_mock_db_input(value='', placeholder='', title=''): input_element = SchemaElement() input_element.tag = "input" input_element.value = value input_element.options = { 'title': title, 'placeholder': placeholder } input_element.pk = 'mock' return input_element
def test_template_list_not_dict(self): xsd_data = SchemaElement() xsd_data.tag = "test" template_list = None try: for template_list in self.type_generator.generate_types_excluding(['dict', 'none']): with self.assertRaises(Exception): AbstractListRenderer(xsd_data, template_list) except AssertionError as error: template_list_type = str(type(template_list)) error.message += ' (template_list type: ' + template_list_type + ')' raise AssertionError(error.message)
def test_template_list_is_correct_dict(self): xsd_data = SchemaElement() xsd_data.tag = "test" template_list = { "t1": Template('a'), "t2": Template('b'), "t3": Template('c') } base_renderer = AbstractListRenderer(xsd_data) base_renderer.templates.update(template_list) renderer = AbstractListRenderer(xsd_data, template_list) self.assertEqual(renderer.templates.keys(), base_renderer.templates.keys())
def test_template_list_is_incorrect_dict(self): xsd_data = SchemaElement() xsd_data.tag = "test" template = None try: for template in self.type_generator.generate_types_excluding([]): template_list = {"wrong": template} with self.assertRaises(Exception): AbstractListRenderer(xsd_data, template_list) except AssertionError as error: template_type = str(type(template)) error.message += ' (template type: ' + template_type + ')' raise AssertionError(error.message)
def test_template_list_is_incorrect_dict(self): xsd_data = SchemaElement() xsd_data.tag = "test" template = None try: for template in self.type_generator.generate_types_excluding([]): template_list = { "wrong": template } with self.assertRaises(Exception): AbstractListRenderer(xsd_data, template_list) except AssertionError as error: template_type = str(type(template)) error.message += ' (template type: ' + template_type + ')' raise AssertionError(error.message)
def create_mock_html_renderer(): element = SchemaElement() element.tag = "mock" return HtmlRenderer(element)
MONGODB_URI = 'mongodb://*****:*****@localhost/mgi' projectIDs = set(["blank"]) DB_NAME = "mgi" client = MongoClient(MONGODB_URI) db = client[DB_NAME] xmldata = db['xmldata'] for cursor in xmldata.find({ "content.amProjectDB.@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance" }): projectIDs.add(cursor["content"]["amProjectDB"]["amProject"]["projectID"]) product_element = SchemaElement() product_element.tag = u'restriction' product_element.value = u"blank" product_element.options = {u'base': u'xs:string'} product_element.children = [] product_element.save() for ProjectID in projectIDs: xsd_element = SchemaElement() xsd_element.tag = u'enumeration' xsd_element.value = str(ProjectID).decode('UTF-8') xsd_element.options = {} xsd_element.children = [] xsd_element.save() product_element.children.append(xsd_element) product_element.save() a = product_element