【问题标题】:django - how to replace repeated code in form validationdjango - 如何替换表单验证中的重复代码
【发布时间】:2017-07-07 16:54:03
【问题描述】:

在我的 django forms.py 文件中,我试图替换两次出现的重复验证代码。我每次尝试只出现一次,似乎都不起作用。

我不知道如何编写代码,以便在验证中每个重复代码只出现一次。这应该是可能的,但我不明白如何实现这一点。

我希望有人可以帮助我,因为这让我很困惑。

这是我的验证码:

def clean(self):
    cd_cdf = super(CertificationDetailsForm, self).clean()

    # Must check the most specific cases first, then the general cases.
    if 'certification_type' in cd_cdf and cd_cdf['certification_type'] == '':
        self._errors['certification_type'] = self.error_class([_("This field is required.")])

    elif 'certification_type' in cd_cdf and cd_cdf['certification_type'] == display_types.ENTER_MY_OWN_TYPE_DESCRIPTION:
        if 'certification_type_description' in cd_cdf and len(cd_cdf['certification_type_description'].strip()) == 0:
            self._errors['certification_type_description'] = self.error_class([_("This field is required.")])

        # repeated code occurrence #1.1.
        if 'certification_title' in cd_cdf and len(cd_cdf['certification_title'].strip()) == 0:
            self._errors['certification_title'] = self.error_class([_("This field is required.")])

        # repeated code occurrence #2.1.
        if 'certification_date' in cd_cdf and cd_cdf['certification_date'] is not None:
            if cd_cdf['certification_date'] > date.today():
                self._errors['certification_date'] = self.error_class([_("Date must not be greater than today.")])

    elif 'certification_type' in cd_cdf and cd_cdf['certification_type'] != display_types.ENTER_MY_OWN_DETAILS:
        # repeated code occurrence #1.2.
        if 'certification_title' in cd_cdf and len(cd_cdf['certification_title'].strip()) == 0:
            self._errors['certification_title'] = self.error_class([_("This field is required.")])

        # repeated code occurrence #2.2.
        if 'certification_date' in cd_cdf and cd_cdf['certification_date'] is not None:
            if cd_cdf['certification_date'] > date.today():
                self._errors['certification_date'] = self.error_class([_("Date must not be greater than today.")])

    elif 'certification_type' in cd_cdf and cd_cdf['certification_type'] == display_types.ENTER_MY_OWN_DETAILS:
        if 'certification_description' in cd_cdf and len(cd_cdf['certification_description'].strip()) == 0:
            self._errors['certification_description'] = self.error_class([_("This field is required.")])
        #  remove the entered value and/or assign a default value, when the certification type only requires minimum data.

        if 'certification_type_description' in cd_cdf and len(cd_cdf['certification_type_description'].strip()) > 0:
            cd_cdf['certification_type_description'] = None

        if 'certification_title' in cd_cdf and len(cd_cdf['certification_title'].strip()) > 0:
            cd_cdf['certification_title'] = None

        if 'certification_institution' in cd_cdf and len(cd_cdf['certification_institution'].strip()) > 0:
            cd_cdf['certification_institution'] = None

        if 'certification_date' in cd_cdf and cd_cdf['certification_date'] is not None:
            cd_cdf['certification_date'] = None

    return cd_cdf

这是类型代码,以防万一:

CERTIFICATE = 1
CERTIFICATE_LEVEL_I = 2
CERTIFICATE_LEVEL_II = 3
CERTIFICATE_LEVEL_III = 4
CERTIFICATE_LEVEL_IV = 5
STANDARD_CERTIFICATE = 6
INTERMEDIATE_CERTIFICATE = 7
ADVANCED_CERTIFICATE = 8
ACADEMIC_CERTIFICATE = 9
PROFESSIONAL_CERTIFICATE = 10
OTHER_CERTIFICATE = 11
ENTER_MY_OWN_TYPE_DESCRIPTION = 7777  # 7777 triggers a hidden text field to be displayed.
ENTER_MY_OWN_DETAILS = 9999

