# -*- coding: utf-8 -*- from django import forms from project.commons.forms import set_field_error from .base_forms import BaseModelForm from ..models import Platejka from .. import consts class PlatejkaForm(BaseModelForm): """Форма редактирования платежного поручения.""" conditional_fields = ['nds_type', 'nds_value', 'tax_status', 'tax_base', 'tax_type', 'tax_bk', 'tax_okato', 'tax_period',] class Meta: model = Platejka fields = ('platej_type', 'doc_num', 'doc_date', 'bank_account', 'client', 'nds_type', 'nds_value', # поля только для перевода денег # поля только для оплаты налогов 'tax_status', 'tax_base', 'tax_type', 'tax_num', 'tax_date', 'tax_bk', 'tax_okato', 'tax_period', # опять общие поля 'doc_total', 'payment_type', 'payment_order', 'doc_info', ) _radioselect = forms.RadioSelect _textarea = forms.Textarea(attrs={'cols': 80, 'rows': 5}) widgets = { #'platej_type': _radioselect, 'nds_type': _radioselect, 'doc_info': _textarea, } def __init__(self, user, *args, **kwargs): super(PlatejkaForm, self).__init__(user, *args, **kwargs) f = self.fields f['tax_date'].widget.attrs['class'] = 'has-datepicker' def clean(self): super(PlatejkaForm, self).clean() cleaned_data = self.cleaned_data # поля становятся обязательными в зависимости от того, какой тип платежки выбран platej_type = cleaned_data.get('platej_type') if platej_type == consts.PLATEJ_TYPE_COMMERCE: # коммерческое (перевод денег) nds_type = cleaned_data.get('nds_type') nds_value = cleaned_data.get('nds_value') if not nds_type: set_field_error(self, 'nds_type') if not nds_value: set_field_error(self, 'nds_value') elif platej_type == consts.PLATEJ_TYPE_TAX: # налоги tax_status = cleaned_data.get('tax_status') tax_base = cleaned_data.get('tax_base') tax_type = cleaned_data.get('tax_type') tax_bk = cleaned_data.get('tax_bk') tax_okato = cleaned_data.get('tax_okato') tax_period = cleaned_data.get('tax_period') if not tax_status: set_field_error(self, 'tax_status') if not tax_base: set_field_error(self, 'tax_base') if not tax_type: set_field_error(self, 'tax_type') if not tax_bk: set_field_error(self, 'tax_bk') if not tax_okato: set_field_error(self, 'tax_okato') if not tax_period: set_field_error(self, 'tax_period') return cleaned_data class PlatejkaAdminForm(PlatejkaForm): """Форма редактирования платежного поручения - для админки.""" class Meta(PlatejkaForm.Meta): fields = None widgets = { 'doc_info': forms.Textarea(attrs={'cols': 80, 'rows': 5}), } def __init__(self, *args, **kwargs): # обязательно нужно вызывать родительский __init__ и передавать ему None вместо user - иначе глюки ! super(PlatejkaAdminForm, self).__init__(None, *args, **kwargs)