From 53bb419566c500ae37997fed528c162a5792f694 Mon Sep 17 00:00:00 2001 From: Adil Mohak Date: Sun, 6 Oct 2024 10:52:30 +0300 Subject: [PATCH] Fix: course registration --- course/views.py | 100 ++++++++++++++++++++++++++++------------- templates/sidebar.html | 2 +- 2 files changed, 71 insertions(+), 31 deletions(-) diff --git a/course/views.py b/course/views.py index a4b8080..4169e8e 100644 --- a/course/views.py +++ b/course/views.py @@ -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 diff --git a/templates/sidebar.html b/templates/sidebar.html index fe2558d..1976991 100644 --- a/templates/sidebar.html +++ b/templates/sidebar.html @@ -104,7 +104,7 @@ {% trans 'Assesment Results' %}
  • - {% trans 'Add' %} &{% trans 'Drop Course' %} + {% trans 'Add' %} & {% trans 'Drop Course' %}
  • {% endif %}