add some basic checks when batch creating users
This commit is contained in:
@@ -730,6 +730,35 @@ def accounts_check_users(request):
|
||||
|
||||
return
|
||||
|
||||
@user_is_cid_user_manager
|
||||
def accounts_bulk_create_check(request):
|
||||
if not request.method == "POST":
|
||||
return HttpResponse("Invalid request")
|
||||
|
||||
user = json.loads(request.POST.get("user"))
|
||||
|
||||
if User.objects.filter(username=user["email"]).exists():
|
||||
return HttpResponse(format_html("<span class='error'>{}</span>", "User already exists"))
|
||||
|
||||
if not UserGrades.objects.filter(name=user["grade"]):
|
||||
return HttpResponse(format_html("<span class='error'>{}</span>", "Invalid grade"))
|
||||
|
||||
if "supervisor_email" in user:
|
||||
supervisor = Supervisor.objects.filter(email=user["supervisor_email"])
|
||||
|
||||
if not supervisor:
|
||||
return HttpResponse("Ok, supervisor will be created")
|
||||
|
||||
if supervisor.count() > 1:
|
||||
return HttpResponse(format_html("<span class='error'>{}</span>", "More than one supervisor with that email"))
|
||||
|
||||
if supervisor.first().name != user["supervisor_name"]:
|
||||
return HttpResponse(format_html("<span class='error'>{}</span>", "Supervisor name does not match"))
|
||||
|
||||
return HttpResponse("OK, supervisor exists")
|
||||
|
||||
return HttpResponse("OK")
|
||||
|
||||
|
||||
@user_is_cid_user_manager
|
||||
def accounts_bulk_create(request):
|
||||
@@ -781,6 +810,7 @@ def accounts_bulk_create(request):
|
||||
try:
|
||||
user_profile = UserProfile.objects.get(user=new_user)
|
||||
user_profile.peninsula_trainee = True
|
||||
# TODO: check supervisor details are correct
|
||||
if "supervisor_email" in user and "supervisor_name" in user:
|
||||
s, created = Supervisor.objects.get_or_create(
|
||||
email=user["supervisor_email"],
|
||||
|
||||
Reference in New Issue
Block a user