本文整理汇总了Python中inbox.models.Folder类的典型用法代码示例。如果您正苦于以下问题:Python Folder类的具体用法?Python Folder怎么用?Python Folder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Folder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_raw_folder_change
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
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
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
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: save_folder_names
def save_folder_names(log, account, folder_names, db_session):
"""
Create Folder objects & map special folder names on Account objects.
Folders that belong to an account and no longer exist in `folder_names`
ARE DELETED.
"""
# NOTE: We don't do anything like canonicalizing to lowercase because
# different backends may be case-sensitive or not. Code that references
# saved folder names should canonicalize if needed when doing comparisons.
assert 'inbox' in folder_names, 'Account {} has no detected inbox folder'\
.format(account.email_address)
folders = {f.name.lower(): f for f in
db_session.query(Folder).filter_by(account=account)}
for canonical_name in ['inbox', 'drafts', 'sent', 'spam', 'trash',
'starred', 'important', 'archive', 'all']:
if canonical_name in folder_names:
backend_folder_name = folder_names[canonical_name].lower()
if backend_folder_name not in folders:
folder = Folder.create(account, folder_names[canonical_name],
db_session,
canonical_name)
attr_name = '{}_folder'.format(canonical_name)
setattr(account, attr_name, verify_folder_name(
account.id, getattr(account, attr_name), folder))
else:
del folders[backend_folder_name]
# Gmail labels, user-created IMAP/EAS folders, etc.
if 'extra' in folder_names:
for name in folder_names['extra']:
name = name[:MAX_FOLDER_NAME_LENGTH]
if name.lower() not in folders:
folder = Folder.create(account, name, db_session)
db_session.add(folder)
if name.lower() in folders:
del folders[name.lower()]
# This may cascade to FolderItems and ImapUid (ONLY), which is what we
# want--doing the update here short-circuits us syncing that change later.
log.info("Folders were deleted from the remote: {}".format(folders.keys()))
for folder in folders.values():
db_session.delete(folder)
# TODO(emfree) delete associated tag
# Create associated tags for any new folders.
for folder in account.folders:
folder.get_associated_tag(db_session)
db_session.commit()
示例6: test_imap_search_unicode
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']
示例7: test_gmail_search_unicode
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: test_gmail_search_unicode
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']
示例9: save_sent_email
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)
示例10: save_draft
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)
示例11: add_new_imapuids
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()
示例12: create_db_objects
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
示例13: download_and_commit_uids
def download_and_commit_uids(self, crispin_client, uids):
start = datetime.utcnow()
raw_messages = crispin_client.uids(uids)
if not raw_messages:
return
new_uids = set()
with self.syncmanager_lock:
with session_scope() as db_session:
account = Account.get(self.account_id, db_session)
folder = Folder.get(self.folder_id, db_session)
raw_messages = self.__deduplicate_message_object_creation(
db_session, raw_messages, account)
if not raw_messages:
return 0
for msg in raw_messages:
uid = self.create_message(db_session, account, folder,
msg)
if uid is not None:
db_session.add(uid)
db_session.commit()
new_uids.add(uid)
log.info('Committed new UIDs',
new_committed_message_count=len(new_uids))
# If we downloaded uids, record message velocity (#uid / latency)
if self.state == "initial" and len(new_uids):
self._report_message_velocity(datetime.utcnow() - start,
len(new_uids))
if self.is_first_message:
self._report_first_message()
self.is_first_message = False
self.saved_uids.update(new_uids)
示例14: __init__
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
示例15: download_and_commit_uids
def download_and_commit_uids(self, crispin_client, uids):
start = datetime.utcnow()
raw_messages = crispin_client.uids(uids)
if not raw_messages:
return 0
new_uids = set()
with self.syncmanager_lock:
with session_scope(self.namespace_id) as db_session:
account = Account.get(self.account_id, db_session)
folder = Folder.get(self.folder_id, db_session)
for msg in raw_messages:
uid = self.create_message(db_session, account,
folder, msg)
if uid is not None:
db_session.add(uid)
db_session.flush()
new_uids.add(uid)
db_session.commit()
log.debug('Committed new UIDs', new_committed_message_count=len(new_uids))
# If we downloaded uids, record message velocity (#uid / latency)
if self.state == 'initial' and len(new_uids):
self._report_message_velocity(datetime.utcnow() - start,
len(new_uids))
if self.is_first_message:
self._report_first_message()
self.is_first_message = False
return len(new_uids)