<s> from django import forms <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from easytree import utils <EOL> from easytree . exceptions import EasyTreeException <EOL> pos_map = { <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT:left>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT:right>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) <EOL> } <EOL> class EasyTreeModelChoiceField ( forms . ModelChoiceField ) : <EOL> def label_from_instance ( self , obj ) : <EOL> return u'<STR_LIT>' % ( <EOL> u'<STR_LIT>' * ( ( obj . depth or <NUM_LIT:1> ) - <NUM_LIT:1> ) , <EOL> super ( EasyTreeModelChoiceField , self ) . label_from_instance ( obj ) <EOL> ) <EOL> class BaseEasyTreeForm ( forms . ModelForm ) : <EOL> toplevel_model_cache = None <EOL> def get_toplevel_model ( self ) : <EOL> if not self . toplevel_model_cache : <EOL> self . toplevel_model_cache = utils . get_toplevel_model ( self . _meta . model ) <EOL> return self . toplevel_model_cache <EOL> toplevel_model = property ( get_toplevel_model ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( BaseEasyTreeForm , self ) . __init__ ( * args , ** kwargs ) <EOL> raw_relative_to = getattr ( self . instance . _easytree_meta , '<STR_LIT>' , False ) <EOL> choice_field_kwargs = { <EOL> '<STR_LIT>' : self . toplevel_model . objects . order_by ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT:label>' : _ ( "<STR_LIT>" ) % { '<STR_LIT>' : self . toplevel_model . _meta . verbose_name } <EOL> } <EOL> if raw_relative_to : <EOL> choice_field_kwargs [ '<STR_LIT>' ] = forms . TextInput <EOL> self . fields [ '<STR_LIT>' ] = EasyTreeModelChoiceField ( ** choice_field_kwargs ) <EOL> max_depth = getattr ( self . instance . _easytree_meta , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if max_depth == <NUM_LIT:1> : <EOL> relative_positions_choices = ( '<STR_LIT:left>' , '<STR_LIT:right>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> elif getattr ( self . instance , '<STR_LIT>' , None ) : <EOL> relative_positions_choices = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> relative_positions_choices = [ k for k in pos_map . keys ( ) if k not in ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> self . fields [ '<STR_LIT>' ] = forms . ChoiceField ( <EOL> required = False , <EOL> choices = [ ( '<STR_LIT>' , '<STR_LIT>' ) ] + [ ( k , v ) for k , v in pos_map . items ( ) if k in relative_positions_choices ] , <EOL> label = _ ( "<STR_LIT>" ) <EOL> ) <EOL> def clean ( self ) : <EOL> cleaned_data = self . cleaned_data <EOL> model = self . toplevel_model <EOL> relative_to = cleaned_data . get ( '<STR_LIT>' ) <EOL> relative_position = cleaned_data . get ( '<STR_LIT>' ) <EOL> if not self . instance . pk : <EOL> if not relative_to : <EOL> try : <EOL> model . objects . validate_root ( None , relative_to , pos = relative_position , cleaned_data = cleaned_data ) <EOL> except EasyTreeException , e : <EOL> raise forms . ValidationError , e . message <EOL> else : <EOL> if relative_position in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> try : <EOL> model . objects . validate_child ( None , relative_to , pos = relative_position , cleaned_data = cleaned_data ) <EOL> except EasyTreeException , e : <EOL> raise forms . ValidationError , e . message <EOL> else : <EOL> try : <EOL> model . objects . validate_sibling ( None , relative_to , pos = relative_position , cleaned_data = cleaned_data ) <EOL> except EasyTreeException , e : <EOL> raise forms . ValidationError , e . message <EOL> else : <EOL> if relative_to : <EOL> try : <EOL> model . objects . validate_move ( self . instance , relative_to , pos = relative_position , cleaned_data = cleaned_data ) <EOL> except EasyTreeException , e : <EOL> raise forms . ValidationError , e . message <EOL> cleaned_data [ '<STR_LIT>' ] = relative_to <EOL> return cleaned_data <EOL> def save ( self , ** kwargs ) : <EOL> instance = super ( BaseEasyTreeForm , self ) . save ( commit = False ) <EOL> relative_to = self . cleaned_data . get ( '<STR_LIT>' , None ) <EOL> relative_position = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> if relative_to : <EOL> instance . easytree_relative_position = relative_position <EOL> instance . easytree_relative_to = relative_to <EOL> if kwargs . get ( '<STR_LIT>' , False ) : <EOL> instance . save ( ) <EOL> return instance </s>
