Skip to content

Commit d7d9811

Browse files
committed
fix: collection of fixes
1 parent 83788ad commit d7d9811

File tree

8 files changed

+111
-69
lines changed

8 files changed

+111
-69
lines changed

intranet/apps/eighth/migrations/0072_auto_20250429_1128.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ def generate_attendance_codes(apps, schema_editor):
88
HistoricalEighthScheduledActivity = apps.get_model('eighth', 'HistoricalEighthScheduledActivity')
99

1010
for activity in EighthScheduledActivity.objects.all():
11-
activity.attendance_code = intranet.apps.eighth.models.EighthScheduledActivity.random_code()
11+
activity.attendance_code = intranet.apps.eighth.models.random_code()
1212
activity.save(update_fields=['attendance_code'])
1313

1414
for historical_activity in HistoricalEighthScheduledActivity.objects.all():
15-
historical_activity.attendance_code = intranet.apps.eighth.models.EighthScheduledActivity.random_code()
15+
historical_activity.attendance_code = intranet.apps.eighth.models.random_code()
1616
historical_activity.save(update_fields=['attendance_code'])
1717

1818

@@ -44,4 +44,14 @@ class Migration(migrations.Migration):
4444
name='code_mode',
4545
field=models.IntegerField(choices=[(0, 'Auto'), (1, 'Open'), (2, 'Closed')], default=0),
4646
),
47+
migrations.AddField(
48+
model_name='eighthsignup',
49+
name='attendance_marked',
50+
field=models.BooleanField(blank=True, default=False),
51+
),
52+
migrations.AddField(
53+
model_name='historicaleighthsignup',
54+
name='attendance_marked',
55+
field=models.BooleanField(blank=True, default=False),
56+
),
4757
]

intranet/apps/eighth/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,6 +1750,7 @@ class EighthSignup(AbstractBaseEighthModel):
17501750

17511751
pass_accepted = models.BooleanField(default=False, blank=True)
17521752
was_absent = models.BooleanField(default=False, blank=True)
1753+
attendance_marked = models.BooleanField(default=False, blank=True)
17531754
absence_acknowledged = models.BooleanField(default=False, blank=True)
17541755
absence_emailed = models.BooleanField(default=False, blank=True)
17551756

@@ -1839,6 +1840,7 @@ def accept_pass(self):
18391840
"""Accepts an eighth period pass for the EighthSignup object."""
18401841
self.was_absent = False
18411842
self.pass_accepted = True
1843+
self.attendance_marked = True
18421844
self.save(update_fields=["was_absent", "pass_accepted"])
18431845

18441846
def reject_pass(self):

intranet/apps/eighth/views/attendance.py

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,11 @@ def take_attendance_view(request, scheduled_activity_id):
307307
status=403,
308308
)
309309

310-
if "att_code_mode" in request.POST:
311-
selected_mode = int(request.POST["att_code_mode"])
312-
if scheduled_activity.code_mode != selected_mode:
313-
scheduled_activity.set_code_mode(selected_mode)
314-
redirect_url = reverse(url_name, args=[scheduled_activity.id])
315-
return redirect(redirect_url)
310+
att_code_mode = request.POST.get("att_code_mode")
311+
if att_code_mode is not None and int(att_code_mode) != scheduled_activity.code_mode:
312+
scheduled_activity.set_code_mode(int(att_code_mode))
313+
redirect_url = reverse(url_name, args=[scheduled_activity.id])
314+
return redirect(redirect_url)
316315

317316
if not scheduled_activity.block.locked and request.user.is_eighth_admin:
318317
messages.success(request, "Note: Taking attendance on an unlocked block.")
@@ -363,6 +362,7 @@ def take_attendance_view(request, scheduled_activity_id):
363362

364363
present_signups = EighthSignup.objects.filter(scheduled_activity=scheduled_activity, user__in=present_user_ids)
365364
present_signups.update(was_absent=False)
365+
present_signups.update(attendance_marked=True)
366366

367367
for s in present_signups:
368368
invalidate_obj(s)
@@ -430,7 +430,7 @@ def take_attendance_view(request, scheduled_activity_id):
430430
"show_checkboxes": (scheduled_activity.block.locked or request.user.is_eighth_admin),
431431
"show_icons": (scheduled_activity.block.locked and scheduled_activity.block.attendance_locked() and not request.user.is_eighth_admin),
432432
"bbcu_script": settings.BBCU_SCRIPT,
433-
"qrurl": request.build_absolute_uri(reverse("qr_attendance", args=[scheduled_activity.id, scheduled_activity.attendance_code])),
433+
"qr_url": request.build_absolute_uri(reverse("qr_attendance", args=[scheduled_activity.id, scheduled_activity.attendance_code])),
434434
}
435435

