<s> from django import forms <EOL> from django . forms . util import ErrorList , ErrorDict <EOL> from django . utils . translation import ugettext as _ <EOL> from django . utils . functional import lazy <EOL> from django . utils . safestring import mark_safe <EOL> from . helpers import uniqify_list , camelcase_to_underscores <EOL> def add_css_classes ( f , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> field = f . formfield ( ** kwargs ) <EOL> if field and '<STR_LIT:class>' not in field . widget . attrs : <EOL> field . widget . attrs [ '<STR_LIT:class>' ] = '<STR_LIT>' % field . __class__ . __name__ <EOL> return field <EOL> class CombinedFieldsMixin ( object ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( CombinedFieldsMixin , self ) . __init__ ( * args , ** kwargs ) <EOL> if hasattr ( self , '<STR_LIT:Meta>' ) : <EOL> for field_name in getattr ( self . Meta , '<STR_LIT>' , ( ) ) : <EOL> if field_name in self . fields : <EOL> del self . fields [ field_name ] <EOL> self . combined_fields = getattr ( self . Meta , '<STR_LIT>' , [ ] ) <EOL> for combination in self . combined_fields : <EOL> _label_set = [ ] <EOL> _css_classes_set = [ ] <EOL> _first_field = None <EOL> for counter , field_name in enumerate ( combination ) : <EOL> field = self . fields [ field_name ] <EOL> field . combined = { '<STR_LIT>' : False , '<STR_LIT>' : False } <EOL> if counter == <NUM_LIT:0> : <EOL> _first_field = field <EOL> field . combined [ '<STR_LIT>' ] = True <EOL> elif counter == len ( combination ) - <NUM_LIT:1> : <EOL> field . combined [ '<STR_LIT>' ] = True <EOL> _label_set . append ( u"<STR_LIT:%s>" % field . label ) <EOL> _first_field . combined [ '<STR_LIT:label>' ] = u"<STR_LIT>" . join ( uniqify_list ( _label_set , preserve_order = True ) ) <EOL> _first_field . combined [ '<STR_LIT:label>' ] = _first_field . combined [ '<STR_LIT:label>' ] <EOL> _first_field . combined [ '<STR_LIT>' ] = ErrorDict ( ) <EOL> _first_field . combined [ '<STR_LIT>' ] = '<STR_LIT:U+0020>' . join ( _css_classes_set ) <EOL> def _post_clean ( self , * args , ** kwargs ) : <EOL> super ( CombinedFieldsMixin , self ) . _post_clean ( * args , ** kwargs ) <EOL> if hasattr ( self , '<STR_LIT:Meta>' ) : <EOL> self . combined_fields = getattr ( self . Meta , '<STR_LIT>' , ( ) ) <EOL> for combination in self . combined_fields : <EOL> _error_set = [ ] <EOL> _first_field = None <EOL> for counter , field_name in enumerate ( combination ) : <EOL> field = self . fields [ field_name ] <EOL> if counter == <NUM_LIT:0> : <EOL> _first_field = field <EOL> _error_set += self . _errors . get ( field_name , [ ] ) <EOL> _first_field . combined [ '<STR_LIT>' ] = ErrorList ( uniqify_list ( _error_set , True ) ) <EOL> class EnhancedForm ( CombinedFieldsMixin , forms . Form ) : <EOL> error_css_class = '<STR_LIT:error>' <EOL> required_css_class = '<STR_LIT>' <EOL> formfield_callback = add_css_classes <EOL> @ property <EOL> def name ( self ) : <EOL> return camelcase_to_underscores ( self . __class__ . __name__ ) <EOL> class EnhancedModelForm ( CombinedFieldsMixin , forms . ModelForm ) : <EOL> error_css_class = '<STR_LIT:error>' <EOL> required_css_class = '<STR_LIT>' <EOL> formfield_callback = add_css_classes <EOL> @ property <EOL> def name ( self ) : <EOL> return camelcase_to_underscores ( self . __class__ . __name__ ) </s>
