From 8e1b8783b29463d500208538b25e531dea18095b Mon Sep 17 00:00:00 2001 From: wei-kuochen Date: Tue, 21 Jan 2025 16:13:17 +0900 Subject: [PATCH 1/4] weko#49129 fix merge author issue --- modules/weko-deposit/weko_deposit/tasks.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/modules/weko-deposit/weko_deposit/tasks.py b/modules/weko-deposit/weko_deposit/tasks.py index acfc68e5c0..814a64a5b5 100644 --- a/modules/weko-deposit/weko_deposit/tasks.py +++ b/modules/weko-deposit/weko_deposit/tasks.py @@ -36,6 +36,7 @@ from sqlalchemy.exc import SQLAlchemyError from weko_authors.models import Authors, AuthorsPrefixSettings, AuthorsAffiliationSettings from weko_records.api import ItemsMetadata +from weko_records.models import FeedbackMailList from weko_schema_ui.models import PublishStatus from weko_workflow.utils import delete_cache_data, update_cache_data @@ -418,6 +419,7 @@ def _process(data_size, data_from): if update_gather_flg: process_counter[ORIGIN_LABEL] = get_origin_data(origin_pkid_list) update_db_es_data(origin_pkid_list, origin_id_list) + update_feedback_mail_data(target, origin_pkid_list) delete_cache_data("update_items_by_authorInfo_{}".format(user_id)) update_cache_data( "update_items_status_{}".format(user_id), @@ -486,6 +488,21 @@ def update_db_es_data(origin_pkid_list, origin_id_list): db.session.rollback() +def update_feedback_mail_data(target, origin_pkid_list): + try: + # update DB of FeedbackMailList + with db.session.begin_nested(): + feedback_mail_datas = FeedbackMailList.query.filter( + FeedbackMailList.account_author.in_(origin_pkid_list)).all() + for fdata in feedback_mail_datas: + fdata.account_author = target.get('pk_id') + db.session.merge(fdata) + db.session.commit() + except Exception as ex: + current_app.logger.debug(ex) + db.session.rollback() + + def make_stats_file(raw_stats): """Make TSV/CSV report file for stats.""" file_format = current_app.config.get('WEKO_ADMIN_OUTPUT_FORMAT', 'tsv').lower() From 1d6f638790d0f7e604693835a63477564f93306b Mon Sep 17 00:00:00 2001 From: wei-kuochen Date: Tue, 21 Jan 2025 16:22:00 +0900 Subject: [PATCH 2/4] weko#49129 added test code --- modules/weko-deposit/tests/test_tasks.py | 31 ++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/modules/weko-deposit/tests/test_tasks.py b/modules/weko-deposit/tests/test_tasks.py index beca0774a1..a8f76b277e 100644 --- a/modules/weko-deposit/tests/test_tasks.py +++ b/modules/weko-deposit/tests/test_tasks.py @@ -19,7 +19,7 @@ # MA 02111-1307, USA. """Module tests.""" - +import uuid import pytest import json from elasticsearch.exceptions import NotFoundError @@ -27,8 +27,9 @@ from mock import patch, MagicMock from invenio_pidstore.errors import PIDDoesNotExistError from weko_authors.models import AuthorsAffiliationSettings,AuthorsPrefixSettings +from weko_records.models import FeedbackMailList -from weko_deposit.tasks import update_items_by_authorInfo +from weko_deposit.tasks import update_items_by_authorInfo, update_feedback_mail_data [ { "recid": "1", @@ -223,3 +224,29 @@ def test_update_authorInfo(app, db, records,mocker): with patch("weko_deposit.tasks.RecordIndexer", MockRecordIndexer): update_items_by_authorInfo(["1","xxx"], _target) + +# .tox/c1/bin/pytest --cov=weko_deposit tests/test_tasks.py::test_update_feedback_mail_data -v -s -vv --cov-branch --cov-report=term --cov-config=tox.ini --basetemp=/code/modules/weko-deposit/.tox/c1/tmp +def test_update_feedback_mail_data(app, db): + feedback_mail_list1 = FeedbackMailList( + id=1, + item_id=uuid.uuid4(), + mail_list=[], + account_author="2" + ) + feedback_mail_list2 = FeedbackMailList( + id=2, + item_id=uuid.uuid4(), + mail_list=[], + account_author="3" + ) + db.session.add(feedback_mail_list1) + db.session.add(feedback_mail_list2) + db.session.commit() + + update_feedback_mail_data({"pk_id": "1"}, ["2", "3"]) + + feedback_mail_datas = FeedbackMailList.query.filter( + FeedbackMailList.account_author.in_(["1"])).all() + + assert feedback_mail_datas[0].account_author == "1" + assert feedback_mail_datas[1].account_author == "1" From 3db1d1b67e9162ac4569c6ac9e7b65104ee3dbea Mon Sep 17 00:00:00 2001 From: wei-kuochen Date: Wed, 22 Jan 2025 15:33:32 +0900 Subject: [PATCH 3/4] weko#49129 fix merge author issue --- modules/weko-deposit/weko_deposit/tasks.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/modules/weko-deposit/weko_deposit/tasks.py b/modules/weko-deposit/weko_deposit/tasks.py index 814a64a5b5..8ea7c2de68 100644 --- a/modules/weko-deposit/weko_deposit/tasks.py +++ b/modules/weko-deposit/weko_deposit/tasks.py @@ -34,11 +34,13 @@ from invenio_records.models import RecordMetadata from invenio_search import RecordsSearch from sqlalchemy.exc import SQLAlchemyError +from sqlalchemy.orm.attributes import flag_modified from weko_authors.models import Authors, AuthorsPrefixSettings, AuthorsAffiliationSettings from weko_records.api import ItemsMetadata from weko_records.models import FeedbackMailList from weko_schema_ui.models import PublishStatus from weko_workflow.utils import delete_cache_data, update_cache_data +from weko_workflow.models import ActionFeedbackMail, Activity, ActivityStatusPolicy from .api import WekoDeposit @@ -497,6 +499,23 @@ def update_feedback_mail_data(target, origin_pkid_list): for fdata in feedback_mail_datas: fdata.account_author = target.get('pk_id') db.session.merge(fdata) + action_feedback_mails = ActionFeedbackMail.query \ + .outerjoin(Activity, + ActionFeedbackMail.activity_id==Activity.activity_id) \ + .filter(Activity.activity_status.in_( + [ActivityStatusPolicy.ACTIVITY_BEGIN, + ActivityStatusPolicy.ACTIVITY_MAKING])) \ + .all() + for adata in action_feedback_mails: + for j, fdata in enumerate(adata.feedback_maillist): + if str(fdata.get("author_id")) in origin_pkid_list: + adata.feedback_maillist[j]["author_id"] = target.get('pk_id') + if len(target.get("emailInfo", [])) > 0 and \ + target.get("emailInfo")[0].get("email"): + adata.feedback_maillist[j]["email"] = \ + target.get("emailInfo")[0].get("email") + flag_modified(adata, 'feedback_maillist') + db.session.merge(adata) db.session.commit() except Exception as ex: current_app.logger.debug(ex) From 98446f1a7d08dfdf5f5c63bde816e7c0d831cb2e Mon Sep 17 00:00:00 2001 From: wei-kuochen Date: Wed, 22 Jan 2025 15:34:06 +0900 Subject: [PATCH 4/4] weko#49129 added test code --- modules/weko-deposit/tests/conftest.py | 54 ++++++++++++++++-------- modules/weko-deposit/tests/test_tasks.py | 27 ++++++++++-- 2 files changed, 59 insertions(+), 22 deletions(-) diff --git a/modules/weko-deposit/tests/conftest.py b/modules/weko-deposit/tests/conftest.py index a65a861813..9bb40453c8 100644 --- a/modules/weko-deposit/tests/conftest.py +++ b/modules/weko-deposit/tests/conftest.py @@ -845,24 +845,42 @@ def db_activity(app, db,users,location,db_itemtype,db_actions): with db.session.begin_nested(): db.session.add(no_location_workflow) db.session.add(location_workflow) - no_location_activity = Activity(activity_id='1',workflow_id=no_location_workflow.id, flow_id=flow_define.id, - action_id=1, activity_login_user=1, - activity_update_user=1, - activity_start=datetime.strptime('2022/04/14 3:01:53.931', '%Y/%m/%d %H:%M:%S.%f'), - activity_community_id=3, - activity_confirm_term_of_use=True, - title='test', shared_user_id=-1, extra_info={}, - action_order=1, - ) - location_activity = Activity(activity_id='2',workflow_id=location_workflow.id, flow_id=flow_define.id, - action_id=1, activity_login_user=1, - activity_update_user=1, - activity_start=datetime.strptime('2022/04/14 3:01:53.931', '%Y/%m/%d %H:%M:%S.%f'), - activity_community_id=3, - activity_confirm_term_of_use=True, - title='test', shared_user_id=-1, extra_info={}, - action_order=1, - ) + activity = Activity( + activity_id='A1', + workflow_id=location_workflow.id, + flow_id=flow_define.id, + action_id=3, + activity_status='M', + activity_login_user=1, + activity_update_user=1, + activity_start=datetime.strptime('2022/04/14 3:01:53.931', '%Y/%m/%d %H:%M:%S.%f'), + activity_community_id=3, + activity_confirm_term_of_use=True, + title='test', + shared_user_id=-1, + extra_info={}, + action_order=1, + ) + no_location_activity = Activity( + activity_id='1',workflow_id=no_location_workflow.id, flow_id=flow_define.id, + action_id=1, activity_login_user=1, + activity_update_user=1, + activity_start=datetime.strptime('2022/04/14 3:01:53.931', '%Y/%m/%d %H:%M:%S.%f'), + activity_community_id=3, + activity_confirm_term_of_use=True, + title='test', shared_user_id=-1, extra_info={}, + action_order=1, + ) + location_activity = Activity( + activity_id='2',workflow_id=location_workflow.id, flow_id=flow_define.id, + action_id=1, activity_login_user=1, + activity_update_user=1, + activity_start=datetime.strptime('2022/04/14 3:01:53.931', '%Y/%m/%d %H:%M:%S.%f'), + activity_community_id=3, + activity_confirm_term_of_use=True, + title='test', shared_user_id=-1, extra_info={}, + action_order=1, + ) with db.session.begin_nested(): db.session.add(no_location_activity) db.session.add(location_activity) diff --git a/modules/weko-deposit/tests/test_tasks.py b/modules/weko-deposit/tests/test_tasks.py index a8f76b277e..717b2204f4 100644 --- a/modules/weko-deposit/tests/test_tasks.py +++ b/modules/weko-deposit/tests/test_tasks.py @@ -28,6 +28,7 @@ from invenio_pidstore.errors import PIDDoesNotExistError from weko_authors.models import AuthorsAffiliationSettings,AuthorsPrefixSettings from weko_records.models import FeedbackMailList +from weko_workflow.models import ActionFeedbackMail from weko_deposit.tasks import update_items_by_authorInfo, update_feedback_mail_data [ @@ -226,7 +227,7 @@ def test_update_authorInfo(app, db, records,mocker): # .tox/c1/bin/pytest --cov=weko_deposit tests/test_tasks.py::test_update_feedback_mail_data -v -s -vv --cov-branch --cov-report=term --cov-config=tox.ini --basetemp=/code/modules/weko-deposit/.tox/c1/tmp -def test_update_feedback_mail_data(app, db): +def test_update_feedback_mail_data(app, db, db_activity): feedback_mail_list1 = FeedbackMailList( id=1, item_id=uuid.uuid4(), @@ -247,6 +248,24 @@ def test_update_feedback_mail_data(app, db): feedback_mail_datas = FeedbackMailList.query.filter( FeedbackMailList.account_author.in_(["1"])).all() - - assert feedback_mail_datas[0].account_author == "1" - assert feedback_mail_datas[1].account_author == "1" + assert len(feedback_mail_datas) == 2 + + action_feedback_mail = ActionFeedbackMail( + id=1, + activity_id='A1', + action_id=3, + feedback_maillist=[ + {"email": "c@test.com", "author_id": ""}, + {"email": "b@test.com", "author_id": "2"} + ] + ) + db.session.add(action_feedback_mail) + db.session.commit() + + update_feedback_mail_data({"pk_id": "1", "emailInfo": [{"email": "a@test.com"}]}, ["2"]) + + action_feedback_mails = ActionFeedbackMail.query.all() + assert action_feedback_mails.feedback_maillist == [ + {"email": "c@test.com", "author_id": ""}, + {"email": "a@test.com", "author_id": "1"} + ] \ No newline at end of file