<commit_before>from django.urls import resolve
from django.utils.datastructures import MultiValueDictKeyError


class FileExportMiddleware:

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        return response

    @staticmethod
    def process_template_response(request, response):
        # So here we check if the view that we're dealing with
        # is actually an api view
        if request.resolver_match.app_name == 'api':
            try:
                formatz = request.GET['format']

                if formatz == 'csv' or formatz == 'xls':
                    current_url = resolve(request.path_info).url_name

                    try:
                        file_name = request.GET['export_name']
                    except MultiValueDictKeyError:
                        file_name = current_url \
                            if current_url is not None else 'export'

                    response['Content-Disposition'] = \
                        "attachment; filename={}.{}".format(file_name, formatz)

            except MultiValueDictKeyError:
                pass

        return response<commit_msg>updated the comments, removed the try excepts.<commit_after>from django.urls import resolve


# This middleware is used to give a name and an extension for
# files exported by using the renderers.py(currently only csv and xls)
# as the 'Content-Disposition' given in those renderers
# response is not applied(or maybe overriden by something else)
# we need to apply it here
class FileExportMiddleware:

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        return response

    @staticmethod
    def process_template_response(request, response):
        # So here we check if the view that we're dealing with
        # is actually an api view
        if request.resolver_match.app_name == 'api':
            if 'format' in request.GET:
                formatz = request.GET['format']

                if formatz == 'csv' or formatz == 'xls':
                    current_url = resolve(request.path_info).url_name

                    if 'export_name' in request.GET:
                        file_name = request.GET['export_name']
                    else:
                        file_name = current_url \
                            if current_url is not None else 'export'

                    response['Content-Disposition'] = \
                        "attachment; filename={}.{}".format(file_name, formatz)

        return response
{'fraction_non_alphanumeric': 0.07374301675977654, 'fraction_numerical': 0.0, 'mean_word_length': 1.9451754385964912, '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': '3789589', 'n_tokens_mistral': 686, 'n_tokens_neox': 649, 'n_words': 205}