Пример #1
0
    def post(self, request, *args, **kwargs):
        data = request.data.copy()

        if 'course' in request.data:
            permissions.check_course_permission(request.user.pk, request.data['course'])
        if 'event' in request.data:
            permissions.check_event_permission(request.user.pk, request.data['event'])
        if 'homework' in request.data:
            permissions.check_homework_permission(request.user.pk, request.data['homework'])

        response_data = []
        errors = []
        for upload in request.data.getlist('file[]'):
            data['title'] = upload.name
            data['attachment'] = upload

            serializer = self.get_serializer(data=data)

            if serializer.is_valid():
                serializer.save(user=request.user)

                logger.info(
                    f'Attachment {serializer.instance.pk} created for user {request.user.get_username()}')

                response_data.append(serializer.data)
            else:
                errors.append(serializer.errors)

        if len(errors) > 0:
            return Response(errors, status=status.HTTP_400_BAD_REQUEST)
        elif len(response_data) > 0:
            return Response(response_data, status=status.HTTP_201_CREATED)
        else:
            return Response({'details': 'An unknown error occurred.'}, status=status.HTTP_400_BAD_REQUEST)
Пример #2
0
    def post(self, request, *args, **kwargs):
        for course_id in request.data.get('courses', []):
            permissions.check_course_permission(request.user.pk, course_id)

        response = self.create(request, *args, **kwargs)

        logger.info(
            'Material {} created in MaterialGroup {} for user {}'.format(
                response.data['id'], request.data['material_group'],
                request.user.get_username()))

        return response
Пример #3
0
    def put(self, request, *args, **kwargs):
        permissions.check_course_permission(request.user.pk, request.data['course'])
        if 'category' in request.data:
            permissions.check_category_permission(request.user.pk, request.data['category'])
        for material_id in request.data.get('materials', []):
            permissions.check_material_permission(request.user.pk, material_id)

        response = self.update(request, *args, **kwargs)

        logger.info(f"Homework {kwargs['pk']} updated for user {request.user.get_username()}")

        return response
Пример #4
0
    def put(self, request, *args, **kwargs):
        if 'material_group' in request.data:
            permissions.check_material_group_permission(
                request.user.pk, request.data['material_group'])
        for course_id in request.data.get('courses', []):
            permissions.check_course_permission(request.user.pk, course_id)

        response = self.update(request, *args, **kwargs)

        logger.info('Material {} updated for user {}'.format(
            kwargs['pk'], request.user.get_username()))

        return response
Пример #5
0
    def patch(self, request, *args, **kwargs):
        if 'course' in request.data:
            permissions.check_course_permission(request.user.pk,
                                                request.data['course'])
        if 'category' in request.data:
            permissions.check_category_permission(request.user.pk,
                                                  request.data['category'])
        for material_id in request.data.get('materials', []):
            permissions.check_material_permission(request.user.pk, material_id)

        response = self.partial_update(request, *args, **kwargs)

        logger.info('Homework {} patched for user {}'.format(
            kwargs['pk'], request.user.get_username()))

        return response