Ejemplo n.º 1
0
def group_create(request, assignment_id):
    assignment = get_object_or_404(Assignment, id=assignment_id)

    # students = assignment.discipline.group.profiles.filter(student=True)
    students = []
    for profile in assignment.discipline.group.profiles.all():
        if profile.is_student():
            students.append(profile)

    if request.method == 'POST':
        form = GroupForm(students, request.POST)
        if form.is_valid():
            g = Group()
            g.name = form.cleaned_data['name']
            g.save()

            for selected_student in form.cleaned_data['students']:
                g.profiles.add(Profile.objects.get(pk=selected_student))
            g.profiles.add(assignment.discipline.teacher.profile)

            assignment.group.add(g)
            return HttpResponseRedirect(reverse("educational:assignment_detail", kwargs={'pk': assignment.pk}))

    else:
        form = GroupForm(students)

    return render(request, 'educational/teacher/group_create.html', {'form': form})
Ejemplo n.º 2
0
def group_update(request, group_id):
    group = get_object_or_404(Group, id=group_id)
    assignment = Assignment.objects.filter(group=group)[0]

    # students = assignment.discipline.group.profiles.filter(student=True)
    students = []
    for profile in assignment.discipline.group.profiles.all():
        if profile.is_student():
            students.append(profile)

    if request.method == 'POST':
        form = GroupForm(students, request.POST)
        if form.is_valid():
            group.name = form.cleaned_data['name']
            group.save()

            for profile in group.profiles.all():
                group.profiles.remove(profile)

            for selected_student in form.cleaned_data['students']:
                group.profiles.add(Profile.objects.get(pk=selected_student))
            group.profiles.add(assignment.discipline.teacher.profile)

            return HttpResponseRedirect(reverse("educational:group_detail", kwargs={'pk': group.pk}))
    else:
        old_students = []
        for profile in group.profiles.all():
            if profile.is_student():
                old_students.append(profile)
        form = GroupForm(students, initial={'name': group.name, 'students': [s.pk for s in old_students]})

    return render(request, 'educational/teacher/group_update.html', {'form': form})