Example #1
0
def _course_attendance_list_detail(request, course, course_lesson, attendance_list):
    course_members = CourseMember.objects.filter(lessonattendee__attendance_list=attendance_list)
    checked_course_members = {
        "course_members": [a.course_member.pk for a in attendance_list.lessonattendee_set.all() if a.present]
    }
    if request.method == "POST":
        form = AttendanceListForm(request.POST, instance=attendance_list)
        attendee_form = LessonAttendeeCreateForm(course_members, request.POST, initial=checked_course_members)
        if form.is_valid() and attendee_form.is_valid():
            form.instance.lector_price = lector_price(
                form.cleaned_data["lector"], course, form.cleaned_data["start"], form.cleaned_data["end"]
            )
            attendance_list = form.save()

            for lesson_attendee in attendance_list.lessonattendee_set.all():
                lesson_attendee.present = lesson_attendee.course_member in attendee_form.cleaned_data["course_members"]
                lesson_attendee.course_member_price = course_member_price(
                    lesson_attendee.course_member, attendance_list.start, attendance_list.end, course_members
                )
                lesson_attendee.save()

    else:
        form = AttendanceListForm(instance=attendance_list)
        attendee_form = LessonAttendeeCreateForm(course_members, initial=checked_course_members)
    return render_to_response(
        "courses/course_attendance_list_form.html",
        {"lesson_list": course.lesson_set.all(), "course": course, "form": form, "attendee_form": attendee_form},
        context_instance=RequestContext(request),
    )
Example #2
0
def _course_attendance_list_create(request, course, course_lesson, attendance_list):
    if request.method == "POST":
        form = AttendanceListForm(request.POST, instance=attendance_list)
        start, end = (
            (form.cleaned_data["start"], form.cleaned_data["end"])
            if form.is_valid()
            else (attendance_list.start, attendance_list.end)
        )
        course_members = course_members_on_lesson(course, start, end)
        attendee_form = LessonAttendeeCreateForm(course_members, request.POST)

        if form.is_valid() and attendee_form.is_valid():
            form.instance.lector_price = lector_price(
                form.cleaned_data["lector"], course, form.cleaned_data["start"], form.cleaned_data["end"]
            )
            attendance_list = form.save()

            for course_member in course_members:
                attendee = {
                    "present": course_member in attendee_form.cleaned_data["course_members"],
                    "course_member": course_member,
                    "course_member_price": course_member_price(course_member, start, end, course_members),
                }
                attendance_list.lessonattendee_set.create(**attendee)

            return HttpResponseRedirect(attendance_list.get_absolute_url())
    else:
        form = AttendanceListForm(instance=attendance_list)
        attendee_form = LessonAttendeeCreateForm(
            course_members_on_lesson(course, attendance_list.start, attendance_list.end)
        )
    return render_to_response(
        "courses/course_attendance_list_create.html",
        {"form": form, "attendee_form": attendee_form, "lesson_list": course.lesson_set.all(), "course": course},
        context_instance=RequestContext(request),
    )