def save(request, inst_slug, course_slug=None): inst = get_object_or_404(Institution, slug__exact=inst_slug) course = None form = None ctx = dict() if request.method == 'GET': if course_slug: course = inst.course_set.get(slug__exact=course_slug) ctx['picture'] = course.picture form = CourseForm(instance=course) else: form = CourseForm() else: if course_slug: course = inst.course_set.get(slug__exact=course_slug) form = CourseForm(request.POST, instance=course) else: form = CourseForm(request.POST) if form.is_valid(): course = form.save(commit=False) if request.POST['picture_id']: picture = Picture.objects.get(pk=int(request.POST['picture_id'])) course.picture = picture course.institution = inst course.save() if not RelCourseOwner.objects.filter(course=course, owner=request.user.get_profile()).exists(): RelCourseOwner(course=course, owner=request.user.get_profile()).save() if not RelUserCourse.objects.filter(course=course, role=UserCourseRole.objects.teacher_role(), user=request.user.get_profile()).exists(): RelUserCourse(course=course, role=UserCourseRole.objects.teacher_role(), user=request.user.get_profile()).save() rebuild_caches(course.id) if not RelUserInstitution.objects.filter(institution=course.institution, role=UserInstitutionRole.objects.teacher_role(), user=request.user.get_profile()).exists(): RelUserInstitution(institution=course.institution, role=UserInstitutionRole.objects.teacher_role(), user=request.user.get_profile()).save() rebuild_institution_caches(course.institution.id) return HttpResponseRedirect(reverse('portal.courses.views.detail', args=[inst.slug, course.slug,])) ctx['form'] = form return direct_to_template(request, 'courses/form.html', ctx)
def save(request, inst_slug, course_slug, disc_slug=None): institution = get_object_or_404(Institution, slug__exact=inst_slug) course = institution.course_set.get(slug__exact=course_slug) disc = None form = None ctx = dict() if request.method == "GET": if disc_slug: disc = course.discipline_set.get(slug__exact=disc_slug) ctx["picture"] = disc.picture form = DisciplineForm(instance=disc) else: data = {"registration_type": MODERATE_REGISTRATION, "access_type": PUBLIC_ACCESS} form = DisciplineForm(initial=data) else: if disc_slug: disc = course.discipline_set.get(slug__exact=disc_slug) form = DisciplineForm(request.POST, instance=disc) else: form = DisciplineForm(request.POST) if form.is_valid(): disc = form.save(commit=False) if request.POST["picture_id"]: picture = Picture.objects.get(pk=int(request.POST["picture_id"])) disc.picture = picture disc.course = course disc.save() mdata = DisciplineMetadata() mdata.discipline = disc mdata.cod_turma = "123" mdata.periodo = "123" mdata.senha = "123" mdata.save() if not RelDisciplineOwner.objects.filter(discipline=disc, owner=request.user.get_profile()).exists(): RelDisciplineOwner(discipline=disc, owner=request.user.get_profile()).save() if not RelUserDiscipline.objects.filter( discipline=disc, role=UserDisciplineRole.objects.teacher_role(), user=request.user.get_profile() ).exists(): RelUserDiscipline( discipline=disc, role=UserDisciplineRole.objects.teacher_role(), user=request.user.get_profile() ).save() if not RelUserCourse.objects.filter( course=disc.course, role=UserCourseRole.objects.teacher_role(), user=request.user.get_profile() ).exists(): RelUserCourse( course=disc.course, role=UserCourseRole.objects.teacher_role(), user=request.user.get_profile() ).save() rebuild_course_caches(course.id) if not RelUserInstitution.objects.filter( institution=disc.course.institution, role=UserInstitutionRole.objects.teacher_role(), user=request.user.get_profile(), ).exists(): RelUserInstitution( institution=disc.course.institution, role=UserInstitutionRole.objects.teacher_role(), user=request.user.get_profile(), ).save() rebuild_institution_caches(course.institution.id) return HttpResponseRedirect( reverse("portal.disciplines.views.detail", args=[inst_slug, course_slug, disc.slug]) ) ctx["form"] = form return direct_to_template(request, "disciplines/form.html", ctx)