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(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 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 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 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 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.") input_element = data_structure_element_api.get_by_id(request.POST['id']) input_previous_value = input_element.value input_element.value = request.POST['value'] data_structure_element_api.upsert(input_element) return HttpResponse(json.dumps({'replaced': input_previous_value}), content_type='application/json')
def test_data_structure_element_upsert_return_data_structure_element( self, mock_save): # Arrange mock_data_structure_element = DataStructureElement("tag", "value") mock_save.return_value = mock_data_structure_element # Act result = data_structure_element_api.upsert(mock_data_structure_element) # Assert self.assertIsInstance(result, DataStructureElement)
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']) # 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) except Exception, e: raise ModuleError('Something went wrong during module update: ' + e.message)
def test_data_structure_element_upsert_return_data_structure_element( self, mock_save): # Arrange mock_user = create_mock_user("1", is_superuser=True) mock_request = Mock(spec=HttpRequest) mock_request.user = mock_user mock_data_structure_element = DataStructureElement( user="******", tag="tag", value="value", data_structure=MockDataStructure()) mock_save.return_value = mock_data_structure_element # Act result = data_structure_element_api.upsert(mock_data_structure_element, mock_request) # Assert self.assertIsInstance(result, DataStructureElement)