Skip to content

Commit

Permalink
Fix transfer of tags when merging 2 users
Browse files Browse the repository at this point in the history
  • Loading branch information
asaunier committed May 19, 2020
1 parent 74d28d0 commit 290763d
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions c2corg_api/scripts/users/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from c2corg_api.models.document_tag import DocumentTag, DocumentTagLog
from c2corg_api.models.feed import DocumentChange, FollowedUser, FilterArea
from c2corg_api.models.mailinglist import Mailinglist
from c2corg_api.models.route import ROUTE_TYPE
from c2corg_api.models.sso import SsoExternalId
from c2corg_api.models.token import Token
from c2corg_api.models.user import User
Expand Down Expand Up @@ -155,13 +156,14 @@ def _transfer_tags(source_user_id, target_user_id):
document_id for (document_id,) in target_document_ids_result]
# move the current tags (only if the target user has not
# already tagged the same document)
DBSession.execute(
transfered_document_ids_result = DBSession.execute(
DocumentTag.__table__.update().
where(_and_in(
DocumentTag.user_id == source_user_id,
DocumentTag.document_id, target_document_ids
)).
values(user_id=target_user_id)
values(user_id=target_user_id).
returning(DocumentTag.document_id)
)

# remove remaining tags
Expand All @@ -170,22 +172,23 @@ def _transfer_tags(source_user_id, target_user_id):
where(DocumentTag.user_id == source_user_id)
)

# Also reassign the tag author in tag logs
# to the target user:
DBSession.execute(
DocumentTagLog.__table__.update().
where(_and_in(
DocumentTagLog.user_id == source_user_id,
DocumentTagLog.document_id, target_document_ids
)).
values(user_id=target_user_id)
)

# remove all existing logs
DBSession.execute(
DocumentTagLog.__table__.delete().
where(DocumentTagLog.user_id == source_user_id)
)

# and create new ones for the transfered tags
DBSession.execute(
DocumentTagLog.__table__.insert().values([{
'document_id': document_id,
'user_id': target_user_id,
# FIXME OK as long as tags are only used for routes:
'document_type': ROUTE_TYPE,
'is_creation': True,
'written_at': func.now(),
} for (document_id,) in transfered_document_ids_result]))


def _update_feed_entries(source_user_id, target_user_id):
# Transfer feed entries to the target user only if no similar entry
Expand Down

0 comments on commit 290763d

Please sign in to comment.