from crispy_forms.helper import FormHelper from crispy_forms.layout import HTML, Fieldset, Layout, Submit from django import forms from django.db import transaction from django.forms import ModelForm from django.utils.translation import ugettext_lazy as _ from floppyforms.widgets import ClearableFileInput import crispy_layout_mixin import sapl from crispy_layout_mixin import form_actions from .models import Filiacao, Legislatura, Mandato, Parlamentar class ImageThumbnailFileInput(ClearableFileInput): template_name = 'floppyforms/image_thumbnail.html' class ParlamentarForm(ModelForm): class Meta: model = Parlamentar exclude = [] widgets = {'fotografia': sapl.utils.ImageThumbnailFileInput, 'biografia': forms.Textarea( attrs={'id': 'texto-rico'})} class ParlamentarCreateForm(ParlamentarForm): legislatura = forms.ModelChoiceField( label=_('Legislatura'), required=True, queryset=Legislatura.objects.all().order_by('-data_inicio'), empty_label='----------', ) data_expedicao_diploma = forms.DateField( label=_('Expedição do Diploma'), required=True, ) @transaction.atomic def save(self, commit=True): parlamentar = super(ParlamentarCreateForm, self).save(commit) legislatura = self.cleaned_data['legislatura'] Mandato.objects.create( parlamentar=parlamentar, legislatura=legislatura, data_fim_mandato=legislatura.data_fim, data_expedicao_diploma=self.cleaned_data['data_expedicao_diploma']) return parlamentar class FiliacaoForm(ModelForm): class Meta: model = Filiacao fields = ['partido', 'data', 'data_desfiliacao'] def __init__(self, *args, **kwargs): row1 = crispy_layout_mixin.to_row( [('partido', 4), ('data', 4), ('data_desfiliacao', 4)]) self.helper = FormHelper() self.helper.layout = Layout( Fieldset(_('Adicionar Filiação'), row1, form_actions()) ) super(FiliacaoForm, self).__init__( *args, **kwargs) class FiliacaoEditForm(FiliacaoForm): def __init__(self, *args, **kwargs): super(FiliacaoEditForm, self).__init__( *args, **kwargs) self.helper.layout[0][-1:] = form_actions(more=[ HTML(' '), Submit('excluir', 'Excluir', css_class='btn btn-primary')])