def remove_attachment(user_profile, attachment): # type: (UserProfile, Attachment) -> None try: delete_message_image(attachment.path_id) except Exception: raise JsonableError(_("An error occured while deleting the attachment. Please try again later.")) attachment.delete()
def remove_attachment(user_profile: UserProfile, attachment: Attachment) -> None: try: delete_message_image(attachment.path_id) except Exception: raise JsonableError( _("An error occurred while deleting the attachment. Please try again later." )) attachment.delete()
def do_delete_old_unclaimed_attachments(weeks_ago: int) -> None: old_unclaimed_attachments, old_unclaimed_archived_attachments = get_old_unclaimed_attachments( weeks_ago) for attachment in old_unclaimed_attachments: delete_message_image(attachment.path_id) attachment.delete() for attachment in old_unclaimed_archived_attachments: delete_message_image(attachment.path_id) attachment.delete()
def test_delete_old_unclaimed_attachments(self) -> None: # Upload some files and make them older than a weeek self.login(self.example_email("hamlet")) d1 = StringIO("zulip!") d1.name = "dummy_1.txt" result = self.client_post("/json/user_uploads", {'file': d1}) d1_path_id = re.sub('/user_uploads/', '', result.json()['uri']) d2 = StringIO("zulip!") d2.name = "dummy_2.txt" result = self.client_post("/json/user_uploads", {'file': d2}) d2_path_id = re.sub('/user_uploads/', '', result.json()['uri']) two_week_ago = timezone_now() - datetime.timedelta(weeks=2) d1_attachment = Attachment.objects.get(path_id=d1_path_id) d1_attachment.create_time = two_week_ago d1_attachment.save() self.assertEqual(str(d1_attachment), u'<Attachment: dummy_1.txt>') d2_attachment = Attachment.objects.get(path_id=d2_path_id) d2_attachment.create_time = two_week_ago d2_attachment.save() # Send message referring only dummy_1 self.subscribe(self.example_user("hamlet"), "Denmark") body = "Some files here ...[zulip.txt](http://localhost:9991/user_uploads/" + d1_path_id + ")" self.send_stream_message(self.example_email("hamlet"), "Denmark", body, "test") # dummy_2 should not exist in database or the uploads folder do_delete_old_unclaimed_attachments(2) self.assertTrue(not Attachment.objects.filter( path_id=d2_path_id).exists()) self.assertTrue(not delete_message_image(d2_path_id))
def test_delete_old_unclaimed_attachments(self) -> None: # Upload some files and make them older than a weeek self.login(self.example_email("hamlet")) d1 = StringIO("zulip!") d1.name = "dummy_1.txt" result = self.client_post("/json/user_uploads", {'file': d1}) d1_path_id = re.sub('/user_uploads/', '', result.json()['uri']) d2 = StringIO("zulip!") d2.name = "dummy_2.txt" result = self.client_post("/json/user_uploads", {'file': d2}) d2_path_id = re.sub('/user_uploads/', '', result.json()['uri']) two_week_ago = timezone_now() - datetime.timedelta(weeks=2) d1_attachment = Attachment.objects.get(path_id = d1_path_id) d1_attachment.create_time = two_week_ago d1_attachment.save() self.assertEqual(str(d1_attachment), u'<Attachment: dummy_1.txt>') d2_attachment = Attachment.objects.get(path_id = d2_path_id) d2_attachment.create_time = two_week_ago d2_attachment.save() # Send message referring only dummy_1 self.subscribe(self.example_user("hamlet"), "Denmark") body = "Some files here ...[zulip.txt](http://localhost:9991/user_uploads/" + d1_path_id + ")" self.send_stream_message(self.example_email("hamlet"), "Denmark", body, "test") # dummy_2 should not exist in database or the uploads folder do_delete_old_unclaimed_attachments(2) self.assertTrue(not Attachment.objects.filter(path_id = d2_path_id).exists()) self.assertTrue(not delete_message_image(d2_path_id))
def test_delete_message_image_local(self) -> None: self.login(self.example_email("hamlet")) fp = StringIO("zulip!") fp.name = "zulip.txt" result = self.client_post("/json/user_uploads", {'file': fp}) path_id = re.sub('/user_uploads/', '', result.json()['uri']) self.assertTrue(delete_message_image(path_id))
def test_message_image_delete_s3(self) -> None: conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY) conn.create_bucket(settings.S3_AUTH_UPLOADS_BUCKET) user_profile = self.example_user('hamlet') uri = upload_message_image(u'dummy.txt', len(b'zulip!'), u'text/plain', b'zulip!', user_profile) path_id = re.sub('/user_uploads/', '', uri) self.assertTrue(delete_message_image(path_id))
def test_message_image_delete_s3(self) -> None: conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY) conn.create_bucket(settings.S3_AUTH_UPLOADS_BUCKET) user_profile = self.example_user('hamlet') uri = upload_message_file(u'dummy.txt', len(b'zulip!'), u'text/plain', b'zulip!', user_profile) path_id = re.sub('/user_uploads/', '', uri) self.assertTrue(delete_message_image(path_id))
def test_message_image_delete_s3(self): # type: () -> None conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY) conn.create_bucket(settings.S3_AUTH_UPLOADS_BUCKET) sender_email = "*****@*****.**" user_profile = get_user_profile_by_email(sender_email) uri = upload_message_image(u'dummy.txt', u'text/plain', b'zulip!', user_profile) path_id = re.sub('/user_uploads/', '', uri) self.assertTrue(delete_message_image(path_id))
def test_delete_message_image_local(self): # type: () -> None self.login("*****@*****.**") fp = StringIO("zulip!") fp.name = "zulip.txt" result = self.client_post("/json/upload_file", {'file': fp}) json = ujson.loads(result.content) uri = json["uri"] path_id = re.sub('/user_uploads/', '', uri) self.assertTrue(delete_message_image(path_id))
def test_delete_old_unclaimed_attachments(self): # type: () -> None # Upload some files and make them older than a weeek self.login("*****@*****.**") d1 = StringIO("zulip!") d1.name = "dummy_1.txt" result = self.client_post("/json/upload_file", {'file': d1}) json = ujson.loads(result.content) uri = json["uri"] d1_path_id = re.sub('/user_uploads/', '', uri) d2 = StringIO("zulip!") d2.name = "dummy_2.txt" result = self.client_post("/json/upload_file", {'file': d2}) json = ujson.loads(result.content) uri = json["uri"] d2_path_id = re.sub('/user_uploads/', '', uri) two_week_ago = timezone.now() - datetime.timedelta(weeks=2) d1_attachment = Attachment.objects.get(path_id = d1_path_id) d1_attachment.create_time = two_week_ago d1_attachment.save() d2_attachment = Attachment.objects.get(path_id = d2_path_id) d2_attachment.create_time = two_week_ago d2_attachment.save() # Send message refering only dummy_1 self.subscribe_to_stream("*****@*****.**", "Denmark") body = "Some files here ...[zulip.txt](http://localhost:9991/user_uploads/" + d1_path_id + ")" self.send_message("*****@*****.**", "Denmark", Recipient.STREAM, body, "test") # dummy_2 should not exist in database or the uploads folder do_delete_old_unclaimed_attachments(2) self.assertTrue(not Attachment.objects.filter(path_id = d2_path_id).exists()) self.assertTrue(not delete_message_image(d2_path_id))
def remove_attachment(user_profile, attachment): # type: (UserProfile, Attachment) -> None delete_message_image(attachment.path_id) attachment.delete()