<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> from copy import deepcopy <EOL> from django import forms <EOL> try : <EOL> from django . forms . utils import flatatt , ErrorDict <EOL> except ImportError : <EOL> from django . forms . util import flatatt , ErrorDict <EOL> from django . utils import six <EOL> from django . utils . safestring import mark_safe <EOL> def with_metaclass ( meta , * bases ) : <EOL> """<STR_LIT>""" <EOL> return meta ( str ( "<STR_LIT>" ) , bases , { } ) <EOL> class Fieldset ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , form , name , boundfields , legend = '<STR_LIT>' , classes = '<STR_LIT>' , <EOL> description = '<STR_LIT>' ) : <EOL> self . form = form <EOL> self . boundfields = boundfields <EOL> if legend is None : <EOL> legend = name <EOL> self . legend = legend and mark_safe ( legend ) <EOL> self . classes = classes <EOL> self . description = mark_safe ( description ) <EOL> self . name = name <EOL> def _errors ( self ) : <EOL> return ErrorDict ( ( ( k , v ) for ( k , v ) in six . iteritems ( self . form . errors ) <EOL> if k in [ f . name for f in self . boundfields ] ) ) <EOL> errors = property ( _errors ) <EOL> def __iter__ ( self ) : <EOL> for bf in self . boundfields : <EOL> yield _mark_row_attrs ( bf , self . form ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . __class__ . __name__ , self . name , <EOL> [ f . name for f in self . boundfields ] , self . legend , self . classes , <EOL> self . description ) <EOL> class FieldsetCollection ( object ) : <EOL> def __init__ ( self , form , fieldsets ) : <EOL> self . form = form <EOL> self . fieldsets = fieldsets <EOL> self . _cached_fieldsets = [ ] <EOL> def __len__ ( self ) : <EOL> return len ( self . fieldsets ) or <NUM_LIT:1> <EOL> def __iter__ ( self ) : <EOL> if not self . _cached_fieldsets : <EOL> self . _gather_fieldsets ( ) <EOL> for field in self . _cached_fieldsets : <EOL> yield field <EOL> def __getitem__ ( self , key ) : <EOL> if not self . _cached_fieldsets : <EOL> self . _gather_fieldsets ( ) <EOL> for field in self . _cached_fieldsets : <EOL> if field . name == key : <EOL> return field <EOL> raise KeyError <EOL> def _gather_fieldsets ( self ) : <EOL> if not self . fieldsets : <EOL> self . fieldsets = ( ( '<STR_LIT>' , { '<STR_LIT>' : self . form . fields . keys ( ) , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) , ) <EOL> for name , options in self . fieldsets : <EOL> try : <EOL> field_names = [ n for n in options [ '<STR_LIT>' ] <EOL> if n in self . form . fields ] <EOL> except KeyError : <EOL> message = "<STR_LIT>" <EOL> raise ValueError ( message ) <EOL> boundfields = [ forms . forms . BoundField ( self . form , <EOL> self . form . fields [ n ] , n ) <EOL> for n in field_names ] <EOL> self . _cached_fieldsets . append ( Fieldset ( self . form , name , <EOL> boundfields , options . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT:U+0020>' . join ( options . get ( '<STR_LIT>' , ( ) ) ) , <EOL> options . get ( '<STR_LIT:description>' , '<STR_LIT>' ) ) ) <EOL> def _get_meta_attr ( attrs , attr , default ) : <EOL> try : <EOL> ret = getattr ( attrs [ '<STR_LIT:Meta>' ] , attr ) <EOL> except ( KeyError , AttributeError ) : <EOL> ret = default <EOL> return ret <EOL> def _set_meta_attr ( attrs , attr , value ) : <EOL> try : <EOL> setattr ( attrs [ '<STR_LIT:Meta>' ] , attr , value ) <EOL> return True <EOL> except KeyError : <EOL> return False <EOL> def get_fieldsets ( bases , attrs ) : <EOL> """<STR_LIT>""" <EOL> fieldsets = _get_meta_attr ( attrs , '<STR_LIT>' , None ) <EOL> if fieldsets is None : <EOL> for base in bases : <EOL> fieldsets = getattr ( base , '<STR_LIT>' , None ) <EOL> if fieldsets is not None : <EOL> break <EOL> fieldsets = fieldsets or [ ] <EOL> return fieldsets <EOL> def get_fields_from_fieldsets ( fieldsets ) : <EOL> """<STR_LIT>""" <EOL> fields = [ ] <EOL> try : <EOL> for name , options in fieldsets : <EOL> fields . extend ( options [ '<STR_LIT>' ] ) <EOL> except ( TypeError , KeyError ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return fields or None <EOL> def get_row_attrs ( bases , attrs ) : <EOL> """<STR_LIT>""" <EOL> return _get_meta_attr ( attrs , '<STR_LIT>' , { } ) <EOL> def _mark_row_attrs ( bf , form ) : <EOL> row_attrs = deepcopy ( form . _row_attrs . get ( bf . name , { } ) ) <EOL> if bf . field . required : <EOL> req_class = '<STR_LIT>' <EOL> else : <EOL> req_class = '<STR_LIT>' <EOL> if bf . errors : <EOL> req_class += '<STR_LIT>' <EOL> if '<STR_LIT:class>' in row_attrs : <EOL> row_attrs [ '<STR_LIT:class>' ] = row_attrs [ '<STR_LIT:class>' ] + '<STR_LIT:U+0020>' + req_class <EOL> else : <EOL> row_attrs [ '<STR_LIT:class>' ] = req_class <EOL> bf . row_attrs = mark_safe ( flatatt ( row_attrs ) ) <EOL> return bf <EOL> class BetterFormBaseMetaclass ( type ) : <EOL> def __new__ ( cls , name , bases , attrs ) : <EOL> attrs [ '<STR_LIT>' ] = get_fieldsets ( bases , attrs ) <EOL> fields = get_fields_from_fieldsets ( attrs [ '<STR_LIT>' ] ) <EOL> if ( _get_meta_attr ( attrs , '<STR_LIT>' , None ) is None and <EOL> _get_meta_attr ( attrs , '<STR_LIT>' , None ) is None ) : <EOL> _set_meta_attr ( attrs , '<STR_LIT>' , fields ) <EOL> attrs [ '<STR_LIT>' ] = get_row_attrs ( bases , attrs ) <EOL> new_class = super ( BetterFormBaseMetaclass , <EOL> cls ) . __new__ ( cls , name , bases , attrs ) <EOL> return new_class <EOL> class BetterFormMetaclass ( BetterFormBaseMetaclass , <EOL> forms . forms . DeclarativeFieldsMetaclass ) : <EOL> pass <EOL> class BetterModelFormMetaclass ( BetterFormBaseMetaclass , <EOL> forms . models . ModelFormMetaclass ) : <EOL> pass <EOL> class BetterBaseForm ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . _fieldsets = deepcopy ( self . base_fieldsets ) <EOL> self . _row_attrs = deepcopy ( self . base_row_attrs ) <EOL> self . _fieldset_collection = None <EOL> super ( BetterBaseForm , self ) . __init__ ( * args , ** kwargs ) <EOL> @ property <EOL> def fieldsets ( self ) : <EOL> if not self . _fieldset_collection : <EOL> self . _fieldset_collection = FieldsetCollection ( <EOL> self , self . _fieldsets ) <EOL> return self . _fieldset_collection <EOL> def __iter__ ( self ) : <EOL> for bf in super ( BetterBaseForm , self ) . __iter__ ( ) : <EOL> yield _mark_row_attrs ( bf , self ) <EOL> def __getitem__ ( self , name ) : <EOL> bf = super ( BetterBaseForm , self ) . __getitem__ ( name ) <EOL> return _mark_row_attrs ( bf , self ) <EOL> class BetterForm ( with_metaclass ( BetterFormMetaclass , BetterBaseForm ) , <EOL> forms . Form ) : <EOL> __doc__ = BetterBaseForm . __doc__ <EOL> class BetterModelForm ( with_metaclass ( BetterModelFormMetaclass , <EOL> BetterBaseForm ) , forms . ModelForm ) : <EOL> __doc__ = BetterBaseForm . __doc__ <EOL> class BasePreviewFormMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( BasePreviewFormMixin , self ) . __init__ ( * args , ** kwargs ) <EOL> self . preview = self . check_preview ( kwargs . get ( '<STR_LIT:data>' , None ) ) <EOL> def check_preview ( self , data ) : <EOL> if data and data . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) == u'<STR_LIT>' : <EOL> return True <EOL> return False <EOL> def is_valid ( self , * args , ** kwargs ) : <EOL> if self . preview : <EOL> return False <EOL> return super ( BasePreviewFormMixin , self ) . is_valid ( ) <EOL> class PreviewModelForm ( BasePreviewFormMixin , BetterModelForm ) : <EOL> pass <EOL> class PreviewForm ( BasePreviewFormMixin , BetterForm ) : <EOL> pass </s>
