Spaces:
Running
Running
import argparse | |
import sys | |
import os | |
# Add project root to Python path | |
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) | |
if project_root not in sys.path: | |
sys.path.insert(0, project_root) | |
from utils.database import get_db | |
from data.models import Annotator | |
from utils.logger import Logger | |
log = Logger() | |
def update_annotator_name(old_name: str, new_name: str): | |
""" | |
Updates the name of an existing annotator. | |
Keeps the password and annotation intervals the same. | |
""" | |
with get_db() as db: | |
try: | |
# Check if the new name already exists | |
existing_annotator_with_new_name = db.query(Annotator).filter(Annotator.name == new_name).first() | |
if existing_annotator_with_new_name: | |
log.error(f"Error: An annotator with the name '{new_name}' already exists.") | |
return | |
annotator = db.query(Annotator).filter(Annotator.name == old_name).first() | |
if not annotator: | |
log.error(f"Error: Annotator with name '{old_name}' not found.") | |
return | |
annotator.name = new_name | |
db.commit() | |
log.info(f"Successfully updated annotator name from '{old_name}' to '{new_name}'.") | |
log.info(f"ID: {annotator.id}, New Name: {annotator.name}") | |
log.info("Password and annotation intervals remain unchanged.") | |
except Exception as e: | |
db.rollback() | |
log.error(f"Failed to update annotator name: {e}") | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Update an annotator's name.") | |
parser.add_argument("old_name", type=str, help="The current name of the annotator.") | |
parser.add_argument("new_name", type=str, help="The new name for the annotator.") | |
args = parser.parse_args() | |
update_annotator_name(args.old_name, args.new_name) | |