436436
if request.user.is_eighth_admin:
@@ -540,7 +540,9 @@ def accept_all_passes_view(request, scheduled_activity_id):
540540
if not can_accept:
541541
return render(request, "error/403.html", {"reason": "You do not have permission to take accept these passes."}, status=403)
542542

543-
EighthSignup.objects.filter(after_deadline=True, scheduled_activity=scheduled_activity).update(pass_accepted=True, was_absent=False)
543+
EighthSignup.objects.filter(after_deadline=True, scheduled_activity=scheduled_activity).update(
544+
pass_accepted=True, was_absent=False, attendance_taken=True
545+
)
544546
invalidate_obj(scheduled_activity)
545547

546548
if "admin" in request.path:
@@ -774,71 +776,92 @@ def email_students_view(request, scheduled_activity_id):
774776

775777
@login_required
776778
@deny_restricted
777-
def student_attendance_view(request, attc=None, attf=None, attimef=None, atteachf=None):
779+
def student_attendance_view(request):
778780
blocks = EighthBlock.objects.get_blocks_today()
781+
mark_block = None
782+
mark_result = None
779783
if request.method == "POST":
780784
now = timezone.localtime()
781-
dayblks = Day.objects.select_related("day_type").get(date=now).day_type.blocks
782-
for blk in blocks:
783-
blklet = blk.block_letter
784-
code = request.POST.get(blklet)
785+
try:
786+
day_blocks = Day.objects.select_related("day_type").get(date=now).day_type.blocks
787+
except Day.DoesNotExist:
788+
messages.error(request, "Error. Attendance is only available on school days.")
789+
return redirect("index")
790+
for block in blocks:
791+
block_letter = block.block_letter
792+
code = request.POST.get(block_letter)
785793
if code is None:
786794
continue
787-
act = request.user.eighthscheduledactivity_set.get(block=blk)
795+
act = request.user.eighthscheduledactivity_set.get(block=block)
788796
if act.get_code_mode_display() == "Auto":
789797
try:
790-
dayblk = dayblks.get(name="8" + blklet)
798+
day_block = day_blocks.get(name="8" + block_letter)
791799
except Exception:
792-
attimef = blk
800+
mark_result = "invalid_time"
801+
mark_block = block
793802
break
794-
start_time = shift_time(tm(hour=dayblk.start.hour, minute=dayblk.start.minute), -20)
795-
end_time = shift_time(tm(hour=dayblk.end.hour, minute=dayblk.end.minute), 20)
803+
start_time = shift_time(tm(hour=day_block.start.hour, minute=day_block.start.minute), -20)
804+
end_time = shift_time(tm(hour=day_block.end.hour, minute=day_block.end.minute), 20)
796805
if not start_time <= now.time() <= end_time:
797-
attimef = blk
806+
mark_result = "invalid_time"
807+
mark_block = block
798808
break
799809
elif act.get_code_mode_display() == "Closed":
800-
atteachf = blk
810+
mark_result = "code_closed"
811+
mark_block = block
801812
break
802813
code = code.upper()
803814
if code == act.attendance_code:
804815
try:
805816
present = EighthSignup.objects.get(scheduled_activity=act, user__in=[request.user.id])
806817
present.was_absent = False
818+
present.attendance_marked = True
807819
invalidate_obj(present)
808820
act.attendance_taken = True
809821
act.save()
810822
invalidate_obj(act)
811-
attc = blk
823+
mark_result = "code_correct"
824+
mark_block = block
812825
except Exception:
813-
attf = blk
826+
mark_result = "code_fail"
827+
mark_block = block
814828
break
815829
else:
816-
attf = blk
830+
mark_result = "code_fail"
831+
mark_block = block
817832
break
818-
return student_frontend(request, attc, attf, attimef, atteachf)
833+
return student_frontend(request, mark_block, mark_result)
819834

820835

