-
Notifications
You must be signed in to change notification settings - Fork 130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attendance of students by AJAX call #113
Conversation
@garg3133, could you help me to solve this linting error? |
apps/students/urls.py
Outdated
# path('attendance/view/', views.view_attendance, name='view_attendance'), | ||
path('profile/update/<int:pk>/', views.update_profile, name='update_profile'), # Not needed, update in profile/ only | ||
|
||
path('update_from_sheets/', views.update_from_sheets, name='update_from_sheets'), | ||
|
||
# AJAX calls | ||
path('ajax/ajax_attendance_fetch_students/', views.ajax_attendance_fetch_students, name='ajax_attendance_fetch_students'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't repeat ajax
in the endpoint. ajax/fetch_students/
and ajax/mark_attendance
should work fine. Replace /
with _
for view name and URL namespace.
apps/students/views.py
Outdated
def ajax_student_attendance(request): | ||
stu_id = request.GET['std_id'] | ||
stu_att = StudentAttendance.objects.get(student__id=stu_id, cal_date=today_date) | ||
stu_att.present = not stu_att.present |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't just toggle the attendance, be sure whether you want to mark the student present or absent. Bring in one more key-value pair for this in GET request. (Send True if the checkbox is checked and False if it is unchecked).
}, | ||
dataType: 'json', | ||
success: function (data) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a success toast here showing that the attendance was marked successfully. You may refer b4715eb to see how I implemented toast in your previous PR.
Also, add the error
parameter to in this AJAX and toggle the checkbox to it's original state if an error occurs along with showing a toast displaying the failure message.
81250da
to
30de2fb
Compare
@sagarpandyansit Leave the linting errors for now, just improve the code as suggested in the review above. We'll handle the linting errors separately as the whole codebase needs to be lint properly. |
@garg3133, Okay. There is a problem. as soon as we mark the attendance, the database is going to update but, what about the HTML page. It remains the same. for example |
@sagarpandyansit The |
@garg3133, no issue with the creation. I am saying that the value of it is not changing as per the database. If it was |
It is not synchronous with the AJAX call. I made AJAX call for marking the attendance of the student. It works and made |
Yeah, that's not a problem. These variables are only used at the reload time for rendering the HTML. Once the HTML is rendered completely, these variables are of no use on the page (unless you are using them in JavaScript). The value of there variables won't cause any problem on the page and will automatically get updated once the page reloads. |
No. It will cause a problem. What if you click on that button again. Again you have to check whether |
No, you should not check the value of Also, if the AJAX fails for some reason, toggle the checkbox back to its original state (but make your it doesn't get stuck in a loop). |
@garg3133, kindly review the AJAX call now. It's working fine without suspending the event. I will remove the submit button and make changes accordingly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is working great @sagarpandyansit, thanks for working on this.
Below are the few changes required. Plus, you can now remove the "Submit" button as you said above.
@@ -173,14 +172,22 @@ def ajax_attendance(request): | |||
return JsonResponse(data) | |||
|
|||
|
|||
def ajax_mark_attendance(request): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the two decorators above this function. (Refer to the other ajax_fetch_students
function above).
<input type="checkbox" name="attended" value="{{ stu_att.student.id }}" | ||
class="custom-control-input" | ||
id="s{{ stu_att.id }}" | ||
onchange="attendance({{ stu_att.student.id }}, {{ stu_att.id }})" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can pass the arguments as strings here. Also, pass the id of the checkbox instead of stu_att.id
.
Try this: onchange="attendance('{{ stu_att.student.id }}', 's{{ stu_att.id }}')"
stu_att_table += '<tr><td>' + name + '</td><td>' + school_class + '</td> \ | ||
<script> | ||
function attendance(student_id, checkboxID) { | ||
let student = document.getElementById('s' + checkboxID); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable name student
doesn't seem correct here. You should use a more descriptive name like att_checkbox
instead.
dataType: 'json', | ||
success: function (data, status) { | ||
$('#alert-toast-div > div').html( | ||
`<div class="toast" id="alert_toast" role="alert" aria-live="assertive" aria-atomic="true" data-delay="3500"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change the data-delay
to 500. As this toast is going to appear again and again, we don't need to show it for 3.5 sec.
$('#alert-toast-div > div').html( | ||
`<div class="toast" id="alert_toast" role="alert" aria-live="assertive" aria-atomic="true" data-delay="3500"> | ||
<div class="toast-body p-2 px-3"> | ||
<strong class="mr-auto">Attendance marked successfully.</strong> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change the text here to "Attendence updated".
$("#alert_toast").addClass("bg-success"); | ||
}, | ||
error: function (data, status) { | ||
is_present ? student.checked = false : student.checked = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment before this line: "// Un-toggle the checkbox".
@sagarpandyansit Any updates on this? The program is going to end in about 4 hrs and I don't want you to miss your points on this PR. |
@garg3133, I have viva tomorrow though I tried. I don't know why but, I can't schedule a class of students for today as well. They don't appear in the list for attendance. Please check, maybe it's a bug. |
As I had already made most of the requested changes on my local system while reviewing this PR, I'm merging this PR with those after applying those changes in 8a90fb5. I've opened a new issue #118 for the remaining changes and I'm assigning it to you. Also, I really appreciate your willingness to contribute to this project even after SWOC and all the best for your viva 👍 |
Thanks @garg3133. I will start working on that issue after the viva phase. |
Issue that resolved:
#82
Description of change:
I have changed the traditional way of submitting the form for attendance. I have made a function which triggers asynchronous request to change in the database for the attendance.