Esempio n. 1
0
def cohort_edit(request, cohort_id):
    if not can_edit_cohort(request, cohort_id):
        raise PermissionDenied
    cohort = Cohort.objects.get(pk=cohort_id)
    teachers_selected = []
    students_selected = []
    courses_selected = []

    if request.method == 'POST':
        form = CohortForm(request.POST)
        if form.is_valid():
            cohort.description = form.cleaned_data.get("description").strip()
            cohort.start_date = form.cleaned_data.get("start_date")
            cohort.end_date = form.cleaned_data.get("end_date")
            cohort.save()

            Participant.objects.filter(cohort=cohort).delete()

            students = form.cleaned_data.get("students")
            cohort_add_roles(cohort, Participant.STUDENT, students)

            teachers = form.cleaned_data.get("teachers")
            cohort_add_roles(cohort, Participant.TEACHER, teachers)

            CourseCohort.objects.filter(cohort=cohort).delete()
            courses = form.cleaned_data.get("courses")
            cohort_add_courses(cohort, courses)

            return HttpResponseRedirect('../../')

    else:
        form = CohortForm(
            initial={
                'description': cohort.description,
                'start_date': cohort.start_date,
                'end_date': cohort.end_date
            })

    teachers_selected = User.objects.filter(
        participant__role=Participant.TEACHER, participant__cohort=cohort)
    students_selected = User.objects.filter(
        participant__role=Participant.STUDENT, participant__cohort=cohort)
    courses_selected = Course.objects.filter(coursecohort__cohort=cohort)

    ordering, users = get_paginated_users(request)
    c_ordering, courses = get_paginated_courses(request)

    return render(
        request, 'cohort/form.html', {
            'form': form,
            'page': users,
            'selected_teachers': teachers_selected,
            'selected_students': students_selected,
            'selected_courses': courses_selected,
            'courses_page': courses,
            'courses_ordering': c_ordering,
            'page_ordering': ordering,
            'users_list_template': 'select'
        })
Esempio n. 2
0
def cohort_add(request):
    if not can_add_cohort(request):
        raise PermissionDenied

    if request.method == 'POST':
        form = CohortForm(request.POST.copy())
        if form.is_valid():  # All validation rules pass
            cohort = Cohort()
            cohort.start_date = timezone.make_aware(
                datetime.datetime.strptime(form.cleaned_data.get("start_date"),
                                           constants.STR_DATE_FORMAT),
                timezone.get_current_timezone())
            cohort.end_date = timezone.make_aware(
                datetime.datetime.strptime(form.cleaned_data.get("end_date"),
                                           constants.STR_DATE_FORMAT),
                timezone.get_current_timezone())
            cohort.description = form.cleaned_data.get("description").strip()
            cohort.save()

            students = form.cleaned_data.get("students")
            cohort_add_roles(cohort, Participant.STUDENT, students)

            teachers = form.cleaned_data.get("teachers")
            cohort_add_roles(cohort, Participant.TEACHER, teachers)

            courses = form.cleaned_data.get("courses")
            cohort_add_courses(cohort, courses)

            return HttpResponseRedirect('../')  # Redirect after POST
        else:
            # If form is not valid, clean the groups data
            form.data['teachers'] = None
            form.data['courses'] = None
            form.data['students'] = None

    else:
        form = CohortForm()

    ordering, users = get_paginated_users(request)
    c_ordering, courses = get_paginated_courses(request)

    return render(
        request, 'cohort/form.html', {
            'form': form,
            'page': users,
            'courses_page': courses,
            'courses_ordering': c_ordering,
            'page_ordering': ordering,
            'users_list_template': 'select'
        })
