-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrename_class.py
58 lines (46 loc) · 1.62 KB
/
rename_class.py
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
54
55
56
57
58
# A script to rename the Numeracy class in Kolibri to Learners on Program
import kolibri # noqa F401
import django
import sys
import argparse
from colors import *
from django.core.exceptions import ObjectDoesNotExist
django.setup()
from kolibri.core.auth.models import Classroom # noqa E402
argParser = argparse.ArgumentParser()
argParser.add_argument(
"--old_name", "-o", help="Current name of the class you with to rename"
)
argParser.add_argument(
"--new_name", "-n", help="New name of the class you with to rename"
)
def rename_class(old_name, new_name):
try:
Classroom.objects.get(name=old_name)
except ObjectDoesNotExist:
print_colored(
"A classroom with the name {} was not found".format(old_name),
colors.fg.red,
)
sys.exit("The Classroom was not renamed. Please check the error(s) above")
# get a reference to the classroom object to rename
class_to_rename = Classroom.objects.get(name=old_name)
# change to name of the class
class_to_rename.name = new_name
# save the object
class_to_rename.save()
# print a message to the console to indicate that it was successful
print_colored(
"Done! Class {} was renamed to {}".format(old_name, new_name),
colors.fg.lightgreen,
)
if __name__ == "__main__":
args = argParser.parse_args()
if args.old_name and args.new_name:
old_name = args.old_name
new_name = args.new_name
rename_class(old_name, new_name)
else:
sys.exit(
"Please supply the old name and new name of the class with the appropriate arguments"
)