<commit_before>from __future__ import unicode_literals
from crispy_forms import layout
from django import forms
from django.db import models
from django_cradmin import crapp
from django_cradmin import crinstance
from django_cradmin.viewhelpers import create
from django_cradmin.viewhelpers import crudbase
from django.utils.translation import ugettext_lazy as _
from django_cradmin.widgets.datetimepicker import DateTimePickerWidget

from devilry.apps.core.models import Period
from devilry.utils import nodenamesuggestor


class CreateUpdateForm(forms.ModelForm):
    class Meta:
        model = Period
        fields = [
            'long_name',
            'short_name',
            'start_time',
            'end_time',
        ]

    def __init__(self, *args, **kwargs):
        super(CreateUpdateForm, self).__init__(*args, **kwargs)
        self.fields['long_name'].help_text = _('Type the name of your semester.')
        self.fields['start_time'].widget = DateTimePickerWidget(required=True)
        self.fields['end_time'].widget = DateTimePickerWidget(required=True)


class CreateForm(forms.ModelForm):
    class Meta:
        model = Period
        fields = CreateUpdateForm.Meta.fields + [
            'parentnode'
        ]

    def __init__(self, *args, **kwargs):
        self.subject = kwargs.pop('subject')
        super(CreateForm, self).__init__(*args, **kwargs)

        # We ignore this, we just need to include it to be able to
        # change the value in clean()
        self.fields['parentnode'].widget = forms.HiddenInput()
        self.fields['parentnode'].required = False

    def clean(self):
        cleaned_data = super(CreateForm, self).clean()
        cleaned_data['parentnode'] = self.subject
        return cleaned_data


class CreateUpdateMixin(object):
    def get_field_layout(self):
        return [
            layout.Div(
                layout.Field('long_name', placeholder=_('Example: Spring 2025'),
                             focusonme='focusonme'),
                layout.Field('short_name', placeholder=_('Example: spring2025')),
                layout.Div(
                    layout.Div(
                        layout.Field('start_time'),
                        css_class='col-sm-6'
                    ),
                    layout.Div(
                        layout.Field('end_time'),
                        css_class='col-sm-6'
                    ),
                    css_class='row'
                ),
                css_class='cradmin-globalfields'
            )
        ]


class CreateView(crudbase.OnlySaveButtonMixin, CreateUpdateMixin, create.CreateView):
    form_class = CreateForm
    model = Period

    def dispatch(self, *args, **kwargs):
        self.subject = self.request.cradmin_role
        self.previous_period = self.subject.periods \
            .order_by('end_time') \
            .last()
        return super(CreateView, self).dispatch(*args, **kwargs)

    def get_pagetitle(self):
        return u'{} - {}'.format(self.get_pageheading(), self.subject.short_name)

    def get_pageheading(self):
        return _('Create new semester')

    def get_form_kwargs(self):
        kwargs = super(CreateView, self).get_form_kwargs()
        kwargs['subject'] = self.subject
        return kwargs

    def get_initial(self):
        initial = super(CreateView, self).get_initial()
        if self.previous_period:
            namesuggestion = nodenamesuggestor.Suggest(long_name=self.previous_period.long_name,
                                                       short_name=self.previous_period.short_name)
            if namesuggestion.has_suggestion():
                namecollision_queryset = self.subject.periods.filter(
                    models.Q(long_name=namesuggestion.suggested_long_name) |
                    models.Q(short_name=namesuggestion.suggested_short_name))
                if not namecollision_queryset.exists():
                    initial['long_name'] = namesuggestion.suggested_long_name
                    initial['short_name'] = namesuggestion.suggested_short_name
        return initial

    def get_success_url(self):
        return crinstance.reverse_cradmin_url(
            instanceid='devilry_admin_periodadmin',
            appname='overview',
            roleid=self.created_period.id
        )

    def form_saved(self, object):
        self.created_period = object


class App(crapp.App):
    appurls = [
        crapp.Url(r'^$', CreateView.as_view(), name=crapp.INDEXVIEW_NAME)
    ]
