def test_is_image_gif(self): attachment = Attachment() attachment.file = self.gif_upload attachment._signal = self.signal attachment.mimetype = "image/gif" attachment.save() self.assertTrue(attachment.is_image)
def test_is_image_doc_renamed_to_gif(self): with open(self.doc_upload_location, "rb") as f: doc_upload = SimpleUploadedFile("file.gif", f.read(), content_type="application/msword") attachment = Attachment() attachment.file = doc_upload attachment.mimetype = "application/msword" attachment._signal = self.signal attachment.save() self.assertFalse(attachment.is_image)
def add_image_attachments(signal, n=1): """ Adds n image attachments to signal. Returns added attachments """ attachments = [] for _ in range(n): image = SimpleUploadedFile('image.gif', small_gif, content_type='image/gif') attachment = Attachment() attachment.file = image attachment._signal = signal attachment.save() attachments.append(attachment) return attachments
def test_cache_file_with_word_doc(self): with open(self.doc_upload_location, "rb") as f: doc_upload = SimpleUploadedFile("file.doc", f.read(), content_type="application/msword") attachment = Attachment() attachment.file = doc_upload attachment.mimetype = "application/msword" attachment._signal = self.signal attachment.save() with self.assertRaises(Attachment.NotAnImageException): attachment.image_crop() resp = requests.get(self.live_server_url + attachment.file.url) self.assertEqual(200, resp.status_code, "Original file is not reachable")
def test_cropping_image_cache_file(self): attachment = Attachment() attachment.file = self.gif_upload attachment._signal = self.signal attachment.mimetype = "image/gif" attachment.save() self.assertIsInstance(attachment.image_crop.url, str) self.assertTrue(attachment.image_crop.url.endswith(".jpg")) resp = requests.get(self.live_server_url + attachment.file.url) self.assertEqual(200, resp.status_code, "Original image is not reachable") resp = requests.get(self.live_server_url + attachment.image_crop.url) self.assertEqual(200, resp.status_code, "Cropped image is not reachable")
def add_non_image_attachments(signal, n=1): """ Adds n file attachments to signal. Returns added attachments """ doc_upload_location = os.path.join(os.path.dirname(__file__), 'sia-ontwerp-testfile.doc') attachments = [] for _ in range(n): with open(doc_upload_location, "rb") as f: doc_upload = SimpleUploadedFile("file.doc", f.read(), content_type="application/msword") attachment = Attachment() attachment.file = doc_upload attachment._signal = signal attachment.save() attachments.append(attachment) return attachments
def add_attachment(self, file, signal): from .models import Attachment with transaction.atomic(): # Do not take lock, uploads are slow and the lock is not needed for # consistency of the history and the signal itself because there # is no foreign key from Signal to Attachment. # Fixes: SIG-2367 (second and third upload fail because database # is locked while the first upload is taking place). attachment = Attachment() attachment._signal = signal attachment.file = file attachment.save() # SIG-2213 use transaction.on_commit to send relevant Django signals transaction.on_commit(lambda: add_attachment.send_robust( sender=self.__class__, signal_obj=signal, attachment=attachment)) return attachment
def test_cache_file_without_mimetype(self): with open(self.json_upload_location, "rb") as f: doc_upload = SimpleUploadedFile("upload.json", f.read(), content_type="application/json") attachment = Attachment() attachment.file = doc_upload attachment._signal = self.signal attachment.save() with self.assertRaises(Attachment.NotAnImageException): attachment.image_crop() self.assertEqual("application/json", attachment.mimetype, "Mimetype should be set " "automatically when not set " "explicitly") resp = requests.get(self.live_server_url + attachment.file.url) self.assertEqual(200, resp.status_code, "Original file is not reachable")