<s> from django import forms <EOL> from django . conf import settings <EOL> from django . utils . html import escape <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . encoding import force_unicode <EOL> from django . contrib . admin . util import flatten_fieldsets <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> ACTION_CHECKBOX_NAME = '<STR_LIT>' <EOL> class ActionForm ( forms . Form ) : <EOL> action = forms . ChoiceField ( label = _ ( '<STR_LIT>' ) ) <EOL> checkbox = forms . CheckboxInput ( { '<STR_LIT:class>' : '<STR_LIT>' } , lambda value : False ) <EOL> class AdminForm ( object ) : <EOL> def __init__ ( self , form , fieldsets , prepopulated_fields ) : <EOL> self . form , self . fieldsets = form , normalize_fieldsets ( fieldsets ) <EOL> self . prepopulated_fields = [ { <EOL> '<STR_LIT>' : form [ field_name ] , <EOL> '<STR_LIT>' : [ form [ f ] for f in dependencies ] <EOL> } for field_name , dependencies in prepopulated_fields . items ( ) ] <EOL> def __iter__ ( self ) : <EOL> for name , options in self . fieldsets : <EOL> yield Fieldset ( self . form , name , ** options ) <EOL> def first_field ( self ) : <EOL> try : <EOL> fieldset_name , fieldset_options = self . fieldsets [ <NUM_LIT:0> ] <EOL> field_name = fieldset_options [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> if not isinstance ( field_name , basestring ) : <EOL> field_name = field_name [ <NUM_LIT:0> ] <EOL> return self . form [ field_name ] <EOL> except ( KeyError , IndexError ) : <EOL> pass <EOL> try : <EOL> return iter ( self . form ) . next ( ) <EOL> except StopIteration : <EOL> return None <EOL> def _media ( self ) : <EOL> media = self . form . media <EOL> for fs in self : <EOL> media = media + fs . media <EOL> return media <EOL> media = property ( _media ) <EOL> class Fieldset ( object ) : <EOL> def __init__ ( self , form , name = None , fields = ( ) , classes = ( ) , description = None ) : <EOL> self . form = form <EOL> self . name , self . fields = name , fields <EOL> self . classes = u'<STR_LIT:U+0020>' . join ( classes ) <EOL> self . description = description <EOL> def _media ( self ) : <EOL> if '<STR_LIT>' in self . classes : <EOL> return forms . Media ( js = [ '<STR_LIT>' % settings . ADMIN_MEDIA_PREFIX ] ) <EOL> return forms . Media ( ) <EOL> media = property ( _media ) <EOL> def __iter__ ( self ) : <EOL> for field in self . fields : <EOL> yield Fieldline ( self . form , field ) <EOL> class Fieldline ( object ) : <EOL> def __init__ ( self , form , field ) : <EOL> self . form = form <EOL> if isinstance ( field , basestring ) : <EOL> self . fields = [ field ] <EOL> else : <EOL> self . fields = field <EOL> def __iter__ ( self ) : <EOL> for i , field in enumerate ( self . fields ) : <EOL> yield AdminField ( self . form , field , is_first = ( i == <NUM_LIT:0> ) ) <EOL> def errors ( self ) : <EOL> return mark_safe ( u'<STR_LIT:\n>' . join ( [ self . form [ f ] . errors . as_ul ( ) for f in self . fields ] ) . strip ( '<STR_LIT:\n>' ) ) <EOL> class AdminField ( object ) : <EOL> def __init__ ( self , form , field , is_first ) : <EOL> self . field = form [ field ] <EOL> self . is_first = is_first <EOL> self . is_checkbox = isinstance ( self . field . field . widget , forms . CheckboxInput ) <EOL> def label_tag ( self ) : <EOL> classes = [ ] <EOL> if self . is_checkbox : <EOL> classes . append ( u'<STR_LIT>' ) <EOL> contents = force_unicode ( escape ( self . field . label ) ) <EOL> else : <EOL> contents = force_unicode ( escape ( self . field . label ) ) + u'<STR_LIT::>' <EOL> if self . field . field . required : <EOL> classes . append ( u'<STR_LIT>' ) <EOL> if not self . is_first : <EOL> classes . append ( u'<STR_LIT>' ) <EOL> attrs = classes and { '<STR_LIT:class>' : u'<STR_LIT:U+0020>' . join ( classes ) } or { } <EOL> return self . field . label_tag ( contents = contents , attrs = attrs ) <EOL> class InlineAdminFormSet ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , inline , formset , fieldsets ) : <EOL> self . opts = inline <EOL> self . formset = formset <EOL> self . fieldsets = fieldsets <EOL> def __iter__ ( self ) : <EOL> for form , original in zip ( self . formset . initial_forms , self . formset . get_queryset ( ) ) : <EOL> yield InlineAdminForm ( self . formset , form , self . fieldsets , self . opts . prepopulated_fields , original ) <EOL> for form in self . formset . extra_forms : <EOL> yield InlineAdminForm ( self . formset , form , self . fieldsets , self . opts . prepopulated_fields , None ) <EOL> def fields ( self ) : <EOL> fk = getattr ( self . formset , "<STR_LIT>" , None ) <EOL> for field_name in flatten_fieldsets ( self . fieldsets ) : <EOL> if fk and fk . name == field_name : <EOL> continue <EOL> yield self . formset . form . base_fields [ field_name ] <EOL> def _media ( self ) : <EOL> media = self . opts . media + self . formset . media <EOL> for fs in self : <EOL> media = media + fs . media <EOL> return media <EOL> media = property ( _media ) <EOL> class InlineAdminForm ( AdminForm ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , formset , form , fieldsets , prepopulated_fields , original ) : <EOL> self . formset = formset <EOL> self . original = original <EOL> if original is not None : <EOL> self . original_content_type_id = ContentType . objects . get_for_model ( original ) . pk <EOL> self . show_url = original and hasattr ( original , '<STR_LIT>' ) <EOL> super ( InlineAdminForm , self ) . __init__ ( form , fieldsets , prepopulated_fields ) <EOL> def __iter__ ( self ) : <EOL> for name , options in self . fieldsets : <EOL> yield InlineFieldset ( self . formset , self . form , name , ** options ) <EOL> def has_auto_field ( self ) : <EOL> if self . form . _meta . model . _meta . has_auto_field : <EOL> return True <EOL> for parent in self . form . _meta . model . _meta . get_parent_list ( ) : <EOL> if parent . _meta . has_auto_field : <EOL> return True <EOL> return False <EOL> def field_count ( self ) : <EOL> num_of_fields = <NUM_LIT:0> <EOL> if self . has_auto_field ( ) : <EOL> num_of_fields += <NUM_LIT:1> <EOL> num_of_fields += len ( self . fieldsets [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ "<STR_LIT>" ] ) <EOL> if self . formset . can_order : <EOL> num_of_fields += <NUM_LIT:1> <EOL> if self . formset . can_delete : <EOL> num_of_fields += <NUM_LIT:1> <EOL> return num_of_fields <EOL> def pk_field ( self ) : <EOL> return AdminField ( self . form , self . formset . _pk_field . name , False ) <EOL> def fk_field ( self ) : <EOL> fk = getattr ( self . formset , "<STR_LIT>" , None ) <EOL> if fk : <EOL> return AdminField ( self . form , fk . name , False ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> def deletion_field ( self ) : <EOL> from django . forms . formsets import DELETION_FIELD_NAME <EOL> return AdminField ( self . form , DELETION_FIELD_NAME , False ) <EOL> def ordering_field ( self ) : <EOL> from django . forms . formsets import ORDERING_FIELD_NAME <EOL> return AdminField ( self . form , ORDERING_FIELD_NAME , False ) <EOL> class InlineFieldset ( Fieldset ) : <EOL> def __init__ ( self , formset , * args , ** kwargs ) : <EOL> self . formset = formset <EOL> super ( InlineFieldset , self ) . __init__ ( * args , ** kwargs ) <EOL> def __iter__ ( self ) : <EOL> fk = getattr ( self . formset , "<STR_LIT>" , None ) <EOL> for field in self . fields : <EOL> if fk and fk . name == field : <EOL> continue <EOL> yield Fieldline ( self . form , field ) <EOL> class AdminErrorList ( forms . util . ErrorList ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , form , inline_formsets ) : <EOL> if form . is_bound : <EOL> self . extend ( form . errors . values ( ) ) <EOL> for inline_formset in inline_formsets : <EOL> self . extend ( inline_formset . non_form_errors ( ) ) <EOL> for errors_in_inline_form in inline_formset . errors : <EOL> self . extend ( errors_in_inline_form . values ( ) ) <EOL> def normalize_fieldsets ( fieldsets ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> for name , options in fieldsets : <EOL> result . append ( ( name , normalize_dictionary ( options ) ) ) <EOL> return result <EOL> def normalize_dictionary ( data_dict ) : <EOL> """<STR_LIT>""" <EOL> for key , value in data_dict . items ( ) : <EOL> if not isinstance ( key , str ) : <EOL> del data_dict [ key ] <EOL> data_dict [ str ( key ) ] = value <EOL> return data_dict </s>
