<s> from django . core . exceptions import ImproperlyConfigured <EOL> from django . utils import six <EOL> from . detail import DetailView <EOL> from . edit import WrapDocumentForm , djmod <EOL> class EmbeddedFormMixin ( djmod . FormMixin ) : <EOL> """<STR_LIT>""" <EOL> embedded_form_class = None <EOL> embedded_context_name = '<STR_LIT>' <EOL> def get_form_class ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . embedded_form_class : <EOL> return self . embedded_form_class <EOL> else : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" ) <EOL> def get_form ( self , form_class = None ) : <EOL> """<STR_LIT>""" <EOL> if form_class is None : <EOL> form_class = self . get_form_class ( ) <EOL> return form_class ( self . object , ** self . get_form_kwargs ( ) ) <EOL> def get_embedded_object ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . embedded_object ( ) <EOL> else : <EOL> klass = self . get_form_class ( ) <EOL> return klass . Meta . document ( ) <EOL> def get_form_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> kwargs = super ( EmbeddedFormMixin , self ) . get_form_kwargs ( ) <EOL> kwargs . update ( { '<STR_LIT>' : self . get_embedded_object ( ) } ) <EOL> if not '<STR_LIT>' in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = { } <EOL> return kwargs <EOL> def get_success_url ( self ) : <EOL> object = getattr ( self , '<STR_LIT:object>' , self . get_object ( ) ) <EOL> if self . success_url : <EOL> url = self . success_url % object . __dict__ <EOL> else : <EOL> try : <EOL> url = object . get_absolute_url ( ) <EOL> except AttributeError : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return url <EOL> def form_valid ( self , form ) : <EOL> self . embedded_object = form . save ( ) <EOL> return super ( EmbeddedFormMixin , self ) . form_valid ( form ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( EmbeddedFormMixin , self ) . get_context_data ( ** kwargs ) <EOL> self . object = getattr ( self , '<STR_LIT:object>' , self . get_object ( ) ) <EOL> if '<STR_LIT>' in kwargs : <EOL> form = kwargs [ '<STR_LIT>' ] <EOL> else : <EOL> form = self . get_form ( self . get_form_class ( ) ) <EOL> context [ self . embedded_context_name ] = form <EOL> return context <EOL> class ProcessEmbeddedFormMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> self . object = self . get_object ( ) <EOL> form_class = self . get_form_class ( ) <EOL> form = self . get_form ( form_class ) <EOL> if form . is_valid ( ) : <EOL> return self . form_valid ( form ) <EOL> else : <EOL> return self . form_invalid ( form ) <EOL> super ( ProcessEmbeddedFormMixin , self ) . post ( request , * args , ** kwargs ) <EOL> class BaseEmbeddedFormMixin ( EmbeddedFormMixin , ProcessEmbeddedFormMixin ) : <EOL> """<STR_LIT>""" <EOL> class EmbeddedDetailView ( six . with_metaclass ( <EOL> WrapDocumentForm , <EOL> BaseEmbeddedFormMixin , DetailView ) ) : <EOL> """<STR_LIT>""" </s>