<commit_msg>admin...createperiod: Inherit from CreateUpdateForm - makes it use the datetime picker.<commit_after>from __future__ import unicode_literals
from crispy_forms import layout
from django import forms
from django.db import models
from django_cradmin import crapp
from django_cradmin import crinstance
from django_cradmin.viewhelpers import create
from django_cradmin.viewhelpers import crudbase
from django.utils.translation import ugettext_lazy as _
from django_cradmin.widgets.datetimepicker import DateTimePickerWidget

from devilry.apps.core.models import Period
from devilry.utils import nodenamesuggestor


class CreateUpdateForm(forms.ModelForm):
    class Meta:
        model = Period
        fields = [
            'long_name',
            'short_name',
            'start_time',
            'end_time',
        ]

    def __init__(self, *args, **kwargs):
        super(CreateUpdateForm, self).__init__(*args, **kwargs)
        self.fields['long_name'].help_text = _('Type the name of your semester.')
        self.fields['start_time'].widget = DateTimePickerWidget(required=True)
        self.fields['end_time'].widget = DateTimePickerWidget(required=True)


class CreateForm(CreateUpdateForm):
    class Meta:
        model = Period
        fields = CreateUpdateForm.Meta.fields + [
            'parentnode'
        ]

    def __init__(self, *args, **kwargs):
        self.subject = kwargs.pop('subject')
        super(CreateForm, self).__init__(*args, **kwargs)

        # We ignore this, we just need to include it to be able to
        # change the value in clean()
        self.fields['parentnode'].widget = forms.HiddenInput()
        self.fields['parentnode'].required = False

    def clean(self):
        cleaned_data = super(CreateForm, self).clean()
        cleaned_data['parentnode'] = self.subject
        return cleaned_data


class CreateUpdateMixin(object):
    def get_field_layout(self):
        return [
            layout.Div(
                layout.Field('long_name', placeholder=_('Example: Spring 2025'),
                             focusonme='focusonme'),
                layout.Field('short_name', placeholder=_('Example: spring2025')),
                layout.Div(
                    layout.Div(
                        layout.Field('start_time'),
                        css_class='col-sm-6'
                    ),
                    layout.Div(
                        layout.Field('end_time'),
                        css_class='col-sm-6'
                    ),
                    css_class='row'
                ),
                css_class='cradmin-globalfields'
            )
        ]


class CreateView(crudbase.OnlySaveButtonMixin, CreateUpdateMixin, create.CreateView):
    form_class = CreateForm
    model = Period

    def dispatch(self, *args, **kwargs):
        self.subject = self.request.cradmin_role
        self.previous_period = self.subject.periods \
            .order_by('end_time') \
            .last()
        return super(CreateView, self).dispatch(*args, **kwargs)

    def get_pagetitle(self):
        return u'{} - {}'.format(self.get_pageheading(), self.subject.short_name)

    def get_pageheading(self):
        return _('Create new semester')

    def get_form_kwargs(self):
        kwargs = super(CreateView, self).get_form_kwargs()
        kwargs['subject'] = self.subject
        return kwargs

    def get_initial(self):
        initial = super(CreateView, self).get_initial()
        if self.previous_period:
            namesuggestion = nodenamesuggestor.Suggest(long_name=self.previous_period.long_name,
                                                       short_name=self.previous_period.short_name)
            if namesuggestion.has_suggestion():
                namecollision_queryset = self.subject.periods.filter(
                    models.Q(long_name=namesuggestion.suggested_long_name) |
                    models.Q(short_name=namesuggestion.suggested_short_name))
                if not namecollision_queryset.exists():
                    initial['long_name'] = namesuggestion.suggested_long_name
                    initial['short_name'] = namesuggestion.suggested_short_name
        return initial

    def get_success_url(self):
        return crinstance.reverse_cradmin_url(
            instanceid='devilry_admin_periodadmin',
            appname='overview',
            roleid=self.created_period.id
        )

    def form_saved(self, object):
        self.created_period = object


class App(crapp.App):
    appurls = [
        crapp.Url(r'^$', CreateView.as_view(), name=crapp.INDEXVIEW_NAME)
    ]

{'fraction_non_alphanumeric': 0.09595350367364842, 'fraction_numerical': 0.002193222941111964, 'mean_word_length': 2.2583065380493035, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4224192', 'n_tokens_mistral': 2517, 'n_tokens_neox': 2450, 'n_words': 481}