重构Django表格的清洁方法

时间:2019-11-03 21:47:11

标签: django refactoring

我正在尝试重构Django应用程序的表单代码。这种形式的观点属于脆脆的形式,但我不知道这是相关的。我的疑问是,是否存在一种简化或分离表格清洁方法的方法。

def clean(self):
        cleaned_data = super(RequirementsForm,self).clean()
        stand_type = cleaned_data.get('stand_type')
        interviewers = cleaned_data.get('numeroentrevistadores')
        lunch_number = cleaned_data.get('lunch_number')
        veggie_lunch_number = cleaned_data.get('veggie_lunch_number')
        dp1 = cleaned_data.get('day_preference_1')
        st1 = cleaned_data.get('stand_preference_1')
        st2 = cleaned_data.get('stand_preference_2')
        st3 = cleaned_data.get('stand_preference_3')
        rs = cleaned_data.get('require_stand')
        pn = cleaned_data.get('producerName')
        pp = cleaned_data.get('producerPhone')
        pe = cleaned_data.get('producerEmail')

        total_lunch = lunch_number + veggie_lunch_number

        #if stand_type and total_lunch:
         #   if stand_type == "1" and total_lunch > 2:
          #      raise forms.ValidationError(
           #         "error1"
            #    )
            #if stand_type == "2" and  total_lunch > 4:
             #   raise forms.ValidationError(
              #      "error2"
               # )

        if interviewers:

            if (stand_type == "1" or stand_type == "2") and interviewers > 2:
                raise forms.ValidationError(
                    "error3"
                )
            if (stand_type == "4" or stand_type == "5") and interviewers > 4:
                raise forms.ValidationError(
                    "error4"
                )
            if stand_type == "3" and interviewers > 3:
                raise forms.ValidationError(
                    "error5"
                )
        if total_lunch:
            if (stand_type == "1" or stand_type == "2") and (lunch_number + veggie_lunch_number) > 2:
                raise forms.ValidationError(
                    "error6"
                )
            if (stand_type == "4" or stand_type == "5") and (lunch_number + veggie_lunch_number) > 4:
                raise forms.ValidationError(
                    "error7"
                )
            if stand_type == "3"  and (lunch_number + veggie_lunch_number) > 3:
                raise forms.ValidationError(
                    "error8"
                )
            if lunch_number <0 or veggie_lunch_number<0:
                raise forms.ValidationError(
                    "error9"
                )

        #if dp1 and dp2 and dp3:
         #   if dp1 == dp2 or dp1 == dp3 or dp2 == dp3:
          #      raise forms.ValidationError(
           #         "error10"
            #    )

        if st1 and st2 and st3:
            if (st1 != 'A' and st1 != 'B'):
                if st1 == st2 or st1 == st3 or st2 == st3 :
                    raise forms.ValidationError(
                        "error11"
                    )

        if rs:
            if rs == "1" and (pn=="" or pp=="" or pe==""):
                raise forms.ValidationError(
                    "error12"
                )

我正在阅读文档的part,但是我不确定如何分离所有的验证,我的意思是我知道我可以创建不同的函数来分离clean方法的代码,但是想知道这样做是否存在。

0 个答案:

没有答案