Beispiel #1
0
    def _check_fields(self, cls, model):
        """ Check that `fields` only refer to existing fields, doesn't contain
        duplicates. Check if at most one of `fields` and `fieldsets` is defined.
        """

        if cls.fields is None:
            return []
        elif not isinstance(cls.fields, (list, tuple)):
            return must_be('a list or tuple', option='fields', obj=cls, id='admin.E004')
        elif cls.fieldsets:
            return [
                checks.Error(
                    "Both 'fieldsets' and 'fields' are specified.",
                    hint=None,
                    obj=cls,
                    id='admin.E005',
                )
            ]
        fields = flatten(cls.fields)
        if len(fields) != len(set(fields)):
            return [
                checks.Error(
                    "The value of 'fields' contains duplicate field(s).",
                    hint=None,
                    obj=cls,
                    id='admin.E006',
                )
            ]

        return list(chain(*[
            self._check_field_spec(cls, model, field_name, 'fields')
            for field_name in cls.fields
        ]))
Beispiel #2
0
    def _check_fieldsets_item(self, cls, model, fieldset, label):
        """ Check an item of `fieldsets`, i.e. check that this is a pair of a
        set name and a dictionary containing "fields" key. """

        if not isinstance(fieldset, (list, tuple)):
            return must_be('a list or tuple', option=label, obj=cls, id='admin.E008')
        elif len(fieldset) != 2:
            return must_be('of length 2', option=label, obj=cls, id='admin.E009')
        elif not isinstance(fieldset[1], dict):
            return must_be('a dictionary', option='%s[1]' % label, obj=cls, id='admin.E010')
        elif 'fields' not in fieldset[1]:
            return [
                checks.Error(
                    "The value of '%s[1]' must contain the key 'fields'." % label,
                    hint=None,
                    obj=cls,
                    id='admin.E011',
                )
            ]
        elif not isinstance(fieldset[1]['fields'], (list, tuple)):
            return must_be('a list or tuple', option="fieldsets[1]['fields']", obj=cls, id='admin.E008')

        fields = flatten(fieldset[1]['fields'])
        if len(fields) != len(set(fields)):
            return [
                checks.Error(
                    "There are duplicate field(s) in '%s[1]'." % label,
                    hint=None,
                    obj=cls,
                    id='admin.E012',
                )
            ]
        return list(chain(*[
            self._check_field_spec(cls, model, fieldset_fields, '%s[1]["fields"]' % label)
            for fieldset_fields in fieldset[1]['fields']
        ]))