<commit_before>import json
import logging

from ratelimit.decorators import ratelimit

from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.sites.shortcuts import get_current_site
from django.core.urlresolvers import reverse_lazy
from django.http import HttpResponse
from django.shortcuts import render
from django.utils.html import conditional_escape
from django.views import generic as views

from . import forms, models
from ..evaluation.models import EvalRow
from ..utils.api import MatchingApi, NotificationApi
from .models import MatchReport, Report, SentFullReport
from .report_delivery import MatchReportContent, PDFFullReport

User = get_user_model()
logger = logging.getLogger(__name__)


class SecretKeyStorageHelper(object):

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

    def set_secret_key(self, key):
        self.view.request.session['secret_key'] = key

    @property
    def secret_key(self):
        return self.view.request.session.get('secret_key')


class ReportBaseView(views.edit.ModelFormMixin):
    model = models.Report
    storage_helper = SecretKeyStorageHelper
    template_name = 'callisto_core/delivery/form.html'
    context_object_name = 'report'
    slug_field = 'uuid'
    slug_url_kwarg = 'uuid'

    @property
    def storage(self):
        return self.storage_helper(self)

    def form_valid(self, form):
        self.storage.set_secret_key(form.data['key'])
        return super().form_valid(form)


class ReportCreateView(
    ReportBaseView,
    views.edit.CreateView,
):
    form_class = forms.ReportCreateForm

    @property
    def report(self):
        return self.object

    def get_success_url(self):
        return reverse_lazy(
            'wizard_update',
            kwargs={
                'step': 0,
                'uuid': self.report.uuid,
            },
        )


class ReportAccessView(
    ReportBaseView,
    views.edit.UpdateView,
):<commit_msg>only set key if its defined<commit_after>import json
import logging

from ratelimit.decorators import ratelimit

from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.sites.shortcuts import get_current_site
from django.core.urlresolvers import reverse_lazy
from django.http import HttpResponse
from django.shortcuts import render
from django.utils.html import conditional_escape
from django.views import generic as views

from . import forms, models
from ..evaluation.models import EvalRow
from ..utils.api import MatchingApi, NotificationApi
from .models import MatchReport, Report, SentFullReport
from .report_delivery import MatchReportContent, PDFFullReport

User = get_user_model()
logger = logging.getLogger(__name__)


class SecretKeyStorageHelper(object):

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

    def set_secret_key(self, key):
        self.view.request.session['secret_key'] = key

    @property
    def secret_key(self):
        return self.view.request.session.get('secret_key')


class ReportBaseView(views.edit.ModelFormMixin):
    model = models.Report
    storage_helper = SecretKeyStorageHelper
    template_name = 'callisto_core/delivery/form.html'
    context_object_name = 'report'
    slug_field = 'uuid'
    slug_url_kwarg = 'uuid'

    @property
    def storage(self):
        return self.storage_helper(self)

    def _set_key_from_form(self, form):
        if form.data.get('key'):
            self.storage.set_secret_key(form.data['key'])

    def form_valid(self, form):
        self._set_key_from_form(form)
        return super().form_valid(form)


class ReportCreateView(
    ReportBaseView,
    views.edit.CreateView,
):
    form_class = forms.ReportCreateForm

    @property
    def report(self):
        return self.object

    def get_success_url(self):
        return reverse_lazy(
            'wizard_update',
            kwargs={
                'step': 0,
                'uuid': self.report.uuid,
            },
        )


class ReportAccessView(
    ReportBaseView,
    views.edit.UpdateView,
):
{'fraction_non_alphanumeric': 0.08300589390962672, 'fraction_numerical': 0.0004911591355599214, 'mean_word_length': 3.3701716738197427, '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': '6320998', 'n_tokens_mistral': 1206, 'n_tokens_neox': 1166, 'n_words': 285}