Esempio n. 1
0
    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)
Esempio n. 2
0
 def update(self, instance, validated_data: dict):
     """
     Update and save an existing instance
     """
     save = False
     cohort_settings = {}
     for field, value in validated_data.items():
         if field in self.Meta.fields:
             setattr(instance, field, value)
             save = True
         elif field in self.Meta.fields_cohorts:
             cohort_settings[field] = value
     if cohort_settings:
         discussion_settings = CourseDiscussionSettings.get(instance.id)
         serializer = DiscussionSettingsSerializer(
             discussion_settings,
             context={
                 'course': instance,
                 'settings': discussion_settings,
             },
             data=cohort_settings,
             partial=True,
         )
         if serializer.is_valid(raise_exception=True):
             serializer.save()
     if save:
         modulestore().update_item(instance, self.context['user_id'])
     return instance
Esempio n. 3
0
    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)
Esempio n. 4
0
    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)
Esempio n. 5
0
 def to_representation(self, instance) -> dict:
     """
     Serialize data into a dictionary, to be used as a response
     """
     settings = {
         field.name: field.read_json(instance)
         for field in instance.fields.values()
         if field.name in self.Meta.fields
     }
     discussion_settings = CourseDiscussionSettings.get(instance.id)
     serializer = DiscussionSettingsSerializer(
         discussion_settings,
         context={
             'course': instance,
             'settings': discussion_settings,
         },
         partial=True,
     )
     settings.update({
         key: value
         for key, value in serializer.data.items() if key != 'id'
     })
     return settings
Esempio n. 6
0
    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