CERTIFICATION_TYPES = (
    (CERTIFICATE, _('Certificate')),
    (CERTIFICATE_LEVEL_I, _('Certificate Level I')),
    (CERTIFICATE_LEVEL_II, _('Certificate Level II')),
    (CERTIFICATE_LEVEL_III, _('Certificate Level III')),
    (CERTIFICATE_LEVEL_IV, _('Certificate Level IV')),
    (STANDARD_CERTIFICATE, _('Standard Certificate')),
    (INTERMEDIATE_CERTIFICATE, _('Intermediate Certificate')),
    (ADVANCED_CERTIFICATE, _('Advanced Certificate')),
    (ACADEMIC_CERTIFICATE, _('Academic Certificate')),
    (PROFESSIONAL_CERTIFICATE, _('Professional Certificate')),
    (OTHER_CERTIFICATE, _('Other Certificate')),
    (ENTER_MY_OWN_TYPE_DESCRIPTION, _('Enter my own Type Description')),
    (ENTER_MY_OWN_DETAILS, _('Enter my own details'))
)

【问题讨论】:

    标签: django validation django-forms


    【解决方案1】:

    像这样:

    def clean(self):
        cd_cdf = super(CertificationDetailsForm, self).clean()
    
        ctype = 'certification_type'
        ctypedesc = 'certification_type_description'
        ctitle = 'certification_title'
        cdate = 'certification_date'
        cdesc = 'certification_description'
        cinst = 'certification_institution'
    
        # Must check the most specific cases first, then the general cases.
        if ctype in cd_cdf:
            if cd_cdf[ctype] == '':
                self._errors[ctype] = self.error_class([_("This field is required.")])
    
            elif (cd_cdf[ctype] == display_types.ENTER_MY_OWN_TYPE_DESCRIPTION) or (cd_cdf[ctype] != display_types.ENTER_MY_OWN_DETAILS):
                if cd_cdf[ctype] == display_types.ENTER_MY_OWN_TYPE_DESCRIPTION:
                    if ctypedesc in cd_cdf and len(cd_cdf[ctypedesc].strip()) == 0:
                        self._errors[ctypedesc] = self.error_class([_("This field is required.")])
                else:
                    if ctitle in cd_cdf and len(cd_cdf[ctitle].strip()) == 0:
                        self._errors[ctitle] = self.error_class([_("This field is required.")])
    
                    if cdate in cd_cdf and cd_cdf[cdate] is not None:
                        if cd_cdf[cdate] > date.today():
                            self._errors[cdate] = self.error_class([_("Date must not be greater than today.")])
    
            elif cd_cdf[ctype] == display_types.ENTER_MY_OWN_DETAILS:
                if cdesc in cd_cdf and len(cd_cdf[cdesc].strip()) == 0:
                    self._errors[cdesc] = self.error_class([_("This field is required.")])
                #  remove the entered value and/or assign a default value, when the certification type only requires minimum data.
    
                forcheck = [ctypedesc, ctitle, cinst]
                for i in forcheck:
                    if i in cd_cdf and len(cd_cdf[i].strip()) > 0:
                        cd_cdf[i] = None
    
                if cdate in cd_cdf and cd_cdf[cdate] is not None:
                    cd_cdf[cdate] = None
    
        return cd_cdf
    

    我用不言自明的变量替换了您经常提到的字符串,并加入了第二个和第三个条件。我还没有测试过。

    我同意第一个答案,它不雅且不符合标准,但我不知道所有这些条件是什么,所以我无法进一步缩短代码。

    【讨论】:

      猜你喜欢
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      • 2020-12-08
      • 2011-01-29
      • 2021-03-16
      • 2014-03-07
      • 2016-05-30
      相关资源
      最近更新 更多