Пример #1
0
    def case_check_image(self, img_name: str, expected_result: str):
        path = UnitTests.path_to_test_img(img_name)

        with patch(
            'editor.editor.tests.ImageProcess.generate_path',
            return_value=path
        ):
            instance = ImageProcess(img_name)
            actual_result = instance.check_image()
            instance.generate_path.assert_called()

        self.assertEqual(expected_result, actual_result)
Пример #2
0
class ImageUpload():
    def __init__(self, request):
        self.request = request
        self.impr_instance = None

    def get_image_data(self) -> Sequence[Tuple[Dict, str]]:
        """Parse image data from request."""
        image = self.request.FILES['upload']
        filename = self.request.FILES['upload'].name

        self.impr_instance = ImageProcess(filename)
        image.name = self.impr_instance.filename

        return {'image': image}

    def get_response(self) -> JsonResponse:
        """Check image and get corresponding response."""
        img_status = self.impr_instance.check_image()
        if img_status == StatusMessages.OK:
            result_url = self.impr_instance.generate_img_url()
            return JsonResponse({'url': result_url})
        else:
            return JsonResponse({'error': {'message': img_status}}, status=415)

    def process_images(self) -> JsonResponse:
        image_data = self.get_image_data()

        form = ImageForm(self.request.POST, image_data)
        if form.is_valid():
            form.save()
            return self.get_response()

        # Add function to cleanup form data from server
        # self.impr_instance.remove_image()

        return JsonResponse(
            {'error': {
                'message': 'Failed to load image file'
            }}, status=400)