<commit_before>from pathlib import PurePath
from tempfile import mkdtemp

from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponse
from django.views.generic import TemplateView, View

from .io.xlsx import generate_template


class DownloadXLSXTemplateView(LoginRequiredMixin, TemplateView):

    template_name = 'submission/download_xlsx_template.html'

    def get(self, request, *args, **kwargs):
        check = True if request.GET.get('check') else False
        context = self.get_context_data(check=check, **kwargs)
        return self.render_to_response(context)

    def get_context_data(self, check=False, **kwargs):
        # TODO
        # reverse the upload url
        next_step_url = '#'

        ctx = super().get_context_data(**kwargs)
        ctx.update({
            'step': 'download',
            'next_step_url': next_step_url,
            'check': check,
        })

        meta = self.request.session.get('template')
        if meta:
            ctx.update({
                'version': meta.get('version'),
                'checksum': meta.get('checksum'),
            })
        return ctx


class GenerateXLSXTemplateView(LoginRequiredMixin, View):

    def post(self, request, *args, **kwargs):

        template_file_name = 'meta.xlsx'
        template_path = PurePath(mkdtemp(), template_file_name)
        checksum, version = generate_template(filename=template_path)

        # Save file checksum in the session
        request.session['template'] = {
            'checksum': checksum,
            'version': version,
        }

        response = HttpResponse(
            content_type=(
                'application/vnd'
                '.openxmlformats-officedocument'
                '.spreadsheetml'
                '.sheet'
            )
        )
        content_disposition = 'attachment; filename="{}"'.format(
            '{}-{}.xlsx'.format(
                template_path.stem,
                version[:8]
            )<commit_msg>Add a message when user try to display checksum…<commit_after>from pathlib import PurePath
from tempfile import mkdtemp

from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponse
from django.utils.translation import ugettext as _
from django.views.generic import TemplateView, View

from .io.xlsx import generate_template


class DownloadXLSXTemplateView(LoginRequiredMixin, TemplateView):

    template_name = 'submission/download_xlsx_template.html'

    def get(self, request, *args, **kwargs):
        check = True if request.GET.get('check') else False
        context = self.get_context_data(check=check, **kwargs)
        return self.render_to_response(context)

    def get_context_data(self, check=False, **kwargs):
        # TODO
        # reverse the upload url
        next_step_url = '#'

        ctx = super().get_context_data(**kwargs)
        ctx.update({
            'step': 'download',
            'next_step_url': next_step_url,
            'check': check,
        })

        meta = self.request.session.get('template')
        if meta:
            ctx.update({
                'version': meta.get('version'),
                'checksum': meta.get('checksum'),
            })

        if check and not meta:
            messages.warning(
                self.request,
                _(
                    "Download the meta.xlsx template first. "
                    "Then you will be able to display its checksum."
                )
            )

        return ctx


class GenerateXLSXTemplateView(LoginRequiredMixin, View):

    def post(self, request, *args, **kwargs):

        template_file_name = 'meta.xlsx'
        template_path = PurePath(mkdtemp(), template_file_name)
        checksum, version = generate_template(filename=template_path)

        # Save file checksum in the session
        request.session['template'] = {
            'checksum': checksum,
            'version': version,
        }

        response = HttpResponse(
            content_type=(
                'application/vnd'
                '.openxmlformats-officedocument'
                '.spreadsheetml'
                '.sheet'
            )
        )
        content_disposition = 'attachment; filename="{}"'.format(
            '{}-{}.xlsx'.format(
                template_path.stem,
                version[:8]
            )
{'fraction_non_alphanumeric': 0.10024892509617561, 'fraction_numerical': 0.00045259108395564606, 'mean_word_length': 2.0779944289693595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2325204', 'n_tokens_mistral': 1223, 'n_tokens_neox': 1168, 'n_words': 270}