<s> from __future__ import absolute_import , unicode_literals <EOL> from django . core . exceptions import ValidationError <EOL> from django . forms import Form <EOL> from django . forms . fields import IntegerField , BooleanField <EOL> from django . forms . util import ErrorList <EOL> from django . forms . widgets import Media , HiddenInput <EOL> from django . utils . encoding import python_2_unicode_compatible <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils import six <EOL> from django . utils . six . moves import xrange <EOL> from django . utils . translation import ugettext as _ <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> TOTAL_FORM_COUNT = '<STR_LIT>' <EOL> INITIAL_FORM_COUNT = '<STR_LIT>' <EOL> MAX_NUM_FORM_COUNT = '<STR_LIT>' <EOL> ORDERING_FIELD_NAME = '<STR_LIT>' <EOL> DELETION_FIELD_NAME = '<STR_LIT>' <EOL> DEFAULT_MAX_NUM = <NUM_LIT:1000> <EOL> class ManagementForm ( Form ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . base_fields [ TOTAL_FORM_COUNT ] = IntegerField ( widget = HiddenInput ) <EOL> self . base_fields [ INITIAL_FORM_COUNT ] = IntegerField ( widget = HiddenInput ) <EOL> self . base_fields [ MAX_NUM_FORM_COUNT ] = IntegerField ( required = False , widget = HiddenInput ) <EOL> super ( ManagementForm , self ) . __init__ ( * args , ** kwargs ) <EOL> @ python_2_unicode_compatible <EOL> class BaseFormSet ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data = None , files = None , auto_id = '<STR_LIT>' , prefix = None , <EOL> initial = None , error_class = ErrorList ) : <EOL> self . is_bound = data is not None or files is not None <EOL> self . prefix = prefix or self . get_default_prefix ( ) <EOL> self . auto_id = auto_id <EOL> self . data = data or { } <EOL> self . files = files or { } <EOL> self . initial = initial <EOL> self . error_class = error_class <EOL> self . _errors = None <EOL> self . _non_form_errors = None <EOL> self . _construct_forms ( ) <EOL> def __str__ ( self ) : <EOL> return self . as_table ( ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return iter ( self . forms ) <EOL> def __getitem__ ( self , index ) : <EOL> """<STR_LIT>""" <EOL> return self . forms [ index ] <EOL> def __len__ ( self ) : <EOL> return len ( self . forms ) <EOL> def __bool__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def __nonzero__ ( self ) : <EOL> return type ( self ) . __bool__ ( self ) <EOL> @ property <EOL> def management_form ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . is_bound : <EOL> form = ManagementForm ( self . data , auto_id = self . auto_id , prefix = self . prefix ) <EOL> if not form . is_valid ( ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> else : <EOL> form = ManagementForm ( auto_id = self . auto_id , prefix = self . prefix , initial = { <EOL> TOTAL_FORM_COUNT : self . total_form_count ( ) , <EOL> INITIAL_FORM_COUNT : self . initial_form_count ( ) , <EOL> MAX_NUM_FORM_COUNT : self . max_num <EOL> } ) <EOL> return form <EOL> def total_form_count ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . is_bound : <EOL> return self . management_form . cleaned_data [ TOTAL_FORM_COUNT ] <EOL> else : <EOL> initial_forms = self . initial_form_count ( ) <EOL> total_forms = initial_forms + self . extra <EOL> if initial_forms > self . max_num >= <NUM_LIT:0> : <EOL> total_forms = initial_forms <EOL> elif total_forms > self . max_num >= <NUM_LIT:0> : <EOL> total_forms = self . max_num <EOL> return total_forms <EOL> def initial_form_count ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . is_bound : <EOL> return self . management_form . cleaned_data [ INITIAL_FORM_COUNT ] <EOL> else : <EOL> initial_forms = self . initial and len ( self . initial ) or <NUM_LIT:0> <EOL> if initial_forms > self . max_num >= <NUM_LIT:0> : <EOL> initial_forms = self . max_num <EOL> return initial_forms <EOL> def _construct_forms ( self ) : <EOL> self . forms = [ ] <EOL> for i in xrange ( min ( self . total_form_count ( ) , self . absolute_max ) ) : <EOL> self . forms . append ( self . _construct_form ( i ) ) <EOL> def _construct_form ( self , i , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> defaults = { <EOL> '<STR_LIT>' : self . auto_id , <EOL> '<STR_LIT>' : self . add_prefix ( i ) , <EOL> '<STR_LIT>' : self . error_class , <EOL> } <EOL> if self . is_bound : <EOL> defaults [ '<STR_LIT:data>' ] = self . data <EOL> defaults [ '<STR_LIT>' ] = self . files <EOL> if self . initial and not '<STR_LIT>' in kwargs : <EOL> try : <EOL> defaults [ '<STR_LIT>' ] = self . initial [ i ] <EOL> except IndexError : <EOL> pass <EOL> if i >= self . initial_form_count ( ) : <EOL> defaults [ '<STR_LIT>' ] = True <EOL> defaults . update ( kwargs ) <EOL> form = self . form ( ** defaults ) <EOL> self . add_fields ( form , i ) <EOL> return form <EOL> @ property <EOL> def initial_forms ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . forms [ : self . initial_form_count ( ) ] <EOL> @ property <EOL> def extra_forms ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . forms [ self . initial_form_count ( ) : ] <EOL> @ property <EOL> def empty_form ( self ) : <EOL> form = self . form ( <EOL> auto_id = self . auto_id , <EOL> prefix = self . add_prefix ( '<STR_LIT>' ) , <EOL> empty_permitted = True , <EOL> ) <EOL> self . add_fields ( form , None ) <EOL> return form <EOL> @ property <EOL> def cleaned_data ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_valid ( ) : <EOL> raise AttributeError ( "<STR_LIT>" % self . __class__ . __name__ ) <EOL> return [ form . cleaned_data for form in self . forms ] <EOL> @ property <EOL> def deleted_forms ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_valid ( ) or not self . can_delete : <EOL> raise AttributeError ( "<STR_LIT>" % self . __class__ . __name__ ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _deleted_form_indexes = [ ] <EOL> for i in range ( <NUM_LIT:0> , self . total_form_count ( ) ) : <EOL> form = self . forms [ i ] <EOL> if i >= self . initial_form_count ( ) and not form . has_changed ( ) : <EOL> continue <EOL> if self . _should_delete_form ( form ) : <EOL> self . _deleted_form_indexes . append ( i ) <EOL> return [ self . forms [ i ] for i in self . _deleted_form_indexes ] <EOL> @ property <EOL> def ordered_forms ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_valid ( ) or not self . can_order : <EOL> raise AttributeError ( "<STR_LIT>" % self . __class__ . __name__ ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _ordering = [ ] <EOL> for i in range ( <NUM_LIT:0> , self . total_form_count ( ) ) : <EOL> form = self . forms [ i ] <EOL> if i >= self . initial_form_count ( ) and not form . has_changed ( ) : <EOL> continue <EOL> if self . can_delete and self . _should_delete_form ( form ) : <EOL> continue <EOL> self . _ordering . append ( ( i , form . cleaned_data [ ORDERING_FIELD_NAME ] ) ) <EOL> def compare_ordering_key ( k ) : <EOL> if k [ <NUM_LIT:1> ] is None : <EOL> return ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> return ( <NUM_LIT:0> , k [ <NUM_LIT:1> ] ) <EOL> self . _ordering . sort ( key = compare_ordering_key ) <EOL> return [ self . forms [ i [ <NUM_LIT:0> ] ] for i in self . _ordering ] <EOL> @ classmethod <EOL> def get_default_prefix ( cls ) : <EOL> return '<STR_LIT>' <EOL> def non_form_errors ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _non_form_errors is not None : <EOL> return self . _non_form_errors <EOL> return self . error_class ( ) <EOL> @ property <EOL> def errors ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _errors is None : <EOL> self . full_clean ( ) <EOL> return self . _errors <EOL> def _should_delete_form ( self , form ) : <EOL> """<STR_LIT>""" <EOL> return form . cleaned_data . get ( DELETION_FIELD_NAME , False ) <EOL> def is_valid ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_bound : <EOL> return False <EOL> forms_valid = True <EOL> err = self . errors <EOL> for i in range ( <NUM_LIT:0> , self . total_form_count ( ) ) : <EOL> form = self . forms [ i ] <EOL> if self . can_delete : <EOL> if self . _should_delete_form ( form ) : <EOL> continue <EOL> forms_valid &= form . is_valid ( ) <EOL> return forms_valid and not bool ( self . non_form_errors ( ) ) <EOL> def full_clean ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _errors = [ ] <EOL> if not self . is_bound : <EOL> return <EOL> for i in range ( <NUM_LIT:0> , self . total_form_count ( ) ) : <EOL> form = self . forms [ i ] <EOL> self . _errors . append ( form . errors ) <EOL> try : <EOL> self . clean ( ) <EOL> except ValidationError as e : <EOL> self . _non_form_errors = self . error_class ( e . messages ) <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def has_changed ( self ) : <EOL> """<STR_LIT>""" <EOL> return any ( form . has_changed ( ) for form in self ) <EOL> def add_fields ( self , form , index ) : <EOL> """<STR_LIT>""" <EOL> if self . can_order : <EOL> if index is not None and index < self . initial_form_count ( ) : <EOL> form . fields [ ORDERING_FIELD_NAME ] = IntegerField ( label = _ ( '<STR_LIT>' ) , initial = index + <NUM_LIT:1> , required = False ) <EOL> else : <EOL> form . fields [ ORDERING_FIELD_NAME ] = IntegerField ( label = _ ( '<STR_LIT>' ) , required = False ) <EOL> if self . can_delete : <EOL> form . fields [ DELETION_FIELD_NAME ] = BooleanField ( label = _ ( '<STR_LIT>' ) , required = False ) <EOL> def add_prefix ( self , index ) : <EOL> return '<STR_LIT>' % ( self . prefix , index ) <EOL> def is_multipart ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . forms : <EOL> return self . forms [ <NUM_LIT:0> ] . is_multipart ( ) <EOL> else : <EOL> return self . empty_form . is_multipart ( ) <EOL> @ property <EOL> def media ( self ) : <EOL> if self . forms : <EOL> return self . forms [ <NUM_LIT:0> ] . media <EOL> else : <EOL> return self . empty_form . media <EOL> def as_table ( self ) : <EOL> "<STR_LIT>" <EOL> forms = '<STR_LIT:U+0020>' . join ( [ form . as_table ( ) for form in self ] ) <EOL> return mark_safe ( '<STR_LIT:\n>' . join ( [ six . text_type ( self . management_form ) , forms ] ) ) <EOL> def as_p ( self ) : <EOL> "<STR_LIT>" <EOL> forms = '<STR_LIT:U+0020>' . join ( [ form . as_p ( ) for form in self ] ) <EOL> return mark_safe ( '<STR_LIT:\n>' . join ( [ six . text_type ( self . management_form ) , forms ] ) ) <EOL> def as_ul ( self ) : <EOL> "<STR_LIT>" <EOL> forms = '<STR_LIT:U+0020>' . join ( [ form . as_ul ( ) for form in self ] ) <EOL> return mark_safe ( '<STR_LIT:\n>' . join ( [ six . text_type ( self . management_form ) , forms ] ) ) <EOL> def formset_factory ( form , formset = BaseFormSet , extra = <NUM_LIT:1> , can_order = False , <EOL> can_delete = False , max_num = None ) : <EOL> """<STR_LIT>""" <EOL> if max_num is None : <EOL> max_num = DEFAULT_MAX_NUM <EOL> absolute_max = max ( DEFAULT_MAX_NUM , max_num ) <EOL> attrs = { '<STR_LIT>' : form , '<STR_LIT>' : extra , <EOL> '<STR_LIT>' : can_order , '<STR_LIT>' : can_delete , <EOL> '<STR_LIT>' : max_num , '<STR_LIT>' : absolute_max } <EOL> return type ( form . __name__ + str ( '<STR_LIT>' ) , ( formset , ) , attrs ) <EOL> def all_valid ( formsets ) : <EOL> """<STR_LIT>""" <EOL> valid = True <EOL> for formset in formsets : <EOL> if not formset . is_valid ( ) : <EOL> valid = False <EOL> return valid </s>
