Skip to content

Commit

Permalink
To merge two Keywords(Source Keyword and Target Keyword)
Browse files Browse the repository at this point in the history
  • Loading branch information
Veda-Gogineni committed Feb 26, 2024
1 parent 415bf17 commit b39e782
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
35 changes: 35 additions & 0 deletions app/modules/keywords/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,38 @@ def number_referenced_dependencies(self):
refs += AnnotationKeywords.query.filter_by(keyword_guid=self.guid).all()
refs += AssetTags.query.filter_by(tag_guid=self.guid).all()
return len(refs)

def merge(self, other):
# self.guid is the traget_keyword.guid
# other.guid is the source_keyword.guid
# the goal of this function is to merge the source_keyword into the target_keyword
# and update all the references to the source_keyword to point to the target_keyword
# and then delete the source_keyword
# A keyword can be referenced by annotations and assets
# When a keyword is merged, all the references to the source_keyword should be updated to point to the target_keyword

# Import necessary modules
from app.modules.annotations.models import AnnotationKeywords
from app.modules.assets.models import AssetTags

# Get and update all the annotations that reference the other keyword
ref_annos = AnnotationKeywords.query.filter_by(keyword_guid=other.guid).all()
for ref_ann in ref_annos:
ref_ann.keyword_guid = self.guid

# Get and update all the assets that reference the other keyword
ref_assets = AssetTags.query.filter_by(tag_guid=other.guid).all()
for ref_asset in ref_assets:
ref_asset.tag_guid = self.guid

# Delete the other keyword
other.delete()

try:
updated_ref_ann = AnnotationKeywords.query.filter_by(keyword_guid=self.guid).first()
if updated_ref_ann.keyword_guid == self.guid:
log.info("The annotation's keyword_guid has been successfully updated.")
else:
log.info("The update did not take place as expected.")
except Exception as e:
log.info(f"Error during merge commit: {e}")
36 changes: 36 additions & 0 deletions app/modules/keywords/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,39 @@ def delete(self, keyword):
with context:
db.session.delete(keyword)
return None


# @api.resolve_object_by_model(Keyword, 'keyword')
#POST API: /api/v1/keyword/source_keyword_guid/target_keyword_guid/merge. I am getting 500 error when i am trying to run this api. How to retify this error?
@api.route('/<uuid:source_keyword_guid>/<uuid:target_keyword_guid>/merge')
@api.login_required(oauth_scopes=['keywords:write'])
@api.response(
code=HTTPStatus.NOT_FOUND,
description='Keyword not found or merge failed.',
)
@api.resolve_object_by_model(Keyword, 'source_keyword')
@api.resolve_object_by_model(Keyword, 'target_keyword')
class MergeKeyword(Resource):
"""
Merge source_keyword to target_keyword .
"""
@api.permission_required(
permissions.ModuleAccessPermission,
kwargs_on_request=lambda kwargs: {
'module': Keyword,
'action': AccessOperation.WRITE,
},
)
@api.response(schemas.BaseKeywordSchema())
def post(self, source_keyword, target_keyword):
"""
Merge source_keyword to target_keyword .
"""
try:
log.info("MergeKeyword: source_keyword: %s", source_keyword)
target_keyword.merge(source_keyword)

except Exception as e:
log.error("MergeKeyword: post: %s", e)

return target_keyword

0 comments on commit b39e782

Please sign in to comment.