예제 #1
0
    def test_collect_items_pops_items_that_are_instance_of_given_class(self):
        attrs = SortedDict(collect_items(self.attrs, Field))

        for key in ['project', 'description', 'status']:
            assert key in attrs

        for key in ['title']:
            assert key not in attrs
예제 #2
0
    def __new__(cls, cls_name, bases, attrs):
        super_new = super(FieldSetMetaclass, cls).__new__

        # first proccess meta attribute
        meta_classes = collect_meta_classes(bases)
        Meta = attrs.get('Meta')
        Meta = merge_meta_attributes(meta_classes, Meta)
        opts = Options(Meta)

        if opts.model and opts.form:
            raise ImproperlyConfigured('You cannot setup "model" and "form" options at the same time')

        # extract user defined fields
        declared_fields = collect_items(attrs, opts.field_class)

        # bases_fields - each item of list is a 'base_fields' dict
        bases_fields = collect_bases_fields(bases)

        for base_fields in bases_fields:
            declared_fields = base_fields.items() + declared_fields

        # generating fields from model
        fields = []
        if opts.model:
            for dbfield in opts.model._meta.fields:
                if not should_be_excluded(dbfield.name, opts.fields, opts.exclude):
                    item = (dbfield.name, opts.field_mapper.get(dbfield))
                    fields.append(item)

        if opts.form:
            for name, formfield in opts.form.base_fields.items():
                if not should_be_excluded(name, opts.fields, opts.exclude):
                    item = (name, opts.field_mapper.get(formfield))
                    fields.append(item)

        fields = SortedDict(fields + declared_fields)

        # set the fields names
        for name, field in fields.items():
            field.name = name

        attrs['base_fields'] = fields
        attrs['_meta'] = opts
        new_class = super_new(cls, cls_name, bases, attrs)
        return new_class
예제 #3
0
 def test_collect_items_sort_the_results(self):
     expected = ['project', 'description', 'status']
     result = map(itemgetter(0), collect_items(self.attrs, Field))
     self.assertEquals(expected, result)