-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinsert_coaches.py
163 lines (139 loc) · 5.87 KB
/
insert_coaches.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import kolibri # noqa F401
import django
import sys
import uuid
import csv
import argparse
from colors import *
from django.contrib.auth.hashers import make_password
from django.core.exceptions import ObjectDoesNotExist
django.setup()
from kolibri.core.auth.models import Facility, FacilityUser # noqa F402
# Initalize argparse and define all of the arguments to this module
argParser = argparse.ArgumentParser()
argParser.add_argument(
"--file",
"-f",
help="CSV file to import users from. Must contain the fields id,username,full_name",
)
argParser.add_argument(
"--centre",
"-c",
help="Name of Facility( centre_id) in the case of multiple facilities on 1 device",
)
# get the name of the default facility on the device
def_facility = str(Facility.get_default_facility().name)
def insert_coaches(input_file, facility=def_facility):
"""Insert users into a Facility from a csv file.
Fields expected in the csv file:
user_id
full_name
username
centre
Generally used to recreate users that already existed before without generating new user_ids
Args:
input_file (string): Path to the input csv file
facility (string): The name of the facility, default is the first facility that was created on a device
Returns:
None
"""
# get a reference to the Facility with the name supplied and it's dataset_id
try:
facility_obj = Facility.objects.get(name=facility)
facility_id = facility_obj.id
dataset_id = facility_obj.dataset_id
# catch the exception when the object does not exist
except ObjectDoesNotExist:
# print out the id that does not exist
print("Error: Facility with the name {} does not exist".format(facility))
# exit in an error state
sys.exit("Learners were not inserted successfully. Check the error(s) above")
with open(input_file) as f:
reader = csv.DictReader(f)
users = [r for r in reader]
# Initialize num_inserted to 0
num_inserted = 0
for user in users:
if user["centre"] == str(facility_obj.name):
username_exists = FacilityUser.objects.filter(
username=user["username"], facility_id=facility_id
).exists()
user_id_exists = FacilityUser.objects.filter(
id=user["user_id"], facility_id=facility_id
).exists()
if user_id_exists:
# if a user with the same user_id already exists in the facility
# raise a value error and terminate the script
print_colored(
"Duplicate user ID. There is already a user with ID {}".format(
user["user_id"]
),
colors.fg.yellow,
)
continue
elif username_exists:
# if a user with the same username already exists in the facility
# raise a value error and terminate the script
print_colored(
"Duplicate username. There is already a user called {}".format(
user["username"]
),
colors.fg.yellow,
)
continue
else:
# Generate the morango partition
_morango_partition = "{dataset_id}:user-ro:{user_id}".format(
dataset_id=dataset_id, user_id=user["user_id"]
)
# Create the user
new_coach = FacilityUser.objects.create(
id=user["user_id"],
full_name=user["full_name"],
username=user["username"],
password=make_password(user["username"]),
dataset_id=dataset_id,
facility_id=facility_id,
_morango_partition=_morango_partition,
_morango_source_id=uuid.uuid4(),
)
print_colored(
"Created coach: {}".format(user["full_name"]),
colors.fg.yellow,
)
facility_obj.add_role(new_coach, "coach")
print_colored(
"Added coach role for user: {} in Facility: {}".format(
user["full_name"], str(facility_obj.name)
),
colors.fg.yellow,
)
# Increment num_inserted by 1
num_inserted += 1
else:
continue
# Print out the total number of users that were inserted
if num_inserted == 0:
# If not learners were inserted, something is wrong and there will be errors displayed in the console
print_colored(
"No Coaches were inserted. Kindly check the errors above",
colors.fg.red,
)
else:
print_colored(
"{} Coaches were inserted".format(num_inserted),
colors.fg.lightgreen,
)
if __name__ == "__main__":
args = argParser.parse_args()
if args.file and not (args.centre):
open_file = args.file
insert_coaches(open_file)
elif args.file and args.centre:
facility = args.centre
open_file = args.file
insert_coaches(open_file, facility)
else:
sys.exit(
"No arguments passed in. Please pass in the path of the file and centre_id (optional)"
)