class UserForm(forms.Form): age = forms.IntegerField( label="Tuổi", required=True ) sex = forms.ModelChoiceField( queryset=Sex.objects.all(), label=u'Giới tính', empty_label=None ) height = forms.DecimalField( label="Chiều cao", required=True, widget=forms.NumberInput( attrs={'placeholder': 'Đơn vị là cm'} ) ) weight = forms.DecimalField( label="Cân nặng", required=True, widget=forms.NumberInput( attrs={'placeholder': 'Đơn vị là kg'} ) ) activity_level = forms.ModelChoiceField( queryset=ActivityLevel.objects.all(), label=u'Mức độ hoạt động', empty_label=None, ) def __init__(self, *args, **kwargs): super(UserForm, self).__init__(*args, **kwargs) self.helper = FormHelper() self.helper.form_class = "form-horizontal" self.helper.form_method = 'post' self.helper.form_action = '' self.helper.html5_required = True self.helper.layout = Layout( 'age', 'height', 'weight', InlineRadios('sex'), 'activity_level', ) self.helper.add_input(Submit('submit', 'Tư vấn'))
class Meta: model = Patient fields = ('studyId', 'patientCode', 'age', 'gender') widgets = { 'age': forms.NumberInput(attrs={ 'max': 200, 'min': 0, 'step': 1 }), 'studyId': forms.Select }
class NumberForm(forms.Form): num = forms.DecimalField(widget=forms.NumberInput(attrs={ 'min': 5, 'max': 10 }))
def __init__(self, path, user, *args, **kwargs): super(DynForm, self).__init__(*args, **kwargs) pathComplete = str(path) elements = pathComplete.split("/") studyName = elements[2] stageName = elements[3] for study in Study.objects.filter(studyName=str(studyName)): tempEntity = [] for entity in Patient.objects.filter( studyId__idStudy=study.idStudy, userOwner=user): tempLabel = str(entity).split(" ") patientLabel = tempLabel[0] + ". ID: " + tempLabel[1] tempEntity.append((patientLabel, patientLabel)) choiceEnt = tuple(tempEntity) self.fields['Paciente'] = ChoiceField( tempEntity, initial=tempEntity[len(tempEntity) - 1]) self.fields['Paciente'].widget.attrs['class'] = 'form-control' for stage in Stage.objects.filter(studyId=study.idStudy): if stage.stageType == str(stageName): questionList = [] for questionGroups in Question_Groups.objects.filter( groupStage__idStage=stage.idStage).order_by( 'order'): for question in questionGroups.idQuestions.all(): questionList.append(question) for question in questionList: if question.questionsType == 'Char': self.fields['%s' % question] = CharField( max_length=255, required=False) self.fields['%s' % question].label = str( question.questionsLabel) self.fields['%s' % question].help_text = str( question.questionHelp) self.fields['%s' % question].widget.attrs[ 'class'] = 'form-control' if question.questionsType == 'Int': self.fields['%s' % question] = IntegerField( widget=forms.NumberInput(), required=False) self.fields['%s' % question].label = str( question.questionsLabel) self.fields['%s' % question].help_text = str( question.questionHelp) self.fields['%s' % question].widget.attrs[ 'class'] = 'form-control' self.fields['%s' % question].widget.attrs[ 'min'] = question.questionMin self.fields['%s' % question].widget.attrs[ 'max'] = question.questionMax self.fields['%s' % question].widget.attrs['step'] = 1 if question.questionsType == 'Real': self.fields['%s' % question] = FloatField( widget=forms.NumberInput(), required=False) self.fields['%s' % question].label = str( question.questionsLabel) self.fields['%s' % question].help_text = str( question.questionHelp) self.fields['%s' % question].widget.attrs[ 'class'] = 'form-control' self.fields['%s' % question].widget.attrs[ 'min'] = question.questionMin self.fields['%s' % question].widget.attrs[ 'max'] = question.questionMax self.fields[ '%s' % question].widget.attrs['step'] = 0.1 if question.questionsType == 'Date': self.fields['%s' % question] = DateField( widget=forms.DateInput(), required=False) self.fields['%s' % question].label = str( question.questionsLabel) self.fields['%s' % question].help_text = str( question.questionHelp) self.fields['%s' % question].widget.attrs[ 'class'] = 'form-control' if question.questionsType == 'Time': self.fields['%s' % question] = TimeField( widget=forms.TimeInput(), required=False) self.fields['%s' % question].label = str( question.questionsLabel) self.fields['%s' % question].help_text = str( question.questionHelp) self.fields['%s' % question].widget.attrs[ 'class'] = 'form-control' if question.questionsType == 'Bool': self.fields[ '%s' % question] = NullBooleanField( widget=forms.NullBooleanSelect(), required=False) self.fields['%s' % question].label = str( question.questionsLabel) self.fields['%s' % question].help_text = str( question.questionHelp) self.fields['%s' % question].widget.attrs.update({ 'onclick': "toggle_id_%s()" % question, }) self.fields['%s' % question].widget.attrs[ 'class'] = 'form-control' if question.questionsType == 'Img': self.fields['%s' % question] = FileField( required=False) self.fields['%s' % question].label = str( question.questionsLabel) self.fields['%s' % question].help_text = str( question.questionHelp) self.fields['%s' % question].widget.attrs[ 'class'] = 'form-control' if question.questionsType == 'Enum': choices = Choices.objects.filter( questionId__questionsId=question. questionsId) list_of_choices = [] for choice in choices: list_of_choices.append((choice, choice)) tuple_of_choices = tuple(list_of_choices) self.fields['%s' % question] = ChoiceField( widget=forms.Select(), choices=tuple_of_choices, required=False) self.fields['%s' % question].label = str( question.questionsLabel) self.fields['%s' % question].help_text = str( question.questionHelp) self.fields['%s' % question].widget.attrs.update({ 'onchange': "toggle_id_%s()" % question, }) self.fields['%s' % question].widget.attrs[ 'class'] = 'form-control'
class RatioForm(forms.Form): """ Form to display bank transaction sums grouped by tags. """ SINGLE_CREDIT = 'single_credit' SINGLE_DEBIT = 'single_debit' SUM_CREDIT = 'sum_credit' SUM_DEBIT = 'sum_debit' TYPES = ( (ugettext_lazy('Sum'), ( (SUM_DEBIT, ugettext_lazy('Expenses')), (SUM_CREDIT, ugettext_lazy('Income')), )), (ugettext_lazy('Single'), ( (SINGLE_DEBIT, ugettext_lazy('Expenses')), (SINGLE_CREDIT, ugettext_lazy('Income')), )), ) type = forms.ChoiceField(choices=TYPES, initial=SUM_DEBIT) CHART_DOUGHNUT = 'doughtnut' CHART_PIE = 'pie' CHART_POLAR = 'polar' CHART_TYPES = ( (CHART_DOUGHNUT, ugettext_lazy('Doughnut')), (CHART_PIE, ugettext_lazy('Pie')), (CHART_POLAR, ugettext_lazy('Polar area')), ) chart = forms.ChoiceField( choices=CHART_TYPES, initial=CHART_DOUGHNUT, required=False, ) date_start = forms.DateField( widget=Datepicker(attrs={ 'placeholder': ugettext_lazy('Date start'), }), ) date_end = forms.DateField( widget=Datepicker(attrs={ 'placeholder': ugettext_lazy('Date end'), }), ) reconciled = forms.NullBooleanField(required=False) tags = forms.ModelMultipleChoiceField( queryset=BankTransactionTag.objects.none(), required=False) sum_min = forms.DecimalField( max_digits=10, decimal_places=2, localize=True, required=False, widget=forms.NumberInput(attrs={ 'placeholder': _('Minimum sum'), }), ) sum_max = forms.DecimalField( max_digits=10, decimal_places=2, localize=True, required=False, widget=forms.NumberInput(attrs={ 'placeholder': _('Maximum sum'), }), ) def __init__(self, user, *args, **kwargs): super(RatioForm, self).__init__(*args, **kwargs) self.fields['tags'].queryset = ( BankTransactionTag.objects.get_user_tags_queryset(user)) self.fields['reconciled'].widget.choices[0] = ('1', _('Reconciled?')) def clean(self): cleaned_data = super(RatioForm, self).clean() 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', ) sum_min = cleaned_data.get('sum_min', None) sum_max = cleaned_data.get('sum_max', None) if sum_min is not None and sum_max is not None and sum_min > sum_max: raise forms.ValidationError( _("Minimum sum could not be greater than maximum sum."), code='sum_min_greater', ) return cleaned_data