def create_datarequest(original_action, context, data_dict): """ Action to create a new data request. The function checks the access rights of the user before creating the data request. If the user is not allowed a NotAuthorized exception will be risen. In addition, you should note that the parameters will be checked and an exception (ValidationError) will be risen if some of these parameters are not valid. Data QLD modification Will send email notification to users of assigned organisation with admin access :param title: The title of the data request :type title: string :param description: A brief description for your data request :type description: string :param organiztion_id: The ID of the organization you want to asign the data request (optional). :type organization_id: string :returns: A dict with the data request (id, user_id, title, description, organization_id, open_time, accepted_dataset, close_time, closed, followers) :rtype: dict """ model = context['model'] session = context['session'] # Init the data base db.init_db(model) # Check access tk.check_access(constants.CREATE_DATAREQUEST, context, data_dict) # Validate data validator.validate_datarequest(context, data_dict) # Store the data data_req = db.DataRequest() _undictize_datarequest_basic(data_req, data_dict) data_req.user_id = context['auth_user_obj'].id data_req.open_time = datetime.datetime.utcnow() session.add(data_req) session.commit() datarequest_dict = _dictize_datarequest(data_req) if datarequest_dict['organization']: # Data QLD modification users = _get_admin_users_from_organisation(datarequest_dict) users.discard(context['auth_user_obj'].id) _send_mail(users, 'new_datarequest_organisation', datarequest_dict, 'Data Request Created Email') return datarequest_dict
def test_invalid_org(self): context = {} org_validator = validator.tk.get_validator.return_value org_validator.side_effect = self._tk.ValidationError({'Organization': 'Invalid ORG'}) with self.assertRaises(self._tk.ValidationError) as c: validator.validate_datarequest(context, self.request_data) self.assertEquals({'Organization': ['Organization is not valid']}, c.exception.error_dict)
def test_validate_name_description(self, field, value, title_exists, excepction_msg): context = {} # request_data fields are always in lowercase self.request_data[field.lower()] = value validator.db.DataRequest.datarequest_exists.return_value = title_exists with self.assertRaises(self._tk.ValidationError) as c: validator.validate_datarequest(context, self.request_data) self.assertEquals({field: [excepction_msg]}, c.exception.error_dict)
def test_invalid_org(self): context = {} org_validator = validator.tk.get_validator.return_value org_validator.side_effect = self._tk.ValidationError( {'Organization': 'Invalid ORG'}) with self.assertRaises(self._tk.ValidationError) as c: validator.validate_datarequest(context, self.request_data) self.assertEquals({'Organization': ['Organization is not valid']}, c.exception.error_dict)
def test_validate_valid_data_request(self, avoid_existing_title_check): context = {'avoid_existing_title_check': avoid_existing_title_check} self.assertIsNone(validator.validate_datarequest(context, self.request_data)) validator.tk.get_validator.assert_called_once_with('group_id_exists') group_validator = validator.tk.get_validator.return_value group_validator.assert_called_once_with(self.request_data['organization_id'], context) if avoid_existing_title_check: self.assertEquals(0, validator.db.DataRequest.datarequest_exists.call_count) else: validator.db.DataRequest.datarequest_exists.assert_called_once_with(self.request_data['title'])
def test_validate_valid_data_request(self, avoid_existing_title_check): context = {"avoid_existing_title_check": avoid_existing_title_check} self.assertIsNone(validator.validate_datarequest(context, self.request_data)) validator.tk.get_validator.assert_called_once_with("group_id_exists") group_validator = validator.tk.get_validator.return_value group_validator.assert_called_once_with( self.request_data["organization_id"], context ) if avoid_existing_title_check: self.assertEquals(0, validator.db.DataRequest.datarequest_exists.call_count) else: validator.db.DataRequest.datarequest_exists.assert_called_once_with( self.request_data["title"] )
def update_datarequest(original_action, context, data_dict): """ Action to update a data request. The function checks the access rights of the user before updating the data request. If the user is not allowed a NotAuthorized exception will be risen. In addition, you should note that the parameters will be checked and an exception (ValidationError) will be risen if some of these parameters are invalid. Data QLD modification Will send email notification if organisation was changed to users of assigned organisation with admin access :param id: The ID of the data request to be updated :type id: string :param title: The title of the data request :type title: string :param description: A brief description for your data request :type description: string :param organiztion_id: The ID of the organization you want to asign the data request. :type organization_id: string :returns: A dict with the data request (id, user_id, title, description, organization_id, open_time, accepted_dataset, close_time, closed, followers) :rtype: dict """ model = context['model'] session = context['session'] datarequest_id = data_dict.get('id', '') if not datarequest_id: raise tk.ValidationError(tk._('Data Request ID has not been included')) # Init the data base db.init_db(model) # Check access tk.check_access(constants.UPDATE_DATAREQUEST, context, data_dict) # Get the initial data result = db.DataRequest.get(id=datarequest_id) if not result: raise tk.ObjectNotFound( tk._('Data Request %s not found in the data base') % datarequest_id) data_req = result[0] # Avoid the validator to return an error when the user does not change the title context['avoid_existing_title_check'] = data_req.title == data_dict[ 'title'] # Validate data validator.validate_datarequest(context, data_dict) # Data QLD modification organisation_updated = data_req.organization_id != data_dict[ 'organization_id'] if organisation_updated: unassigned_organisation_id = data_req.organization_id # Set the data provided by the user in the data_red _undictize_datarequest_basic(data_req, data_dict) session.add(data_req) session.commit() datarequest_dict = _dictize_datarequest(data_req) if datarequest_dict['organization'] and organisation_updated: # Data QLD modification # Email Admin users of the assigned organisation users = _get_admin_users_from_organisation(datarequest_dict) users.discard(context['auth_user_obj'].id) _send_mail(users, 'new_datarequest_organisation', datarequest_dict, 'Data Request Assigned Email') # Email Admin users of unassigned organisation org_dict = { 'organization': _get_organization(unassigned_organisation_id) } users = _get_admin_users_from_organisation(org_dict) users.discard(context['auth_user_obj'].id) _send_mail(users, 'unassigned_datarequest_organisation', datarequest_dict, 'Data Request Unassigned Email') return datarequest_dict
def test_missing_org(self): self.request_data["organization_id"] = "" context = MagicMock() self.assertIsNone(validator.validate_datarequest(context, self.request_data)) self.assertEquals(0, validator.tk.get_validator.call_count)
def test_missing_org(self): self.request_data['organization_id'] = '' context = MagicMock() self.assertIsNone(validator.validate_datarequest(context, self.request_data)) self.assertEquals(0, validator.tk.get_validator.call_count)