def create(request): if request.method == 'POST': form = CourseForm(request.POST) if form.is_valid(): created = form.save(commit=False) created.save() created.teacher.add(request.user.userinformation) Schedule.objects.create(_type=request.POST['schedule_type'], course=created) assign_perm('change_course', request.user, created) assign_perm('delete_course', request.user, created) return redirect('course', created.id) else: form = CourseForm( initial={ 'description': DEFAULT_COURSE_DESCRIPTION, 'max_participants': 30, 'archiving': 't' }) if 'subject' in request.GET: subj = int(request.GET['subject'][0]) if Subject.objects.filter(id=subj).exists(): form.initial['subject'] = subj return render(request, 'course/create.html', { 'title': _('New Course'), 'form': form })
def edit_course(request, id): course = Course.objects.get(course_id=id) init = { 'exam': (True if course.exam == 1 else False), 'exercises': course.exercises, 'laboratories': course.laboratories, 'project': course.project, 'seminars': course.seminars, 'if_exer': (True if course.exercises else False), 'if_lab': (True if course.laboratories else False), 'if_proj': (True if course.project else False), 'if_sem': (True if course.seminars else False) } if request.method == "POST": form = CourseForm(request.POST, instance=course, initial=init) if form.is_valid(): if request.POST.get('del_btn'): object = Course.objects.get(course_id=id) object.delete() return redirect(courses) else: course = form.save(commit=False) if request.POST.get('exam'): course.exam = 1 course.save() return redirect(courses) else: form = CourseForm(instance=course, initial=init) return render(request, 'create-course.html', {"form": form, "edit": 1})
def edit_course(request: HttpRequest, course_id: str): """ Edit form for changing a course and handler for submitted data. :param request: request object :param course_id: id for the course :return: """ try: current_course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) if request.method == "POST": form = CourseForm(request.POST, instance=current_course) if form.is_valid(): current_course.save() return redirect('course', course_id) else: form = CourseForm(instance=current_course) return render( request, 'course/edit.html', { 'title': _('Edit course'), 'form': form, 'course_id': course_id, 'allowed_tags': html_clean.DESCR_ALLOWED_TAGS, 'course_is_active': current_course.active, })
def add_course(request): if request.method == 'GET': form = CourseForm() return render(request, 'add_course.html', {'form': form}) elif request.method == 'POST': form = CourseForm(request.POST) form.save() return HttpResponseRedirect(reverse('course:courses'))
def create(request): if request.method == "POST": form = CourseForm(request.POST, request.FILES) if form.is_valid(): form.save() else: form = CourseForm() return render(request, 'admin/course/create.html')
def create_course(request): if request.method == "POST": form = CourseForm(request.POST) course = form.save(commit=False) if request.POST.get('exam'): course.exam = 1 course.save() return redirect(courses) else: form = CourseForm() return render(request, 'create-course.html', {"form": form})
def add_course(request: HttpRequest, course_id: int = None): course = None if course_id: course = get_object_or_404(models.Course, pk=course_id) if request.method == "POST": course_form = CourseForm(instance=course, data=request.POST, files=request.FILES) if course_form.is_valid(): course: models.Course = course_form.save(commit=False) course.created_by = request.user course.save() messages.success(request, f"Course {course.title} created successfully") else: course_form = CourseForm(instance=course) return render(request, "course/course_create.html", {"course_form": course_form})
def edit_course(request): if request.method == 'GET': if 'id' in request.GET and Course.objects.filter( id=request.GET['id']).count() > 0: course = Course.objects.get(id=request.GET['id']) form = CourseForm(instance=course) return render(request, 'edit_course.html', { 'id': request.GET['id'], 'form': form }) else: return render(request, 'not_found.html', {}) elif request.method == 'POST': cid = request.POST['course_id'] course = Course.objects.get(id=cid) form = CourseForm(request.POST, instance=course) form.save() return HttpResponseRedirect(reverse('course:courses'))
def edit_course(request: HttpRequest, course_id: str): """ Edit form for changing a course and handler for submitted data. :param request: request object :param course_id: id for the course :return: """ try: current_course = Course.objects.get(id=course_id) current_schedule = Schedule.objects.get(course_id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) if request.method == "POST": form = CourseForm(request.POST, instance=current_course) if form.is_valid(): current_course.save() current_schedule.set_type(request.POST['schedule_type']) current_schedule.save() return redirect('course', course_id) else: # FIXME(feliix42): Manually setting the start & end date here is required beacuse I just can't get django to format the date correctly in the Form setup form = CourseForm(instance=current_course,initial={ 'schedule_type':current_schedule.get_type(), 'start_time': current_course.start_time.strftime('%d.%m.%Y'), 'end_time': current_course.end_time.strftime('%d.%m.%Y') }) return render( request, 'course/edit.html', { 'title': _('Edit course'), 'form': form, 'create': False, 'course_id': course_id, 'allowed_tags': html_clean.DESCR_ALLOWED_TAGS, 'course_is_active': current_course.active, } )
def create_course(request): if settings.DEBUG: print('is_authenticated: {}'.format(request.user.is_authenticated())) if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('login')) if request.method == 'POST': course_form = CourseForm(request.POST, user=request.user) if course_form.is_valid(): course = course_form.save(commit=False) course.creator = request.user student_key = generate_key() while Course.objects.filter(student_key=student_key).exists(): student_key = generate_key() course.student_key = student_key instructor_key = generate_key() while Course.objects.filter(instructor_key=instructor_key).exists(): instructor_key = generate_key() course.instructor_key = instructor_key course.save() kwargs = { 'username' : request.user.username } return HttpResponseRedirect(reverse('course_list', kwargs=kwargs)) else: print(course_form.errors) else: course_form = CourseForm(user=request.user) context = { 'form' : course_form, } return render(request, 'course/create_course.html', context=context)
def post(self, request): errors = "" form = CourseForm(request.POST, request.FILES) if form.is_valid(): f = request.FILES['image'] static_path = '/static/product/img/' + f.name default_storage.save('course/' + static_path, ContentFile(f.read())) course(name=form.data.get("name"), subtitle=form.data.get("subtitle"), image=static_path, price=form.data.get("price"), hitcount=0).save() else: errors = "Error!" return render(request, self.template_name, { "form": self.form, "errors": errors })
def test_course_form_rejects_invalid_data(self): form = CourseForm(self.bad_data) self.assertFalse(form.is_valid())
def test_course_form_accepts_valid_data(self): form = CourseForm(self.data) self.assertTrue(form.is_valid())