<commit_before>"""
Formtools Preview application.
"""
from django.conf import settings
from django.shortcuts import render_to_response
from django.utils.crypto import constant_time_compare
from django.contrib.formtools.utils import form_hmac
from django.views.generic import FormView

AUTO_ID = 'formtools_%s' # Each form here uses this as its auto_id parameter.
STAGE_FIELD = 'stage'
HASH_FIELD = 'hash'

class FormPreview(FormView):
    preview_template = 'formtools/preview.html'
    form_template = 'formtools/form.html'

    # METHODS SUBCLASSES SHOULDN'T OVERRIDE ###################################

    def __init__(self, form_class, *args, **kwargs):
        super(FormPreview, self).__init__(*args, **kwargs)
        # form should be a Form class, not an instance.
        self.form_class = form_class
        # A relic from the past; override get_context_data to pass extra context
        # to the template. Left in for backwards compatibility.
        self.state = {}

    def __call__(self, request, *args, **kwargs):
        return self.dispatch(request, *args, **kwargs)

    def dispatch(self, request, *args, **kwargs):
        self.preview_stage = 'preview'
        self.post_stage = 'post'
        stages = {'1': self.preview_stage, '2': self.post_stage}

        posted_stage = request.POST.get(self.unused_name(STAGE_FIELD))
        self.stage = stages.get(posted_stage, self.preview_stage)

        # For backwards compatiblity
        self.parse_params(*args, **kwargs)

        return super(FormPreview, self).dispatch(request, *args, **kwargs)

    def unused_name(self, name):
        """
        Given a first-choice name, adds an underscore to the name until it
        reaches a name that isn't claimed by any field in the form.

        This is calculated rather than being hard-coded so that no field names
        are off-limits for use in the form.
        """
        while 1:
            try:
                self.form_class.base_fields[name]
            except KeyError:
                break # This field name isn't being used by the form.
            name += '_'
        return name

    def _get_context_data(self, form):
        """ For backwards compatiblity. """
        context = self.get_context_data()
        context.update(self.get_context(self.request, form))
        return context

    def get(self, request, *args, **kwargs):
        "Displays the form"
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        context = self._get_context_data(form)
        self.template_name = self.form_template
        return self.render_to_response(context)

    def _check_security_hash(self, token, form):
        expected = self.security_hash(self.request, form)
        return constant_time_compare(token, expected)

    def preview_post(self, request):
        """ For backwards compatibility. failed_hash calls this method by
        default. """
        self.stage = self.preview_stage
        return self.post(request)

    def form_valid(self, form):
        context = self._get_context_data(form)
        if self.stage == self.preview_stage:
            self.process_preview(self.request, form, context)
            context['hash_field'] = self.unused_name(HASH_FIELD)
            context['hash_value'] = self.security_hash(self.request, form)
            self.template_name = self.preview_template
            return self.render_to_response(context)
        else:
            form_hash = self.request.POST.get(self.unused_name(HASH_FIELD), '')
            if not self._check_security_hash(form_hash, form):
                return self.failed_hash(self.request) # Security hash failed.
            return self.done(self.request, form.cleaned_data)

    def form_invalid(self, form):
        context = self._get_context_data(form)
        self.template_name = self.form_template
        return render_to_response(context)
<commit_msg>Use "private" attribute names.<commit_after>"""
Formtools Preview application.
"""
from django.conf import settings
from django.shortcuts import render_to_response
from django.utils.crypto import constant_time_compare
from django.contrib.formtools.utils import form_hmac
from django.views.generic import FormView

PREVIEW_STAGE = 'preview'
POST_STAGE = 'post'
AUTO_ID = 'formtools_%s' # Each form here uses this as its auto_id parameter.
STAGE_FIELD = 'stage'
HASH_FIELD = 'hash'

class FormPreview(FormView):
    preview_template = 'formtools/preview.html'
    form_template = 'formtools/form.html'

    # METHODS SUBCLASSES SHOULDN'T OVERRIDE ###################################

    def __init__(self, form_class, *args, **kwargs):
        super(FormPreview, self).__init__(*args, **kwargs)
        # form should be a Form class, not an instance.
        self.form_class = form_class
        self._preview_stage = PREVIEW_STAGE
        self._post_stage = POST_STAGE
        self._stages = {'1': self._preview_stage, '2': self._post_stage}
        # A relic of the past; override get_context_data to pass extra context
        # to the template. Left in for backwards compatibility.
        self.state = {}

    def __call__(self, request, *args, **kwargs):
        return self.dispatch(request, *args, **kwargs)

    def dispatch(self, request, *args, **kwargs):
        posted_stage = request.POST.get(self.unused_name(STAGE_FIELD))
        self._stage = self._stages.get(posted_stage, self._preview_stage)

        # For backwards compatiblity
        self.parse_params(*args, **kwargs)

        return super(FormPreview, self).dispatch(request, *args, **kwargs)

    def unused_name(self, name):
        """
        Given a first-choice name, adds an underscore to the name until it
        reaches a name that isn't claimed by any field in the form.

        This is calculated rather than being hard-coded so that no field names
        are off-limits for use in the form.
        """
        while 1:
            try:
                self.form_class.base_fields[name]
            except KeyError:
                break # This field name isn't being used by the form.
            name += '_'
        return name

    def _get_context_data(self, form):
        """ For backwards compatiblity. """
        context = self.get_context_data()
        context.update(self.get_context(self.request, form))
        return context

    def get(self, request, *args, **kwargs):
        "Displays the form"
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        context = self._get_context_data(form)
        self.template_name = self.form_template
        return self.render_to_response(context)

    def _check_security_hash(self, token, form):
        expected = self.security_hash(self.request, form)
        return constant_time_compare(token, expected)

    def preview_post(self, request):
        """ For backwards compatibility. failed_hash calls this method by
        default. """
        self._stage = self._preview_stage
        return self.post(request)

    def form_valid(self, form):
        context = self._get_context_data(form)
        if self._stage == self._preview_stage:
            self.process_preview(self.request, form, context)
            context['hash_field'] = self.unused_name(HASH_FIELD)
            context['hash_value'] = self.security_hash(self.request, form)
            self.template_name = self.preview_template
            return self.render_to_response(context)
        else:
            form_hash = self.request.POST.get(self.unused_name(HASH_FIELD), '')
            if not self._check_security_hash(form_hash, form):
                return self.failed_hash(self.request) # Security hash failed.
            return self.done(self.request, form.cleaned_data)

    def form_invalid(self, form):
        context = self._get_context_data(form)
        self.template_name = self.form_template
        return render_to_response(context)

{'fraction_non_alphanumeric': 0.10055513499873833, 'fraction_numerical': 0.000757002271006813, 'mean_word_length': 2.8184007707129095, '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': '783593', 'n_tokens_mistral': 2265, 'n_tokens_neox': 2208, 'n_words': 589}