<commit_before>from django import forms
from django.utils.translation import ugettext_lazy as _

from open_municipio.acts.models import Act, Transition, ActHasSpeech, Interrogation
from open_municipio.people.models import Institution, InstitutionCharge

class ActTitleForm(forms.ModelForm):
    class Meta:
        model = Act
        fields = ('id', 'title',)


class ActDescriptionForm(forms.ModelForm):
    class Meta:
        model = Act
        fields = ('id', 'description',)


class ActTransitionForm(forms.ModelForm):
    """
    A form to change status of act
    """
    def save(self, commit=True):
        instance = super(forms.ModelForm, self).save(commit=False)
        instance.act = instance.act.downcast()
        if commit:
            instance.save()
        return instance

    class Meta:
        model = Transition
        fields = ('transition_date', 'note', 'final_status', 'act')
        widgets = {
            'act': forms.HiddenInput(),
            'final_status': forms.HiddenInput(),
            'transition_date': forms.DateTimeInput(attrs={'class':'datepicker'})
        }


class ActFinalTransitionForm(ActTransitionForm):
    """
    Extends ActTransitionForm to provide a vote selection
    """

    class Meta(ActTransitionForm.Meta):
        fields = ('transition_date', 'note', 'final_status', 'act', 'votation')
        widgets = {
            'act': forms.HiddenInput(),
            'final_status': forms.Select(choices=[]),
            'transition_date': forms.DateTimeInput(attrs={'class':'datepicker'})
        }

class SpeechAdminForm(forms.ModelForm):

    def is_valid(self):
        if not super(SpeechAdminForm,self).is_valid():
            return False

        author = self.cleaned_data["author"]
        author_external = self.cleaned_data["author_name_when_external"]

        if author == None and (author_external == None or len(author_external) == 0):
            self._errors['author'] = self.error_class([ _("If the author is present in Open Municipio, search it") ])
            self._errors['author_name_when_external'] = self.error_class([ _("Populate the full author name when not present in Open Municipio") ])
            return False

        return True

    class Meta:
        fields = ( 'author', 'author_name_when_external', 'sitting_item','seq_order','title','text','text_url','file','file_url','audio_url','audio_file','votation','initial_time','duration',)


class SpeechInActInlineFormSet(forms.models.BaseInlineFormSet):
    def clean(self):
        super(SpeechInActInlineFormSet,self).clean()

        if len(self.forms) > 2:
            raise forms.ValidationError(_("A maximum of two speech is allowed for each act"))
 
        found_speeches = ()
        speech_types = ()

        for form in self.forms:
            try:
                if "speech" not in form.cleaned_data:
                    continue


                curr_speech = form.cleaned_data["speech"]
                if curr_speech in found_speeches:
                    raise forms.ValidationError(_("The same speech cannot be referred twice by an act"))
                
                found_speeches += (curr_speech, )

                curr_type = form.cleaned_data["relation_type"]
                if curr_type in speech_types:
                    raise forms.ValidationError(_("Speeches must be of different types"))
                if curr_type == u"REF":
                    raise forms.ValidationError(_("Speeches of type REF not allowed in this context"))
                speech_types += (curr_type, )

            except AttributeError:
                # invalid data
                pass

class InterpellationAdminForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(InterpellationAdminForm, self).__init__(*args, **kwargs)
    
        # interpellation targets can be the mayor or members of city government
        self.fields["recipient_set"].queryset = InstitutionCharge.objects.filter(institution__institution_type__in=( Institution.MAYOR, Institution.CITY_GOVERNMENT )).order_by("institution__institution_type","person__last_name","person__first_name")

class InterrogationAdminForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(InterrogationAdminForm, self).__init__(*args, **kwargs)
   
        # interrogation targets can be the mayor or members of city government
        self.fields["recipient_set"].queryset = InstitutionCharge.objects.filter(institution__institution_type__in=( Institution.MAYOR, Institution.CITY_GOVERNMENT )).order_by("institution__institution_type","person__last_name","person__first_name")

    class Meta:
        model = Interrogation
