<commit_before>"""
Comm helper functions for FOIA views
"""

from django.contrib import messages
from django.contrib.auth.decorators import user_passes_test
from django.core.validators import ValidationError
from django.http import HttpResponse, Http404
from django.shortcuts import redirect, get_object_or_404

from datetime import datetime

from muckrock.foia.models import FOIACommunication, FOIARequest

def save_foia_comm(foia, from_who, comm, formset=None, appeal=False, snail=False):
    """Save the FOI Communication"""
    #pylint:disable=too-many-arguments
    comm = FOIACommunication.objects.create(
        foia=foia,
        from_who=from_who,
        to_who=foia.get_to_who(),
        date=datetime.now(),
        response=False,
        full_html=False,
        communication=comm
    )
    if formset is not None:
        foia_files = formset.save(commit=False)
        for foia_file in foia_files:
            foia_file.comm = comm
            foia_file.title = foia_file.name()
            foia_file.date = comm.date
            foia_file.save()
    foia.submit(appeal=appeal, snail=snail)

@user_passes_test(lambda u: u.is_staff)
def move_comm(request, next_):
    """Admin moves a communication to a different FOIA"""
    try:
        comm_pk = request.POST['comm_pk']
        comm = FOIACommunication.objects.get(pk=comm_pk)
        new_foia_pks = request.POST['new_foia_pks'].split(',')
        comm.move(new_foia_pks)
        msg = 'Communication moved successfully'
        messages.success(request, msg)
    except (KeyError, FOIACommunication.DoesNotExist):
        messages.error(request, 'The communication does not exist.')
    except ValueError:
        messages.error(request, 'No move destination provided.')
    return redirect(next_)

@user_passes_test(lambda u: u.is_staff)
def delete_comm(request, next_):
    """Admin deletes a communication"""
    try:
        comm = FOIACommunication.objects.get(pk=request.POST['comm_pk'])
        files = comm.files.all()
        for file_ in files:
            file_.delete()
        comm.delete()
        messages.success(request, 'The communication was deleted.')
    except (KeyError, FOIACommunication.DoesNotExist):
        messages.error(request, 'The communication does not exist.')
    return redirect(next_)

@user_passes_test(lambda u: u.is_staff)
def resend_comm(request, next_):
    """Resend the FOI Communication"""
    try:
        comm = FOIACommunication.objects.get(pk=request.POST['comm_pk'])
        comm.resend(request.POST['email'])
        messages.success(request, 'The communication was resent.')
    except (KeyError, FOIACommunication.DoesNotExist):
        messages.error(request, 'The communication does not exist.')
    except ValidationError:
        messages.error(request, 'The provided email was invalid')
    except ValueError:
        messages.error(request, 'The communication is an orphan and cannot be resent.')
    return redirect(next_)

@user_passes_test(lambda u: u.is_staff)
def raw(request, idx):
    """Get the raw email for a communication"""
    # pylint: disable=unused-argument
    comm = get_object_or_404(FOIACommunication, pk=idx)
    if not comm.rawemail:
        raise Http404()
    return HttpResponse(comm.rawemail.raw_email, content_type='text/plain')
<commit_msg>pylint<commit_after>"""
Comm helper functions for FOIA views
"""

from django.contrib import messages
from django.contrib.auth.decorators import user_passes_test
from django.core.validators import ValidationError
from django.http import HttpResponse, Http404
from django.shortcuts import redirect, get_object_or_404

from datetime import datetime

from muckrock.foia.models import FOIACommunication

def save_foia_comm(foia, from_who, comm, formset=None, appeal=False, snail=False):
    """Save the FOI Communication"""
    #pylint:disable=too-many-arguments
    comm = FOIACommunication.objects.create(
        foia=foia,
        from_who=from_who,
        to_who=foia.get_to_who(),
        date=datetime.now(),
        response=False,
        full_html=False,
        communication=comm
    )
    if formset is not None:
        foia_files = formset.save(commit=False)
        for foia_file in foia_files:
            foia_file.comm = comm
            foia_file.title = foia_file.name()
            foia_file.date = comm.date
            foia_file.save()
    foia.submit(appeal=appeal, snail=snail)

@user_passes_test(lambda u: u.is_staff)
def move_comm(request, next_):
    """Admin moves a communication to a different FOIA"""
    try:
        comm_pk = request.POST['comm_pk']
        comm = FOIACommunication.objects.get(pk=comm_pk)
        new_foia_pks = request.POST['new_foia_pks'].split(',')
        comm.move(new_foia_pks)
        msg = 'Communication moved successfully'
        messages.success(request, msg)
    except (KeyError, FOIACommunication.DoesNotExist):
        messages.error(request, 'The communication does not exist.')
    except ValueError:
        messages.error(request, 'No move destination provided.')
    return redirect(next_)

@user_passes_test(lambda u: u.is_staff)
def delete_comm(request, next_):
    """Admin deletes a communication"""
    try:
        comm = FOIACommunication.objects.get(pk=request.POST['comm_pk'])
        files = comm.files.all()
        for file_ in files:
            file_.delete()
        comm.delete()
        messages.success(request, 'The communication was deleted.')
    except (KeyError, FOIACommunication.DoesNotExist):
        messages.error(request, 'The communication does not exist.')
    return redirect(next_)

@user_passes_test(lambda u: u.is_staff)
def resend_comm(request, next_):
    """Resend the FOI Communication"""
    try:
        comm = FOIACommunication.objects.get(pk=request.POST['comm_pk'])
        comm.resend(request.POST['email'])
        messages.success(request, 'The communication was resent.')
    except (KeyError, FOIACommunication.DoesNotExist):
        messages.error(request, 'The communication does not exist.')
    except ValidationError:
        messages.error(request, 'The provided email was invalid')
    except ValueError:
        messages.error(request, 'The communication is an orphan and cannot be resent.')
    return redirect(next_)

@user_passes_test(lambda u: u.is_staff)
def raw(request, idx):
    """Get the raw email for a communication"""
    # pylint: disable=unused-argument
    comm = get_object_or_404(FOIACommunication, pk=idx)
    if not comm.rawemail:
        raise Http404()
    return HttpResponse(comm.rawemail.raw_email, content_type='text/plain')

{'fraction_non_alphanumeric': 0.10139860139860139, 'fraction_numerical': 0.0036485253876558225, 'mean_word_length': 3.469429347826087, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2857786', 'n_tokens_mistral': 2022, 'n_tokens_neox': 2012, 'n_words': 477}