<s> from django import forms <EOL> from django . core . urlresolvers import reverse , NoReverseMatch <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . core . validators import URLValidator <EOL> from treenav . models import MenuItem <EOL> from mptt . forms import TreeNodeChoiceField , MPTTAdminForm <EOL> class MenuItemFormMixin ( object ) : <EOL> def clean_link ( self ) : <EOL> link = self . cleaned_data [ '<STR_LIT>' ] or '<STR_LIT>' <EOL> if any ( [ link . startswith ( s ) for s in ( '<STR_LIT>' , '<STR_LIT>' ) ] ) : <EOL> URLValidator ( ) ( link ) <EOL> elif link and not any ( [ link . startswith ( s ) for s in ( '<STR_LIT>' , '<STR_LIT:/>' ) ] ) : <EOL> try : <EOL> reverse ( link ) <EOL> except NoReverseMatch : <EOL> raise forms . ValidationError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return self . cleaned_data [ '<STR_LIT>' ] <EOL> def clean ( self ) : <EOL> super ( MenuItemFormMixin , self ) . clean ( ) <EOL> content_type = self . cleaned_data [ '<STR_LIT>' ] <EOL> object_id = self . cleaned_data [ '<STR_LIT>' ] <EOL> if ( content_type and not object_id ) or ( not content_type and object_id ) : <EOL> raise forms . ValidationError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> if content_type and object_id : <EOL> try : <EOL> obj = content_type . get_object_for_this_type ( pk = object_id ) <EOL> except ObjectDoesNotExist as e : <EOL> raise forms . ValidationError ( str ( e ) ) <EOL> try : <EOL> obj . get_absolute_url ( ) <EOL> except AttributeError as e : <EOL> raise forms . ValidationError ( str ( e ) ) <EOL> if '<STR_LIT>' in self . cleaned_data and self . cleaned_data [ '<STR_LIT>' ] and '<STR_LIT>' in self . cleaned_data and self . cleaned_data [ '<STR_LIT>' ] . startswith ( '<STR_LIT>' ) : <EOL> raise forms . ValidationError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return self . cleaned_data <EOL> class MenuItemForm ( MenuItemFormMixin , MPTTAdminForm ) : <EOL> class Meta : <EOL> model = MenuItem <EOL> fields = "<STR_LIT>" <EOL> class MenuItemInlineForm ( MenuItemFormMixin , forms . ModelForm ) : <EOL> class Meta : <EOL> model = MenuItem <EOL> fields = "<STR_LIT>" <EOL> class GenericInlineMenuItemForm ( forms . ModelForm ) : <EOL> parent = TreeNodeChoiceField ( <EOL> queryset = MenuItem . objects . all ( ) , <EOL> required = False <EOL> ) <EOL> class Meta : <EOL> model = MenuItem <EOL> fields = ( '<STR_LIT>' , '<STR_LIT:label>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) </s>
