Ticket #3295: groups_in_forms.patch

File groups_in_forms.patch, 1.8 KB (added by Honza Král <Honza.Kral@…>, 18 years ago)

proper patch format

  • django/newforms/forms.py

     
    3131    def __new__(cls, name, bases, attrs):
    3232        fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)]
    3333        fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter))
     34        for field_name, obj in attrs.items():
     35            if field_name.startswith('group_'):
     36                name_func = field_name.replace('group_','')
     37                new_class.add_to_class(name_func, obj)
     38                delattr(new_class, field_name)
    3439        attrs['fields'] = SortedDictFromList(fields)
    3540        return type.__new__(cls, name, bases, attrs)
    3641
     
    171176        association with the field named '__all__'.
    172177        """
    173178        return self.clean_data
    174 
     179    def _prepare_HTML_output(self, list_fileds, func_HTML):
     180        oldfields  = self.fields.copy() # save original fields
     181        data = []
     182        for field in list_fileds:
     183            if oldfields.has_key(field):
     184                data.append( (field, oldfields[field]) )
     185        self.fields = SortedDictFromList(data)
     186        output = func_HTML()
     187        self.fields = oldfields # restore original fields
     188        return output
     189       
     190    def add_to_class(cls, name, value):
     191        setattr(cls, '%s_as_p' % name, lambda self: self._prepare_HTML_output( value, self.as_p ))
     192        setattr(cls, '%s_as_table' % name, lambda self: self._prepare_HTML_output_HTML_output( value, self.as_table ))
     193    add_to_class = classmethod(add_to_class)
    175194class Form(BaseForm):
    176195    "A collection of Fields, plus their associated data."
    177196    # This is a separate class from BaseForm in order to abstract the way
Back to Top