def school_view(request): from glml.utils import get_school, get_working_year school = get_school(request) working_year = get_working_year(request) if not school: add_message(request, u"Please select a school first.") return HttpResponseRedirect(reverse(home)) try: school_id = school.schoolid_set.get(district__year__id=working_year.id) except SchoolID.DoesNotExist: school_id = None if allow(request): if school_id: student_ids = school_id.studentid_set.all() else: student_ids = None try: import_year = Year.objects.get(start=working_year.start - 1) import_school_id = school.schoolid_set.get(district__year__id=import_year.id) except (Year.DoesNotExist, SchoolID.DoesNotExist): can_import = False else: can_import = True if student_ids: test_dates = TestDate.objects.filter(year=working_year) students = [] for student_id in student_ids: tests = [] for test_date in test_dates: if test_date.test_set.filter(student_id=student_id): tests.append(test_date.test_set.get(student_id=student_id)) else: tests.append(None) students.append({"student_id": student_id, "tests": tests}) else: test_dates = None students = None return render_to_response( "web/school.html", {"school_id": school_id, "test_dates": test_dates, "students": students, "can_import": can_import}, RequestContext(request), ) return HttpResponseRedirect(reverse(permission_denied))
def import_students(request): if not allow(request): return HttpResponseRedirect(reverse(permission_denied)) from glml.utils import get_school, get_working_year school = get_school(request) working_year = get_working_year(request) if not school: add_message(request, u"Please select a school first.") return HttpResponseRedirect(reverse(home)) if working_year != Year.get_current_year(): add_message(request, u"You may not import students into a past season.") return HttpResponseRedirect(reverse(school_view)) try: import_year = Year.objects.get(start=working_year.start - 1) import_school_id = school.schoolid_set.get(district__year__id=import_year.id) except (Year.DoesNotExist, SchoolID.DoesNotExist): add_message(request, u"There is no previous year to import students from.") return HttpResponseRedirect(reverse(school_view)) try: school_id = school.schoolid_set.get(district__year__id=working_year.id) except SchoolID.DoesNotExist: return HttpResponseRedirect(reverse(school_view)) students = [ {"name": student_id.student.name(), "grade": student_id.grade + 1, "id": student_id.student.id} for student_id in import_school_id.studentid_set.exclude(grade=12) ] if not students: add_message(request, u"There are no students from the previous year to import.") return HttpResponseRedirect(reverse(school_view)) if request.method == "POST": for student_id_number in request.POST: if request.POST[student_id_number] == "on": student = get_object_or_404(Student, id=student_id_number) grade = int(request.POST["%s_grade" % student_id_number]) if not StudentID.objects.filter(student=student, school_id=school_id): StudentID.objects.create(student=student, grade=grade, school_id=school_id) else: student_id = StudentID.objects.get(student=student, school_id=school_id) student_id.grade = grade student_id.save() return HttpResponseRedirect(reverse(school_view)) return render_to_response("web/import.html", {"students": students}, RequestContext(request))