Esempio n. 3
0
def cohort_edit(request, cohort_id):
    if not can_edit_cohort(request, cohort_id):
        raise exceptions.PermissionDenied
    cohort = Cohort.objects.get(pk=cohort_id)
    teachers_selected = []
    students_selected = []
    courses_selected = []

    if request.method == 'POST':
        form = CohortForm(request.POST)
        if form.is_valid():
            cohort.description = form.cleaned_data.get("description").strip()
            cohort.start_date = form.cleaned_data.get("start_date")
            cohort.end_date = form.cleaned_data.get("end_date")
            cohort.save()

            Participant.objects.filter(cohort=cohort).delete()

            students = form.cleaned_data.get("students").split(",")
            if len(students) > 0:
                for s in students:
                    try:
                        participant = Participant()
                        participant.cohort = cohort
                        participant.user = User.objects.get(username=s.strip())
                        participant.role = Participant.STUDENT
                        participant.save()
                    except User.DoesNotExist:
                        pass
            teachers = form.cleaned_data.get("teachers").split(",")
            if len(teachers) > 0:
                for t in teachers:
                    try:
                        participant = Participant()
                        participant.cohort = cohort
                        participant.user = User.objects.get(username=t.strip())
                        participant.role = Participant.TEACHER
                        participant.save()
                    except User.DoesNotExist:
                        pass

            CourseCohort.objects.filter(cohort=cohort).delete()
            courses = form.cleaned_data.get("courses").strip().split(",")
            print(courses)
            if len(courses) > 0:
                for c in courses:
                    try:
                        course = Course.objects.get(shortname=c.strip())
                        CourseCohort(cohort=cohort, course=course).save()
                    except Course.DoesNotExist:
                        pass

            return HttpResponseRedirect('../../')
        else:
            print(form.errors)
            print('Form invalidad!!')

    else:
        form = CohortForm(
            initial={
                'description': cohort.description,
                'start_date': cohort.start_date,
                'end_date': cohort.end_date
            })

    teachers_selected = User.objects.filter(
        participant__role=Participant.TEACHER, participant__cohort=cohort)
    students_selected = User.objects.filter(
        participant__role=Participant.STUDENT, participant__cohort=cohort)
    courses_selected = Course.objects.filter(coursecohort__cohort=cohort)

    ordering, users = get_paginated_users(request)
    c_ordering, courses = get_paginated_courses(request)

    return render(
        request, 'cohort/form.html', {
            'form': form,
            'page': users,
            'selected_teachers': teachers_selected,
            'selected_students': students_selected,
            'selected_courses': courses_selected,
            'courses_page': courses,
            'courses_ordering': c_ordering,
            'page_ordering': ordering,
            'users_list_template': 'select'
        })
Esempio n. 4
0
def cohort_add(request):
    if not can_add_cohort(request):
        raise exceptions.PermissionDenied

    if request.method == 'POST':
        form = CohortForm(request.POST.copy())
        if form.is_valid():  # All validation rules pass
            cohort = Cohort()
            cohort.start_date = form.cleaned_data.get("start_date")
            cohort.end_date = form.cleaned_data.get("end_date")
            cohort.description = form.cleaned_data.get("description").strip()
            cohort.save()

            students = form.cleaned_data.get("students").strip().split(",")
            if len(students) > 0:
                for s in students:
                    try:
                        student = User.objects.get(username=s.strip())
                        participant = Participant()
                        participant.cohort = cohort
                        participant.user = student
                        participant.role = Participant.STUDENT
                        participant.save()
                    except User.DoesNotExist:
                        pass

            teachers = form.cleaned_data.get("teachers").strip().split(",")
            if len(teachers) > 0:
                for t in teachers:
                    try:
                        teacher = User.objects.get(username=t.strip())
                        participant = Participant()
                        participant.cohort = cohort
                        participant.user = teacher
                        participant.role = Participant.TEACHER
                        participant.save()
                    except User.DoesNotExist:
                        pass

            courses = form.cleaned_data.get("courses").strip().split(",")
            if len(courses) > 0:
                for c in courses:
                    try:
                        course = Course.objects.get(shortname=c.strip())
                        CourseCohort(cohort=cohort, course=course).save()
                    except Course.DoesNotExist:
                        pass

            return HttpResponseRedirect('../')  # Redirect after POST
        else:
            # If form is not valid, clean the groups data
            form.data['teachers'] = None
            form.data['courses'] = None
            form.data['students'] = None

    else:
        form = CohortForm()

    ordering, users = get_paginated_users(request)
    c_ordering, courses = get_paginated_courses(request)

    return render(
        request, 'cohort/form.html', {
            'form': form,
            'page': users,
            'courses_page': courses,
            'courses_ordering': c_ordering,
            'page_ordering': ordering,
            'users_list_template': 'select'
        })
