def test_data_structure_element_get_by_id_raises_does_not_exist_error_if_not_found( self, mock_get): # Arrange mock_get.side_effect = exceptions.DoesNotExist('') # Act # Assert with self.assertRaises(exceptions.DoesNotExist): data_structure_element_api.get_by_id(1)
def test_data_get_by_id_raises_does_not_exist_error_if_not_found(self): # Arrange mock_request = Mock(spec=HttpRequest) mock_request.user = self.fixtures.default_owner_with_perm # Act # Assert with self.assertRaises(exceptions.DoesNotExist): data_structure_element_api.get_by_id(ObjectId(), mock_request)
def test_data_structure_element_get_by_id_raises_model_error_if_not_found( self): self.mock_request.user = self.users["user"] # Act # Assert with self.assertRaises(exceptions.ModelError): data_structure_element_api.get_by_id(1, self.mock_request)
def _get(self, request): """Manage the GET requests Args: request: Returns: """ module_id = request.GET["module_id"] url = ( request.GET["url"] if "url" in request.GET else data_structure_element_api.get_by_id(module_id, request).options["url"] ) template_data = { "module_id": module_id, "module": "", "display": "", "url": url, } try: # retrieve module's data self.data = self._retrieve_data(request) # get module's rendering template_data["module"] = self._render_module(request) # get nodule's data rendering template_data["display"] = self._render_data(request) # get module element module_element = data_structure_element_api.get_by_id(module_id, request) # get its options options = module_element.options # update module element data options["data"] = self.data # set updated options module_element.options = options # save module element data_structure_element_api.upsert(module_element, request) except Exception as e: raise ModuleError( "Something went wrong during module initialization: " + str(e) ) # Check that values are not None for key, val in list(template_data.items()): if val is None: raise ModuleError( "Variable " + key + " cannot be None. Module initialization cannot be completed." ) # TODO Add additional checks # Apply tags to the template html_string = AbstractModule.render_template(self.template_name, template_data) return HttpResponse(html_string, status=HTTP_200_OK)
def test_user_not_owner_cannot_perform_operation(self): self.mock_request.user = self.users["user"] with self.assertRaises(DoesNotExist): data_structure_element_api.get_by_id( self.fixtures.data_structure_element_collection["root"].id, self.mock_request, )
def test_user_not_owner_cannot_perform_operation(self): self.mock_request.user = self.users["user"] # Ensure the call raises an error with self.assertRaises(AccessControlError): data_structure_element_api.upsert( self.mock_data_structure_element, self.mock_request ) # Ensure the object has not been created self.mock_request.user = self.users["owner"] with self.assertRaises(DoesNotExist): data_structure_element_api.get_by_id( self.mock_data_structure_element.id, self.mock_request )
def test_data_get_by_id_return_data_if_found(self): # Act result = api_data_structure_element.get_by_id( self.fixture.data_structure_element_collection[1].id) # Assert self.assertEqual(result, self.fixture.data_structure_element_collection[1])
def save_data_structure_element_value(request): """Saves the value of a data structure element Args: request: Returns: """ if "id" not in request.POST or "value" not in request.POST: return HttpResponseBadRequest( "Error when trying to data structure element: id or value is missing." ) try: input_element = data_structure_element_api.get_by_id( request.POST["id"], request) input_previous_value = input_element.value input_element.value = request.POST["value"] data_structure_element_api.upsert(input_element, request) return HttpResponse( json.dumps({"replaced": input_previous_value}), content_type="application/json", ) except (AccessControlError, DoesNotExist) as exc: return HttpResponseBadRequest(json.dumps({"message": str(exc)}))
def get_data_structure_element_value(request): """Gets the value of a data structure element Args: request: Returns: """ if "id" not in request.GET: return HttpResponseBadRequest() try: element = data_structure_element_api.get_by_id(request.GET["id"], request) element_value = element.value if element.tag == "module": element_value = { "data": element.options["data"], "attributes": element.options["attributes"], } return HttpResponse(json.dumps({"value": element_value}), content_type="application/json") except (AccessControlError, DoesNotExist) as exc: return HttpResponseBadRequest(json.dumps({"message": str(exc)}))
def render(self): """Renders form as XML Returns: """ 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 = data_structure_element_api.get_by_id( root_elem_id, self.request) root_name = root_elem.options["name"] if (content[0] == "" ): # Multi-root with element (no need for an element wrapper) return content[1] else: # Multi-root with complexType if ("xmlns" in root_elem.options and root_elem.options["xmlns"] is not None): xml_ns = ' xmlns="{}"'.format(root_elem.options["xmlns"]) content[0] += xml_ns return self._render_xml(root_name, content[0], content[1]) else: message = "render: " + self.data.tag + " not handled" self.warnings.append(message) return ""
def render(self): """Renders form as XML Returns: """ 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 = data_structure_element_api.get_by_id(root_elem_id) root_name = root_elem.options['name'] if content[ 0] == "": # Multi-root with element (no need for an element wrapper) return content[1] else: # Multi-root with complexType if 'xmlns' in root_elem.options and root_elem.options[ 'xmlns'] is not None: xml_ns = ' xmlns="{}"'.format(root_elem.options['xmlns']) content[0] += xml_ns return self._render_xml(root_name, content[0], content[1]) else: message = 'render: ' + self.data.tag + ' not handled' self.warnings.append(message) return ''
def update_user_input(request): """Updates the user input of the query builder according to the type of the selected element Args: request: Returns: """ from_element_id = request.POST['elementID'] template_id = request.POST['templateID'] # get schema element data_structure_element = data_structure_element_api.get_by_id( from_element_id) # get template template = template_api.get(template_id) # convert xml path to mongo dot notation namespaces = get_namespaces(template.content) default_prefix = get_default_prefix(namespaces) element_type = data_structure_element.options['type'] user_inputs = get_user_inputs(element_type, data_structure_element, default_prefix) response_dict = {'userInputs': user_inputs, 'element_type': element_type} return HttpResponse(json.dumps(response_dict), content_type='application/javascript')
def generate_form(xsd_string, xml_string=None, data_structure=None, request=None): """Generate the form using the parser, returns the root element. Args: xsd_string: xml_string: data_structure: request: Returns: """ # build parser parser = get_parser(request=request) # generate form root_element_id = parser.generate_form(xsd_string, xml_string, data_structure, request=request) # get the root element root_element = data_structure_element_api.get_by_id( root_element_id, request) return root_element
def _fields_to_query(form_values, template_id, get_dot_notation_to_element_func, use_wildcard): """Takes values from the html tree and creates a query from them Args: form_values: template_id: use_wildcard: get_dot_notation_to_element_func: Returns: """ # get template template = template_api.get(template_id) # get namespaces namespaces = get_namespaces(template.content) # get default prefix default_prefix = get_default_prefix(namespaces) query = dict() for field in form_values: bool_comp = field['operator'] is_not = bool_comp == 'NOT' element_type = field.get('type', None) # get element value value = get_element_value(field) # get comparison operator comparison = get_element_comparison(field) element_id = field['id'] if element_type == "query": try: saved_query = saved_query_api.get_by_id(element_id) except DoesNotExist: raise MongoQueryException( "The saved query does not exist anymore.") criteria = build_query_criteria(json.loads(saved_query.query), is_not) else: data_structure_element = data_structure_element_api.get_by_id( element_id) element = get_dot_notation_to_element_func(data_structure_element, namespaces) criteria = build_criteria(element, comparison, value, element_type, default_prefix, is_not, use_wildcard) if bool_comp == 'OR': query = build_or_criteria(query, criteria) elif bool_comp == 'AND': query = build_and_criteria(query, criteria) else: if form_values.index(field) == 0: query.update(criteria) else: query = build_and_criteria(query, criteria) return query
def _get_curate_datastructure_from_module_id(module_id, request): module_element = data_structure_element_api.get_by_id(module_id, request) data_structure_element = data_structure_element_api.get_root_element( module_element, request ) return curate_data_structure_api.get_by_data_structure_element_root_id( data_structure_element, request.user )
def get_updated_keys(request): """Return current keys updated_keys[key] = {'ids': [], 'tagIDs': []} key = key name ids = list of possible values for a key tagIDs = HTML element that needs to be updated with the values (keyrefs) Args: request: Returns: """ try: # get root id root_id = request.GET["root_id"] # get root element from id root_element = data_structure_element_api.get_by_id(root_id, request) # get auto key manager from root auto_key = auto_key_api.get_by_root(root_element) # go through all existing keys for key, module_ids in auto_key.keys.items(): # get list of current module ids current_module_ids = _get_current_module_ids(module_ids, request) # update list of module ids in auto key manager auto_key.keys[key] = current_module_ids # update auto key manager auto_key_api.upsert(auto_key) # get auto keyref manager from root auto_keyref = auto_keyref_api.get_by_root(root_element) # go through all existing keyrefs for keyref, module_ids in auto_keyref.keyrefs.items(): # get list of current module ids current_module_ids = _get_current_module_ids(module_ids, request) # update list of module ids in auto keyref manager auto_keyref.keyrefs[keyref] = current_module_ids # update auto keyref manager auto_keyref_api.upsert(auto_keyref) # get the list of keyrefs to update updated_keyrefs = [] for keyref, module_ids in auto_keyref.keyrefs.items(): updated_keyrefs.extend(module_ids) except Exception as e: raise ModuleError( "An unexpected error occurred while getting updated list of keys: " + str(e)) return HttpResponse(json.dumps(updated_keyrefs), content_type="application/javascript")
def test_data_structure_element_get_by_id_return_data_if_found( self, mock_get): # Arrange mock_data_structure_element = DataStructureElement("tag", "value") mock_get.return_value = mock_data_structure_element # Act result = data_structure_element_api.get_by_id(1) # Assert self.assertIsInstance(result, DataStructureElement)
def test_superuser_can_perform_operation(self): self.mock_request.user = self.users["superuser"] result = data_structure_element_api.get_by_id( self.fixtures.data_structure_element_collection["root"].id, self.mock_request, ) self.assertEquals( result, self.fixtures.data_structure_element_collection["root"] )
def test_data_structure_element_get_by_id_return_data_if_found( self, mock_get): # Arrange mock_get.return_value = self.mock_data_structure_element self.mock_request.user = self.users["owner"] # Act result = data_structure_element_api.get_by_id(1, self.mock_request) # Assert self.assertIsInstance(result, DataStructureElement)
def __init__(self, request): try: element = data_structure_element_api.get_by_id( request.POST['module_id']) self.xpath = element.options['xpath']['xml'] self.values = {} self.set_XpathAccessor(request) except Exception, e: message = 'XPathAccessor error: ' raise XPathAccessorError(message + e.message)
def test_data_get_by_id_return_data_if_found(self): # Arrange mock_request = Mock(spec=HttpRequest) mock_request.user = self.fixtures.default_owner_with_perm # Act result = data_structure_element_api.get_by_id( self.fixtures.data_structure_element_collection["2000"].id, mock_request) # Assert self.assertEqual( result, self.fixtures.data_structure_element_collection["2000"])
def _get(self, request): """ Manage the GET requests Args: request: Returns: """ module_id = request.GET['module_id'] url = request.GET['url'] if 'url' in request.GET else \ data_structure_element_api.get_by_id(module_id) .options['url'] template_data = { 'module_id': module_id, 'module': '', 'display': '', 'url': url } try: # retrieve module's data self.data = self._retrieve_data(request) # get module's rendering template_data['module'] = self._render_module(request) # get nodule's data rendering template_data['display'] = self._render_data(request) # get module element module_element = data_structure_element_api.get_by_id(module_id) # get its options options = module_element.options # update module element data options['data'] = self.data # set updated options module_element.options = options # save module element data_structure_element_api.upsert(module_element) except Exception, e: raise ModuleError( 'Something went wrong during module initialization: ' + e.message)
def post(self, request, *args, **kwargs): """Manage POST requests Args: request: *args: **kwargs: Returns: """ template_data = {"display": "", "url": ""} try: if "module_id" not in request.POST: return HttpResponseBadRequest( {"error": 'No "module_id" parameter provided'} ) module_element = data_structure_element_api.get_by_id( request.POST["module_id"], request ) # retrieve module's data self.data = self._retrieve_data(request) template_data["display"] = self._render_data(request) options = module_element.options # TODO: needs to be updated if type(self.data) == dict: options["data"] = self.data["data"] options["attributes"] = self.data["attributes"] else: options["data"] = self.data # TODO Implement this system instead # options['content'] = self._get_content(request) # options['attributes'] = self._get_attributes(request) module_element.options = options data_structure_element_api.upsert(module_element, request) except Exception as e: raise ModuleError("Something went wrong during module update: " + str(e)) html_code = AbstractModule.render_template(self.template_name, template_data) response_dict = dict() response_dict["html"] = html_code if hasattr(self, "get_XpathAccessor"): response_dict.update(self.get_XpathAccessor()) return HttpResponse(json.dumps(response_dict))
def __init__(self, request): self.request = request try: element = data_structure_element_api.get_by_id( request.POST["module_id"], self.request) self.xpath = element.options["xpath"]["xml"] self.values = {} self.set_XpathAccessor(request) except Exception as e: message = "XPathAccessor error: " raise XPathAccessorError(message + str(e))
def test_superuser_can_perform_operation(self): self.mock_request.user = self.users["superuser"] data_structure_element_api.upsert( self.mock_data_structure_element, self.mock_request ) self.assertEquals( data_structure_element_api.get_by_id( self.mock_data_structure_element.id, self.mock_request ), self.mock_data_structure_element, )
def _get_element(self, form_id, xpath): form_root = data_structure_element_api.get_by_id(form_id) if self.element_has_xpath(form_root, xpath): return form_root if len(form_root.children) == 0: return None for child in form_root.children: element = self._get_element(child.pk, xpath) if element is not None: return element
def get_sub_elements_query_builder(request): """Build the form for queries on sub elements Args: request: Returns: """ leaves_id = request.POST["leavesID"] template_id = request.POST["templateID"] # get list of ids from string list_leaves_id = leaves_id.split(" ") # get template template = template_api.get(template_id, request=request) # get template namespaces namespaces = get_namespaces(template.content) # get default prefix default_prefix = get_default_prefix(namespaces) # get the parent name using the first schema element of the list parent_name = get_parent_name(list_leaves_id[0], namespaces, request) form_fields = [] for leaf_id in list_leaves_id: data_structure_element = data_structure_element_api.get_by_id( leaf_id, request) element_type = data_structure_element.options["type"] element_name = data_structure_element.options["name"] user_inputs = get_user_inputs(element_type, data_structure_element, default_prefix) form_fields.append({ "element_id": leaf_id, "element_name": element_name, "element_type": element_type, "html": user_inputs, }) response_dict = { "subElementQueryBuilder": render_sub_elements_query(parent_name, form_fields) } return HttpResponse(json.dumps(response_dict), content_type="application/javascript")
def _get_current_module_ids(module_ids, request): """Return list of module ids still present in the data structure Args: module_ids: Returns: """ # Initialize list of current module ids current_module_ids = [] # go through all module ids for module_id in module_ids: try: # try to get element data_structure_element_api.get_by_id(module_id, request) # add id to list if element still exists current_module_ids.append(module_id) except DoesNotExist as e: logger.warning( "_get_current_module_ids threw an exception: {0}".format( str(e))) return current_module_ids
def generate_form(xsd_string): """Generates the form using the parser, returns the root element Args: xsd_string: Returns: """ # build parser parser = get_parser() # generate form root_element_id = parser.generate_form(xsd_string) # get the root element root_element = data_structure_element_api.get_by_id(root_element_id) return root_element
def remove_element(request): """Remove an element from the form. Args: request: Returns: """ element_id = request.POST["id"] element_list = data_structure_element_api.get_all_by_child_id( element_id, request) if len(element_list) == 0: raise ValueError("No Data Structure Element found") elif len(element_list) > 1: raise ValueError("More than one Data Structure Element found") # Removing the element from the data structure data_structure_element = element_list[0] data_structure_element_to_pull = data_structure_element_api.get_by_id( element_id, request) # number of children after deletion children_number = len(data_structure_element.children) - 1 data_structure_element = remove_child_element( data_structure_element, data_structure_element_to_pull, request) response = {"code": 0, "html": ""} if children_number > data_structure_element.options["min"]: return HttpResponse(json.dumps(response)) else: # len(schema_element.children) == schema_element.options['min'] if data_structure_element.options["min"] != 0: response["code"] = 1 else: # schema_element.options['min'] == 0 renderer = ListRenderer(data_structure_element, request) html_form = renderer.render(True) response["code"] = 2 response["html"] = html_form return HttpResponse(json.dumps(response))