class Email(models.Model): """Email model eid is a convience property that outputs a hexidec ID flags is a BitField for flags such as deleted, read, etc. The body and headers can be found in the root of the PartList tree on Email.parts """ inbox = models.ForeignKey(Inbox) flags = BitField(flags=("deleted", "read", "seen", "important", "view_all_headers"), default=0) received_date = models.DateTimeField(db_index=True) deleted = models.BooleanField(default=False) read = models.BooleanField(default=False) seen = models.BooleanField(default=False) important = models.BooleanField(default=False) view_all_headers = models.BooleanField(default=False) objects = EmailQuerySet.as_manager() @property def eid(self): """Return a hexidecimal version of ID""" return hex(self.id)[2:].rstrip("L") _bool_label_order = ["read", "seen", "important"] def get_bools_for_labels(self): for key in self._bool_label_order: yield (key, getattr(self, key)) def __str__(self): return u"{0}".format(self.eid) def get_parts(self): """Fetches the MIME tree of the email and returns the root part. All subsequent calls to `part.parent` or `part.get_children()` will not cause additional database queries """ root_parts = self.parts.all().get_cached_trees() assert len( root_parts ) <= 1, "Expected to find a single part, found %s" % len(root_parts) try: return root_parts[0] except IndexError: return None
class Email(SearchableAbstract): """Email model eid is a convience property that outputs a hexidec ID The body and headers can be found in the root of the PartList tree on Email.parts """ inbox = models.ForeignKey(Inbox, on_delete=models.CASCADE) received_date = models.DateTimeField(db_index=True) deleted = models.BooleanField(default=False) read = models.BooleanField(default=False) seen = models.BooleanField(default=False) important = models.BooleanField(default=False) view_all_headers = models.BooleanField(default=False) objects = EmailQuerySet.as_manager() @property def eid(self): """Return a hexidecimal version of ID""" return hex(self.id)[2:].rstrip("L") _bool_label_order = ["read", "seen", "important"] def get_bools_for_labels(self): for key in self._bool_label_order: yield (key, getattr(self, key)) def __str__(self): return u"{0}".format(self.eid) def get_parts(self): """Fetches the MIME tree of the email and returns the root part. All subsequent calls to `part.parent` or `part.get_children()` will not cause additional database queries """ root_parts = self.parts.all().get_cached_trees() assert len( root_parts ) <= 1, "Expected to find a single part, found %s" % len(root_parts) try: return root_parts[0] except IndexError: return None def index_search_a(self): try: subject = HeaderData.objects.filter( header__part__parent__isnull=True, header__name__name="Subject", header__part__email__id=self.id, ).first() return unicode_damnit(subject.data) except AttributeError: return "" def index_search_b(self): try: from_header = HeaderData.objects.filter( header__part__parent__isnull=True, header__name__name="From", header__part__email__id=self.id, ).first() return unicode_damnit(from_header.data) except AttributeError: return "" class Meta: indexes = [GinIndex(fields=["search_tsv"])]