def test_file_transactions(db, default_namespace): from inbox.models.message import Message account = default_namespace.account thread = add_fake_thread(db.session, default_namespace.id) mime_msg = mime.create.multipart('mixed') mime_msg.append( mime.create.text('plain', 'This is a message with attachments'), mime.create.attachment('image/png', 'filler', 'attached_image.png', 'attachment'), mime.create.attachment('application/pdf', 'filler', 'attached_file.pdf', 'attachment')) msg = Message.create_from_synced(account, 22, '[Gmail]/All Mail', datetime.utcnow(), mime_msg.to_string()) msg.thread = thread db.session.add(msg) db.session.commit() assert len(msg.parts) == 2 assert all(part.content_disposition == 'attachment' for part in msg.parts) block_ids = [part.block.id for part in msg.parts] with db.session.no_autoflush: transaction = get_latest_transaction(db.session, 'file', block_ids[0], default_namespace.id) assert transaction.command == 'insert' transaction = get_latest_transaction(db.session, 'file', block_ids[1], default_namespace.id) assert transaction.command == 'insert'
def create_imap_message(db_session, log, account, folder, msg): """ IMAP-specific message creation logic. This is the one function in this file that gets to take an account object instead of an account_id, because we need to relate the account to ImapUids for versioning to work, since it needs to look up the namespace. Returns ------- imapuid : inbox.models.tables.imap.ImapUid New db object, which links to new Message and Block objects through relationships. All new objects are uncommitted. """ new_msg = Message.create_from_synced(account=account, mid=msg.uid, folder_name=folder.name, received_date=msg.internaldate, body_string=msg.body) # Check to see if this is a copy of a message that was first created by the # Inbox API. If so, don't create a new object; just use the old one. existing_copy = reconcile_message(new_msg, db_session) if existing_copy is not None: new_msg = existing_copy imapuid = ImapUid(account=account, folder=folder, msg_uid=msg.uid, message=new_msg) imapuid.update_imap_flags(msg.flags, msg.g_labels) new_msg.is_draft = imapuid.is_draft new_msg.is_read = imapuid.is_seen update_contacts_from_message(db_session, new_msg, account.namespace) return imapuid
def test_file_transactions(db, default_namespace): from inbox.models.message import Message account = default_namespace.account thread = add_fake_thread(db.session, default_namespace.id) mime_msg = mime.create.multipart("mixed") mime_msg.append( mime.create.text("plain", "This is a message with attachments"), mime.create.attachment("image/png", "filler", "attached_image.png", "attachment"), mime.create.attachment("application/pdf", "filler", "attached_file.pdf", "attachment"), ) msg = Message.create_from_synced(account, 22, "[Gmail]/All Mail", datetime.utcnow(), mime_msg.to_string()) msg.thread = thread db.session.add(msg) db.session.commit() assert len(msg.parts) == 2 assert all(part.content_disposition == "attachment" for part in msg.parts) block_ids = [part.block.id for part in msg.parts] with db.session.no_autoflush: transaction = get_latest_transaction(db.session, "file", block_ids[0], default_namespace.id) assert transaction.command == "insert" transaction = get_latest_transaction(db.session, "file", block_ids[1], default_namespace.id) assert transaction.command == "insert"
def create_imap_message(db_session, log, account, folder, msg): """ IMAP-specific message creation logic. This is the one function in this file that gets to take an account object instead of an account_id, because we need to relate the account to ImapUids for versioning to work, since it needs to look up the namespace. Returns ------- imapuid : inbox.models.tables.imap.ImapUid New db object, which links to new Message and Block objects through relationships. All new objects are uncommitted. """ new_msg = Message.create_from_synced(account=account, mid=msg.uid, folder_name=folder.name, received_date=msg.internaldate, body_string=msg.body) # Check to see if this is a copy of a message that was first created by the # Inbox API. If so, don't create a new object; just use the old one. existing_copy = reconcile_message(new_msg, db_session) if existing_copy is not None: new_msg = existing_copy imapuid = ImapUid(account=account, folder=folder, msg_uid=msg.uid, message=new_msg) imapuid.update_imap_flags(msg.flags, msg.g_labels) new_msg.is_draft = imapuid.is_draft new_msg.is_read = imapuid.is_seen update_contacts_from_message(db_session, new_msg, account.namespace) return imapuid