<commit_msg>using TinyMCE for interrogation, interpellation and speeches admin<commit_after>from django import forms
from django.utils.translation import ugettext_lazy as _
from django.forms import CharField
from tinymce.widgets import TinyMCE

from open_municipio.acts.models import Act, Transition, ActHasSpeech, \
        Interrogation, Interpellation
from open_municipio.people.models import Institution, InstitutionCharge

class ActTitleForm(forms.ModelForm):
    class Meta:
        model = Act
        fields = ('id', 'title',)


class ActDescriptionForm(forms.ModelForm):
    class Meta:
        model = Act
        fields = ('id', 'description',)


class ActTransitionForm(forms.ModelForm):
    """
    A form to change status of act
    """
    def save(self, commit=True):
        instance = super(forms.ModelForm, self).save(commit=False)
        instance.act = instance.act.downcast()
        if commit:
            instance.save()
        return instance

    class Meta:
        model = Transition
        fields = ('transition_date', 'note', 'final_status', 'act')
        widgets = {
            'act': forms.HiddenInput(),
            'final_status': forms.HiddenInput(),
            'transition_date': forms.DateTimeInput(attrs={'class':'datepicker'})
        }


class ActFinalTransitionForm(ActTransitionForm):
    """
    Extends ActTransitionForm to provide a vote selection
    """

    class Meta(ActTransitionForm.Meta):
        fields = ('transition_date', 'note', 'final_status', 'act', 'votation')
        widgets = {
            'act': forms.HiddenInput(),
            'final_status': forms.Select(choices=[]),
            'transition_date': forms.DateTimeInput(attrs={'class':'datepicker'})
        }

class SpeechAdminForm(forms.ModelForm):

    text = CharField(widget=TinyMCE(
        attrs={'cols':80,'rows':25},
        mce_attrs={
            'theme': "advanced",
            'theme_advanced_buttons1': "formatselect,bold,italic,underline|,bullist,numlist,|,undo,redo,|,link,unlink,|,code,help",
            'theme_advanced_buttons2': "",
            'theme_advanced_buttons3': "",
            'theme_advanced_blockformats': "p,blockquote",
            'theme_advanced_resizing': True,
            'theme_advanced_statusbar_location': "bottom",
            'theme_advanced_toolbar_location': "top",
            'theme_advanced_path': False
        },
    ),
    required=False)



    def is_valid(self):
        if not super(SpeechAdminForm,self).is_valid():
            return False

        author = self.cleaned_data["author"]
        author_external = self.cleaned_data["author_name_when_external"]

        if author == None and (author_external == None or len(author_external) == 0):
            self._errors['author'] = self.error_class([ _("If the author is present in Open Municipio, search it") ])
            self._errors['author_name_when_external'] = self.error_class([ _("Populate the full author name when not present in Open Municipio") ])
            return False

        return True

    class Meta:
        fields = ( 'author', 'author_name_when_external', 'sitting_item','seq_order','title','text','text_url','file','file_url','audio_url','audio_file','votation','initial_time','duration',)


class SpeechInActInlineFormSet(forms.models.BaseInlineFormSet):
    def clean(self):
        super(SpeechInActInlineFormSet,self).clean()

        if len(self.forms) > 2:
            raise forms.ValidationError(_("A maximum of two speech is allowed for each act"))
 
        found_speeches = ()
        speech_types = ()

        for form in self.forms:
            try:
                if "speech" not in form.cleaned_data:
                    continue


                curr_speech = form.cleaned_data["speech"]
                if curr_speech in found_speeches:
                    raise forms.ValidationError(_("The same speech cannot be referred twice by an act"))
                
                found_speeches += (curr_speech, )

                curr_type = form.cleaned_data["relation_type"]
                if curr_type in speech_types:
                    raise forms.ValidationError(_("Speeches must be of different types"))
                if curr_type == u"REF":
                    raise forms.ValidationError(_("Speeches of type REF not allowed in this context"))
                speech_types += (curr_type, )

            except AttributeError:
                # invalid data
                pass

