This commit is contained in:
Ross
2025-11-03 10:34:46 +00:00
2 changed files with 123 additions and 36 deletions
+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 %}