This commit is contained in:
Ross
2025-11-03 10:34:46 +00:00
2 changed files with 123 additions and 36 deletions
+53 -15
View File
@@ -815,8 +815,20 @@ def accounts_bulk_create_check(request):
if not UserGrades.objects.filter(name=user["grade"]):
errors.append("Invalid grade")
if user["supervisor_email"] == "undefined":
errors.append("No supervisor email provided")
# Treat empty, missing or the string 'undefined' as no supervisor email provided
if not user.get("supervisor_email") or user["supervisor_email"] == "undefined":
# Supervisor email not provided; try to match by name
if not user.get("supervisor_name"):
errors.append("No supervisor provided")
else:
supervisor = Supervisor.objects.filter(name__iexact=user["supervisor_name"])
if not supervisor.exists():
info.append("Supervisor does not exist (will be created)")
else:
info.append("Supervisor exists (matched by name)")
if supervisor.count() > 1:
errors.append("More than one supervisor with that name")
# no further name check needed since matched by name
else:
supervisor = Supervisor.objects.filter(email=user["supervisor_email"])
@@ -828,8 +840,14 @@ def accounts_bulk_create_check(request):
if supervisor.count() > 1:
errors.append("More than one supervisor with that email")
if supervisor.first().name != user["supervisor_name"]:
errors.append("Supervisor name does not match")
# Compare names case-insensitively and trimmed to avoid false mismatches
try:
sup_name = (supervisor.first().name or '').strip().lower()
posted_name = (user.get("supervisor_name") or '').strip().lower()
if posted_name and sup_name != posted_name:
errors.append("Supervisor name does not match")
except Exception:
pass
if errors:
return HttpResponse(
@@ -869,11 +887,22 @@ def accounts_bulk_create(request):
grade = UserGrades.objects.get(name=user["grade"])
user_profile.grade = grade
if "supervisor_email" in user and "supervisor_name" in user:
s, created = Supervisor.objects.get_or_create(
email=user["supervisor_email"],
name=user["supervisor_name"],
)
if "supervisor_name" in user and user.get("supervisor_name"):
sup_email = user.get("supervisor_email")
# If supervisor email provided, prefer that. Otherwise try to match by name or create placeholder
if sup_email and sup_email != 'undefined':
s, created = Supervisor.objects.get_or_create(email=sup_email, defaults={"name": user.get("supervisor_name")})
else:
# try match by name
s_qs = Supervisor.objects.filter(name__iexact=user.get("supervisor_name"))
if s_qs.exists():
s = s_qs.first()
created = False
else:
import uuid
placeholder = f"no-email-{uuid.uuid4().hex}@local.invalid"
s = Supervisor.objects.create(email=placeholder, name=user.get("supervisor_name"))
created = True
if created:
s.save()
@@ -913,11 +942,20 @@ def accounts_bulk_create(request):
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"],
name=user["supervisor_name"],
)
if "supervisor_name" in user and user.get("supervisor_name"):
sup_email = user.get("supervisor_email")
if sup_email and sup_email != 'undefined':
s, created = Supervisor.objects.get_or_create(email=sup_email, defaults={"name": user.get("supervisor_name")})
else:
s_qs = Supervisor.objects.filter(name__iexact=user.get("supervisor_name"))
if s_qs.exists():
s = s_qs.first()
created = False
else:
import uuid
placeholder = f"no-email-{uuid.uuid4().hex}@local.invalid"
s = Supervisor.objects.create(email=placeholder, name=user.get("supervisor_name"))
created = True
if created:
s.save()
@@ -962,7 +1000,7 @@ def request_cid_details(request):
email = request.POST.get("email")
try:
validate_email(email)
except ValidationError as e:
except ValidationError:
return HttpResponse("Invalid email.")
cid_users = CidUser.objects.filter(email=email)
+70 -21
View File
@@ -36,12 +36,20 @@
</p>
<p>Please note users created this way bypass some of the validations so check details such as email addresses are correct</p>
<div class="mb-2">
<label for="input-format" class="form-label">Input format</label>
<select id="input-format" class="form-select" style="width:300px">
<option value="spreadsheet">First,Last,Email,Grade,Supervisor,SupervisorEmail (spreadsheet)</option>
<option value="name-supervisor-email-grade">User \t Educational Supervisor \t Email \t Grade</option>
</select>
</div>
<textarea id="csv" placeholder="Paste users content here" style="width: 300px; height: 100px;"></textarea><br/>
<input type="button" value="Load Data" _="on click createTable()" >
<div role="alert" id="alert" class="alert alert-info "
_="on load hide me">
_="on load hide me">
</div>
@@ -67,6 +75,23 @@
// split into rows
excelRow = excelData.split(String.fromCharCode(10));
// If the first row looks like a header (contains words like 'user', 'email', 'grade', 'supervisor'), drop it
if (excelRow.length) {
var firstRowCols = excelRow[0].split(String.fromCharCode(9)).map(function(c){return c.trim().toLowerCase();});
var headerKeywords = ['user','first name','last name','email','grade','educational supervisor','supervisor','first','last'];
var isHeader = firstRowCols.some(function(c){
if (!c) return false;
if (headerKeywords.indexOf(c) !== -1) return true;
if (c.indexOf('email') !== -1) return true;
if (c.indexOf('supervisor') !== -1) return true;
return false;
});
if (isHeader) {
// remove header row
excelRow.shift();
}
}
// split rows into columns
for (i=0; i<excelRow.length; i++) {
excelRow[i] = excelRow[i].split(String.fromCharCode(9));
@@ -79,7 +104,14 @@
// Loop over the rows
var format = document.getElementById('input-format').value;
var elements = ["first_name", "last_name", "email", "grade", "supervisor_name", "supervisor_email"];
// For alternate format: User\tEducational Supervisor\tEmail\tGrade
// columns: full name, supervisor name, email, grade
if (format === 'name-supervisor-email-grade') {
elements = ['full_name', 'supervisor_name', 'email', 'grade'];
}
var users = [];
$("#users-list").empty();
@@ -95,15 +127,32 @@
// Loop over the columns and add TD to the TR
for (j=0; j<excelRow[i].length; j++) {
// Loop over the row columns
if (excelRow[i][j].length != 0) {
var myCell = document.createElement("td");
myCell.innerHTML = excelRow[i][j].trim();
//users[i][elements[j]] = excelRow[i][j];
user[elements[j]] = excelRow[i][j].trim();
var key = elements[j] || null;
if (key) {
user[key] = excelRow[i][j].trim();
}
}
myRow.appendChild(myCell);
}
// If alternate format, split full_name into first_name/last_name and ensure supervisor_email empty
if (format === 'name-supervisor-email-grade') {
var full = user.full_name || '';
full = full.trim();
if (full.indexOf(' ') > -1) {
var parts = full.split(/\s+/);
user.first_name = parts[0];
user.last_name = parts.slice(1).join(' ');
} else {
user.first_name = full;
user.last_name = '';
}
// supervisor email not provided in this format
user.supervisor_email = '';
}
myTbody.appendChild(myRow);
users.push(user)
emails.push(user.email)
@@ -129,8 +178,8 @@
$.post("{% url 'accounts_check_users' %}", { csrfmiddlewaretoken: "{{ csrf_token }}",
user_list: JSON.stringify(emails) }, function(data) {
console.log("Existing users", data)
})
console.log("Existing users", data)
})
$("#users-list").append(`${users.length} users to create/update.`)
@@ -154,23 +203,23 @@
</script>
<script type="text/hyperscript">
def updateWarnings()
log "test"
then
if <#users-list li:has(.error)/> is not empty
put "Errors<br/>" into #alert
then for error in<#users-list li:has(.error)/>
put textContent of .name in error at end of #alert
then
put " " + textContent of .error in error at end of #alert
then make a <br/> then put it at the end of #alert
then add .alert-warning to #alert
log "test"
then
if <#users-list li:has(.error)/> is not empty
put "Errors<br/>" into #alert
then for error in<#users-list li:has(.error)/>
put textContent of .name in error at end of #alert
then
put " " + textContent of .error in error at end of #alert
then make a <br/> then put it at the end of #alert
then add .alert-warning to #alert
end
end
else
else
put "No errors" into #alert
end
then show #alert
end
then show #alert
end
</script>
@@ -207,6 +256,6 @@
#users-list li:has(.error) {
border: 1px solid red;
}
</style>
{% endblock content %}