def add_data_review_messsage(publication_recid, data_recid): """ Adds a new review message for a data submission. :param publication_recid: :param data_recid: """ trace = [] message = encode_string(request.form.get('message', '')) version = request.form['version'] userid = current_user.get_id() try: datareview_query = DataReview.query.filter_by(data_recid=data_recid, version=version) # if the data review is not already created, create one. try: data_review_record = datareview_query.one() trace.append("adding data review record") except: data_review_record = create_data_review(data_recid, publication_recid) trace.append("created a new data review record") data_review_message = Message(user=userid, message=message) data_review_record.messages.append(data_review_message) db.session.commit() current_user_obj = get_user_from_id(userid) update_action_for_submission_participant(publication_recid, userid, 'reviewer') send_new_review_message_email(data_review_record, data_review_message, current_user_obj) return json.dumps( { "publication_recid": data_review_record.publication_recid, "data_recid": data_review_record.data_recid, "status": data_review_record.status, "message": decode_string(data_review_message.message), "post_time": data_review_message.creation_date, 'user': current_user_obj.email }, default=default_time) except Exception as e: db.session.rollback() raise e
def test_data_review_cascades(app): # Create a data submission recid = "12345" datasubmission = DataSubmission(publication_recid=recid) db.session.add(datasubmission) db.session.commit() # Add a data review with a message message = Message(user=1, message="Test review message") datareview = DataReview(publication_recid=recid, data_recid=datasubmission.id, messages=[message]) db.session.add(datareview) db.session.commit() # Check that message is created reviews = DataReview.query.filter_by(publication_recid=recid).all() assert (len(reviews) == 1) assert (len(reviews[0].messages) == 1) assert (reviews[0].messages[0].message == message.message) review_messages = list( db.engine.execute( "select * from review_messages where datareview_id = %s" % datareview.id)) assert (len(review_messages) == 1) assert (review_messages[0].datareview_id == datareview.id) db.session.delete(datareview) db.session.commit() # Check that datareview is deleted reviews = DataReview.query.filter_by(publication_recid=recid).all() assert (len(reviews) == 0) # Check that message is deleted messages = Message.query.filter_by(id=review_messages[0].message_id).all() assert (len(messages) == 0)
def test_data_submission_cascades(app): # Create a data submission recid = "12345" datasubmission = DataSubmission(publication_recid=recid) db.session.add(datasubmission) db.session.commit() # Add a data review with a message message = Message(user=1, message="Test review message") datareview = DataReview(publication_recid=recid, data_recid=datasubmission.id, messages=[message]) db.session.add(datareview) db.session.commit() reviews = DataReview.query.filter_by(publication_recid=recid).all() assert (len(reviews) == 1) assert (reviews[0] == datareview) messages = Message.query.all() assert (len(messages) == 1) # Add some data resources with files files_dir = os.path.join(app.config['CFG_DATADIR'], 'models_test') os.makedirs(files_dir, exist_ok=True) resources = [] for i in range(3): file_path = os.path.join(files_dir, f'file{i}.txt') f = open(file_path, 'w') f.close() dataresource = DataResource(file_location=file_path, file_type="data") db.session.add(dataresource) db.session.commit() resources.append(dataresource) # Create an extra DataResource linking to file0.txt but # not linked to the submission # (because this situation has arisen in prod) dataresource = DataResource(file_location=os.path.join( files_dir, 'file0.txt'), file_type="data") db.session.add(dataresource) db.session.commit() assert (len(os.listdir(files_dir)) == 3) datasubmission.data_file = resources[0].id datasubmission.resources = resources[1:] db.session.add(datasubmission) db.session.commit() # Check we can find resources in db dataresources = DataResource.query.filter( DataResource.id.in_([x.id for x in resources])).all() assert (len(dataresources) == 3) # Delete datasubmission db.session.delete(datasubmission) db.session.commit() # Check that datareview is deleted reviews = DataReview.query.filter_by(publication_recid=recid).all() assert (len(reviews) == 0) # Check that message is deleted messages = Message.query.all() assert (len(messages) == 0) # Check all resources have been deleted dataresources = DataResource.query.filter( DataResource.id.in_([x.id for x in resources])).all() assert (len(dataresources) == 0) # Check files are also deleted, apart from file0 # as that's referenced by another DataResource assert (os.listdir(files_dir) == ['file0.txt']) # Tidy up shutil.rmtree(files_dir)