Skip to content
This repository was archived by the owner on Sep 3, 2025. It is now read-only.

Commit 9c6c6c3

Browse files
committed
#67, #98, allow creation of multiple students with comma separated lists
1 parent 4850bcb commit 9c6c6c3

3 files changed

Lines changed: 45 additions & 13 deletions

File tree

coldcall/templates/coldcall/addedit_student_manual.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
{% endfor %}
2222
{% endif %}
2323
<h2>{% if student %}Edit Student{% else %}Add New Student{% endif %}</h2>
24+
{% if not student %}
25+
<p> Multiple students can be added at once by entering a comma separated list of names in the first name and last name fields. <br> Example: <br> First Name: John, Jane <br> Last Name: Doe, Smith</p>
26+
{% endif %}
2427
<form method="post">
2528
{% csrf_token %}
2629

@@ -45,13 +48,12 @@ <h2>{% if student %}Edit Student{% else %}Add New Student{% endif %}</h2>
4548
</div>
4649

4750
<div class="form-group">
48-
<label for="usc_id">USC ID</label>
51+
<label for="usc_id">USC ID (Max. 9 characters)</label>
4952
<input
5053
type="text"
5154
id="usc_id"
5255
name="usc_id"
5356
value="{{ student.usc_id|default:'' }}"
54-
maxlength="9"
5557
required />
5658
</div>
5759

coldcall/templates/coldcall/mobile/addedit_student_manual.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
{% endfor %}
2222
{% endif %}
2323
<h2>{% if student %}Edit Student{% else %}Add New Student{% endif %}</h2>
24+
25+
{% if not student %}
26+
<p> Multiple students can be added at once by entering a comma separated list of names in the first name and last name fields. <br> Example: <br> First Name: John, Jane <br> Last Name: Doe, Smith</p>
27+
{% endif %}
2428
<form method="post">
2529
{% csrf_token %}
2630

@@ -45,13 +49,12 @@ <h2>{% if student %}Edit Student{% else %}Add New Student{% endif %}</h2>
4549
</div>
4650

4751
<div class="form-group">
48-
<label for="usc_id">USC ID</label>
52+
<label for="usc_id">USC ID (Max. 9 characters)</label>
4953
<input
5054
type="text"
5155
id="usc_id"
5256
name="usc_id"
5357
value="{{ student.usc_id|default:'' }}"
54-
maxlength="9"
5558
required />
5659
</div>
5760

coldcall/views/views_student.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.contrib import messages
22
from django.contrib.auth.mixins import LoginRequiredMixin
3+
from django.db import transaction
34
from django.http import HttpResponseBadRequest, HttpResponseForbidden, JsonResponse
45
from django.shortcuts import get_object_or_404, redirect, render
56
from django.views import View
@@ -35,17 +36,43 @@ def post(self, request, class_id=None):
3536
class_key = Class.objects.get(id=class_key_id)
3637
except Class.DoesNotExist:
3738
return HttpResponseBadRequest("Invalid class ID.")
39+
40+
#helper function to ensure input is valid
41+
def same_length_arr(*arrays):
42+
lengths = [len(arr) for arr in arrays]
43+
return all(length == lengths[0] for length in lengths)
44+
45+
# convert all into lists if user provided multiple inputs
46+
usc_id = usc_id.split(",")
47+
first_name = first_name.split(",")
48+
last_name = last_name.split(",")
49+
email = email.split(",")
3850

39-
Student.objects.create(
40-
usc_id=usc_id,
41-
first_name=first_name,
42-
last_name=last_name,
43-
class_key=class_key,
44-
seating=seating,
45-
email=email,
46-
)
51+
if not same_length_arr(usc_id, first_name, last_name, email):
52+
messages.error(request, "All fields must have the same number of entries.")
53+
return render(request, self.template_name, {'classes': Class.objects.filter(professor_key=request.user)})
54+
55+
# add all students at once, abort on failure
56+
with transaction.atomic():
57+
for i in range(len(usc_id)):
58+
if len(usc_id[i]) > 9:
59+
messages.error(request, "USC ID must be 9 characters long.")
60+
return render(request, self.template_name, {'classes': Class.objects.filter(professor_key=request.user)})
61+
62+
student = Student(
63+
usc_id=usc_id[i],
64+
first_name=first_name[i],
65+
last_name=last_name[i],
66+
class_key=class_key,
67+
seating=seating,
68+
email=email[i]
69+
)
70+
student.save()
4771

48-
messages.success(request, f"Student {first_name} {last_name} added successfully!")
72+
if len(usc_id) == 1:
73+
messages.success(request, f"Student {first_name[0]} {last_name[0]} added successfully!")
74+
else:
75+
messages.success(request, f"{len(usc_id)} students added successfully!")
4976

5077
# Clear the form fields and allow the user to add another student
5178
return render(request, self.template_name, {'classes': Class.objects.filter(professor_key=request.user)})

0 commit comments

Comments
 (0)