def add_new_imapuid(db_session, gmessage, folder_name, acc): """ Add ImapUid object for this GMessage if we don't already have one. Parameters ---------- message : GMessage Message to add ImapUid for. folder_name : str Which folder to add the ImapUid in. acc : GmailAccount Which account to associate the message with. (Not looking this up within this function is a db access optimization.) """ if not db_session.query(ImapUid.msg_uid).join(Folder).filter( Folder.name == folder_name, ImapUid.account_id == acc.id, ImapUid.msg_uid == gmessage.uid).all(): try: message = db_session.query(Message).join(ImapThread).filter( ImapThread.g_thrid == gmessage.g_metadata.thrid, Message.g_thrid == gmessage.g_metadata.thrid, Message.g_msgid == gmessage.g_metadata.msgid, ImapThread.namespace_id == acc.namespace.id).one() except NoResultFound: # this may occur when a thread is expanded and those messages are # downloaded and committed, then new messages on that thread arrive # and get added to the download queue before this code is run log.debug('no Message object found, skipping imapuid creation', uid=gmessage.uid, g_msgid=gmessage.g_metadata.msgid) return new_imapuid = ImapUid(account=acc, folder=Folder.find_or_create( db_session, acc, folder_name), msg_uid=gmessage.uid, message=message) new_imapuid.update_imap_flags(gmessage.flags, gmessage.labels) new_imapuid.g_labels = [label for label in gmessage.labels] db_session.add(new_imapuid) db_session.commit() else: log.debug('skipping imapuid creation', uid=gmessage.uid, g_msgid=gmessage.g_metadata.msgid)
def add_new_imapuid(db_session, gmessage, folder_name, acc): """ Add ImapUid object for this GMessage if we don't already have one. Parameters ---------- message : GMessage Message to add ImapUid for. folder_name : str Which folder to add the ImapUid in. acc : GmailAccount Which account to associate the message with. (Not looking this up within this function is a db access optimization.) """ if not db_session.query(ImapUid.msg_uid).join(Folder).filter( Folder.name == folder_name, ImapUid.account_id == acc.id, ImapUid.msg_uid == gmessage.uid).all(): try: message = db_session.query(Message).join(ImapThread).filter( ImapThread.g_thrid == gmessage.g_metadata.thrid, Message.g_thrid == gmessage.g_metadata.thrid, Message.g_msgid == gmessage.g_metadata.msgid, ImapThread.namespace_id == acc.namespace.id).one() except NoResultFound: # this may occur when a thread is expanded and those messages are # downloaded and committed, then new messages on that thread arrive # and get added to the download queue before this code is run log.debug('no Message object found, skipping imapuid creation', uid=gmessage.uid, g_msgid=gmessage.g_metadata.msgid) return new_imapuid = ImapUid( account=acc, folder=Folder.find_or_create(db_session, acc, folder_name), msg_uid=gmessage.uid, message=message) new_imapuid.update_imap_flags(gmessage.flags, gmessage.labels) new_imapuid.g_labels = [label for label in gmessage.labels] db_session.add(new_imapuid) db_session.commit() else: log.debug('skipping imapuid creation', uid=gmessage.uid, g_msgid=gmessage.g_metadata.msgid)