def test_image_url(self): """Test image url generation.""" impr_instance = ImageProcess('simple.jpg') img_name = impr_instance.filename ref_url = f'http://127.0.0.1:8000/media/uploads/{img_name}' tested_url = impr_instance.generate_img_url() self.assertEqual(tested_url, ref_url)
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)