def test_category_sanitize_name(): assert sanitize_name(42) == u"42" assert sanitize_name("42") == u"42" assert sanitize_name(u" Boîte de réception ") == u" Boîte de réception" long_name = "N" * (MAX_INDEXABLE_LENGTH + 10) assert sanitize_name(long_name) == "N" * MAX_INDEXABLE_LENGTH long_name = "N" * (MAX_INDEXABLE_LENGTH - 2) + " " assert sanitize_name(long_name) == "N" * (MAX_INDEXABLE_LENGTH - 2)
def test_category_sanitize_name(): assert sanitize_name(42) == u'42' assert sanitize_name('42') == u'42' assert sanitize_name(u' Boîte de réception ') ==\ u' Boîte de réception' long_name = 'N' * (MAX_INDEXABLE_LENGTH + 10) assert sanitize_name(long_name) == 'N' * MAX_INDEXABLE_LENGTH long_name = 'N' * (MAX_INDEXABLE_LENGTH - 2) + ' ' assert sanitize_name(long_name) == 'N' * (MAX_INDEXABLE_LENGTH - 2)
def validate_name(self, key, name): sanitized_name = sanitize_name(name) if sanitized_name != name: log.warning("Truncating label name for account", account_id=self.account_id, name=name) return sanitized_name
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 = {sanitize_name(f.display_name) 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 = Folder.find_or_create(db_session, account, raw_folder.display_name, raw_folder.role) if folder.canonical_name != raw_folder.role: folder.canonical_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()
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 = { sanitize_name(f.display_name) 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 = Folder.find_or_create(db_session, account, raw_folder.display_name, raw_folder.role) if folder.canonical_name != raw_folder.role: folder.canonical_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()