821836
@login_required
822837
@deny_restricted
823-
def student_frontend(request, attc=None, attf=None, attimef=None, atteachf=None):
838+
def student_frontend(request, mark_block: EighthBlock = None, mark_result: str = None):
824839
blocks = EighthBlock.objects.get_blocks_today()
825840
if blocks:
826841
sch_acts = []
827842
att_marked = []
828-
for b in blocks:
843+
for block in blocks:
829844
try:
830-
act = request.user.eighthscheduledactivity_set.get(block=b)
845+
act = request.user.eighthscheduledactivity_set.get(block=block)
831846
if act.activity.name != "z - Hybrid Sticky":
832-
sch_acts.append([b, act, ", ".join([r.name for r in act.get_true_rooms()]), ", ".join([s.name for s in act.get_true_sponsors()])])
847+
sch_acts.append(
848+
[block, act, ", ".join([r.name for r in act.get_true_rooms()]), ", ".join([s.name for s in act.get_true_sponsors()])]
849+
)
833850
signup = EighthSignup.objects.get(user=request.user, scheduled_activity=act)
834-
if not signup.was_absent:
835-
att_marked.append(b)
851+
if signup.attendance_marked:
852+
att_marked.append(block)
836853
except EighthScheduledActivity.DoesNotExist:
837-
sch_acts.append([b, None])
854+
sch_acts.append([block, None])
855+
results = {
856+
"code_correct": '<p style="color: green;">Attendance marked successfully.</p>',
857+
"code_closed": '<p style="color: red;">Error. Ask your teacher to open the attendance code.</p>',
858+
"code_fail": '<p style="color: red;">Invalid Code.</p>',
859+
"invalid_time": f'<p style="color: red;">Invalid time. Please fill this out during {block.block_letter} block.</p>',
860+
}
838861
response = render(
839862
request,
840863
"eighth/student_submit_attendance.html",
841-
context={"sch_acts": sch_acts, "att_marked": att_marked, "attc": attc, "attf": attf, "attimef": attimef, "atteachf": atteachf},
864+
context={"sch_acts": sch_acts, "att_marked": att_marked, "mark_block": mark_block, "attendance_result": results.get(mark_result)},
842865
)
843866
else:
844867
messages.error(request, "There are no eighth period blocks scheduled today.")
@@ -852,25 +875,22 @@ def qr_attendance_view(request, act_id, code):
852875
act = get_object_or_404(EighthScheduledActivity, id=act_id)
853876
error = False
854877
block = act.block
855-
attc = None
856-
attf = None
857-
attimef = None
858-
atteachf = None
878+
mark_result = None
859879
if act.get_code_mode_display() == "Auto":
860880
now = timezone.localtime()
861-
dayblks = Day.objects.select_related("day_type").get(date=now).day_type.blocks
881+
day_blocks = Day.objects.select_related("day_type").get(date=now).day_type.blocks
862882
try:
863-
dayblk = dayblks.get(name="8" + block.block_letter)
864-
start_time = shift_time(tm(hour=dayblk.start.hour, minute=dayblk.start.minute), -20)
865-
end_time = shift_time(tm(hour=dayblk.end.hour, minute=dayblk.end.minute), 20)
883+
day_block = day_blocks.get(name="8" + block.block_letter)
884+
start_time = shift_time(tm(hour=day_block.start.hour, minute=day_block.start.minute), -20)
885+
end_time = shift_time(tm(hour=day_block.end.hour, minute=day_block.end.minute), 20)
866886
if not start_time <= now.time() <= end_time:
867-
attimef = block
887+
mark_result = "invalid_time"
868888
error = True
869889
except Exception:
870-
attimef = block
890+
mark_result = "invalid_time"
871891
error = True
872892
elif act.get_code_mode_display() == "Closed":
873-
atteachf = block
893+
mark_result = "code_closed"
874894
error = True
875895
if not error:
876896
code = code.upper()
@@ -882,14 +902,14 @@ def qr_attendance_view(request, act_id, code):
882902
act.attendance_taken = True
883903
act.save()
884904
invalidate_obj(act)
885-
attc = block
905+
mark_result = "code_correct"
886906
messages.success(request, "Attendance marked.")
887907
except Exception:
888-
attf = block
908+
mark_result = "code_fail"
889909
messages.error(request, "Failed to mark attendance.")
890910
else:
891-
attf = block
911+
mark_result = "code_fail"
892912
messages.error(request, "Failed to mark attendance.")
893913
else:
894914
messages.error(request, "Failed to mark attendance.")
895-
return student_frontend(request, attc, attf, attimef, atteachf)
915+
return student_frontend(request, block, mark_result)

intranet/apps/eighth/views/signup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def eighth_signup_view(request, block_id=None):
254254
"activities_list": safe_json(block_info["activities"]),
255255
"active_block": block,
256256
"active_block_current_signup": active_block_current_signup,
257-
"attopen": attendance_open,
257+
"attendance_open": attendance_open,
258258
}
259259

260260
#######
@@ -508,7 +508,9 @@ def eighth_location(request):
508508
attendance_open = True
509509
except Exception:
510510
attendance_open = False
511-
response = render(request, "eighth/location.html", context={"sch_acts": sch_acts, "real_user": request.user, "attopen": attendance_open})
511+
response = render(
512+
request, "eighth/location.html", context={"sch_acts": sch_acts, "real_user": request.user, "attendance_open": attendance_open}
513+
)
512514
else:
513515
messages.error(request, "There are no eighth period blocks scheduled today.")
514516
response = redirect("index")

