class ProjectForm(forms.ModelForm): topics = forms.ModelMultipleChoiceField( queryset=Topic.objects.all(), widget=forms.SelectMultiple(attrs={ 'inline': True, 'class': 'multiselect', }), required=False) events = forms.ModelMultipleChoiceField( queryset=Event.objects.all(), widget=forms.SelectMultiple(attrs={ 'inline': True, 'class': 'multiselect', }), required=False) technologies = forms.ModelMultipleChoiceField( queryset=Technology.objects.all(), widget=forms.SelectMultiple(attrs={ 'inline': True, 'class': 'multiselect', }), required=False) color = forms.CharField(widget=forms.TextInput(attrs={ 'inline': True, 'class': 'colorpicker', }), required=False) class Meta: model = Project fields = [ 'title', 'description', 'status', 'public_url', 'dev_url', 'screenshot', 'git_url', 'topics', 'events', 'technologies', 'color' ]
class PontoForm(forms.ModelForm): class Meta: model = Ponto fields = [ 'coll', 'req_min_date', 'req_max_date', 'missoes', 'satelites' ] coll = forms.gis.GeometryCollectionField(required=False, widget=PointWidget, label='') req_min_date = forms.DateField( required=False, widget=forms.DateInput(attrs={ 'format': 'DD-MM-YYYY', 'type': 'datepicker' }), label='Data mínima desejada') #req_max_date = forms.DateField(required=False, widget=SelectDateWidget(years=range(1971, 2019)), label='Data máxima desejada') req_max_date = forms.DateField( required=False, widget=forms.DateInput(attrs={ 'format': 'DD-MM-YYYY', 'type': 'datepicker' }), label='Data máxima desejada') missoes = forms.ModelMultipleChoiceField( queryset=Missao.objects.all(), widget=forms.CheckboxSelectMultiple, required=False, label='Missão') satelites = forms.ModelMultipleChoiceField( queryset=Satelite.objects.all(), widget=forms.CheckboxSelectMultiple, required=False, label='Satélite') #chaves = forms.ModelMultipleChoiceField( #queryset = Chave.objects.all(), #widget = forms.CheckboxSelectMultiple, #required=False, #label='Filtro' #) def clean_req_date(self): cleaned_data = super(PontoForm, self).clean() req_min_date = cleaned_data.get("req_min_date") req_max_date = cleaned_data.get("req_max_date") if req_min_date and req_max_date: if req_max_date < req_min_date: raise forms.ValidationError( "A data máxima não pode ser menor que a data mínima.") return cleaned_data
class ActionForm(forms.Form): action = forms.ChoiceField(label=_('Action'), choices=()) objects = forms.ModelMultipleChoiceField(queryset=None, widget=forms.MultipleHiddenInput, required=False) def __init__(self, *args, **kwargs): self.actions = kwargs.pop('actions') self.queryset = kwargs.pop('queryset') super(ActionForm, self).__init__(*args, **kwargs) self.fields['action'].choices = self.get_action_choices() self.fields['objects'].queryset = self.queryset def get_action_choices(self): return [('', _(u'Select an action …'))] + [ (key, action.name) for key, action in self.actions.items() ] def get_selected_queryset(self): return self.cleaned_data['objects'] def perform_action(self, backend, request): action_slug = self.cleaned_data['action'] action = self.actions[action_slug] queryset = self.get_selected_queryset() action.perform(backend, request, queryset)
class NewChatroomForm(forms.Form): name = forms.CharField() participants = forms.ModelMultipleChoiceField(User.objects.all()) # open_from = ? # open_until = ? def __init__(self, user, *args, **kwargs): super(NewChatroomForm, self).__init__(*args, **kwargs) self.fields['participants'].queryset = User.objects.exclude(pk=user.pk)
class TransactionRefundForm(forms.Form): items = forms.ModelMultipleChoiceField(queryset=BoughtItem.objects.none(), required=False, widget=forms.CheckboxSelectMultiple) amount = forms.DecimalField(required=False, max_digits=9, decimal_places=2, localize=False, min_value=0) def __init__(self, transaction, *args, **kwargs): super(TransactionRefundForm, self).__init__(*args, **kwargs) self.transaction = transaction self.fields[ 'amount'].max_value = self.transaction.get_refundable_amount() self.fields['amount'].initial = self.transaction.get_refundable_amount( ) self.fields['items'].queryset = self.transaction.bought_items.all() self.fields['items'].initial = self.fields['items'].queryset def clean_items(self): """ Check that items being refunded are not already refunded. If they are, ignore them. """ items = self.cleaned_data['items'] return [item for item in items if item.status != BoughtItem.REFUNDED] def clean_amount(self): amount = self.cleaned_data['amount'] refundable_amount = self.transaction.get_refundable_amount() if amount > refundable_amount: msg = 'Amount to refund is larger than amount refundable. Only {0} is left on this transaction.'.format( format_money(refundable_amount, self.transaction.event.currency)) raise ValidationError(msg) return amount def clean(self): cleaned_data = super(TransactionRefundForm, self).clean() amount = cleaned_data.get('amount') items = cleaned_data.get('items') if amount == 0 and len(items) == 0: msg = "Select items or specify a value to refund." raise ValidationError(msg)
class LessonCopyForm(forms.Form): copy_lessonsteps = forms.ModelMultipleChoiceField( queryset=None, widget=CheckboxSelectMultiple, required=False ) copy_lesson = forms.BooleanField( required=False ) def __init__(self, *args, **kwargs): lesson_pk = kwargs.pop('lesson_pk', None) self.lesson = get_object_or_404(Lesson, pk=lesson_pk) super(LessonCopyForm, self).__init__(*args, **kwargs) self.fields['copy_lessonsteps'].queryset = self.lesson.get_children() def clean(self): if not self.cleaned_data['copy_lesson']: if (self.cleaned_data['copy_lessonsteps'] == []): raise ValidationError('''Bitte etwas zum Kopieren auswählen!''')
class MultiModelForm(forms.Form): mods = forms.ModelMultipleChoiceField( queryset=SomeModel2.objects.all())
class BankTransactionListForm(forms.Form): label = forms.CharField( max_length=255, required=False, widget=forms.TextInput(attrs={ 'placeholder': ugettext_lazy('Label'), })) date_start = forms.DateField( required=False, widget=Datepicker(attrs={ 'placeholder': ugettext_lazy('Date start'), }), ) date_end = forms.DateField( required=False, widget=Datepicker(attrs={ 'placeholder': ugettext_lazy('Date end'), }), ) amount_min = forms.DecimalField( max_digits=10, decimal_places=2, localize=True, required=False, widget=forms.NumberInput( attrs={ 'placeholder': ugettext_lazy('Minimum amount'), }), ) amount_max = forms.DecimalField( max_digits=10, decimal_places=2, localize=True, required=False, widget=forms.NumberInput( attrs={ 'placeholder': ugettext_lazy('Maximum amount'), }), ) status = forms.ChoiceField( choices=(('', ugettext_lazy('Status?')), ) + BankTransaction.STATUSES, initial='', required=False, ) reconciled = forms.NullBooleanField(required=False) tags = forms.ModelMultipleChoiceField( queryset=BankTransactionTag.objects.none(), required=False) operation = forms.ChoiceField( choices=(), required=False, ) def __init__(self, user, bt_ids, submit, *args, **kwargs): super(BankTransactionListForm, self).__init__(*args, **kwargs) self.fields['tags'].queryset = ( BankTransactionTag.objects.get_user_tags_queryset(user)) self.fields['reconciled'].widget.choices[0] = ('1', _('Reconciled?')) for pk in bt_ids: self.fields['banktransaction_' + str(pk)] = forms.BooleanField( required=False, widget=forms.CheckboxInput(attrs={'data-id': pk})) choices = () if user.has_perm('banktransactions.change_banktransaction'): choices += ( ('reconcile', _('Reconcile')), ('unreconcile', _('Unreconcile')), ) if user.has_perm('banktransactions.delete_banktransaction'): choices += (('delete', _('Delete')), ) if choices: self.fields['operation'].choices = choices self._submit = submit def clean(self): cleaned_data = super(BankTransactionListForm, self).clean() if self._submit == 'filter': date_start = cleaned_data.get('date_start') date_end = cleaned_data.get('date_end') if date_start and date_end and date_start > date_end: raise forms.ValidationError( _("Date start could not be greater than date end."), code='date_start_greater', ) amount_min = cleaned_data.get('amount_min', None) amount_max = cleaned_data.get('amount_max', None) if (amount_min is not None and amount_max is not None and amount_min > amount_max): raise forms.ValidationError( _("Minimum amount could not be greater than maximum " "amount."), code='amount_min_greater', ) if self._submit == 'action' and cleaned_data['operation']: ids = set() for name, value in cleaned_data.items(): if name.startswith('banktransaction_') and value: ids.add(int(name[len('banktransaction_'):])) if not ids: raise forms.ValidationError( _('To apply operations, you need to select some bank ' 'transactions.'), code='no_id', ) cleaned_data['banktransactions'] = ids return cleaned_data