def _get_module(self, request): # look for existing values try: # keyrefId = request.GET['keyref'] if 'keyref' in request.GET else None module_id = request.GET['module_id'] module = SchemaElement.objects().get(pk=module_id) keyrefId = module.options['params']['keyref'] # register the module id in the structure if str(module_id) not in request.session['keyrefs'][keyrefId]['module_ids']: request.session['keyrefs'][keyrefId]['module_ids'].append(str(module_id)) # get the list of values for this key keyId = request.session['keyrefs'][keyrefId]['refer'] values = [] modules_ids = request.session['keys'][keyId]['module_ids'] for key_module_id in modules_ids: key_module = SchemaElement.objects().get(pk=key_module_id) if key_module.options['data'] is not None: values.append(key_module.options['data']) # add empty value self.options.update({'': ''}) for value in values: self.options.update({str(value): str(value)}) self.selected = '' if 'data' in request.GET: self.selected = request.GET['data'] elif 'data' in module.options and module.options['data'] is not None: self.selected = str(module.options['data']) except Exception: self.options = {} return OptionsModule.get_module(self, request)
def _get_module(self, request): # get the name of the key # keyId = request.GET['key'] module_id = request.GET['module_id'] module = SchemaElement.objects().get(pk=module_id) keyId = module.options['params']['key'] # register the module id in the structure if str(module_id) not in request.session['keys'][keyId]['module_ids']: request.session['keys'][keyId]['module_ids'].append(str(module_id)) # get the list of values for this key values = [] modules_ids = request.session['keys'][keyId]['module_ids'] for key_module_id in modules_ids: key_module = SchemaElement.objects().get(pk=key_module_id) if key_module.options['data'] is not None: values.append(key_module.options['data']) # if data are present if 'data' in request.GET: # set the key coming from data key = request.GET['data'] else: # generate a unique key key = self.generateKey(values) # set the value of the module with the key self.default_value = key return SyncInputModule.get_module(self, request)
def get_parent_element(element): """Get the parent element (tag is element not direct parent) of the current element. Parameters: element: """ try: parent = SchemaElement.objects().get(children__contains=ObjectId(element.id)) while parent.tag != 'element': parent = SchemaElement.objects().get(children__contains=ObjectId(parent.id)) return parent except: return None
def get_parent_element(element): """Get the parent element (tag is element not direct parent) of the current element. Parameters: element: """ try: parent = SchemaElement.objects().get( children__contains=ObjectId(element.id)) while parent.tag != 'element': parent = SchemaElement.objects().get( children__contains=ObjectId(parent.id)) return parent except: return None
def render(self): """ Parameters: partial: :return: """ if self.data.tag == 'element': return self.render_element(self.data) elif self.data.tag == 'choice': content = self.render_choice(self.data) root = self.data.children[0] root_elem_id = root.value root_elem = SchemaElement.objects().get(pk=root_elem_id) root_name = root_elem.options['name'] if 'xmlns' in root_elem.options and root_elem.options[ 'xmlns'] is not None: xmlns = ' xmlns="{}"'.format(root_elem.options['xmlns']) content[0] += xmlns return self._render_xml(root_name, content[0], content[1]) else: message = 'render: ' + self.data.tag + ' not handled' self.warnings.append(message) return ''
def _get(self, request): module_id = request.GET['module_id'] url = request.GET[ 'url'] if 'url' in request.GET else SchemaElement.objects().get( pk=module_id).options['url'] template_data = { 'module_id': module_id, 'module': '', 'display': '', 'result': '', 'url': url } try: template_data['module'] = self._get_module(request) template_data['display'] = self._get_display(request) result = self._get_result(request) template_data['result'] = result module_element = SchemaElement.objects.get( pk=request.GET['module_id']) options = module_element.options options['data'] = result module_element.update(set__options=options) module_element.reload() except Exception, e: raise ModuleError( 'Something went wrong during module initialization: ' + e.message)
def render(self): """ Parameters: partial: :return: """ if self.data.tag == 'element': return self.render_element(self.data) elif self.data.tag == 'choice': content = self.render_choice(self.data) root = self.data.children[0] root_elem_id = root.value root_elem = SchemaElement.objects().get(pk=root_elem_id) root_name = root_elem.options['name'] if 'xmlns' in root_elem.options and root_elem.options['xmlns'] is not None: xmlns = ' xmlns="{}"'.format(root_elem.options['xmlns']) content[0] += xmlns if content[0] == "": # Multi-root with element (no need for an element wrapper) return content[1] else: # Multi-root with complexType return self._render_xml(root_name, content[0], content[1]) else: message = 'render: ' + self.data.tag + ' not handled' self.warnings.append(message) return ''
def _get(self, request): module_id = request.GET['module_id'] url = request.GET['url'] if 'url' in request.GET else SchemaElement.objects().get(pk=module_id).options['url'] template_data = { 'module_id': module_id, 'module': '', 'display': '', 'result': '', 'url': url } try: template_data['module'] = self._get_module(request) template_data['display'] = self._get_display(request) result = self._get_result(request) template_data['result'] = result module_element = SchemaElement.objects.get(pk=request.GET['module_id']) options = module_element.options options['data'] = result module_element.update(set__options=options) module_element.reload() except Exception, e: raise ModuleError('Something went wrong during module initialization: ' + e.message)
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 get_updated_keys(request): """ updated_keys[key] = {'ids': [], 'tagIDs': []} key = key name ids = list of posssible values for a key tagIDs = HTML element that needs to be updated with the values (keyrefs) """ # delete keys that have been deleted for key, values in request.session['keys'].iteritems(): deleted_ids = [] for module_id in values['module_ids']: try: SchemaElement.objects().get(pk=module_id) except Exception: deleted_ids.append(module_id) request.session['keys'][key]['module_ids'] = [ item for item in request.session['keys'][key]['module_ids'] if item not in deleted_ids ] # delete keyrefs that have been deleted for keyref, values in request.session['keyrefs'].iteritems(): deleted_ids = [] for module_id in values['module_ids']: try: SchemaElement.objects().get(pk=module_id) except Exception: deleted_ids.append(module_id) request.session['keyrefs'][keyref]['module_ids'] = [ item for item in request.session['keyrefs'][keyref]['module_ids'] if item not in deleted_ids ] # get the list of keyrefs to update updated_keyrefs = [] for keyref, values in request.session['keyrefs'].iteritems(): updated_keyrefs.extend(values['module_ids']) return HttpResponse(json.dumps(updated_keyrefs), content_type='application/javascript')
def get_updated_keys(request): """ updated_keys[key] = {'ids': [], 'tagIDs': []} key = key name ids = list of posssible values for a key tagIDs = HTML element that needs to be updated with the values (keyrefs) """ # delete keys that have been deleted for key, values in request.session['keys'].iteritems(): deleted_ids = [] for module_id in values['module_ids']: try: SchemaElement.objects().get(pk=module_id) except Exception: deleted_ids.append(module_id) request.session['keys'][key]['module_ids'] = [item for item in request.session['keys'][key]['module_ids'] if item not in deleted_ids] # delete keyrefs that have been deleted for keyref, values in request.session['keyrefs'].iteritems(): deleted_ids = [] for module_id in values['module_ids']: try: SchemaElement.objects().get(pk=module_id) except Exception: deleted_ids.append(module_id) request.session['keyrefs'][keyref]['module_ids'] = [item for item in request.session['keyrefs'][keyref]['module_ids'] if item not in deleted_ids] # get the list of keyrefs to update updated_keyrefs = [] for keyref, values in request.session['keyrefs'].iteritems(): updated_keyrefs.extend(values['module_ids']) return HttpResponse(json.dumps(updated_keyrefs), content_type='application/javascript')
def _get_module(self, request): # look for existing values try: # keyrefId = request.GET['keyref'] if 'keyref' in request.GET else None module_id = request.GET['module_id'] module = SchemaElement.objects().get(pk=module_id) keyrefId = module.options['params']['keyref'] # register the module id in the structure if str(module_id ) not in request.session['keyrefs'][keyrefId]['module_ids']: request.session['keyrefs'][keyrefId]['module_ids'].append( str(module_id)) # get the list of values for this key keyId = request.session['keyrefs'][keyrefId]['refer'] values = [] modules_ids = request.session['keys'][keyId]['module_ids'] for key_module_id in modules_ids: key_module = SchemaElement.objects().get(pk=key_module_id) if key_module.options['data'] is not None: values.append(key_module.options['data']) # add empty value self.options.update({'': ''}) for value in values: self.options.update({str(value): str(value)}) self.selected = '' if 'data' in request.GET: self.selected = request.GET['data'] elif 'data' in module.options and module.options[ 'data'] is not None: self.selected = str(module.options['data']) except Exception: self.options = {} return OptionsModule.get_module(self, request)