<s> from django import forms <EOL> from django . db . models import get_model <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from . . utils import unicode_slugify as slugify <EOL> from . . utils import get_page_model , get_node_model <EOL> PageNode = get_node_model ( ) <EOL> FancyPage = get_page_model ( ) <EOL> PageType = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> PageGroup = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> DATE_FORMAT = '<STR_LIT>' <EOL> class PageNodeForm ( forms . ModelForm ) : <EOL> groups = forms . ModelMultipleChoiceField ( <EOL> label = _ ( "<STR_LIT>" ) , queryset = PageGroup . objects . none ( ) , <EOL> widget = forms . CheckboxSelectMultiple ( ) , required = False ) <EOL> class Meta : <EOL> model = FancyPage <EOL> exclude = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . parent_pk = kwargs . pop ( '<STR_LIT>' , None ) <EOL> super ( PageNodeForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields [ '<STR_LIT>' ] . queryset = PageGroup . objects . all ( ) <EOL> page_field_names = self . fields . keys ( ) <EOL> self . node_field_names = [ ] <EOL> for field in PageNode . _meta . fields : <EOL> if field . editable and field . name not in [ '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:path>' , '<STR_LIT>' ] : <EOL> self . node_field_names . append ( field . name ) <EOL> additional_fields = forms . fields_for_model ( <EOL> PageNode , fields = self . node_field_names ) <EOL> self . fields . update ( additional_fields ) <EOL> instance = kwargs . get ( '<STR_LIT>' ) <EOL> if instance : <EOL> for field_name in self . node_field_names : <EOL> self . fields [ field_name ] . initial = getattr ( <EOL> instance . node , field_name ) <EOL> self . fields . keyOrder = self . node_field_names + page_field_names <EOL> def clean_name ( self ) : <EOL> name = self . cleaned_data . get ( '<STR_LIT:name>' ) <EOL> if self . instance . id is not None : <EOL> return name <EOL> try : <EOL> FancyPage . objects . get ( node__slug = slugify ( name ) ) <EOL> except FancyPage . DoesNotExist : <EOL> pass <EOL> else : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> return name <EOL> def save ( self , * args , ** kwargs ) : <EOL> page_data = { } <EOL> node_data = { } <EOL> for fname , fvalue in self . cleaned_data . iteritems ( ) : <EOL> if fname in self . node_field_names : <EOL> node_data [ fname ] = fvalue <EOL> else : <EOL> page_data [ fname ] = fvalue <EOL> if not self . instance . id : <EOL> try : <EOL> parent = FancyPage . objects . get ( pk = self . parent_pk ) <EOL> except FancyPage . DoesNotExist : <EOL> parent = None <EOL> if parent : <EOL> self . instance . node = parent . node . add_child ( ** node_data ) <EOL> else : <EOL> self . instance . node = PageNode . add_root ( ** node_data ) <EOL> else : <EOL> for key , value in node_data . iteritems ( ) : <EOL> setattr ( self . instance . node , key , value ) <EOL> self . instance . node . save ( ) <EOL> return super ( PageNodeForm , self ) . save ( * args , ** kwargs ) <EOL> class BlockUpdateSelectForm ( forms . Form ) : <EOL> block_code = forms . ChoiceField ( label = _ ( "<STR_LIT>" ) ) <EOL> def __init__ ( self , container , * args , ** kwargs ) : <EOL> super ( BlockUpdateSelectForm , self ) . __init__ ( * args , ** kwargs ) <EOL> block_choices = [ ] <EOL> for block in container . blocks . select_subclasses ( ) : <EOL> block_choices . append ( ( block . id , unicode ( block ) ) ) <EOL> self . fields [ '<STR_LIT>' ] . choices = block_choices <EOL> class BlockForm ( forms . ModelForm ) : <EOL> template_name = "<STR_LIT>" <EOL> class Meta : <EOL> exclude = ( '<STR_LIT>' , ) <EOL> widgets = { '<STR_LIT>' : forms . HiddenInput ( ) } <EOL> class TextBlockForm ( BlockForm ) : <EOL> class Meta : <EOL> exclude = ( '<STR_LIT>' , ) <EOL> widgets = { <EOL> '<STR_LIT>' : forms . HiddenInput ( ) , <EOL> '<STR_LIT:text>' : forms . Textarea ( attrs = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:10> } ) } <EOL> class TitleTextBlockForm ( BlockForm ) : <EOL> class Meta : <EOL> exclude = ( '<STR_LIT>' , ) <EOL> widgets = { <EOL> '<STR_LIT>' : forms . HiddenInput ( ) , <EOL> '<STR_LIT:text>' : forms . Textarea ( attrs = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:10> } ) } <EOL> class TwoColumnLayoutBlockForm ( BlockForm ) : <EOL> left_width = forms . IntegerField ( <EOL> widget = forms . TextInput ( attrs = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:12> , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> } ) , <EOL> label = _ ( "<STR_LIT>" ) <EOL> ) <EOL> class TabBlockForm ( BlockForm ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( TabBlockForm , self ) . __init__ ( * args , ** kwargs ) <EOL> instance = kwargs [ '<STR_LIT>' ] <EOL> if instance : <EOL> for tab in instance . tabs . all ( ) : <EOL> field_name = "<STR_LIT>" % tab . id <EOL> self . fields [ field_name ] = forms . CharField ( ) <EOL> self . fields [ field_name ] . initial = tab . title <EOL> self . fields [ field_name ] . label = _ ( "<STR_LIT>" ) <EOL> def save ( self ) : <EOL> instance = super ( TabBlockForm , self ) . save ( ) <EOL> for tab in instance . tabs . all ( ) : <EOL> field_name = "<STR_LIT>" % tab . id <EOL> tab . title = self . cleaned_data [ field_name ] <EOL> tab . save ( ) <EOL> return instance <EOL> class FormBlockForm ( BlockForm ) : <EOL> form_selection = forms . ChoiceField ( label = _ ( "<STR_LIT>" ) ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( FormBlockForm , self ) . __init__ ( * args , ** kwargs ) <EOL> from fancypages . helpers import BlockFormSettings <EOL> settings = BlockFormSettings ( ) <EOL> self . fields [ '<STR_LIT>' ] . choices = tuple ( settings . as_choices ( ) ) </s>
