def get_context_data(self, **kwargs): if "all-headers" in self.request.GET: headers = None headers_fetch_all = bool(int(self.request.GET["all-headers"])) else: headers = cache.get(self.object.id, version="email-header") headers_fetch_all = bool(self.object.flags.view_all_headers) if headers is None: headers = models.Header.objects.filter(part__email=self.object, part__parent=None) if headers_fetch_all: headers = headers.get_many() else: headers = headers.get_many("Subject", "From") email_dict = {} email_dict["headers"] = headers email_dict["date"] = self.object.received_date email_dict["inbox"] = self.object.inbox email_dict["eid"] = self.object.eid # GET params for users with `ask_image` set in their profile if "imgDisplay" in self.request.GET and int(self.request.GET["imgDisplay"]) == 1: email_dict["display_images"] = True email_dict["ask_images"] = False elif self.request.user.userprofile.flags.ask_images: email_dict["display_images"] = False email_dict["ask_images"] = True else: email_dict["display_images"] = self.request.user.userprofile.flags.display_images email_dict["ask_images"] = False # iterate over MIME parts attachments = self.object.get_parts() email_dict["bodies"] = [] email_dict["has_images"] = False find_bodies(self.request, email_dict, attachments[:1]) for body in email_dict["bodies"]: assert isinstance(body, unicode), "body is %r" % type(body) context = super(EmailView, self).get_context_data(**kwargs) context.update({ "email": email_dict, "attachments": attachments, "headersfetchall": headers_fetch_all, }) self._has_images = email_dict["display_images"] return context
def test_find_bodies_with_bad_mime_tree(self): email = factories.EmailFactory() body = factories.BodyFactory(data=b"This mail body is searchable") # build 1 body and use that # the root part, multipart/alternative root_part = factories.PartListFactory(email=email, body=body) factories.HeaderFactory(part=root_part, name="Content-Type", data="multipart/alternative") # first text part alt1_part = factories.PartListFactory(email=email, body=body, parent=root_part) factories.HeaderFactory(part=alt1_part, name="Content-Type", data="text/plain; charset=\"ascii\"") # second text part alt2_part = factories.PartListFactory(email=email, body=body, parent=root_part) factories.HeaderFactory(part=alt2_part, name="Content-Type", data="text/plain; charset=\"ascii\"") # make first text part invalid by giving it a child alt1_child_part = factories.PartListFactory(email=email, body=body, parent=alt1_part) factories.HeaderFactory(part=alt1_child_part, name="Content-Type", data="text/plain; charset=\"ascii\"") # find_bodies returns a list of lists, so flatten it out bodies = [part for part_list in email_utils.find_bodies(email.get_parts()) for part in part_list] # we should only see one part self.assertEqual(len(bodies), 1) # and it should be a leaf node self.assertTrue(bodies[0].is_leaf_node()) self.assertEqual(bodies[0], alt2_part)
def get_description(self, obj): """Fetch first text/plain body for obj, reading up to `trunc_to_size` bytes """ bodies = six.next(find_bodies(obj.get_parts())) if bodies is not None: return choose_body(bodies)[:self.trunc_to_size] else: return u""
def get_context_data(self, **kwargs): if "all-headers" in self.request.GET: headers = None headers_fetch_all = bool(int(self.request.GET["all-headers"])) else: headers = cache.get(self.object.id, version="email-header") headers_fetch_all = bool(self.object.flags.view_all_headers) if headers is None: headers = models.Header.objects.filter(part__email=self.object, part__parent=None) if headers_fetch_all: headers = headers.get_many() else: headers = headers.get_many("Subject", "From") email_dict = {} email_dict["headers"] = headers email_dict["date"] = self.object.received_date email_dict["inbox"] = self.object.inbox email_dict["eid"] = self.object.eid # GET params for users with `ask_image` set in their profile if "imgDisplay" in self.request.GET and int( self.request.GET["imgDisplay"]) == 1: email_dict["display_images"] = True email_dict["ask_images"] = False elif self.request.user.inboxenprofile.flags.ask_images: email_dict["display_images"] = False email_dict["ask_images"] = True else: email_dict[ "display_images"] = self.request.user.inboxenprofile.flags.display_images email_dict["ask_images"] = False # iterate over MIME parts root_part = self.object.get_parts() email_dict["bodies"] = [] email_dict["has_images"] = False email_dict["bodies"] = [ render_body(self.request, email_dict, parts) for parts in find_bodies(root_part) ] context = super(EmailView, self).get_context_data(**kwargs) context.update({ "email": email_dict, "attachments": root_part, "headersfetchall": headers_fetch_all, }) self._has_images = email_dict["display_images"] return context
def get_content(self, obj): """Fetch all text/plain bodies for obj, reading up to `trunc_to_size` bytes and excluding those that would not be displayed """ data = [] size = 0 for parts in find_bodies(obj.get_parts()): remains = self.trunc_to_size - size if remains <= 0: break body = choose_body(parts)[:remains] size += len(body) data.append(body) return u"\n".join(data)
def get_context_data(self, **kwargs): if "all-headers" in self.request.GET: headers = None headers_fetch_all = bool(int(self.request.GET["all-headers"])) else: headers = cache.get(self.object.id, version="email-header") headers_fetch_all = self.object.view_all_headers if headers is None: headers = models.Header.objects.filter(part__email=self.object, part__parent=None) if headers_fetch_all: headers = headers.get_many() else: headers = headers.get_many("Subject", "From") email_dict = {} email_dict["headers"] = headers email_dict["date"] = self.object.received_date email_dict["inbox"] = self.object.inbox email_dict["eid"] = self.object.eid # GET params for users with `ask_image` set in their profile if "imgDisplay" in self.request.GET and int(self.request.GET["imgDisplay"]) == 1: email_dict["display_images"] = True email_dict["ask_images"] = False elif self.request.user.inboxenprofile.display_images == models.UserProfile.ASK: email_dict["display_images"] = False email_dict["ask_images"] = True else: email_dict["display_images"] = self.request.user.inboxenprofile.display_images == models.UserProfile.DISPLAY email_dict["ask_images"] = False # iterate over MIME parts root_part = self.object.get_parts() email_dict["bodies"] = [] email_dict["has_images"] = False email_dict["bodies"] = [render_body(self.request, email_dict, parts) for parts in find_bodies(root_part)] context = super(EmailView, self).get_context_data(**kwargs) context.update({ "email": email_dict, "attachments": root_part, "headersfetchall": headers_fetch_all, }) self._has_images = email_dict["display_images"] return context