def add_fake_label(db_session, default_account, display_name='My Label', name=None): from inbox.models.label import Label return Label.find_or_create(db_session, default_account, display_name, name)
def update_labels(self, new_labels): # TODO(emfree): This is all mad complicated. Simplify if possible? # Gmail IMAP doesn't use the normal IMAP \\Draft flag. Silly Gmail # IMAP. self.is_draft = '\\Draft' in new_labels self.is_starred = '\\Starred' in new_labels category_map = { '\\Inbox': 'inbox', '\\Important': 'important', '\\Sent': 'sent' } remote_labels = set() for label in new_labels: if label in ('\\Draft', '\\Starred'): continue elif label in category_map: remote_labels.add((category_map[label], category_map[label])) else: remote_labels.add((label, None)) local_labels = {(l.name, l.canonical_name) for l in self.labels} remove = local_labels - remote_labels add = remote_labels - local_labels with object_session(self).no_autoflush: for name, canonical_name in remove: label = Label.find_or_create(object_session(self), self.account, name, canonical_name) self.labels.remove(label) for name, canonical_name in add: label = Label.find_or_create(object_session(self), self.account, name, canonical_name) self.labels.add(label)
def test_deleted_labels_get_gced(empty_db, default_account, thread, message, imapuid, folder): # Check that only the labels without messages attached to them # get deleted. default_namespace = default_account.namespace # Create a label w/ no messages attached. label = Label.find_or_create(empty_db.session, default_account, "dangling label") label.deleted_at = datetime.utcnow() label.category.deleted_at = datetime.utcnow() label_id = label.id empty_db.session.commit() # Create a label with attached messages. msg_uid = imapuid.msg_uid update_metadata( default_account.id, folder.id, folder.canonical_name, {msg_uid: GmailFlags((), ("label", ), None)}, empty_db.session, ) label_ids = [] for cat in message.categories: for l in cat.labels: label_ids.append(l.id) handler = DeleteHandler( account_id=default_account.id, namespace_id=default_namespace.id, provider_name=default_account.provider, uid_accessor=lambda m: m.imapuids, message_ttl=0, ) handler.gc_deleted_categories() empty_db.session.commit() # Check that the first label got gc'ed marked_deleted = empty_db.session.query(Label).get(label_id) assert marked_deleted is None # Check that the other labels didn't. for label_id in label_ids: assert empty_db.session.query(Label).get(label_id) is not None
def update_labels(self, new_labels): # TODO(emfree): This is all mad complicated. Simplify if possible? # Gmail IMAP doesn't use the normal IMAP \\Draft flag. Silly Gmail # IMAP. self.is_draft = "\\Draft" in new_labels self.is_starred = "\\Starred" in new_labels category_map = { "\\Inbox": "inbox", "\\Important": "important", "\\Sent": "sent", "\\Trash": "trash", "\\Spam": "spam", "\\All": "all", } remote_labels = set() for label in new_labels: if label in ("\\Draft", "\\Starred"): continue elif label in category_map: remote_labels.add((category_map[label], category_map[label])) else: remote_labels.add((label, None)) local_labels = {(lbl.name, lbl.canonical_name): lbl for lbl in self.labels} remove = set(local_labels) - remote_labels add = remote_labels - set(local_labels) with object_session(self).no_autoflush: for key in remove: self.labels.remove(local_labels[key]) for name, canonical_name in add: label = Label.find_or_create(object_session(self), self.account, name, canonical_name) self.labels.add(label)
def test_deleted_labels_get_gced(db, default_account, thread, message, imapuid, folder): # Check that only the labels without messages attached to them # get deleted. default_namespace = default_account.namespace # Create a label w/ no messages attached. label = Label.find_or_create(db.session, default_account, 'dangling label') label.deleted_at = datetime.utcnow() label.category.deleted_at = datetime.utcnow() label_id = label.id db.session.commit() # Create a label with attached messages. msg_uid = imapuid.msg_uid update_metadata(default_account.id, folder.id, folder.canonical_name, {msg_uid: GmailFlags((), ('label',), None)}, db.session) label_ids = [] for cat in message.categories: for l in cat.labels: label_ids.append(l.id) handler = DeleteHandler(account_id=default_account.id, namespace_id=default_namespace.id, provider_name=default_account.provider, uid_accessor=lambda m: m.imapuids, message_ttl=0) handler.gc_deleted_categories() db.session.commit() # Check that the first label got gc'ed marked_deleted = db.session.query(Label).get(label_id) assert marked_deleted is None # Check that the other labels didn't. for label_id in label_ids: assert db.session.query(Label).get(label_id) is not None
def custom_label(db, default_account): from inbox.models import Label return Label.find_or_create(db.session, default_account, 'Kraftwerk', '')
def label(db, default_account): from inbox.models import Label return Label.find_or_create(db.session, default_account, 'Inbox', 'inbox')