def attendance(request): today_cal = Calendar.objects.filter(date=date.today()) # TO BE REMOVED... # Update today's date in Calendar if not already there if today_cal.exists(): today_cal = today_cal[0] else: today_cal_new = Calendar(date=date.today()) today_cal_new.save() today_cal = Calendar.objects.get(date=date.today()) # ...TILL HERE context = { 'today_date' : date.today(), } if today_cal.class_scheduled: if not VolunteerAttendence.objects.filter(cal_date__date=date.today()).exists(): # Create Empty Volunteer Attendance Instances today_vol_sch = VolunteerSchedule.objects.filter(day=date.today().strftime("%w")) for vol_sch in today_vol_sch: vol_attendance = VolunteerAttendence(volun=vol_sch.volun, cal_date=today_cal) vol_attendance.save() else: context['no_class_today'] = True return render(request, 'volunteers/attendance.html', context) if request.method == 'POST': vol_array = request.POST.getlist('volunteered') extra_vol_array = request.POST.getlist('extra-vol') # Mark everyone's absent vol_att_today = VolunteerAttendence.objects.filter(cal_date=today_cal) for vol_att in vol_att_today: vol_att.present = False vol_att.save() for vol_id in vol_array: vol_att = VolunteerAttendence.objects.get(volun__id=vol_id, cal_date=today_cal) vol_att.present = True vol_att.save() for extra_vol_roll in extra_vol_array: volun = Volunteer.objects.filter(roll_no=extra_vol_roll) if volun.exists(): extra_vol_att = VolunteerAttendence.objects.filter(volun=volun[0], cal_date=today_cal) if extra_vol_att.exists(): extra_vol_att = extra_vol_att[0] extra_vol_att.present = True else: extra_vol_att = VolunteerAttendence(volun=volun[0], cal_date=today_cal, present=True, extra=True) extra_vol_att.save() messages.success(request, 'Attendance marked successfully!') return redirect('volunteers:attendance') context['today_vol_att'] = VolunteerAttendence.objects.filter(cal_date=today_cal).order_by('volun__roll_no') return render(request, 'volunteers/attendance.html', context)
def attendance(request): """Student attendance page.""" today_cal = Calendar.objects.filter(date=today_date) # TO BE REMOVED... # Update today's date in Calendar if not already there if today_cal.exists(): today_cal = today_cal[0] else: today_cal_new = Calendar(date=today_date) today_cal_new.save() today_cal = Calendar.objects.get(date=today_date) # ...TILL HERE context = { 'today_date': today_date, } if not today_cal.class_scheduled: context['no_class_today'] = True return render(request, 'students/attendance.html', context) today_stu_sch = StudentSchedule.objects.select_related('student').filter( day=today_day) today_stu_att = StudentAttendance.objects.filter(cal_date__date=today_date) if not today_stu_att.exists(): # Create Empty Student Attendance Instances stu_att = [] for stu_sch in today_stu_sch: stu_att.append( StudentAttendance(student=stu_sch.student, cal_date=today_cal)) StudentAttendance.objects.bulk_create(stu_att) elif today_stu_att.count() != today_stu_sch.count(): # Some new students added in today's schedule (not necessarily present today) stu_att = [] for stu_sch in today_stu_sch: if not today_stu_att.filter(student=stu_sch.student).exists(): stu_att.append( StudentAttendance(student=stu_sch.student, cal_date=today_cal)) StudentAttendance.objects.bulk_create(stu_att) context['stu_att_today'] = StudentAttendance.objects.filter( cal_date=today_cal, student__school_class__range=(1, 3)).order_by('student__school_class', 'student__first_name', 'student__last_name') return render(request, 'students/attendance.html', context)
def attendance(request): today_cal = Calendar.objects.filter(date=today_date) # TO BE REMOVED... # Update today's date in Calendar if not already there if today_cal.exists(): today_cal = today_cal[0] else: today_cal_new = Calendar(date=today_date) today_cal_new.save() today_cal = Calendar.objects.get(date=today_date) # ...TILL HERE context = { 'today_date': today_date, } if today_cal.class_scheduled: if not StudentAttendance.objects.filter( cal_date__date=today_date).exists(): # Create Empty Student Attendance Instances today_stu_sch = StudentSchedule.objects.filter(day=today_day) for stu_sch in today_stu_sch: stu_attendance = StudentAttendance(student=stu_sch.student, cal_date=today_cal) stu_attendance.save() elif StudentAttendance.objects.filter(cal_date__date=today_date).count( ) != StudentSchedule.objects.filter(day=today_day).count(): # Some new students added in today's schedule (not necessarily present today) today_stu_sch = StudentSchedule.objects.filter(day=today_day) for stu_sch in today_stu_sch: if not StudentAttendance.objects.filter( student=stu_sch.student, cal_date=today_cal).exists(): stu_attendance = StudentAttendance(student=stu_sch.student, cal_date=today_cal) stu_attendance.save() else: context['no_class_today'] = True return render(request, 'students/attendance.html', context) if request.method == 'POST': stu_array = request.POST.getlist('attended') selected_class = request.POST['selected_class'] # class_range = selected_class.split('-') # class_range_min = class_range[0] # class_range_max = class_range[1] class_range_min, class_range_max = selected_class.split('-') # Mark everyone's absent stu_att_today = StudentAttendance.objects.filter( cal_date=today_cal, student__school_class__range=(class_range_min, class_range_max)) for stu_att in stu_att_today: stu_att.present = False stu_att.save() for stu_id in stu_array: stu_att = StudentAttendance.objects.get(student__id=stu_id, cal_date=today_date) stu_att.present = True stu_att.save() messages.success(request, 'Attendance marked successfully!') return redirect('students:attendance') context['stu_att_today'] = StudentAttendance.objects.filter( cal_date=today_cal, student__school_class__range=(1, 3)).order_by('student__school_class', 'student__first_name', 'student__last_name') return render(request, 'students/attendance.html', context)