Exemplo n.º 1
0
    def message(self):
        if self._message is not None:
            return self._message

        community = self._community
        request = self.request
        profile = self.profile
        blogentry = self._blogentry

        community_href = resource_url(community, request)
        blogentry_href = resource_url(blogentry, request)
        manage_preferences_href = resource_url(profile, request)
        system_name = get_setting(self.context, "system_name", "KARL")
        system_email_domain = get_setting(self.context, "system_email_domain")

        reply_to = '"%s" <%s+blog-%s@%s>' % (community.title,
                                           community.__name__,
                                           docid_to_hex(blogentry.docid),
                                           system_email_domain)

        attachments, attachment_links, attachment_hrefs = self.attachments

        body_template = get_renderer(self._template).implementation()
        from_name = "%s | %s" % (self.creator.title, system_name)
        msg = MIMEMultipart() if attachments else Message()
        msg["From"] = '"%s" <%s>' % (from_name, self.mfrom)
        msg["To"] = '"%s" <%s>' % (profile.title, profile.email)
        msg["Reply-to"] = reply_to
        msg["Subject"] = self._subject
        msg["Precedence"] = 'bulk'
        body_text = body_template(
            context=self.context,
            community=community,
            community_href=community_href,
            blogentry=blogentry,
            blogentry_href=blogentry_href,
            attachments=attachment_links,
            attachment_hrefs=attachment_hrefs,
            manage_preferences_href=manage_preferences_href,
            profile=profile,
            profiles=self.profiles,
            creator=self.creator,
            digest=self.digest,
            alert=self,
            history=self._history,
        )

        if self.digest:
            # Only interested in body for digest
            html = document_fromstring(body_text)
            body_element = html.cssselect('body')[0]
            span = etree.Element("span", nsmap=body_element.nsmap)
            span[:] = body_element[:] # Copy all body elements to an empty span
            body_text = etree.tostring(span, pretty_print=True)

        if isinstance(body_text, unicode):
            body_text = body_text.encode('utf-8')

        if attachments:
            body = MIMEText(body_text, 'html', 'utf-8')
            msg.attach(body)
            for attachment in attachments:
                msg.attach(attachment)
        else:
            msg.set_payload(body_text, 'utf-8')
            msg.set_type("text/html")

        self._message = msg

        return self._message
Exemplo n.º 2
0
 def test_docid_to_hex(self):
     from karl.utils import docid_to_hex
     from karl.utils import _MAX_32BIT_INT
     self.assertEqual(docid_to_hex(0), '7FFFFFFF')
     self.assertEqual(docid_to_hex(_MAX_32BIT_INT), 'FFFFFFFE')
     self.assertEqual(docid_to_hex(-_MAX_32BIT_INT), '00000000')
Exemplo n.º 3
0
 def test_docid_to_hex(self):
     from karl.utils import docid_to_hex
     from karl.utils import _MAX_32BIT_INT
     self.assertEqual(docid_to_hex(0), '7FFFFFFF')
     self.assertEqual(docid_to_hex(_MAX_32BIT_INT), 'FFFFFFFE')
     self.assertEqual(docid_to_hex(-_MAX_32BIT_INT), '00000000')
