Skip to content

Commit

Permalink
Fix: course registration
Browse files Browse the repository at this point in the history
  • Loading branch information
adilmohak committed Oct 6, 2024
1 parent f51e0bd commit 53bb419
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 31 deletions.
100 changes: 70 additions & 30 deletions course/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,40 +390,80 @@ def handle_video_delete(request, slug, video_slug):
@login_required
@student_required
def course_registration(request):
student = get_object_or_404(Student, student__pk=request.user.id)
current_semester = Semester.objects.filter(is_current_semester=True).first()
if not current_semester:
messages.error(request, "No active semester found.")
return render(request, "course/course_registration.html")

if request.method == "POST":
course_ids = request.POST.getlist("course_ids")
for course_id in course_ids:
course = get_object_or_404(Course, pk=course_id)
TakenCourse.objects.get_or_create(student=student, course=course)
student = Student.objects.get(student__pk=request.user.id)
ids = ()
data = request.POST.copy()
data.pop("csrfmiddlewaretoken", None) # remove csrf_token
for key in data.keys():
ids = ids + (str(key),)
for s in range(0, len(ids)):
course = Course.objects.get(pk=ids[s])
obj = TakenCourse.objects.create(student=student, course=course)
obj.save()
messages.success(request, "Courses registered successfully!")
return redirect("course_registration")
else:
current_semester = Semester.objects.filter(is_current_semester=True).first()
if not current_semester:
messages.error(request, "No active semester found.")
return render(request, "course/course_registration.html")

# student = Student.objects.get(student__pk=request.user.id)
student = get_object_or_404(Student, student__id=request.user.id)
taken_courses = TakenCourse.objects.filter(student__student__id=request.user.id)
t = ()
for i in taken_courses:
t += (i.course.pk,)

courses = (
Course.objects.filter(
program__pk=student.program.id,
level=student.level,
semester=current_semester,
)
.exclude(id__in=t)
.order_by("year")
)
all_courses = Course.objects.filter(
level=student.level, program__pk=student.program.id
)

taken_course_ids = TakenCourse.objects.filter(student=student).values_list(
"course__id", flat=True
)
courses = Course.objects.filter(
program=student.program,
level=student.level,
semester=current_semester.semester,
).exclude(id__in=taken_course_ids)

registered_courses = Course.objects.filter(id__in=taken_course_ids)
all_courses = Course.objects.filter(level=student.level, program=student.program)

context = {
"courses": courses,
"registered_courses": registered_courses,
"student": student,
"current_semester": current_semester,
"all_courses_registered": all_courses.count() == registered_courses.count(),
}
return render(request, "course/course_registration.html", context)
no_course_is_registered = False # Check if no course is registered
all_courses_are_registered = False

registered_courses = Course.objects.filter(level=student.level).filter(id__in=t)
if (
registered_courses.count() == 0
): # Check if number of registered courses is 0
no_course_is_registered = True

if registered_courses.count() == all_courses.count():
all_courses_are_registered = True

total_first_semester_credit = 0
total_sec_semester_credit = 0
total_registered_credit = 0
for i in courses:
if i.semester == "First":
total_first_semester_credit += int(i.credit)
if i.semester == "Second":
total_sec_semester_credit += int(i.credit)
for i in registered_courses:
total_registered_credit += int(i.credit)
context = {
"is_calender_on": True,
"all_courses_are_registered": all_courses_are_registered,
"no_course_is_registered": no_course_is_registered,
"current_semester": current_semester,
"courses": courses,
"total_first_semester_credit": total_first_semester_credit,
"total_sec_semester_credit": total_sec_semester_credit,
"registered_courses": registered_courses,
"total_registered_credit": total_registered_credit,
"student": student,
}
return render(request, "course/course_registration.html", context)


@login_required
Expand Down
2 changes: 1 addition & 1 deletion templates/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
<a href="{% url 'ass_results' %}"><i class="fa fa-list-ol"></i> {% trans 'Assesment Results' %}</a>
</li>
<li class="{% if request.path == cr %}active{% endif %}">
<a href="{% url 'course_registration' %}"><i class="fas fa-plus"></i>{% trans 'Add' %} &amp;{% trans 'Drop Course' %}</a>
<a href="{% url 'course_registration' %}"><i class="fas fa-plus"></i>{% trans 'Add' %} &amp; {% trans 'Drop Course' %}</a>
</li>
{% endif %}
<br />
Expand Down

0 comments on commit 53bb419

Please sign in to comment.