def cohort_add(request): if not can_add_cohort(request): return HttpResponse('Unauthorized', status=401) if request.method == 'POST': form = CohortForm(request.POST) 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: form = CohortForm() # An unbound form return render(request, 'oppia/cohort-form.html', { 'form': form, })
def cohort_add(request): if not can_add_cohort(request): return HttpResponse('Unauthorized', status=401) if request.method == 'POST': form = CohortForm(request.POST) 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: form = CohortForm() # An unbound form return render(request, 'oppia/cohort-form.html',{'form': form,})
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' })
def cohort_add(request, course_id): course = check_owner(request, course_id) if request.method == "POST": form = CohortForm(request.POST) if form.is_valid(): # All validation rules pass cohort = Cohort() cohort.course = course 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 return HttpResponseRedirect("../") # Redirect after POST else: form = CohortForm() # An unbound form return render(request, "oppia/cohort-form.html", {"course": course, "form": form})