def get(self, request, course_id, lesson_id, learning_resource_id): """ The get method of the leaders lessons resources edit view is responsible for providing the interface to edit resources of lessons. This is achieved by validating that the lesson belongs to the course and the course belongs to the user and the resource belongs to the lesson and handling HTTP requests as appropriate. """ # Validation # Get user details leader = request.user # Get the lesson and course we want to work with. try: course = get_leaders_course(course_id, leader) lesson = get_leaders_lesson(lesson_id, course, leader) learning_resource = get_leaders_learning_resource( learning_resource_id, lesson, course, leader) # Forward error handle back where the user came from with an error message. except CourseNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the course you asked for! Sorry about that.' ) return redirect("leaders_interfaces:courses") except LessonNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the lesson you asked for! Sorry about that.' ) return redirect("leaders_interfaces:courses_lessons", course_id) except LearningResourceNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the learning resource you asked for! Sorry ' 'about that.') return redirect("leaders_interfaces:lessons_resources", course_id, lesson_id) edit_form = LessonsLearningStylesResourcesEditForm( initial={ 'title': learning_resource.title, 'description': learning_resource.description, 'learning_style': learning_resource.learning_style }, course=course, lesson=lesson, lessonlearningstyleresource=learning_resource) # Render the lessons_edit_template passing in the edit form. return render(request, lessons_resources_edit_template, { 'resources_edit_form': edit_form, 'lesson': lesson, 'course': course })
def test_get_leaders_lesson(self): """ Tests whether get_leaders_lesson returns exceptions correctly. """ Lessons.objects.create(course=self.course, title="", description="", sequence_number=1) test_leader2 = User.objects.create_user(id=2, username='******', email='*****@*****.**', password='******') try: get_leaders_lesson(1, self.course, test_leader2) except LessonNotFoundException: pass try: get_leaders_lesson(2, self.course, test_leader2) except LessonNotFoundException: return True self.fail()
def post(self, request, course_id, lesson_id): """ The post method of the leaders lessons resources delete view is responsible for handling information passed by the interface to delete lesson resources. This is achieved by getting the resources sent by the HTTP post request, performing validation that the lesson belongs to the course and the course belongs to the user, and the resource belongs to the lesson then handling resources as appropriate. """ # Validation # Get user details leader = request.user # Get the lesson and course we want to work with. try: course = get_leaders_course(course_id, leader) lesson = get_leaders_lesson(lesson_id, course, leader) # Forward error handle back where the user came from with an error message. except CourseNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the course you asked for! Sorry about that.' ) return redirect("leaders_interfaces:courses") except LessonNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the lesson you asked for! Sorry about that.' ) return redirect("leaders_interfaces:courses_lessons", course_id) learning_resource_ids = request.POST.getlist('checks') remove_learning_resources_list(lesson, learning_resource_ids) # Get all the resources objects associated with the lesson objects. learning_resources = lesson.lessonslearningstylesresources_set.all() # If there are no resources pass a message. if not learning_resources: messages.error( request, 'It looks like there are no learning resources for us to display (^・x・^). Please ' 'populate ' 'some resources so students have something to see!') return render( request, resources_delete_template, { 'course': course, 'lesson': lesson, 'learning_resources': learning_resources })
def post(self, request, course_id, lesson_id): """ The post method of the leaders lessons edit view is responsible for interpreting the post data sent by the interface associated with creating lessons for the course id specified. This means cleaning the data ( ensuring that uniqueness is not violated) and ensuring that invalid data i.e. lessons for courses that don't belong to the user, are not saved). """ # Validation # Get user details leader = request.user # Get the lesson and course we want to work with. try: course = get_leaders_course(course_id, leader) lesson = get_leaders_lesson(lesson_id, course, leader) # Forward error handle back where the user came from with an error message. except CourseNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the course you asked for! Sorry about that.' ) return redirect("leaders_interfaces:courses") except LessonNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the lesson you asked for! Sorry about that.' ) return redirect("leaders_interfaces:courses_lessons", course_id) edit_form = LessonsEditForm( request.POST, maximum_sequence_number=get_maximum_lesson_sequence_number(course), course=course, lesson=lesson) if edit_form.is_valid(): edit_form.save() return redirect('leaders_interfaces:lessons_resources', course.id, lesson.id) else: return render(request, lessons_edit_template, { 'lessons_edit_form': edit_form, 'course': course, 'lesson': lesson })
def get(self, request, course_id, lesson_id): """ The get method of the leaders lessons resources view is responsible for providing the interface of a lessons existing resources. This is achieved by getting all the resources by a particular lesson post validation that the lesson belongs to the course and the course belongs to the user. """ # Validation # Get user details leader = request.user # Get the lesson and course we want to work with. try: course = get_leaders_course(course_id, leader) lesson = get_leaders_lesson(lesson_id, course, leader) # Forward error handle back where the user came from with an error message. except CourseNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the course you asked for! Sorry about that.' ) return redirect("leaders_interfaces:courses") except LessonNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the lesson you asked for! Sorry about that.' ) return redirect("leaders_interfaces:courses_lessons", course_id) # Get all associated learning_resources objects with the lessons learning_resources = lesson.lessonslearningstylesresources_set.all() # If there are no resources pass a message. if not learning_resources: messages.error( request, 'It looks like there are no learning resources for us to display (^・x・^). Please ' 'populate ' 'some resources so students have something to see!') # Render the template of course resources passing the lesson, course, and resources as context. return render( request, lessons_resources_template, { 'lesson': lesson, 'course': course, 'learning_resources': learning_resources })
def get(self, request, course_id, lesson_id): """ The get method of the leaders lessons edit view is responsible for providing the interface from which users can edit existing lessons. """ # Validation # Get user details leader = request.user # Get the lesson and course we want to work with. try: course = get_leaders_course(course_id, leader) lesson = get_leaders_lesson(lesson_id, course, leader) # Forward error handle back where the user came from with an error message. except CourseNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the course you asked for! Sorry about that.' ) return redirect("leaders_interfaces:courses") except LessonNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the lesson you asked for! Sorry about that.' ) return redirect("leaders_interfaces:courses_lessons", course_id) edit_form = LessonsEditForm( initial={ 'sequence_number': lesson.sequence_number, 'title': lesson.title, 'description': lesson.description }, maximum_sequence_number=get_maximum_lesson_sequence_number(course), course=course, lesson=lesson) # Render the lessons_edit_template passing in the edit form. return render(request, lessons_edit_template, { 'lessons_edit_form': edit_form, 'lesson': lesson, 'course': course })
def get(self, request, course_id, lesson_id): """ The get method of the leaders lessons resources create view is responsible for providing the interface to append a new resource to a lesson. This is achieved by validating that the lesson belongs to the course and the course belongs to the user and handling HTTP requests as appropriate. """ # Validation # Get user details leader = request.user # Get the lesson and course we want to work with. try: course = get_leaders_course(course_id, leader) lesson = get_leaders_lesson(lesson_id, course, leader) # Forward error handle back where the user came from with an error message. except CourseNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the course you asked for! Sorry about that.' ) return redirect("leaders_interfaces:courses") except LessonNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the lesson you asked for! Sorry about that.' ) return redirect("leaders_interfaces:courses_lessons", course_id) # Get the LessonLearningStylesResources form construct with initial values. create_form = LessonsLearningStylesResourcesCreateForm(course=course, lesson=lesson) return render( request, lessons_resources_create_template, { 'resources_create_form': create_form, 'lesson': lesson, 'course': course, })
def post(self, request, course_id, lesson_id, learning_resource_id): """ The get method of the leaders lessons resources edit view is responsible for handling the interface to edit resources of lessons. This is achieved by validating that the lesson belongs to the course and the course belongs to the user and the resource belongs to the lesson and handling HTTP requests as appropriate. """ leader = request.user # Get the lesson and course we want to work with. try: course = get_leaders_course(course_id, leader) lesson = get_leaders_lesson(lesson_id, course, leader) learning_resource = get_leaders_learning_resource( learning_resource_id, lesson, course, leader) # Forward error handle back where the user came from with an error message. except CourseNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the course you asked for! Sorry about that.' ) return redirect("leaders_interfaces:courses") except LessonNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the lesson you asked for! Sorry about that.' ) return redirect("leaders_interfaces:courses_lessons", course_id) except LearningResourceNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the learning resource you asked for! Sorry ' 'about that.') return redirect("leaders_interfaces:lessons_resources", course_id, lesson_id) edit_form = LessonsLearningStylesResourcesEditForm( request.POST, request.FILES, course=course, lesson=lesson, lessonlearningstyleresource=learning_resource) if edit_form.is_valid(): edit_form.save(commit=True) # Return to resources page, users will see the changes. # Get all the resources objects associated with the lesson objects. learning_resources = lesson.lessonslearningstylesresources_set.all( ) # If there are none pass a message. if not learning_resources: messages.error( request, 'It looks like there are no learning resources for us to display (^・x・^). Please ' 'populate ' 'some resources so students have something to see!') return render( request, lessons_resources_template, { 'lesson': lesson, 'course': course, 'learning_resources': learning_resources }) # Re-interface the page with validation errors passed by the form else: return render( request, lessons_resources_edit_template, { 'resources_edit_form': edit_form, 'lesson': lesson, 'course': course })
def post(self, request, course_id, lesson_id): """ The post method of the leaders lessons resources create view is responsible for handling information passed by the interface creating lesson resources. This is achieved by getting the resources sent by the HTTP post request, performing validation that the lesson belongs to the course and the course belongs to the user, then handling resources as appropriate. """ # Validation # Get user details leader = request.user # Get the lesson and course we want to work with. try: course = get_leaders_course(course_id, leader) lesson = get_leaders_lesson(lesson_id, course, leader) # Forward error handle back where the user came from with an error message. except CourseNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the course you asked for! Sorry about that.' ) return redirect("leaders_interfaces:courses") except LessonNotFoundException: messages.error( request, 'An error has occurred (^・x・^). We cannot find the lesson you asked for! Sorry about that.' ) return redirect("leaders_interfaces:courses_lessons", course_id) create_form = LessonsLearningStylesResourcesCreateForm(request.POST, request.FILES, lesson=lesson, course=course) if create_form.is_valid(): create_form.save(commit=True) # Get all associated learning_resources objects with the lessons learning_resources = lesson.lessonslearningstylesresources_set.all( ) # If there are no resources pass a message. if not learning_resources: messages.error( request, 'It looks like there are no learning resources for us to display (^・x・^). Please ' 'populate ' 'some resources so students have something to see!') return render( request, lessons_resources_template, { 'lesson': lesson, 'course': course, 'learning_resources': learning_resources }) # Re-interface the page with validation errors passed by the form else: # Get all the resources objects associated with the lesson objects. learning_resources = lesson.lessonslearningstylesresources_set.all( ) # If there are no resources pass a message. if not learning_resources: messages.error( request, 'It looks like there are no learning resources for us to display (^・x・^). Please ' 'populate ' 'some resources so students have something to see!') return render( request, lessons_resources_create_template, { 'resources_create_form': create_form, 'course': course, 'lesson': lesson, 'learning_resources': learning_resources })