예제 #1
0
    def test_safe_unicode(self):
        self.assertEqual('kjøÔ€ôþâ', safe_unicode('kjøÔ€ôþâ'))
        self.assertEqual('aé‡ae15', safe_unicode(b'a\xe9\x87ae15'))
        self.assertEqual('aé‡ae15', safe_unicode('aé‡ae15'))

        # Custom encoding list
        self.assertEqual('a\ufffdae15', safe_unicode(b'a\xe9\x87ae15', ['utf-8']))
        self.assertEqual('aé‡ae15',     safe_unicode(b'a\xe9\x87ae15', ('cp1252',)))
예제 #2
0
    def test_safe_unicode_object(self):
        class no_unicode_object:
            pass

        class unicode_object:
            def __str__(self):
                return 'aé‡ae15'

        self.assertEqual(
            "<class 'creme.creme_core.tests.utils."
            "test_main.MiscTestCase.test_safe_unicode_object.<locals>.no_unicode_object'>",
            safe_unicode(no_unicode_object)
        )
        self.assertEqual('aé‡ae15', safe_unicode(unicode_object()))
예제 #3
0
    def test_safe_unicode(self):
        # self.assertEqual(u"kjøÔ€ôþâ", safe_unicode(u"kjøÔ€ôþâ"))
        # self.assertEqual(u"aé‡ae15", safe_unicode("a\xe9\x87ae15"))
        # self.assertEqual(u"aé‡ae15", safe_unicode("aé‡ae15"))
        #
        # # Custom encoding list
        # self.assertEqual(u"a\ufffdae15", safe_unicode("a\xe9\x87ae15", ('utf-8',)))
        # self.assertEqual(u"aé‡ae15", safe_unicode("a\xe9\x87ae15", ('cp1252',)))

        # P3K
        self.assertEqual('kjøÔ€ôþâ', safe_unicode('kjøÔ€ôþâ'))
        self.assertEqual('aé‡ae15', safe_unicode(b'a\xe9\x87ae15'))
        self.assertEqual('aé‡ae15', safe_unicode('aé‡ae15'))

        # Custom encoding list
        self.assertEqual(u'a\ufffdae15',
                         safe_unicode(b'a\xe9\x87ae15', ['utf-8']))
        self.assertEqual('aé‡ae15', safe_unicode(b'a\xe9\x87ae15',
                                                 ('cp1252', )))
예제 #4
0
    def test_safe_unicode_object(self):
        # class no_unicode_object:
        #     pass
        #
        # class unicode_object:
        #     def __unicode__(self):
        #         return u"aé‡ae15"
        #
        # class false_unicode_object:
        #     def __init__(self, text):
        #         self.text = text
        #
        #     def __unicode__(self):
        #         return self.text
        #
        # self.assertEqual(u"<class 'creme.creme_core.tests.utils.test_main.no_unicode_object'>",
        #                  safe_unicode(no_unicode_object)
        #                 )
        # self.assertEqual(u"aé‡ae15", safe_unicode(unicode_object()))
        # self.assertEqual(u"aé‡ae15", safe_unicode(false_unicode_object(u"aé‡ae15")))
        # self.assertEqual(u"aé‡ae15", safe_unicode(false_unicode_object("a\xe9\x87ae15")))

        # P3K
        class no_unicode_object:
            pass

        class unicode_object:
            def __str__(self):
                return u'aé‡ae15'

        # class false_unicode_object:
        #     def __init__(self, text):
        #         self.text = text
        #
        #     def __str__(self):
        #         return self.text

        self.assertEqual(
            "<class 'creme.creme_core.tests.utils."
            "test_main.MiscTestCase.test_safe_unicode_object.<locals>.no_unicode_object'>",
            safe_unicode(no_unicode_object))
        self.assertEqual('aé‡ae15', safe_unicode(unicode_object()))
예제 #5
0
    def fetch(self,
              delete=True):  # TODO: args read from configuration instead ?
        client = None
        emails = []

        CREME_GET_EMAIL_SERVER = settings.CREME_GET_EMAIL_SERVER
        CREME_GET_EMAIL_PORT = settings.CREME_GET_EMAIL_PORT

        try:
            if settings.CREME_GET_EMAIL_SSL:
                client = poplib.POP3_SSL(
                    CREME_GET_EMAIL_SERVER,
                    CREME_GET_EMAIL_PORT,
                    settings.CREME_GET_EMAIL_SSL_KEYFILE,
                    settings.CREME_GET_EMAIL_SSL_CERTFILE,
                )
            else:
                client = poplib.POP3(CREME_GET_EMAIL_SERVER,
                                     CREME_GET_EMAIL_PORT)

            client.user(settings.CREME_GET_EMAIL_USERNAME)
            client.pass_(settings.CREME_GET_EMAIL_PASSWORD)

            client.stat()  # TODO: useful ?
            response, messages, total_size = client.list()
        except Exception:  # TODO: Define better exception
            logger.exception("PopFetcher.fetch: POP connection error")

            if client is not None:
                client.quit()

            return []

        getaddresses = email.utils.getaddresses
        parsedate = email.utils.parsedate

        for msg_info in messages:
            attachments = []
            message_number, message_size = msg_info.split()
            r, raw_message_lines, message_size = client.retr(
                int(message_number))

            out_str = b'\n'.join(raw_message_lines)
            out_str = re.sub(b'\r(?!=\n)', b'\r\n', out_str)

            email_message = email.message_from_bytes(out_str)
            get_all = email_message.get_all

            to_emails = [
                addr for name, addr in getaddresses(get_all('to', []))
            ]
            from_emails = [
                addr for name, addr in getaddresses(get_all('from', []))
            ]
            cc_emails = [
                addr for name, addr in getaddresses(get_all('cc', []))
            ]

            subject = ''.join(
                s.decode(enc) if enc is not None else safe_unicode(s)
                for s, enc in email.header.decode_header(
                    email_message.get('subject', [])))

            dates = [
                datetime(*parsedate(d)[:-3]) for d in get_all('date', [])
                if d is not None
            ]

            body_html = ''
            body = ''
            # CONTENT HTML / PLAIN
            if email_message.is_multipart():
                for part in email_message.walk():
                    payload = part.get_payload(decode=True)

                    mct = part.get_content_maintype()
                    cst = part.get_content_subtype()

                    if mct == 'multipart':
                        continue

                    filename = part.get_filename()

                    if mct != 'text' or (mct == 'text'
                                         and filename is not None):
                        attachments.append((
                            filename,
                            SimpleUploadedFile(
                                filename,
                                payload,
                                content_type=part.get_content_type(),
                            ),
                        ))

                    else:
                        content = payload
                        if cst == 'html':
                            body_html = safe_unicode(content)
                        elif cst == 'plain':
                            body = safe_unicode(content)
                        # else:  TODO ??
            else:
                cst = email_message.get_content_subtype()
                content = email_message.get_payload(decode=True)
                if cst == 'plain':
                    body = safe_unicode(content)
                elif cst == 'html':
                    body_html = body = safe_unicode(content)

            emails.append(
                PopEmail(
                    body=body,
                    body_html=body_html,
                    senders=from_emails,
                    tos=to_emails,
                    ccs=cc_emails,
                    subject=subject,
                    dates=dates,
                    attachments=attachments,
                ))

            if delete:
                # We delete the mail from the server when treated
                # TODO: delete only when we are sure it has been saved (clean() method ?)
                client.dele(message_number)

        client.quit()

        return emails