<s> from django . contrib . admin import site <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . core . urlresolvers import reverse <EOL> from django . db . models import get_model <EOL> from django . forms . models import modelform_factory <EOL> from django . http import HttpResponseRedirect , HttpResponseForbidden <EOL> from django . shortcuts import render_to_response <EOL> from django . template . context import RequestContext <EOL> from django . template . loader import get_template <EOL> from django . template import TemplateDoesNotExist <EOL> from django . utils . translation import ugettext as _ <EOL> from django . views . decorators . cache import never_cache <EOL> from django . utils . importlib import import_module <EOL> from django . conf import settings <EOL> from django . forms import CharField <EOL> from django . contrib import messages <EOL> from forms import DeleteRequestForm , FrontendAdminModelForm <EOL> EXCLUDES = getattr ( settings , '<STR_LIT>' , { } ) <EOL> FIELDS = getattr ( settings , '<STR_LIT>' , { } ) <EOL> FORMS = getattr ( settings , '<STR_LIT>' , { } ) <EOL> def import_function ( s ) : <EOL> """<STR_LIT>""" <EOL> a = s . split ( '<STR_LIT:.>' ) <EOL> j = lambda x : '<STR_LIT:.>' . join ( x ) <EOL> return getattr ( import_module ( j ( a [ : - <NUM_LIT:1> ] ) ) , a [ - <NUM_LIT:1> ] ) <EOL> def check_permission ( request , mode_name , app_label , model_name ) : <EOL> '''<STR_LIT>''' <EOL> p = '<STR_LIT>' % ( app_label , mode_name , model_name ) <EOL> return request . user . is_active and request . user . has_perm ( p ) <EOL> def _get_instance ( request , mode_name , app_label , model_name , instance_id = None , <EOL> form = None , <EOL> form_fields = None , <EOL> form_exclude = None ) : <EOL> '''<STR_LIT>''' <EOL> if not check_permission ( request , mode_name , app_label , model_name ) : <EOL> return HttpResponseForbidden ( '<STR_LIT>' ) <EOL> try : <EOL> model = get_model ( app_label , model_name ) <EOL> except AttributeError : <EOL> return HttpResponseForbidden ( '<STR_LIT>' ) <EOL> label = '<STR_LIT>' % ( app_label , model_name ) <EOL> if label in FORMS and not form : <EOL> form = import_function ( FORMS [ label ] ) <EOL> elif model in site . _registry and not form : <EOL> form = site . _registry [ model ] . form <EOL> elif form is None : <EOL> form = FrontendAdminModelForm <EOL> if label in EXCLUDES : <EOL> form_exclude = EXCLUDES [ label ] <EOL> if label in FIELDS : <EOL> form_fields = FIELDS [ label ] <EOL> instance_form = modelform_factory ( model , form = form , <EOL> fields = form_fields , exclude = form_exclude ) <EOL> if instance_id : <EOL> instance = model . objects . get ( pk = instance_id ) <EOL> return model , instance_form , instance <EOL> return model , instance_form <EOL> def _handle_cancel ( request , instance = None ) : <EOL> '''<STR_LIT>''' <EOL> if request . POST . get ( '<STR_LIT>' , False ) : <EOL> if request . GET . get ( '<STR_LIT>' , False ) : <EOL> return HttpResponseRedirect ( request . GET . get ( '<STR_LIT>' ) ) <EOL> if instance and hasattr ( instance , '<STR_LIT>' ) : <EOL> return HttpResponseRedirect ( instance . get_absolute_url ( ) ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> return None <EOL> def _handle_response ( request , instance = None ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> return HttpResponseRedirect ( request . REQUEST [ '<STR_LIT>' ] ) <EOL> if instance and hasattr ( instance , '<STR_LIT>' ) : <EOL> return HttpResponseRedirect ( instance . get_absolute_url ( ) ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> def _find_template ( template_name , app_label = None , model_name = None ) : <EOL> """<STR_LIT>""" <EOL> if app_label is None and model_name is None : <EOL> return '<STR_LIT>' % template_name <EOL> try : <EOL> name = '<STR_LIT>' % ( app_label , model_name , template_name ) <EOL> get_template ( name ) <EOL> return name <EOL> except TemplateDoesNotExist : <EOL> return '<STR_LIT>' % template_name <EOL> def _get_template ( request , app_label = None , model_name = None ) : <EOL> '''<STR_LIT>''' <EOL> return _find_template ( request . is_ajax ( ) and '<STR_LIT>' or '<STR_LIT>' , <EOL> app_label , model_name ) <EOL> @ never_cache <EOL> @ login_required <EOL> def add ( request , app_label , model_name , mode_name = '<STR_LIT>' , <EOL> form_fields = None , <EOL> form_exclude = None ) : <EOL> instance_return = _get_instance ( request , mode_name , app_label , model_name , <EOL> form_fields = form_fields , <EOL> form_exclude = form_exclude ) <EOL> if isinstance ( instance_return , HttpResponseForbidden ) : <EOL> return instance_return <EOL> model , instance_form = instance_return <EOL> cancel = _handle_cancel ( request ) <EOL> if cancel : <EOL> return cancel <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = instance_form ( request . POST , request . FILES ) <EOL> if form . is_valid ( ) : <EOL> instance = form . save ( ) <EOL> msg = _ ( u'<STR_LIT>' ) % { '<STR_LIT>' : model . _meta . verbose_name } <EOL> try : <EOL> request . user . message_set . create ( message = msg ) <EOL> except AttributeError : <EOL> messages . success ( request , msg ) <EOL> if request . is_ajax ( ) : <EOL> return success ( request ) <EOL> return _handle_response ( request , instance ) <EOL> else : <EOL> form = instance_form ( ) <EOL> template_context = { <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : request . get_full_path ( ) , <EOL> '<STR_LIT>' : model . _meta . verbose_name , <EOL> '<STR_LIT>' : form <EOL> } <EOL> return render_to_response ( <EOL> _get_template ( request , app_label , model_name ) , <EOL> template_context , <EOL> RequestContext ( request ) <EOL> ) <EOL> @ never_cache <EOL> @ login_required <EOL> def change ( request , app_label , model_name , instance_id , mode_name = '<STR_LIT>' , <EOL> form_fields = None , <EOL> form_exclude = None ) : <EOL> instance_return = _get_instance ( request , mode_name , app_label , model_name , <EOL> instance_id , <EOL> form_fields = form_fields , <EOL> form_exclude = form_exclude ) <EOL> if isinstance ( instance_return , HttpResponseForbidden ) : <EOL> return instance_return <EOL> model , instance_form , instance = instance_return <EOL> cancel = _handle_cancel ( request ) <EOL> if cancel : <EOL> return cancel <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = instance_form ( request . POST , request . FILES , instance = instance ) <EOL> if form . is_valid ( ) : <EOL> instance = form . save ( ) <EOL> msg = _ ( u'<STR_LIT>' ) % { '<STR_LIT>' : model . _meta . verbose_name } <EOL> try : <EOL> request . user . message_set . create ( message = msg ) <EOL> except AttributeError : <EOL> messages . success ( request , msg ) <EOL> if request . is_ajax ( ) : <EOL> return success ( request ) <EOL> return _handle_response ( request , instance ) <EOL> else : <EOL> form = instance_form ( instance = instance ) <EOL> template_context = { <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : request . get_full_path ( ) , <EOL> '<STR_LIT>' : model . _meta . verbose_name , <EOL> '<STR_LIT>' : form , <EOL> } <EOL> return render_to_response ( <EOL> _get_template ( request , app_label , model_name ) , <EOL> template_context , <EOL> RequestContext ( request ) <EOL> ) <EOL> @ never_cache <EOL> @ login_required <EOL> def delete ( request , app_label , model_name , instance_id , <EOL> delete_form = DeleteRequestForm ) : <EOL> instance_return = _get_instance ( request , '<STR_LIT>' , app_label , model_name , instance_id ) <EOL> if isinstance ( instance_return , HttpResponseForbidden ) : <EOL> return instance_return <EOL> model , instance_form , instance = instance_return <EOL> cancel = _handle_cancel ( request ) <EOL> if cancel : <EOL> return cancel <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = delete_form ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> instance . delete ( ) <EOL> msg = _ ( u'<STR_LIT>' ) % { '<STR_LIT>' : model . _meta . verbose_name } <EOL> try : <EOL> request . user . message_set . create ( message = msg ) <EOL> except AttributeError : <EOL> messages . success ( request , msg ) <EOL> if request . is_ajax ( ) : <EOL> return success_delete ( request ) <EOL> return _handle_response ( request , instance ) <EOL> else : <EOL> form = delete_form ( ) <EOL> template_context = { <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : request . get_full_path ( ) , <EOL> '<STR_LIT>' : model . _meta . verbose_name , <EOL> '<STR_LIT>' : form , <EOL> } <EOL> return render_to_response ( <EOL> _get_template ( request , None , None ) , <EOL> template_context , <EOL> RequestContext ( request ) <EOL> ) <EOL> def success ( request , template_name = '<STR_LIT>' , template_ajax = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> template = _find_template ( request . is_ajax ( ) and template_ajax or template_name ) <EOL> return render_to_response ( template , { } , RequestContext ( request ) ) <EOL> def success_delete ( request , template_name = '<STR_LIT>' , template_ajax = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> template = _find_template ( request . is_ajax ( ) and template_ajax or template_name ) <EOL> return render_to_response ( template , { } , RequestContext ( request ) ) </s>
