File size: 1,903 Bytes
1000353
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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)