class InterpellationAdminForm(forms.ModelForm):

    text = CharField(widget=TinyMCE(
        attrs={'cols':80,'rows':25},
        mce_attrs={
            'theme': "advanced",
            'theme_advanced_buttons1': "formatselect,bold,italic,underline|,bullist,numlist,|,undo,redo,|,link,unlink,|,code,help",
            'theme_advanced_buttons2': "",
            'theme_advanced_buttons3': "",
            'theme_advanced_blockformats': "p,blockquote",
            'theme_advanced_resizing': True,
            'theme_advanced_statusbar_location': "bottom",
            'theme_advanced_toolbar_location': "top",
            'theme_advanced_path': False
        },
    ),
    required=False)

    answer_text = CharField(widget=TinyMCE(
        attrs={'cols':80,'rows':25},
        mce_attrs={
            'theme': "advanced",
            'theme_advanced_buttons1': "formatselect,bold,italic,underline|,bullist,numlist,|,undo,redo,|,link,unlink,|,code,help",
            'theme_advanced_buttons2': "",
            'theme_advanced_buttons3': "",
            'theme_advanced_blockformats': "p,blockquote",
            'theme_advanced_resizing': True,
            'theme_advanced_statusbar_location': "bottom",
            'theme_advanced_toolbar_location': "top",
            'theme_advanced_path': False
        },
    ),
    required=False)


    def __init__(self, *args, **kwargs):
        super(InterpellationAdminForm, self).__init__(*args, **kwargs)
    
        # interpellation targets can be the mayor or members of city government
        self.fields["recipient_set"].queryset = InstitutionCharge.objects.filter(institution__institution_type__in=( Institution.MAYOR, Institution.CITY_GOVERNMENT )).order_by("institution__institution_type","person__last_name","person__first_name")

    class Meta:
        model = Interpellation

class InterrogationAdminForm(forms.ModelForm):

    text = CharField(widget=TinyMCE(
        attrs={'cols':80,'rows':25},
        mce_attrs={
            'theme': "advanced",
            'theme_advanced_buttons1': "formatselect,bold,italic,underline|,bullist,numlist,|,undo,redo,|,link,unlink,|,code,help",
            'theme_advanced_buttons2': "",
            'theme_advanced_buttons3': "",
            'theme_advanced_blockformats': "p,blockquote",
            'theme_advanced_resizing': True,
            'theme_advanced_statusbar_location': "bottom",
            'theme_advanced_toolbar_location': "top",
            'theme_advanced_path': False
        },
    ),
    required=False)

    answer_text = CharField(widget=TinyMCE(
        attrs={'cols':80,'rows':25},
        mce_attrs={
            'theme': "advanced",
            'theme_advanced_buttons1': "formatselect,bold,italic,underline|,bullist,numlist,|,undo,redo,|,link,unlink,|,code,help",
            'theme_advanced_buttons2': "",
            'theme_advanced_buttons3': "",
            'theme_advanced_blockformats': "p,blockquote",
            'theme_advanced_resizing': True,
            'theme_advanced_statusbar_location': "bottom",
            'theme_advanced_toolbar_location': "top",
            'theme_advanced_path': False
        },
    ),
    required=False)

    def __init__(self, *args, **kwargs):
        super(InterrogationAdminForm, self).__init__(*args, **kwargs)
   
        # interrogation targets can be the mayor or members of city government
        self.fields["recipient_set"].queryset = InstitutionCharge.objects.filter(institution__institution_type__in=( Institution.MAYOR, Institution.CITY_GOVERNMENT )).order_by("institution__institution_type","person__last_name","person__first_name")

    class Meta:
        model = Interrogation

{'fraction_non_alphanumeric': 0.113599126160568, 'fraction_numerical': 0.0030428337364437857, 'mean_word_length': 2.8736778482925356, 'pattern_counts': {'":': 2, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '638337', 'n_tokens_mistral': 3708, 'n_tokens_neox': 3555, 'n_words': 719}