Exemplo n.º 4
0
    def message(self):
        if self._message is not None:
            return self._message

        community = self._community
        request = self.request
        profile = self.profile
        blogentry = self._blogentry

        community_href = model_url(community, request)
        blogentry_href = model_url(blogentry, request)
        manage_preferences_href = model_url(profile, request)
        system_name = get_setting(self.context, "system_name", "KARL")
        system_email_domain = get_setting(self.context, "system_email_domain")

        reply_to = "%s <%s+blog-%s@%s>" % (community.title.replace(',', ''),
                                           community.__name__,
                                           docid_to_hex(blogentry.docid),
                                           system_email_domain)

        attachments = []
        attachment_links = []
        attachment_hrefs = {}
        for name,model in self._attachments.items():
            if profile.alert_attachments == 'link':
                attachment_links.append(name)
                attachment_hrefs[name] = model_url(model, request)

            elif profile.alert_attachments == 'attach':
                with model.blobfile.open() as f:
                    f.seek(0, 2)
                    size = f.tell()
                    if size > MAX_ATTACHMENT_SIZE:
                        attachment_links.append(name)
                        attachment_hrefs[name] = model_url(model, request)

                    else:
                        f.seek(0, 0)
                        data = f.read()
                        type, subtype = model.mimetype.split('/', 1)
                        attachment = MIMEBase(type, subtype)
                        attachment.set_payload(data)
                        Encoders.encode_base64(attachment)
                        attachment.add_header(
                            'Content-Disposition',
                            'attachment; filename="%s"' % model.filename)
                        attachments.append(attachment)

        body_template = get_template(self._template)
        from_name = "%s | %s" % (self.creator.title, system_name)
        msg = MIMEMultipart() if attachments else Message()
        msg["From"] = "%s <%s>" % (from_name, self.mfrom)
        msg["To"] = "%s <%s>" % (profile.title, profile.email)
        msg["Reply-to"] = reply_to
        msg["Subject"] = self._subject
        body_text = body_template(
            context=self.context,
            community=community,
            community_href=community_href,
            blogentry=blogentry,
            blogentry_href=blogentry_href,
            attachments=attachment_links,
            attachment_hrefs=attachment_hrefs,
            manage_preferences_href=manage_preferences_href,
            profile=profile,
            profiles=self.profiles,
            creator=self.creator,
            digest=self.digest,
            alert=self,
            history=self._history,
        )

        if self.digest:
            # Only interested in body for digest
            html = document_fromstring(body_text)
            body_element = html.cssselect('body')[0]
            span = etree.Element("span", nsmap=body_element.nsmap)
            span[:] = body_element[:] # Copy all body elements to an empty span
            body_text = etree.tostring(span, pretty_print=True)

        if isinstance(body_text, unicode):
            body_text = body_text.encode('utf-8')

        if attachments:
            body = MIMEText(body_text, 'html', 'utf-8')
            msg.attach(body)
            for attachment in attachments:
                msg.attach(attachment)
        else:
            msg.set_payload(body_text, 'utf-8')
            msg.set_type("text/html")

        self._message = msg

        return self._message
Exemplo n.º 5
0
    def message(self):
        if self._message is not None:
            return self._message

        community = self._community
        request = self.request
        profile = self.profile
        blogentry = self._blogentry

        community_href = resource_url(community, request)
        blogentry_href = resource_url(blogentry, request)
        manage_preferences_href = resource_url(
            profile, request) + '/manage_communities.html'  # noqa
        system_name = get_setting(self.context, "title", "KARL")
        system_email_domain = get_setting(self.context, "system_email_domain")

        reply_to = '"%s" <%s+blog-%s@%s>' % (
            community.title, community.__name__, docid_to_hex(
                blogentry.docid), system_email_domain)

        attachments, attachment_links, attachment_hrefs = self.attachments

        from_name = "%s | %s" % (self.creator.title, system_name)
        msg = MIMEMultipart() if attachments else Message()
        msg["From"] = '"%s" <%s>' % (from_name, self.mfrom)
        msg["To"] = '"%s" <%s>' % (profile.title, profile.email)
        msg["Reply-to"] = reply_to
        msg["Subject"] = self._subject
        msg["Precedence"] = 'bulk'
        body_text = self.template(
            context=self.context,
            community=community,
            community_href=community_href,
            blogentry=blogentry,
            blogentry_href=blogentry_href,
            attachments=attachment_links,
            attachment_hrefs=attachment_hrefs,
            manage_preferences_href=manage_preferences_href,
            profile=profile,
            profiles=self.profiles,
            creator=self.creator,
            digest=self.digest,
            alert=self,
            history=self._history,
            reply_enabled=self.reply_enabled)

        if self.digest:
            # Only interested in body for digest
            html = document_fromstring(body_text)
            body_element = html.cssselect('body')[0]
            span = etree.Element("span", nsmap=body_element.nsmap)
            span[:] = body_element[:]  # Copy all body elements to an empty span
            body_text = etree.tostring(span, pretty_print=True)

        if isinstance(body_text, unicode):
            body_text = body_text.encode('utf-8')

        if attachments:
            body = MIMEText(body_text, 'html', 'utf-8')
            msg.attach(body)
            for attachment in attachments:
                msg.attach(attachment)
        else:
            msg.set_payload(body_text, 'utf-8')
            msg.set_type("text/html")

        self._message = msg

        return self._message