本文整理汇总了Python中inbox.models.backends.imap.ImapUid类的典型用法代码示例。如果您正苦于以下问题:Python ImapUid类的具体用法?Python ImapUid怎么用?Python ImapUid使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ImapUid类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_imap_message
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.
"""
msg_class = SpoolMessage if msg.created else Message
new_msg = msg_class.create(account=account, mid=msg.uid,
folder_name=folder.name,
received_date=msg.internaldate, flags=msg.flags,
body_string=msg.body)
if new_msg:
imapuid = ImapUid(account=account, folder=folder,
msg_uid=msg.uid, message=new_msg)
imapuid.update_imap_flags(msg.flags)
new_msg.is_draft = imapuid.is_draft
new_msg.is_read = imapuid.is_seen
# NOTE: This might be a good place to add FolderItem entries for
# non-Gmail backends.
return imapuid
示例2: create_imap_message
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_flags_and_labels(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
示例3: add_new_imapuid
def add_new_imapuid(db_session, log, 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.msg_uid == gmessage.uid).all():
message = db_session.query(Message).filter_by(
g_msgid=gmessage.g_metadata.msgid).one()
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)
db_session.add(new_imapuid)
db_session.commit()
else:
log.debug('skipping imapuid creation', uid=gmessage.uid)
示例4: create_imap_message
def create_imap_message(db_session, account, folder, msg):
"""
IMAP-specific message creation logic.
Returns
-------
imapuid : inbox.models.backends.imap.ImapUid
New db object, which links to new Message and Block objects through
relationships. All new objects are uncommitted.
"""
new_message = 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 Nylas API. If so, don't create a new object; just use the old one.
existing_copy = reconcile_message(new_message, db_session)
if existing_copy is not None:
new_message = existing_copy
imapuid = ImapUid(account=account, folder=folder, msg_uid=msg.uid, message=new_message)
imapuid.update_flags(msg.flags)
if msg.g_labels is not None:
imapuid.update_labels(msg.g_labels)
# Update the message's metadata
with db_session.no_autoflush:
is_draft = imapuid.is_draft and (folder.canonical_name == "drafts" or folder.canonical_name == "all")
update_message_metadata(db_session, account, new_message, is_draft)
update_contacts_from_message(db_session, new_message, account.namespace)
return imapuid
示例5: resync_uids_impl
def resync_uids_impl(self):
# NOTE: first, let's check if the UIVDALIDITY change was spurious, if
# it is, just discard it and go on, if it isn't, drop the relevant
# entries (filtering by account and folder IDs) from the imapuid table,
# download messages, if necessary - in case a message has changed UID -
# update UIDs, and discard orphaned messages. -siro
with mailsync_session_scope() as db_session:
folder_info = db_session.query(ImapFolderInfo). \
filter_by(account_id=self.account_id,
folder_id=self.folder_id).one()
cached_uidvalidity = folder_info.uidvalidity
with self.conn_pool.get() as crispin_client:
crispin_client.select_folder(self.folder_name,
lambda *args: True)
uidvalidity = crispin_client.selected_uidvalidity
if uidvalidity <= cached_uidvalidity:
log.debug('UIDVALIDITY unchanged')
return
invalid_uids = db_session.query(ImapUid). \
filter_by(account_id=self.account_id,
folder_id=self.folder_id)
data_sha256_message = {uid.message.data_sha256: uid.message
for uid in invalid_uids}
for uid in invalid_uids:
db_session.delete(uid)
# NOTE: this is necessary (and OK since it doesn't persist any
# data) to maintain the order between UIDs deletion and
# insertion. Without this, I was seeing constraints violation
# on the imapuid table. -siro
db_session.flush()
remote_uids = crispin_client.all_uids()
for remote_uid in remote_uids:
raw_message = crispin_client.uids([remote_uid])[0]
data_sha256 = sha256(raw_message.body).hexdigest()
if data_sha256 in data_sha256_message:
message = data_sha256_message[data_sha256]
# Create a new imapuid
uid = ImapUid(msg_uid=raw_message.uid,
message_id=message.id,
account_id=self.account_id,
folder_id=self.folder_id)
uid.update_flags(raw_message.flags)
db_session.add(uid)
# Update the existing message's metadata too
common.update_message_metadata(db_session, uid)
del data_sha256_message[data_sha256]
else:
self.download_and_commit_uids(crispin_client,
[remote_uid])
self.heartbeat_status.publish()
# FIXME: do we want to throttle the account when recovering
# from UIDVALIDITY changes? -siro
for message in data_sha256_message.itervalues():
db_session.delete(message)
folder_info.uidvalidity = uidvalidity
folder_info.highestmodseq = None
示例6: __deduplicate_message_object_creation
def __deduplicate_message_object_creation(self, db_session, raw_messages,
account):
"""
We deduplicate messages based on g_msgid: if we've previously saved a
Message object for this raw message, we don't create a new one. But we
do create a new ImapUid, associate it to the message, and update flags
and categories accordingly.
Note: we could do this prior to downloading the actual message
body, but that's really more complicated than it's worth. This
operation is not super common unless you're regularly moving lots
of messages to trash or spam, and even then the overhead of just
downloading the body is generally not that high.
"""
new_g_msgids = {msg.g_msgid for msg in raw_messages}
existing_g_msgids = g_msgids(self.namespace_id, db_session,
in_=new_g_msgids)
brand_new_messages = [m for m in raw_messages if m.g_msgid not in
existing_g_msgids]
previously_synced_messages = [m for m in raw_messages if m.g_msgid in
existing_g_msgids]
if previously_synced_messages:
log.info('saving new uids for existing messages',
count=len(previously_synced_messages))
account = Account.get(self.account_id, db_session)
folder = Folder.get(self.folder_id, db_session)
for raw_message in previously_synced_messages:
message_obj = db_session.query(Message).filter(
Message.namespace_id == self.namespace_id,
Message.g_msgid == raw_message.g_msgid).first()
if message_obj is None:
log.warning(
'Message disappeared while saving new uid',
g_msgid=raw_message.g_msgid,
uid=raw_message.uid)
brand_new_messages.append(raw_message)
continue
already_have_uid = (
(raw_message.uid, self.folder_id) in
{(u.msg_uid, u.folder_id) for u in message_obj.imapuids}
)
if already_have_uid:
log.warning('Skipping existing UID for message',
uid=raw_message.uid, message_id=message_obj.id)
continue
uid = ImapUid(account=account,
folder=folder,
msg_uid=raw_message.uid,
message=message_obj)
uid.update_flags(raw_message.flags)
uid.update_labels(raw_message.g_labels)
common.update_message_metadata(
db_session, account, message_obj, uid.is_draft)
db_session.commit()
return brand_new_messages
示例7: test_truncate_imapuid_extra_flags
def test_truncate_imapuid_extra_flags(db, default_account, message, folder):
imapuid = ImapUid(message=message, account_id=default_account.id,
msg_uid=2222, folder=folder)
imapuid.update_flags(['We', 'the', 'People', 'of', 'the', 'United',
'States', 'in', 'Order', 'to', 'form', 'a', 'more',
'perfect', 'Union', 'establish', 'Justice',
'insure', 'domestic', 'Tranquility', 'provide',
'for', 'the', 'common', 'defence', 'promote', 'the',
'general', 'Welfare', 'and', 'secure', 'the',
'Blessings', 'of', 'Liberty', 'to', 'ourselves',
'and', 'our', 'Posterity', 'do', 'ordain', 'and',
'establish', 'this', 'Constitution', 'for', 'the',
'United', 'States', 'of', 'America'])
assert len(json.dumps(imapuid.extra_flags)) < 255
示例8: add_new_imapuid
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)
示例9: test_adding_message_to_thread
def test_adding_message_to_thread(db):
"""recompute_thread_labels is not invoked when a new message is added
(only when UID metadata changes, or when a UID is deleted). Test that
tag changes work when adding messages to a thread."""
account = db.session.query(Account).get(ACCOUNT_ID)
account.namespace.create_canonical_tags()
thread = db.session.query(Thread).get(THREAD_ID)
account.trash_folder = Folder(name='Trash', account=account)
FolderItem(thread=thread, folder=account.trash_folder)
folder_names = [folder.name for folder in thread.folders]
m = Message(namespace_id=account.namespace.id, subject='test message',
thread_id=thread.id, received_date=datetime.datetime.now(),
size=64, body="body", snippet="snippet")
uid = ImapUid(account=account, message=m,
g_labels=['\\Inbox', 'test-label'],
msg_uid=22L, folder_id=account.inbox_folder.id)
uid.folder = account.inbox_folder
uid2 = ImapUid(account=account, message=m, g_labels=['test-2'],
msg_uid=24L, folder_id=account.trash_folder.id)
uid2.folder = account.trash_folder
thread.messages.append(m)
add_any_new_thread_labels(thread, uid, db.session)
add_any_new_thread_labels(thread, uid2, db.session)
folder_names = [folder.name for folder in thread.folders]
for folder in folder_names:
assert folder in ['Inbox', 'Trash', 'test-label', 'test-2', '[Gmail]/All Mail', '[Gmail]/Important'],\
"all folders should be present"
# Now, remove the message
m.imapuids.remove(uid2)
db.session.delete(uid2)
db.session.flush()
recompute_thread_labels(thread, db.session)
folder_names = [folder.name for folder in thread.folders]
assert 'test-2' not in folder_names,\
"test-2 label should have been removed from thread"
示例10: test_soft_delete
def test_soft_delete(db, config):
from inbox.models import Folder, Message
from inbox.models.backends.imap import ImapUid
f = Folder(name='DOES NOT EXIST', account_id=ACCOUNT_ID)
db.session.add(f)
db.session.flush()
m = Message()
m.namespace_id = NAMESPACE_ID
m.thread_id = 1
m.received_date = datetime.datetime.utcnow()
m.size = 0
m.sanitized_body = ""
m.snippet = ""
u = ImapUid(message=m, account_id=ACCOUNT_ID, folder_id=f.id,
msg_uid=9999, extra_flags="")
db.session.add_all([m, u])
f.mark_deleted()
u.mark_deleted()
db.session.commit()
m_id = m.id
# bypass custom query method to confirm creation
db.new_session(ignore_soft_deletes=False)
f = db.session.query(Folder).filter_by(name='DOES NOT EXIST').one()
assert f, "Can't find Folder object"
assert f.deleted_at is not None, "Folder not marked as deleted"
db.new_session(ignore_soft_deletes=True)
with pytest.raises(NoResultFound):
db.session.query(Folder).filter(Folder.name == 'DOES NOT EXIST').one()
count = db.session.query(Folder).filter(
Folder.name == 'DOES NOT EXIST').count()
assert count == 0, "Shouldn't find any deleted folders!"
m = db.session.query(Message).filter_by(id=m_id).one()
assert not m.imapuids, "imapuid was deleted!"
示例11: test_truncate_imapuid_extra_flags
def test_truncate_imapuid_extra_flags(db, default_account, message, folder):
imapuid = ImapUid(message=message, account_id=default_account.id, msg_uid=2222, folder=folder)
imapuid.update_flags(
[
"We",
"the",
"People",
"of",
"the",
"United",
"States",
"in",
"Order",
"to",
"form",
"a",
"more",
"perfect",
"Union",
"establish",
"Justice",
"insure",
"domestic",
"Tranquility",
"provide",
"for",
"the",
"common",
"defence",
"promote",
"the",
"general",
"Welfare",
"and",
"secure",
"the",
"Blessings",
"of",
"Liberty",
"to",
"ourselves",
"and",
"our",
"Posterity",
"do",
"ordain",
"and",
"establish",
"this",
"Constitution",
"for",
"the",
"United",
"States",
"of",
"America",
]
)
assert len(json.dumps(imapuid.extra_flags)) < 255