intranet/templates/eighth/location.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ <h5><strong>Sponsor(s):</strong> {{ s.3 }}</h5>
4343
</div>
4444
<br><br>
4545
{% endfor %}
46-
{% if real_user.is_student and attopen%}
46+
{% if real_user.is_student and attendance_open%}
4747
<a class="button" href="{% url 'student_attendance' %}">Enter Attendance Code for Today's Blocks</a>
4848
{% endif %}
4949
<br>

intranet/templates/eighth/signup.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ <h3 class="activity-detail-header">
352352
<%}%>
353353
<br>
354354
{% endverbatim %}
355-
{% if real_user.is_student and attopen and not real_user.is_eighth_admin%}
355+
{% if real_user.is_student and attendance_open and not real_user.is_eighth_admin%}
356356
<a class="button" href="{% url 'student_attendance' %}">Enter Attendance Code for Today's Blocks</a>
357357
{% endif %}
358358
<div class="error-feedback">

intranet/templates/eighth/student_submit_attendance.html

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ <h5><strong>Sponsor(s):</strong> {{ s.3 }}</h5>
3939
<p>{{ s.1.activity.description }}</p>
4040
{% if s.0 in att_marked%}
4141
<p style="color: green;">Attendance marked.</p>
42-
{% endif %}
42+
{% else %}
4343
<form method="post" action="">
4444
{% csrf_token %}
4545
<p><strong>Attendance Code: </strong></p>
@@ -52,14 +52,9 @@ <h5><strong>Sponsor(s):</strong> {{ s.3 }}</h5>
5252
/>
5353
<button type="submit">Submit</button>
5454
</form>
55-
{% if s.0 == attc %}
56-
<p style="color: green;">Attendance marked successfully.</p>
57-
{% elif s.0 == attimef %}
58-
<p style="color: red;">Invalid time. Please fill this out during {{ s.0.block_letter }} block.</p>
59-
{% elif s.0 == attf %}
60-
<p style="color: red;">Invalid Code.</p>
61-
{% elif s.0 == atteachf %}
62-
<p style="color: red;">Error. Ask your teacher to open the attendance code.</p>
55+
{% endif %}
56+
{% if s.0 == mark_block %}
57+
{{ attendance_result|safe }}
6358
{% endif %}
6459
{% else %}
6560
<p>You are not signed up for any activity.</p>

intranet/templates/eighth/take_attendance.html

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,12 @@ <h3>Passes</h3>
466466
&nbsp;&nbsp;<button type="submit">Save</button>
467467

468468
<div class="activity-card">
469-
<button type="button" onclick="showQRModal('{{ qrurl }}')">
469+
<button type="button" id="show-qr-button">
470470
Show QR Code
471471
</button>
472472
</div>
473473

474-
<div id="qrModal" style="display:none; position:fixed; top:0; left:0; width:100vw; height:100vh;
474+
<div id="qr-modal" style="display:none; position:fixed; top:0; left:0; width:100vw; height:100vh;
475475
background:rgba(0,0,0,0.6); align-items:center; justify-content:center; z-index:1000;">
476476

477477
<div style="background:#fff; padding:20px; border-radius:8px; text-align:center; position:relative;">
@@ -481,20 +481,33 @@ <h3>Passes</h3>
481481
{{ scheduled_activity.attendance_code }}
482482
</div>
483483

484-
<div id="qrImageContainer" style="max-width:55000px; max-height:550px;"></div>
484+
<div id="qr-image-container" style="max-width:55000px; max-height:550px;"></div>
485485
</div>
486486

487487
<br>
488-
<button type="button" onclick="closeQRModal()">Close</button>
488+
<button type="button" id="close-qr-button">Close</button>
489489
</div>
490490
</div>
491491

492492
<script>
493+
document.addEventListener("DOMContentLoaded", function () {
494+
const show_qr = document.getElementById("show-qr-button");
495+
const close_qr = document.getElementById("close-qr-button");
496+
show_qr.addEventListener("click", function (event) {
497+
event.preventDefault();
498+
showQRModal('{{ qr_url }}');
499+
});
500+
close_qr.addEventListener("click", function (event) {
501+
event.preventDefault();
502+
closeQRModal();
503+
});
504+
});
505+
493506
function showQRModal(qrUrl) {
494507
event.preventDefault();
495508

496-
const modal = document.getElementById('qrModal');
497-
const qrContainer = document.getElementById('qrImageContainer');
509+
const modal = document.getElementById('qr-modal');
510+
const qrContainer = document.getElementById('qr-image-container');
498511

499512
qrContainer.innerHTML = '';
500513

@@ -509,7 +522,7 @@ <h3>Passes</h3>
509522
}
510523

511524
function closeQRModal() {
512-
document.getElementById('qrModal').style.display = 'none';
525+
document.getElementById('qr-modal').style.display = 'none';
513526
}
514527
</script>
515528
</td>

0 commit comments

Comments
 (0)