<s> from django . forms . models import ModelFormMetaclass , ModelForm <EOL> from django . template import RequestContext , loader <EOL> from django . http import Http404 , HttpResponse , HttpResponseRedirect <EOL> from django . core . xheaders import populate_xheaders <EOL> from django . core . exceptions import ObjectDoesNotExist , ImproperlyConfigured <EOL> from django . utils . translation import ugettext <EOL> from django . contrib . auth . views import redirect_to_login <EOL> from django . views . generic import GenericViewError <EOL> from django . contrib import messages <EOL> import warnings <EOL> warnings . warn ( <EOL> '<STR_LIT>' , <EOL> PendingDeprecationWarning <EOL> ) <EOL> def apply_extra_context ( extra_context , context ) : <EOL> """<STR_LIT>""" <EOL> for key , value in extra_context . iteritems ( ) : <EOL> if callable ( value ) : <EOL> context [ key ] = value ( ) <EOL> else : <EOL> context [ key ] = value <EOL> def get_model_and_form_class ( model , form_class ) : <EOL> """<STR_LIT>""" <EOL> if form_class : <EOL> return form_class . _meta . model , form_class <EOL> if model : <EOL> tmp_model = model <EOL> class Meta : <EOL> model = tmp_model <EOL> class_name = model . __name__ + '<STR_LIT>' <EOL> form_class = ModelFormMetaclass ( class_name , ( ModelForm , ) , { '<STR_LIT:Meta>' : Meta } ) <EOL> return model , form_class <EOL> raise GenericViewError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def redirect ( post_save_redirect , obj ) : <EOL> """<STR_LIT>""" <EOL> if post_save_redirect : <EOL> return HttpResponseRedirect ( post_save_redirect % obj . __dict__ ) <EOL> elif hasattr ( obj , '<STR_LIT>' ) : <EOL> return HttpResponseRedirect ( obj . get_absolute_url ( ) ) <EOL> else : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def lookup_object ( model , object_id , slug , slug_field ) : <EOL> """<STR_LIT>""" <EOL> lookup_kwargs = { } <EOL> if object_id : <EOL> lookup_kwargs [ '<STR_LIT>' % model . _meta . pk . name ] = object_id <EOL> elif slug and slug_field : <EOL> lookup_kwargs [ '<STR_LIT>' % slug_field ] = slug <EOL> else : <EOL> raise GenericViewError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> try : <EOL> return model . objects . get ( ** lookup_kwargs ) <EOL> except ObjectDoesNotExist : <EOL> raise Http404 ( "<STR_LIT>" <EOL> % ( model . _meta . verbose_name , lookup_kwargs ) ) <EOL> def create_object ( request , model = None , template_name = None , <EOL> template_loader = loader , extra_context = None , post_save_redirect = None , <EOL> login_required = False , context_processors = None , form_class = None ) : <EOL> """<STR_LIT>""" <EOL> if extra_context is None : extra_context = { } <EOL> if login_required and not request . user . is_authenticated ( ) : <EOL> return redirect_to_login ( request . path ) <EOL> model , form_class = get_model_and_form_class ( model , form_class ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = form_class ( request . POST , request . FILES ) <EOL> if form . is_valid ( ) : <EOL> new_object = form . save ( ) <EOL> msg = ugettext ( "<STR_LIT>" ) % { "<STR_LIT>" : model . _meta . verbose_name } <EOL> messages . success ( request , msg , fail_silently = True ) <EOL> return redirect ( post_save_redirect , new_object ) <EOL> else : <EOL> form = form_class ( ) <EOL> if not template_name : <EOL> template_name = "<STR_LIT>" % ( model . _meta . app_label , model . _meta . object_name . lower ( ) ) <EOL> t = template_loader . get_template ( template_name ) <EOL> c = RequestContext ( request , { <EOL> '<STR_LIT>' : form , <EOL> } , context_processors ) <EOL> apply_extra_context ( extra_context , c ) <EOL> return HttpResponse ( t . render ( c ) ) <EOL> def update_object ( request , model = None , object_id = None , slug = None , <EOL> slug_field = '<STR_LIT>' , template_name = None , template_loader = loader , <EOL> extra_context = None , post_save_redirect = None , login_required = False , <EOL> context_processors = None , template_object_name = '<STR_LIT:object>' , <EOL> form_class = None ) : <EOL> """<STR_LIT>""" <EOL> if extra_context is None : extra_context = { } <EOL> if login_required and not request . user . is_authenticated ( ) : <EOL> return redirect_to_login ( request . path ) <EOL> model , form_class = get_model_and_form_class ( model , form_class ) <EOL> obj = lookup_object ( model , object_id , slug , slug_field ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = form_class ( request . POST , request . FILES , instance = obj ) <EOL> if form . is_valid ( ) : <EOL> obj = form . save ( ) <EOL> msg = ugettext ( "<STR_LIT>" ) % { "<STR_LIT>" : model . _meta . verbose_name } <EOL> messages . success ( request , msg , fail_silently = True ) <EOL> return redirect ( post_save_redirect , obj ) <EOL> else : <EOL> form = form_class ( instance = obj ) <EOL> if not template_name : <EOL> template_name = "<STR_LIT>" % ( model . _meta . app_label , model . _meta . object_name . lower ( ) ) <EOL> t = template_loader . get_template ( template_name ) <EOL> c = RequestContext ( request , { <EOL> '<STR_LIT>' : form , <EOL> template_object_name : obj , <EOL> } , context_processors ) <EOL> apply_extra_context ( extra_context , c ) <EOL> response = HttpResponse ( t . render ( c ) ) <EOL> populate_xheaders ( request , response , model , getattr ( obj , obj . _meta . pk . attname ) ) <EOL> return response <EOL> def delete_object ( request , model , post_delete_redirect , object_id = None , <EOL> slug = None , slug_field = '<STR_LIT>' , template_name = None , <EOL> template_loader = loader , extra_context = None , login_required = False , <EOL> context_processors = None , template_object_name = '<STR_LIT:object>' ) : <EOL> """<STR_LIT>""" <EOL> if extra_context is None : extra_context = { } <EOL> if login_required and not request . user . is_authenticated ( ) : <EOL> return redirect_to_login ( request . path ) <EOL> obj = lookup_object ( model , object_id , slug , slug_field ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> obj . delete ( ) <EOL> msg = ugettext ( "<STR_LIT>" ) % { "<STR_LIT>" : model . _meta . verbose_name } <EOL> messages . success ( request , msg , fail_silently = True ) <EOL> return HttpResponseRedirect ( post_delete_redirect ) <EOL> else : <EOL> if not template_name : <EOL> template_name = "<STR_LIT>" % ( model . _meta . app_label , model . _meta . object_name . lower ( ) ) <EOL> t = template_loader . get_template ( template_name ) <EOL> c = RequestContext ( request , { <EOL> template_object_name : obj , <EOL> } , context_processors ) <EOL> apply_extra_context ( extra_context , c ) <EOL> response = HttpResponse ( t . render ( c ) ) <EOL> populate_xheaders ( request , response , model , getattr ( obj , obj . _meta . pk . attname ) ) <EOL> return response </s>
