本文整理汇总了Python中inbox.models.Folder.find_or_create方法的典型用法代码示例。如果您正苦于以下问题:Python Folder.find_or_create方法的具体用法?Python Folder.find_or_create怎么用?Python Folder.find_or_create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类inbox.models.Folder
的用法示例。
在下文中一共展示了Folder.find_or_create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_raw_folder_change
# 需要导入模块: from inbox.models import Folder [as 别名]
# 或者: from inbox.models.Folder import find_or_create [as 别名]
def handle_raw_folder_change(self, db_session, account, raw_folder):
folder = db_session.query(Folder).filter(
Folder.account_id == account.id,
Folder.canonical_name == raw_folder.role).first()
if folder:
if folder.name != raw_folder.display_name:
log.info('Folder name changed on remote',
account_id=self.account_id,
role=raw_folder.role,
new_name=raw_folder.display_name,
name=folder.name)
folder.name = raw_folder.display_name
if folder.category:
if folder.category.display_name != \
raw_folder.display_name:
folder.category.display_name = raw_folder.display_name # noqa
else:
log.info('Creating category for folder',
account_id=self.account_id,
folder_name=folder.name)
folder.category = Category.find_or_create(
db_session, namespace_id=account.namespace.id,
name=raw_folder.role,
display_name=raw_folder.display_name,
type_='folder')
else:
Folder.find_or_create(db_session, account,
raw_folder.display_name,
raw_folder.role)
示例2: _folders_for_labels
# 需要导入模块: from inbox.models import Folder [as 别名]
# 或者: from inbox.models.Folder import find_or_create [as 别名]
def _folders_for_labels(g_labels, account, db_session):
"""Given a set of Gmail label strings, return the set of associated Folder
objects. Creates new (un-added, uncommitted) Folder instances if needed."""
# Elements of g_labels may not have unicode type (in particular, if you
# have a numeric label, e.g., '42'), so we need to coerce to unicode.
labels = {unicode(l).lstrip('\\').lower() for l in g_labels}
# The problem here is that Gmail's attempt to squash labels and
# IMAP folders into the same abstraction doesn't work perfectly. In
# particular, there is a '[Gmail]/Sent' folder, but *also* a 'Sent'
# label, and so on. We handle this by only maintaining one folder
# object that encapsulates both of these. If a Gmail user does not
# have these folders enabled via IMAP, we create Folder rows
# with no 'name' attribute and fill in the 'name' if the account
# is later reconfigured.
special_folders = {
'inbox': account.inbox_folder,
'sent': account.sent_folder,
'draft': account.drafts_folder,
'starred': account.starred_folder,
'important': account.important_folder,
}
folders = set()
for label in labels:
if label in special_folders:
folder = special_folders[label]
if folder is None:
folder = Folder.find_or_create(db_session, account, None,
label)
folders.add(folder)
else:
folders.add(
Folder.find_or_create(db_session, account, label))
return folders
示例3: save_folder_names
# 需要导入模块: from inbox.models import Folder [as 别名]
# 或者: from inbox.models.Folder import find_or_create [as 别名]
def save_folder_names(self, db_session, raw_folders):
"""
Save the folders present on the remote backend for an account.
* Create Folder objects.
* Delete Folders that no longer exist on the remote.
Notes
-----
Generic IMAP uses folders (not labels).
Canonical folders ('inbox') and other folders are created as Folder
objects only accordingly.
We don't canonicalize folder names to lowercase when saving because
different backends may be case-sensitive or otherwise - code that
references saved folder names should canonicalize if needed when doing
comparisons.
"""
account = db_session.query(Account).get(self.account_id)
remote_folder_names = {f.display_name.rstrip()[:MAX_FOLDER_NAME_LENGTH]
for f in raw_folders}
assert 'inbox' in {f.role for f in raw_folders},\
'Account {} has no detected inbox folder'.\
format(account.email_address)
local_folders = {f.name: f for f in db_session.query(Folder).filter(
Folder.account_id == self.account_id)}
# Delete folders no longer present on the remote.
# Note that the folder with canonical_name='inbox' cannot be deleted;
# remote_folder_names will always contain an entry corresponding to it.
discard = set(local_folders) - remote_folder_names
for name in discard:
log.info('Folder deleted from remote', account_id=self.account_id,
name=name)
if local_folders[name].category_id is not None:
cat = db_session.query(Category).get(
local_folders[name].category_id)
if cat is not None:
db_session.delete(cat)
del local_folders[name]
# Create new folders
for raw_folder in raw_folders:
Folder.find_or_create(db_session, account, raw_folder.display_name,
raw_folder.role)
# Set the should_run bit for existing folders to True (it's True by
# default for new ones.)
for f in local_folders.values():
if f.imapsyncstatus:
f.imapsyncstatus.sync_should_run = True
db_session.commit()
示例4: test_imap_search_unicode
# 需要导入模块: from inbox.models import Folder [as 别名]
# 或者: from inbox.models.Folder import find_or_create [as 别名]
def test_imap_search_unicode(db, imap_api_client, generic_account,
patch_crispin_client,
sorted_imap_threads):
Folder.find_or_create(db.session, generic_account,
'存档', '存档')
search_client = get_search_client(generic_account)
assert isinstance(search_client, IMAPSearchClient)
threads = imap_api_client.get_data('/threads/search?q=存档')
for sorted_thread, result_thread in zip(sorted_imap_threads, threads):
assert sorted_thread.public_id == result_thread['id']
示例5: test_imap_search_unicode
# 需要导入模块: from inbox.models import Folder [as 别名]
# 或者: from inbox.models.Folder import find_or_create [as 别名]
def test_imap_search_unicode(db, imap_api_client, generic_account,
patch_crispin_client,
patch_handler_from_provider,
sorted_imap_threads):
Folder.find_or_create(db.session, generic_account,
'存档', '存档')
search_client = get_search_client(generic_account)
assert search_client.__class__.__name__ == 'IMAPSearchClient'
threads = imap_api_client.get_data('/threads/search?q=存档')
for sorted_thread, result_thread in zip(sorted_imap_threads, threads):
assert sorted_thread.public_id == result_thread['id']
示例6: test_gmail_search_unicode
# 需要导入模块: from inbox.models import Folder [as 别名]
# 或者: from inbox.models.Folder import find_or_create [as 别名]
def test_gmail_search_unicode(db, api_client, test_gmail_thread,
patch_token_manager,
patch_gmail_search_response,
default_account,
sorted_gmail_threads):
Folder.find_or_create(db.session, default_account,
'存档', '存档')
search_client = get_search_client(default_account)
assert isinstance(search_client, GmailSearchClient)
threads = api_client.get_data('/threads/search?q=存档')
for sorted_thread, result_thread in zip(sorted_gmail_threads, threads):
assert sorted_thread.public_id == result_thread['id']
示例7: test_gmail_search_unicode
# 需要导入模块: from inbox.models import Folder [as 别名]
# 或者: from inbox.models.Folder import find_or_create [as 别名]
def test_gmail_search_unicode(db, api_client, test_gmail_thread,
default_account,
patch_crispin_client,
patch_handler_from_provider,
sorted_gmail_threads):
Folder.find_or_create(db.session, default_account,
'存档', '存档')
search_client = get_search_client(default_account)
assert search_client.__class__.__name__ == 'GmailSearchClient'
threads = api_client.get_data('/threads/search?q=存档')
for sorted_thread, result_thread in zip(sorted_gmail_threads, threads):
assert sorted_thread.public_id == result_thread['id']
示例8: add_new_imapuids
# 需要导入模块: from inbox.models import Folder [as 别名]
# 或者: from inbox.models.Folder import find_or_create [as 别名]
def add_new_imapuids(crispin_client, remote_g_metadata, syncmanager_lock,
uids):
"""
Add ImapUid entries only for (already-downloaded) messages.
If a message has already been downloaded via another folder, we only need
to add `ImapUid` accounting for the current folder. `Message` objects
etc. have already been created.
"""
flags = crispin_client.flags(uids)
with syncmanager_lock:
with mailsync_session_scope() as db_session:
# Since we prioritize download for messages in certain threads, we
# may already have ImapUid entries despite calling this method.
local_folder_uids = {uid for uid, in
db_session.query(ImapUid.msg_uid).join(Folder)
.filter(
ImapUid.account_id ==
crispin_client.account_id,
Folder.name ==
crispin_client.selected_folder_name,
ImapUid.msg_uid.in_(uids))}
uids = [uid for uid in uids if uid not in local_folder_uids]
if uids:
acc = db_session.query(GmailAccount).get(
crispin_client.account_id)
# collate message objects to relate the new imapuids to
imapuid_for = dict([(metadata.msgid, uid) for (uid, metadata)
in remote_g_metadata.items()
if uid in uids])
imapuid_g_msgids = [remote_g_metadata[uid].msgid for uid in
uids]
message_for = dict([(imapuid_for[m.g_msgid], m) for m in
db_session.query(Message).join(ImapThread)
.filter(
Message.g_msgid.in_(imapuid_g_msgids),
ImapThread.namespace_id ==
acc.namespace.id)])
# Stop Folder.find_or_create()'s query from triggering a flush.
with db_session.no_autoflush:
new_imapuids = [ImapUid(
account=acc,
folder=Folder.find_or_create(
db_session, acc,
crispin_client.selected_folder_name),
msg_uid=uid, message=message_for[uid]) for uid in uids
if uid in message_for]
for item in new_imapuids:
# skip uids which have disappeared in the meantime
if item.msg_uid in flags:
item.update_flags_and_labels(
flags[item.msg_uid].flags,
flags[item.msg_uid].labels)
db_session.add_all(new_imapuids)
db_session.commit()
示例9: add_new_imapuid
# 需要导入模块: from inbox.models import Folder [as 别名]
# 或者: from inbox.models.Folder import find_or_create [as 别名]
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)
示例10: __init__
# 需要导入模块: from inbox.models import Folder [as 别名]
# 或者: from inbox.models.Folder import find_or_create [as 别名]
def __init__(self, account_id):
self.account_id = account_id
self.log = get_logger()
self.log.bind(account_id=account_id)
with session_scope() as db_session:
account = db_session.query(ImapAccount).get(self.account_id)
self.email_address = account.email_address
self.provider_name = account.provider
self.sender_name = account.name
self.smtp_endpoint = account.smtp_endpoint
if account.sent_folder is None:
# account has no detected sent folder - create one.
sent_folder = Folder.find_or_create(db_session, account,
'sent', 'sent')
account.sent_folder = sent_folder
self.sent_folder = account.sent_folder.name
self.auth_type = provider_info(self.provider_name,
self.email_address)['auth']
if self.auth_type == 'oauth2':
try:
self.auth_token = account.access_token
except OAuthError:
raise SendMailException('Error logging in.')
else:
assert self.auth_type == 'password'
self.auth_token = account.password
示例11: create_db_objects
# 需要导入模块: from inbox.models import Folder [as 别名]
# 或者: from inbox.models.Folder import find_or_create [as 别名]
def create_db_objects(account_id, db_session, log, folder_name, raw_messages,
msg_create_fn, canonical_name=None, identifier=None):
new_uids = []
# TODO: Detect which namespace to add message to. (shared folders)
# Look up message thread,
acc = db_session.query(Account).get(account_id)
folder = Folder.find_or_create(db_session, acc, folder_name,
canonical_name, identifier)
for msg in raw_messages:
uid = msg_create_fn(db_session, acc, folder, msg)
# Must ensure message objects are flushed because they reference
# threads, which may be new, and later messages may need to belong to
# the same thread. If we don't flush here and disable autoflush within
# the message creation to avoid flushing incomplete messages, we can't
# query for the (uncommitted) new thread id.
#
# We should probably refactor this later to use provider-specific
# Message constructors to avoid creating incomplete objects in the
# first place.
db_session.add(uid)
db_session.flush()
if uid is not None:
new_uids.append(uid)
# imapuid, message, thread, labels
return new_uids
示例12: save_sent_email
# 需要导入模块: from inbox.models import Folder [as 别名]
# 或者: from inbox.models.Folder import find_or_create [as 别名]
def save_sent_email(account_id, message_id, db_session):
"""
Create an email on the remote backend. Only used to work
around providers who don't save sent messages themselves
(I'm looking at you, iCloud).
"""
account = db_session.query(Account).get(account_id)
message = db_session.query(Message).get(message_id)
if message is None:
log.info('tried to create nonexistent message',
message_id=message_id, account_id=account_id)
return
create_backend_sent_folder = False
if account.sent_folder is None:
# account has no detected drafts folder - create one.
sent_folder = Folder.find_or_create(db_session, account,
'Sent', 'sent')
account.sent_folder = sent_folder
create_backend_sent_folder = True
mimemsg = _create_email(account, message)
remote_save_sent = module_registry[account.provider].remote_save_sent
remote_save_sent(account, account.sent_folder.name,
mimemsg, message.created_at,
create_backend_sent_folder)
示例13: save_draft
# 需要导入模块: from inbox.models import Folder [as 别名]
# 或者: from inbox.models.Folder import find_or_create [as 别名]
def save_draft(account_id, message_id, db_session, args):
""" Sync a new/updated draft back to the remote backend. """
account = db_session.query(Account).get(account_id)
message = db_session.query(Message).get(message_id)
version = args.get('version')
if message is None:
log.info('tried to save nonexistent message as draft',
message_id=message_id, account_id=account_id)
return
if not message.is_draft:
log.warning('tried to save non-draft message as draft',
message_id=message_id,
account_id=account_id)
return
if version != message.version:
log.warning('tried to save outdated version of draft')
return
if account.drafts_folder is None:
# account has no detected drafts folder - create one.
drafts_folder = Folder.find_or_create(db_session, account, 'Drafts',
'drafts')
account.drafts_folder = drafts_folder
mimemsg = _create_email(account, message)
remote_save_draft = module_registry[account.provider].remote_save_draft
remote_save_draft(account, account.drafts_folder.name,
mimemsg, db_session, message.created_at)
示例14: save_draft
# 需要导入模块: from inbox.models import Folder [as 别名]
# 或者: from inbox.models.Folder import find_or_create [as 别名]
def save_draft(account_id, message_id, db_session):
""" Sync a new/updated draft back to the remote backend. """
account = db_session.query(Account).get(account_id)
message = db_session.query(Message).get(message_id)
if message is None:
log.info('tried to save nonexistent message as draft',
message_id=message_id, account_id=account_id)
return
if not message.is_draft:
log.warning('tried to save non-draft message as draft',
message_id=message_id,
account_id=account_id)
return
recipients = Recipients(message.to_addr, message.cc_addr,
message.bcc_addr)
blocks = [p.block for p in message.attachments]
attachments = generate_attachments(blocks)
mimemsg = create_email(account.name, account.email_address,
message.inbox_uid, recipients, message.subject,
message.sanitized_body, attachments)
if account.drafts_folder is None:
# account has no detected drafts folder - create one.
drafts_folder = Folder.find_or_create(db_session, account,
'Drafts', 'drafts')
account.drafts_folder = drafts_folder
remote_save_draft = module_registry[account.provider].remote_save_draft
remote_save_draft(account, account.drafts_folder.name,
mimemsg.to_string(), message.created_at)
示例15: save_folder_names
# 需要导入模块: from inbox.models import Folder [as 别名]
# 或者: from inbox.models.Folder import find_or_create [as 别名]
def save_folder_names(self, db_session, raw_folders):
"""
Save the folders, labels present on the remote backend for an account.
* Create Folder/ Label objects.
* Delete Folders/ Labels that no longer exist on the remote.
Notes
-----
Gmail uses IMAP folders and labels.
Canonical folders ('all', 'trash', 'spam') are therefore mapped to both
Folder and Label objects, everything else is created as a Label only.
We don't canonicalize names to lowercase when saving because
different backends may be case-sensitive or otherwise - code that
references saved names should canonicalize if needed when doing
comparisons.
"""
account = db_session.query(Account).get(self.account_id)
remote_label_names = {l.display_name.rstrip()[:MAX_LABEL_NAME_LENGTH] for l in raw_folders}
assert "all" in {f.role for f in raw_folders}, "Account {} has no detected All Mail folder".format(
account.email_address
)
local_labels = {l.name: l for l in db_session.query(Label).filter(Label.account_id == self.account_id).all()}
# Delete labels no longer present on the remote.
# Note that the label with canonical_name='all' cannot be deleted;
# remote_label_names will always contain an entry corresponding to it.
discard = set(local_labels) - set(remote_label_names)
for name in discard:
log.info("Label deleted from remote", account_id=self.account_id, name=name)
db_session.delete(local_labels[name])
# Create new labels, folders
for raw_folder in raw_folders:
Label.find_or_create(db_session, account, raw_folder.display_name, raw_folder.role)
if raw_folder.role in ("all", "spam", "trash"):
folder = Folder.find_or_create(db_session, account, raw_folder.display_name, raw_folder.role)
if folder.name != raw_folder.display_name:
log.info(
"Folder name changed on remote",
account_id=self.account_id,
role=raw_folder.role,
new_name=raw_folder.display_name,
name=folder.name,
)
folder.name = raw_folder.display_name
# Ensure sync_should_run is True for the folders we want to sync (for
# Gmail, that's just all folders, since we created them above if
# they didn't exist.)
for folder in account.folders:
if folder.imapsyncstatus:
folder.imapsyncstatus.sync_should_run = True
db_session.commit()