<commit_before>import re
from datetime import datetime
import pendulum

from django import forms
from django.contrib import messages
from django.db import connection
from django.http import HttpResponse
from django.urls import reverse
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _
from django.views.generic.base import View, TemplateView
from django.views.generic.edit import FormView

from bluebottle.analytics.reports import MetricsReport
from .tasks import generate_participation_metrics


class ParticipationMetricsForm(forms.Form):
    CURRENT_YEAR = datetime.now().year
    START_YEAR_CHOICES = ((year, '{}'.format(year)) for year in range(CURRENT_YEAR - 3, CURRENT_YEAR + 1))
    END_YEAR_CHOICES = ((year, '{}'.format(year)) for year in range(CURRENT_YEAR - 3, CURRENT_YEAR + 1))

    email = forms.EmailField(disabled=True, required=True)
    start_year = forms.ChoiceField(choices=START_YEAR_CHOICES,
                                   initial=CURRENT_YEAR - 3,
                                   label=_('Start Year'),
                                   required=True)
    end_year = forms.ChoiceField(choices=END_YEAR_CHOICES, initial=CURRENT_YEAR, label=_('End Year'), required=True)

    def clean(self):
        cleaned_data = super(ParticipationMetricsForm, self).clean()
        start_year = cleaned_data.get("start_year")
        end_year = cleaned_data.get("end_year")

        if start_year > end_year:
            raise forms.ValidationError(_('Start year cannot be greater than the End year'), code='invalid')

    def generate_metrics(self):
        email = self.cleaned_data['email']
        start_year = self.cleaned_data['start_year']
        end_year = self.cleaned_data['end_year']
        tenant = connection.tenant.client_name
        generate_participation_metrics.delay(tenant, email, start_year, end_year)


class ParticipationMetricsFormView(FormView):
    form_class = ParticipationMetricsForm
    template_name = 'participation_metrics.html'

    def get_success_url(self):
        return reverse('participation-metrics')

    def get_initial(self):
        initial = super(ParticipationMetricsFormView, self).get_initial()
        initial['email'] = self.request.user
        return initial<commit_msg>Use email for email field (#3277)<commit_after>import re
from datetime import datetime
import pendulum

from django import forms
from django.contrib import messages
from django.db import connection
from django.http import HttpResponse
from django.urls import reverse
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _
from django.views.generic.base import View, TemplateView
from django.views.generic.edit import FormView

from bluebottle.analytics.reports import MetricsReport
from .tasks import generate_participation_metrics


class ParticipationMetricsForm(forms.Form):
    CURRENT_YEAR = datetime.now().year
    START_YEAR_CHOICES = ((year, '{}'.format(year)) for year in range(CURRENT_YEAR - 3, CURRENT_YEAR + 1))
    END_YEAR_CHOICES = ((year, '{}'.format(year)) for year in range(CURRENT_YEAR - 3, CURRENT_YEAR + 1))

    email = forms.EmailField(disabled=True, required=True)
    start_year = forms.ChoiceField(choices=START_YEAR_CHOICES,
                                   initial=CURRENT_YEAR - 3,
                                   label=_('Start Year'),
                                   required=True)
    end_year = forms.ChoiceField(choices=END_YEAR_CHOICES, initial=CURRENT_YEAR, label=_('End Year'), required=True)

    def clean(self):
        cleaned_data = super(ParticipationMetricsForm, self).clean()
        start_year = cleaned_data.get("start_year")
        end_year = cleaned_data.get("end_year")

        if start_year > end_year:
            raise forms.ValidationError(_('Start year cannot be greater than the End year'), code='invalid')

    def generate_metrics(self):
        email = self.cleaned_data['email']
        start_year = self.cleaned_data['start_year']
        end_year = self.cleaned_data['end_year']
        tenant = connection.tenant.client_name
        generate_participation_metrics.delay(tenant, email, start_year, end_year)


class ParticipationMetricsFormView(FormView):
    form_class = ParticipationMetricsForm
    template_name = 'participation_metrics.html'

    def get_success_url(self):
        return reverse('participation-metrics')

    def get_initial(self):
        initial = super(ParticipationMetricsFormView, self).get_initial()
        initial['email'] = self.request.user.email
        return initial
{'fraction_non_alphanumeric': 0.08869565217391304, 'fraction_numerical': 0.003043478260869565, 'mean_word_length': 3.5964035964035963, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '639872', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1269, 'n_words': 306}