Esempio n. 5
0
def cohort_edit(request, cohort_id):
    if not can_edit_cohort(request, cohort_id):
        raise exceptions.PermissionDenied
    cohort = Cohort.objects.get(pk=cohort_id)
    teachers_selected = []
    students_selected = []
    courses_selected = []

    if request.method == 'POST':
        form = CohortForm(request.POST)
        if form.is_valid():
            cohort.description = form.cleaned_data.get("description").strip()
            cohort.start_date = form.cleaned_data.get("start_date")
            cohort.end_date = form.cleaned_data.get("end_date")
            cohort.save()

            Participant.objects.filter(cohort=cohort).delete()

            students = form.cleaned_data.get("students").split(",")
            if len(students) > 0:
                for s in students:
                    try:
                        participant = Participant()
                        participant.cohort = cohort
                        participant.user = User.objects.get(username=s.strip())
                        participant.role = Participant.STUDENT
                        participant.save()
                    except User.DoesNotExist:
                        pass
            teachers = form.cleaned_data.get("teachers").split(",")
            if len(teachers) > 0:
                for t in teachers:
                    try:
                        participant = Participant()
                        participant.cohort = cohort
                        participant.user = User.objects.get(username=t.strip())
                        participant.role = Participant.TEACHER
                        participant.save()
                    except User.DoesNotExist:
                        pass

            CourseCohort.objects.filter(cohort=cohort).delete()
            courses = form.cleaned_data.get("courses").strip().split(",")
            if len(courses) > 0:
                for c in courses:
                    try:
                        course = Course.objects.get(shortname=c.strip())
                        CourseCohort(cohort=cohort, course=course).save()
                    except Course.DoesNotExist:
                        pass

            return HttpResponseRedirect('../../')

    else:
        form = CohortForm(initial={'description': cohort.description,
                                   'start_date': cohort.start_date,
                                   'end_date': cohort.end_date,
                                   })

    teachers_selected = User.objects.filter(participant__role=Participant.TEACHER, participant__cohort=cohort)
    students_selected = User.objects.filter(participant__role=Participant.STUDENT, participant__cohort=cohort)
    courses_selected = Course.objects.filter(coursecohort__cohort=cohort)

    ordering, users = get_paginated_users(request)
    c_ordering, courses = get_paginated_courses(request)

    return render(request, 'oppia/cohort-form.html', {
                            'form': form,
                            'page': users,
                            'selected_teachers': teachers_selected,
                            'selected_students': students_selected,
                            'selected_courses': courses_selected,
                            'courses_page': courses,
                            'courses_ordering': c_ordering,
                            'page_ordering': ordering,
                            'users_list_template': 'select'})
Esempio n. 6
0
def cohort_add(request):
    if not can_add_cohort(request):
        raise exceptions.PermissionDenied

    if request.method == 'POST':
        form = CohortForm(request.POST.copy())
        if form.is_valid():  # All validation rules pass
            cohort = Cohort()
            cohort.start_date = form.cleaned_data.get("start_date")
            cohort.end_date = form.cleaned_data.get("end_date")
            cohort.description = form.cleaned_data.get("description").strip()
            cohort.save()

            students = form.cleaned_data.get("students").strip().split(",")
            if len(students) > 0:
                for s in students:
                    try:
                        student = User.objects.get(username=s.strip())
                        participant = Participant()
                        participant.cohort = cohort
                        participant.user = student
                        participant.role = Participant.STUDENT
                        participant.save()
                    except User.DoesNotExist:
                        pass

            teachers = form.cleaned_data.get("teachers").strip().split(",")
            if len(teachers) > 0:
                for t in teachers:
                    try:
                        teacher = User.objects.get(username=t.strip())
                        participant = Participant()
                        participant.cohort = cohort
                        participant.user = teacher
                        participant.role = Participant.TEACHER
                        participant.save()
                    except User.DoesNotExist:
                        pass

            courses = form.cleaned_data.get("courses").strip().split(",")
            if len(courses) > 0:
                for c in courses:
                    try:
                        course = Course.objects.get(shortname=c.strip())
                        CourseCohort(cohort=cohort, course=course).save()
                    except Course.DoesNotExist:
                        pass

            return HttpResponseRedirect('../')  # Redirect after POST
        else:
            #If form is not valid, clean the groups data
            form.data['teachers'] = None
            form.data['courses'] = None
            form.data['students'] = None

    else:
        form = CohortForm()  # An unbound form

    ordering, users = get_paginated_users(request)
    c_ordering, courses = get_paginated_courses(request)

    return render(request, 'oppia/cohort-form.html', {
                                'form': form,
                                'page': users,
                                'courses_page': courses,
                                'courses_ordering': c_ordering,
                                'page_ordering': ordering,
                                'users_list_template': 'select',
                             })