<commit_before>from __future__ import unicode_literals

import re
import json
import logging

from django.conf import settings
from django.core.files import File
from django.urls import reverse
from django.shortcuts import render, get_object_or_404, redirect
from django.views.decorators.http import require_POST
from django.utils.translation import ugettext as _
from django.http import HttpResponse, JsonResponse
from django.contrib import messages
from django.views.static import serve
from django.contrib.staticfiles.templatetags.staticfiles import static

from froide.helper.utils import render_400, render_403
from froide.helper.redaction import redact_file

from ..models import FoiRequest, FoiMessage, FoiAttachment
from ..auth import (can_read_foirequest, can_read_foirequest_authenticated,
                    can_write_foirequest, is_attachment_public)


logger = logging.getLogger(__name__)

X_ACCEL_REDIRECT_PREFIX = getattr(settings, 'X_ACCEL_REDIRECT_PREFIX', '')


def has_attachment_access(request, foirequest, attachment):
    if not can_read_foirequest(foirequest, request):
        return False
    if not attachment.approved:
        # allow only approved attachments to be read
        # do not allow anonymous authentication here
        allowed = can_read_foirequest_authenticated(
            foirequest, request, allow_code=False
        )
        if not allowed:
            return False
    return True


def get_accessible_attachment_url(foirequest, attachment):
    needs_authentication = not is_attachment_public(foirequest, attachment)
    return attachment.get_absolute_domain_file_url(
        authenticated=needs_authentication
    )


def show_attachment(request, slug, message_id, attachment_name):
    foirequest = get_object_or_404(FoiRequest, slug=slug)
    message = get_object_or_404(FoiMessage, id=int(message_id),
                                request=foirequest)
    attachment = get_object_or_404(FoiAttachment, belongs_to=message,
                                   name=attachment_name)

    if not has_attachment_access(request, foirequest, attachment):
        return render_403(request)

    attachment_url = get_accessible_attachment_url(foirequest, attachment)

    return render(request, 'foirequest/attachment/show.html', {
        'attachment': attachment,
        'attachment_url': attachment_url,
        'message': message,
        'foirequest': foirequest
    })


@require_POST
def approve_attachment(request, slug, attachment):
    foirequest = get_object_or_404(FoiRequest, slug=slug)

    if not can_write_foirequest(foirequest, request):
        return render_403(request)
    att = get_object_or_404(FoiAttachment, id=int(attachment))<commit_msg>Redirect attachment view to document if possible<commit_after>from __future__ import unicode_literals

import re
import json
import logging

from django.conf import settings
from django.core.files import File
from django.urls import reverse
from django.shortcuts import render, get_object_or_404, redirect
from django.views.decorators.http import require_POST
from django.utils.translation import ugettext as _
from django.http import HttpResponse, JsonResponse
from django.contrib import messages
from django.views.static import serve
from django.contrib.staticfiles.templatetags.staticfiles import static

from froide.helper.utils import render_400, render_403
from froide.helper.redaction import redact_file

from ..models import FoiRequest, FoiMessage, FoiAttachment
from ..auth import (can_read_foirequest, can_read_foirequest_authenticated,
                    can_write_foirequest, is_attachment_public)


logger = logging.getLogger(__name__)

X_ACCEL_REDIRECT_PREFIX = getattr(settings, 'X_ACCEL_REDIRECT_PREFIX', '')


def has_attachment_access(request, foirequest, attachment):
    if not can_read_foirequest(foirequest, request):
        return False
    if not attachment.approved:
        # allow only approved attachments to be read
        # do not allow anonymous authentication here
        allowed = can_read_foirequest_authenticated(
            foirequest, request, allow_code=False
        )
        if not allowed:
            return False
    return True


def get_accessible_attachment_url(foirequest, attachment):
    needs_authentication = not is_attachment_public(foirequest, attachment)
    return attachment.get_absolute_domain_file_url(
        authenticated=needs_authentication
    )


def show_attachment(request, slug, message_id, attachment_name):
    foirequest = get_object_or_404(FoiRequest, slug=slug)
    message = get_object_or_404(FoiMessage, id=int(message_id),
                                request=foirequest)
    attachment = get_object_or_404(FoiAttachment, belongs_to=message,
                                   name=attachment_name)

    if not has_attachment_access(request, foirequest, attachment):
        return render_403(request)

    if attachment.document is not None and attachment.document.public:
        return redirect(attachment.document)

    attachment_url = get_accessible_attachment_url(foirequest, attachment)

    return render(request, 'foirequest/attachment/show.html', {
        'attachment': attachment,
        'attachment_url': attachment_url,
        'message': message,
        'foirequest': foirequest
    })


@require_POST
def approve_attachment(request, slug, attachment):
    foirequest = get_object_or_404(FoiRequest, slug=slug)

    if not can_write_foirequest(foirequest, request):
        return render_403(request)
    att = get_object_or_404(FoiAttachment, id=int(attachment))
{'fraction_non_alphanumeric': 0.06326164874551972, 'fraction_numerical': 0.010752688172043012, 'mean_word_length': 4.106129917657823, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2872239', 'n_tokens_mistral': 1619, 'n_tokens_neox': 1535, 'n_words': 380}