Esempio n. 1
0
    def process_message(source, message_text):
        from flanker import mime

        metadata_dictionary = {}

        message = mime.from_string(force_bytes(message_text))

        if source.from_metadata_type:
            metadata_dictionary[
                source.from_metadata_type.name
            ] = message.headers.get('From')

        if source.subject_metadata_type:
            metadata_dictionary[
                source.subject_metadata_type.name
            ] = message.headers.get('Subject')

        document_ids, parts_metadata_dictionary = EmailBaseModel._process_message(source=source, message=message)

        metadata_dictionary.update(parts_metadata_dictionary)

        if metadata_dictionary:
            for document in Document.objects.filter(id__in=document_ids):
                set_bulk_metadata(
                    document=document,
                    metadata_dictionary=metadata_dictionary
                )
    def process_message(source, message_text):
        from flanker import mime

        metadata_dictionary = {}

        message = mime.from_string(force_bytes(message_text))

        if source.from_metadata_type:
            # ------------------------------------------------
            try:
                start = message.headers.get('From').index("<")
                metadata_dictionary[source.from_metadata_type.
                                    name] = message.headers.get('From')[start +
                                                                        1:-1]
            except:
                metadata_dictionary[source.from_metadata_type.
                                    name] = message.headers.get('From')

        if source.subject_metadata_type:
            metadata_dictionary[source.subject_metadata_type.
                                name] = message.headers.get('Subject').upper()

        if 'PAYMENT' in metadata_dictionary[source.subject_metadata_type.name]:
            metadata_dictionary['supervisor'] = None

            if message.headers.get('Cc'):
                metadata_dictionary[
                    'supervisor'] = EmailBaseModel._select_email(
                        message.headers.get('Cc'))

            if not metadata_dictionary['supervisor'] and message.headers.get(
                    "To"):
                metadata_dictionary[
                    'supervisor'] = EmailBaseModel._select_email(
                        message.headers.get('To'))

        if 'LEAVE' in metadata_dictionary[source.subject_metadata_type.name]:
            the_email = metadata_dictionary[source.from_metadata_type.name]
            for user_info in UserExtras.objects.raw(
                    f"SELECT * FROM nic_employee WHERE employee = '{the_email}';"
            ):
                metadata_dictionary['supervisor'] = user_info.supervisor
                break

        document_ids, parts_metadata_dictionary = EmailBaseModel._process_message(
            source=source, message=message)

        metadata_dictionary.update(parts_metadata_dictionary)

        # ------------------------------------------------

        if document_ids:
            metadata_dictionary["id"] = document_ids[0]
            metadata_dictionary["hasMultipleAttachments"] = len(document_ids)

            if metadata_dictionary:
                for document in Document.objects.filter(id__in=document_ids):
                    set_bulk_metadata(document=document,
                                      metadata_dictionary=metadata_dictionary)
Esempio n. 3
0
    def process_message(source, message_text, message_properties=None):
        from flanker import mime

        counter = 1
        message = mime.from_string(force_bytes(message_text))
        metadata_dictionary = {}

        if not message_properties:
            message_properties = {}

        message_properties['Subject'] = message_properties.get(
            'Subject', message.headers.get('Subject'))

        message_properties['From'] = message_properties.get(
            'From', message.headers.get('From'))

        if source.subject_metadata_type:
            metadata_dictionary[source.subject_metadata_type.
                                name] = message_properties.get('Subject')

        if source.from_metadata_type:
            metadata_dictionary[source.from_metadata_type.
                                name] = message_properties.get('From')

        # Messages are tree based, do nested processing of message parts until
        # a message with no children is found, then work out way up.
        if message.parts:
            for part in message.parts:
                EmailBaseModel.process_message(
                    source=source,
                    message_text=part.to_string(),
                    message_properties=message_properties)
        else:
            # Treat inlines as attachments, both are extracted and saved as
            # documents
            if message.is_attachment() or message.is_inline():

                # Reject zero length attachments
                if len(message.body) == 0:
                    return

                label = message.detected_file_name or 'attachment-{}'.format(
                    counter)
                with ContentFile(content=message.body,
                                 name=label) as file_object:
                    if label == source.metadata_attachment_name:
                        metadata_dictionary = yaml.load(
                            stream=file_object.read(), Loader=SafeLoader)
                        logger.debug('Got metadata dictionary: %s',
                                     metadata_dictionary)
                    else:
                        documents = source.handle_upload(
                            document_type=source.document_type,
                            file_object=file_object,
                            expand=(source.uncompress ==
                                    SOURCE_UNCOMPRESS_CHOICE_Y))
                        if metadata_dictionary:
                            for document in documents:
                                set_bulk_metadata(
                                    document=document,
                                    metadata_dictionary=metadata_dictionary)
            else:
                # If it is not an attachment then it should be a body message part.
                # Another option is to use message.is_body()
                if message.detected_content_type == 'text/html':
                    label = 'email_body.html'
                else:
                    label = 'email_body.txt'

                if source.store_body:
                    with ContentFile(content=force_bytes(message.body),
                                     name=label) as file_object:
                        documents = source.handle_upload(
                            document_type=source.document_type,
                            expand=SOURCE_UNCOMPRESS_CHOICE_N,
                            file_object=file_object)
                        if metadata_dictionary:
                            for document in documents:
                                set_bulk_metadata(
                                    document=document,
                                    metadata_dictionary=metadata_dictionary)