Пример #1
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
Пример #2
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)