<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2016 <NAME> <<EMAIL>>
#
# Distributed under terms of the MIT license.

from contextlib import contextmanager
from django.conf import settings
from django.core.files import File
from django.dispatch import receiver
from doc_utilities import create_uipa_document_request_from_foi_request
from doc_utilities import prepare_for_final_archiving
from froide.foirequest.file_utils import convert_to_pdf
from froide.foirequest.models import FoiAttachment
from froide.foirequest.models import FoiRequest
import os
from tempfile import mkstemp


@contextmanager
def temp_filename(suffix='', prefix='tmp', dir=None, delete=True):
    fd, name = mkstemp(suffix=suffix, prefix=prefix, dir=dir)
    try:
        os.close(fd)
        yield name
    finally:
        if delete:
            try:
                os.remove(name)
            except OSError:
                pass


@receiver(FoiRequest.request_created,
          dispatch_uid="create_and_attach_uipa_document_request")
def create_and_attach_uipa_document_request(sender, **kwargs):
    foi_request = sender
    document = create_uipa_document_request_from_foi_request(foi_request, kwargs.get('should_waive_fees', False))

    with temp_filename() as temp_fn:
        modified_fn = temp_fn + ".extension_for_convert_to_pdf"
        document.save(modified_fn)

        result_file_path = convert_to_pdf(
            modified_fn,
            binary_name=settings.FROIDE_CONFIG.get(
                'doc_conversion_binary'
            ),
            construct_call=settings.FROIDE_CONFIG.get(
                'doc_conversion_call_func'
            )
        )

        message = foi_request.messages[0]

        if message:
            original_plaintext = message.plaintext
            message.plaintext = prepare_for_final_archiving(original_plaintext)
            message.plaintext_redacted = prepare_for_final_archiving(original_plaintext)
            message.save()
<commit_msg>Fixed redacted bug that was exposing a private user<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2016 <NAME> <<EMAIL>>
#
# Distributed under terms of the MIT license.

from contextlib import contextmanager
from django.conf import settings
from django.core.files import File
from django.dispatch import receiver
from doc_utilities import create_uipa_document_request_from_foi_request
from doc_utilities import prepare_for_final_archiving
from froide.foirequest.file_utils import convert_to_pdf
from froide.foirequest.models import FoiAttachment
from froide.foirequest.models import FoiRequest
import os
from tempfile import mkstemp


@contextmanager
def temp_filename(suffix='', prefix='tmp', dir=None, delete=True):
    fd, name = mkstemp(suffix=suffix, prefix=prefix, dir=dir)
    try:
        os.close(fd)
        yield name
    finally:
        if delete:
            try:
                os.remove(name)
            except OSError:
                pass


@receiver(FoiRequest.request_created,
          dispatch_uid="create_and_attach_uipa_document_request")
def create_and_attach_uipa_document_request(sender, **kwargs):
    foi_request = sender
    document = create_uipa_document_request_from_foi_request(foi_request, kwargs.get('should_waive_fees', False))

    with temp_filename() as temp_fn:
        modified_fn = temp_fn + ".extension_for_convert_to_pdf"
        document.save(modified_fn)

        result_file_path = convert_to_pdf(
            modified_fn,
            binary_name=settings.FROIDE_CONFIG.get(
                'doc_conversion_binary'
            ),
            construct_call=settings.FROIDE_CONFIG.get(
                'doc_conversion_call_func'
            )
        )

        message = foi_request.messages[0]

        if message:
            original_plaintext = message.plaintext
            message.plaintext = prepare_for_final_archiving(original_plaintext)

            original_plaintext_redacted = message.plaintext_redacted
            message.plaintext_redacted = prepare_for_final_archiving(original_plaintext_redacted)
            message.save()

{'fraction_non_alphanumeric': 0.07179238418627214, 'fraction_numerical': 0.002910502061605627, 'mean_word_length': 3.157258064516129, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2194354', 'n_tokens_mistral': 1230, 'n_tokens_neox': 1193, 'n_words': 246}