<s> import warnings <EOL> from django . forms import models as model_forms <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . http import HttpResponseRedirect <EOL> from django . utils . encoding import force_text <EOL> from django . views . generic . base import TemplateResponseMixin , ContextMixin , View <EOL> from django . views . generic . detail import ( SingleObjectMixin , <EOL> SingleObjectTemplateResponseMixin , BaseDetailView ) <EOL> class FormMixin ( ContextMixin ) : <EOL> """<STR_LIT>""" <EOL> initial = { } <EOL> form_class = None <EOL> success_url = None <EOL> prefix = None <EOL> def get_initial ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . initial . copy ( ) <EOL> def get_prefix ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . prefix <EOL> def get_form_class ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . form_class <EOL> def get_form ( self , form_class ) : <EOL> """<STR_LIT>""" <EOL> return form_class ( ** self . get_form_kwargs ( ) ) <EOL> def get_form_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> kwargs = { <EOL> '<STR_LIT>' : self . get_initial ( ) , <EOL> '<STR_LIT>' : self . get_prefix ( ) , <EOL> } <EOL> if self . request . method in ( '<STR_LIT:POST>' , '<STR_LIT>' ) : <EOL> kwargs . update ( { <EOL> '<STR_LIT:data>' : self . request . POST , <EOL> '<STR_LIT>' : self . request . FILES , <EOL> } ) <EOL> return kwargs <EOL> def get_success_url ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . success_url : <EOL> url = force_text ( self . success_url ) <EOL> else : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" ) <EOL> return url <EOL> def form_valid ( self , form ) : <EOL> """<STR_LIT>""" <EOL> return HttpResponseRedirect ( self . get_success_url ( ) ) <EOL> def form_invalid ( self , form ) : <EOL> """<STR_LIT>""" <EOL> return self . render_to_response ( self . get_context_data ( form = form ) ) <EOL> class ModelFormMixin ( FormMixin , SingleObjectMixin ) : <EOL> """<STR_LIT>""" <EOL> fields = None <EOL> def get_form_class ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . form_class : <EOL> return self . form_class <EOL> else : <EOL> if self . model is not None : <EOL> model = self . model <EOL> elif hasattr ( self , '<STR_LIT:object>' ) and self . object is not None : <EOL> model = self . object . __class__ <EOL> else : <EOL> model = self . get_queryset ( ) . model <EOL> if self . fields is None : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" % self . __class__ . __name__ , <EOL> PendingDeprecationWarning ) <EOL> return model_forms . modelform_factory ( model , fields = self . fields ) <EOL> def get_form_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> kwargs = super ( ModelFormMixin , self ) . get_form_kwargs ( ) <EOL> kwargs . update ( { '<STR_LIT>' : self . object } ) <EOL> return kwargs <EOL> def get_success_url ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . success_url : <EOL> url = self . success_url % self . object . __dict__ <EOL> else : <EOL> try : <EOL> url = self . 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> """<STR_LIT>""" <EOL> self . object = form . save ( ) <EOL> return super ( ModelFormMixin , self ) . form_valid ( form ) <EOL> class ProcessFormView ( View ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> form_class = self . get_form_class ( ) <EOL> form = self . get_form ( form_class ) <EOL> return self . render_to_response ( self . get_context_data ( form = form ) ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <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> def put ( self , * args , ** kwargs ) : <EOL> return self . post ( * args , ** kwargs ) <EOL> class BaseFormView ( FormMixin , ProcessFormView ) : <EOL> """<STR_LIT>""" <EOL> class FormView ( TemplateResponseMixin , BaseFormView ) : <EOL> """<STR_LIT>""" <EOL> class BaseCreateView ( ModelFormMixin , ProcessFormView ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> self . object = None <EOL> return super ( BaseCreateView , self ) . get ( request , * args , ** kwargs ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> self . object = None <EOL> return super ( BaseCreateView , self ) . post ( request , * args , ** kwargs ) <EOL> class CreateView ( SingleObjectTemplateResponseMixin , BaseCreateView ) : <EOL> """<STR_LIT>""" <EOL> template_name_suffix = '<STR_LIT>' <EOL> class BaseUpdateView ( ModelFormMixin , ProcessFormView ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> self . object = self . get_object ( ) <EOL> return super ( BaseUpdateView , self ) . get ( request , * args , ** kwargs ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> self . object = self . get_object ( ) <EOL> return super ( BaseUpdateView , self ) . post ( request , * args , ** kwargs ) <EOL> class UpdateView ( SingleObjectTemplateResponseMixin , BaseUpdateView ) : <EOL> """<STR_LIT>""" <EOL> template_name_suffix = '<STR_LIT>' <EOL> class DeletionMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> success_url = None <EOL> def delete ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . object = self . get_object ( ) <EOL> success_url = self . get_success_url ( ) <EOL> self . object . delete ( ) <EOL> return HttpResponseRedirect ( success_url ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> return self . delete ( request , * args , ** kwargs ) <EOL> def get_success_url ( self ) : <EOL> if self . success_url : <EOL> return self . success_url % self . object . __dict__ <EOL> else : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" ) <EOL> class BaseDeleteView ( DeletionMixin , BaseDetailView ) : <EOL> """<STR_LIT>""" <EOL> class DeleteView ( SingleObjectTemplateResponseMixin , BaseDeleteView ) : <EOL> """<STR_LIT>""" <EOL> template_name_suffix = '<STR_LIT>' </s>
