def edit_course(request, course_stub): class_teacher = get_teacher_for_user(request.user) course = Course.objects.get(stub=course_stub, teacher=class_teacher) if request.method == 'POST': class_name = request.POST['course_name'] class_stub = make_stub(class_name) class_id = request.POST['course_id'] class_term = Term.objects.get(pk=int(request.POST['course_term'])) if course is not None: # make sure the new stub doesn't already exist stub_count = Course.objects.filter(stub=class_stub, teacher=class_teacher).count() if stub_count == 0: course.name = class_name course.stub = class_stub course.term = class_term course.course_id = class_id course.save() return redirect('/teacher') else: return render(request, 'teachers/courses/edit_course.html', { 'course': course, 'error': 'Class name already exists.' }) else: return render(request, 'teachers/courses/edit_course.html', { 'course': course, 'error': 'Course could not be found.' }) return render(request, 'teachers/courses/edit_course.html', {'course': course})
def add_course(request): class_teacher = get_teacher_for_user(request.user) courses = Course.objects.filter(teacher=class_teacher).order_by('name') terms = Term.objects.filter(school=class_teacher.school).order_by('begin_date') if request.method == 'POST': try: if request.POST['course_name'] == '' or request.POST['course_id'] == '': return render(request, 'teachers/courses/add_course.html', { 'courses': courses, 'terms': terms, 'error': 'Please enter a course name and ID.' }) class_name = request.POST['course_name'] class_stub = make_stub(class_name) class_id = request.POST['course_id'] class_teacher = get_teacher_for_user(request.user) class_term = Term.objects.get(pk=int(request.POST['course_term'])) # make sure this course doesn't already exist (check via stub) stub_count = Course.objects.filter(stub=class_stub, teacher=class_teacher).count() if stub_count == 0: new_course = Course( name=class_name, stub=class_stub, course_id=class_id, teacher=class_teacher, term=class_term ) new_course.save() # forward to the courses' add student view return redirect('/teacher/courses/' + class_stub + '/roster/add') else: return render(request, 'teachers/courses/add_course.html', { 'courses': courses, 'terms': terms, 'error': 'Class name already exists.' }) except: return render(request, 'teachers/courses/add_course.html', { 'courses': courses, 'terms': terms, 'error': 'Could not create class. Please try again.' }) return render(request, 'teachers/courses/add_course.html', {'courses': courses, 'terms': terms})