<commit_before>import os, re
from datetime import date, datetime

from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.conf import settings
from django.utils.translation import ugettext as _

def reporting_list(request, message=""):
    # Filter on organisation entity
    organisation = request.fmsuser.organisation

    if not organisation:
        raise PermissionDenied

    reporting_root = os.path.join(settings.REPORTING_ROOT, str(organisation.id))

    pdf = []
    xls = None
    try:
        ls = os.listdir(reporting_root)

        for path in ls:
            xls_file = re.search(r'xlsx$', path)

            archive_file_date = None
            archive_file_date_match = re.match(r"\d+_(\d+)\.\w+", path)
            if archive_file_date_match:
                archive_file_date = datetime.strptime(archive_file_date_match.group(1), '%y%m')

            if xls_file or archive_file_date:
                f = {
                    'path': path,
                    'stat': os.stat(os.path.join(reporting_root, path))
                }
                f['modified_date'] = archive_file_date or date.fromtimestamp(int(f['stat'].st_mtime))

                # Check if xls or pdf
                if xls_file:
                    xls = f
                else:
                    pdf.append(f)
    except OSError:
        message = _("Reporting currently unavailable")

    return render_to_response('pro/list_reporting.html', {
        'pdf'     : pdf,
        'xls'     : xls,
        'message' : message
    }, context_instance=RequestContext(request))

import mimetypes

def reporting_download(request, path):
    # Filter on organisation entity
    organisation = request.fmsuser.organisation

    if not organisation:
        raise PermissionDenied

    message =""

    try:
        reporting_file_path = os.path.join(settings.REPORTING_ROOT, str(organisation.id), str(path))

        reporting_file_type, reporting_file_encoding =  mimetypes.guess_type(reporting_file_path)

        reporting_file = open(reporting_file_path, 'r')
        response = HttpResponse(reporting_file.read(), mimetype=reporting_file_type)
        response['Content-Disposition'] = 'attachment; filename=%s' % path
        reporting_file.close()

        return response
    except IOError:
        message = _("An error occured by opening file <strong>%s</strong>" % path)

    return reporting_list(request, message)
<commit_msg>Force correct mimetype when download xlsx report<commit_after>import os, re
from datetime import date, datetime

from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.conf import settings
from django.utils.translation import ugettext as _

def reporting_list(request, message=""):
    # Filter on organisation entity
    organisation = request.fmsuser.organisation

    if not organisation:
        raise PermissionDenied

    reporting_root = os.path.join(settings.REPORTING_ROOT, str(organisation.id))

    pdf = []
    xls = None
    try:
        ls = os.listdir(reporting_root)

        for path in ls:
            xls_file = re.search(r'xlsx$', path)

            archive_file_date = None
            archive_file_date_match = re.match(r"\d+_(\d+)\.\w+", path)
            if archive_file_date_match:
                archive_file_date = datetime.strptime(archive_file_date_match.group(1), '%y%m')

            if xls_file or archive_file_date:
                f = {
                    'path': path,
                    'stat': os.stat(os.path.join(reporting_root, path))
                }
                f['modified_date'] = archive_file_date or date.fromtimestamp(int(f['stat'].st_mtime))

                # Check if xls or pdf
                if xls_file:
                    xls = f
                else:
                    pdf.append(f)
    except OSError:
        message = _("Reporting currently unavailable")

    return render_to_response('pro/list_reporting.html', {
        'pdf'     : pdf,
        'xls'     : xls,
        'message' : message
    }, context_instance=RequestContext(request))

import mimetypes

def reporting_download(request, path):
    # Filter on organisation entity
    organisation = request.fmsuser.organisation

    if not organisation:
        raise PermissionDenied

    message =""

    try:
        reporting_file_path = os.path.join(settings.REPORTING_ROOT, str(organisation.id), str(path))

        # Tricky stuff: the reports coming from BO platform are Windows generated and do not support mimetype.
        # So, force the correct xlsx mimetype
        xls_file = re.search(r'xlsx$', path)
        if xls_file:
            reporting_file_type, reporting_file_encoding = ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', None)
        else:
            reporting_file_type, reporting_file_encoding =  mimetypes.guess_type(reporting_file_path)

        reporting_file = open(reporting_file_path, 'r')
        response = HttpResponse(reporting_file.read(), mimetype=reporting_file_type)
        response['Content-Disposition'] = 'attachment; filename=%s' % path
        reporting_file.close()

        return response
    except IOError:
        message = _("An error occured by opening file <strong>%s</strong>" % path)

    return reporting_list(request, message)

{'fraction_non_alphanumeric': 0.08837881800108441, 'fraction_numerical': 0.0003614675582866438, 'mean_word_length': 2.5841968911917097, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1078312', 'n_tokens_mistral': 1617, 'n_tokens_neox': 1536, 'n_words': 368}