def patch(self, request, course_id): """ Implement a handler for the PATCH method. """ if request.content_type != MergePatchParser.media_type: raise UnsupportedMediaType(request.content_type) kwargs = self._get_request_kwargs(course_id) form = CourseDiscussionSettingsForm(kwargs, request_user=request.user) if not form.is_valid(): raise ValidationError(form.errors) course = form.cleaned_data['course'] course_key = form.cleaned_data['course_key'] discussion_settings = get_course_discussion_settings(course_key) serializer = DiscussionSettingsSerializer( data=request.data, partial=True, course=course, discussion_settings=discussion_settings) if not serializer.is_valid(): raise ValidationError(serializer.errors) settings_to_change = serializer.validated_data['settings_to_change'] try: discussion_settings = set_course_discussion_settings( course_key, **settings_to_change) except ValueError as e: raise ValidationError(text_type(e)) return Response(status=status.HTTP_204_NO_CONTENT)
def patch(self, request, course_id): """ Implement a handler for the PATCH method. """ if request.content_type != MergePatchParser.media_type: raise UnsupportedMediaType(request.content_type) kwargs = self._get_request_kwargs(course_id) form = CourseDiscussionSettingsForm(kwargs, request_user=request.user) if not form.is_valid(): raise ValidationError(form.errors) course = form.cleaned_data['course'] course_key = form.cleaned_data['course_key'] discussion_settings = CourseDiscussionSettings.get(course_key) serializer = DiscussionSettingsSerializer( discussion_settings, context={ 'course': course, 'settings': discussion_settings, }, data=request.data, partial=True, ) if not serializer.is_valid(): raise ValidationError(serializer.errors) serializer.save() return Response(status=status.HTTP_204_NO_CONTENT)
def patch(self, request, course_id): """ Implement a handler for the PATCH method. """ if request.content_type != MergePatchParser.media_type: raise UnsupportedMediaType(request.content_type) kwargs = self._get_request_kwargs(course_id) form = CourseDiscussionSettingsForm(kwargs, request_user=request.user) if not form.is_valid(): raise ValidationError(form.errors) course = form.cleaned_data['course'] course_key = form.cleaned_data['course_key'] discussion_settings = get_course_discussion_settings(course_key) serializer = DiscussionSettingsSerializer( data=request.data, partial=True, course=course, discussion_settings=discussion_settings ) if not serializer.is_valid(): raise ValidationError(serializer.errors) settings_to_change = serializer.validated_data['settings_to_change'] try: discussion_settings = set_course_discussion_settings(course_key, **settings_to_change) except ValueError as e: raise ValidationError(text_type(e)) return Response(status=status.HTTP_204_NO_CONTENT)
def get(self, request, course_id): """ Implement a handler for the GET method. """ kwargs = self._get_request_kwargs(course_id) form = CourseDiscussionSettingsForm(kwargs, request_user=request.user) if not form.is_valid(): raise ValidationError(form.errors) course_key = form.cleaned_data['course_key'] course = form.cleaned_data['course'] discussion_settings = get_course_discussion_settings(course_key) return self._get_representation(course, course_key, discussion_settings)
def get(self, request, course_id): """ Implement a handler for the GET method. """ kwargs = self._get_request_kwargs(course_id) form = CourseDiscussionSettingsForm(kwargs, request_user=request.user) if not form.is_valid(): raise ValidationError(form.errors) course_key = form.cleaned_data['course_key'] course = form.cleaned_data['course'] discussion_settings = get_course_discussion_settings(course_key) return self._get_representation(course, course_key, discussion_settings)
def get(self, request, course_id): """ Implement a handler for the GET method. """ kwargs = self._get_request_kwargs(course_id) form = CourseDiscussionSettingsForm(kwargs, request_user=request.user) if not form.is_valid(): raise ValidationError(form.errors) course_key = form.cleaned_data['course_key'] course = form.cleaned_data['course'] discussion_settings = CourseDiscussionSettings.get(course_key) serializer = DiscussionSettingsSerializer( discussion_settings, context={ 'course': course, 'settings': discussion_settings, }, partial=True, ) response = Response(serializer.data) return response