<s> """<STR_LIT>""" <EOL> import sys <EOL> from django import forms <EOL> from django . conf . urls import url <EOL> from django . contrib import admin <EOL> from django . contrib . admin . helpers import AdminForm , AdminErrorList <EOL> from django . contrib . admin . widgets import AdminRadioSelect <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . core . urlresolvers import RegexURLResolver <EOL> from django . http import Http404 , HttpResponseRedirect <EOL> from django . shortcuts import render_to_response <EOL> from django . template . context import RequestContext <EOL> from django . utils import six <EOL> from django . utils . encoding import force_text <EOL> from django . utils . http import urlencode <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> import django <EOL> try : <EOL> from django . contrib . admin . templatetags . admin_urls import add_preserved_filters <EOL> except ImportError : <EOL> def add_preserved_filters ( context , form_url ) : <EOL> return form_url <EOL> if sys . version_info [ <NUM_LIT:0> ] >= <NUM_LIT:3> : <EOL> long = int <EOL> __all__ = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ) <EOL> class RegistrationClosed ( RuntimeError ) : <EOL> "<STR_LIT>" <EOL> pass <EOL> class ChildAdminNotRegistered ( RuntimeError ) : <EOL> "<STR_LIT>" <EOL> pass <EOL> class PolymorphicModelChoiceForm ( forms . Form ) : <EOL> """<STR_LIT>""" <EOL> type_label = _ ( '<STR_LIT>' ) <EOL> ct_id = forms . ChoiceField ( label = type_label , widget = AdminRadioSelect ( attrs = { '<STR_LIT:class>' : '<STR_LIT>' } ) ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( PolymorphicModelChoiceForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields [ '<STR_LIT>' ] . label = self . type_label <EOL> class PolymorphicChildModelFilter ( admin . SimpleListFilter ) : <EOL> """<STR_LIT>""" <EOL> title = _ ( '<STR_LIT>' ) <EOL> parameter_name = '<STR_LIT>' <EOL> def lookups ( self , request , model_admin ) : <EOL> return model_admin . get_child_type_choices ( request , '<STR_LIT>' ) <EOL> def queryset ( self , request , queryset ) : <EOL> try : <EOL> value = int ( self . value ( ) ) <EOL> except TypeError : <EOL> value = None <EOL> if value : <EOL> for choice_value , _ in self . lookup_choices : <EOL> if choice_value == value : <EOL> return queryset . filter ( polymorphic_ctype_id = choice_value ) <EOL> raise PermissionDenied ( <EOL> '<STR_LIT>' . format ( value ) ) <EOL> return queryset <EOL> class PolymorphicParentModelAdmin ( admin . ModelAdmin ) : <EOL> """<STR_LIT>""" <EOL> base_model = None <EOL> child_models = None <EOL> polymorphic_list = False <EOL> add_type_template = None <EOL> add_type_form = PolymorphicModelChoiceForm <EOL> pk_regex = '<STR_LIT>' <EOL> def __init__ ( self , model , admin_site , * args , ** kwargs ) : <EOL> super ( PolymorphicParentModelAdmin , self ) . __init__ ( model , admin_site , * args , ** kwargs ) <EOL> self . _child_admin_site = self . admin_site . __class__ ( name = self . admin_site . name ) <EOL> self . _child_admin_site . get_app_list = lambda request : ( ) <EOL> self . _is_setup = False <EOL> def _lazy_setup ( self ) : <EOL> if self . _is_setup : <EOL> return <EOL> child_models = self . get_child_models ( ) <EOL> for Model , Admin in child_models : <EOL> self . register_child ( Model , Admin ) <EOL> self . _child_models = dict ( child_models ) <EOL> complete_registry = self . admin_site . _registry . copy ( ) <EOL> complete_registry . update ( self . _child_admin_site . _registry ) <EOL> self . _child_admin_site . _registry = complete_registry <EOL> self . _is_setup = True <EOL> def register_child ( self , model , model_admin ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_setup : <EOL> raise RegistrationClosed ( "<STR_LIT>" ) <EOL> if not issubclass ( model , self . base_model ) : <EOL> raise TypeError ( "<STR_LIT>" . format ( model . __name__ , self . base_model . __name__ ) ) <EOL> if not issubclass ( model_admin , admin . ModelAdmin ) : <EOL> raise TypeError ( "<STR_LIT>" . format ( model_admin . __name__ , admin . ModelAdmin . __name__ ) ) <EOL> self . _child_admin_site . register ( model , model_admin ) <EOL> def get_child_models ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . child_models is None : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> return self . child_models <EOL> def get_child_type_choices ( self , request , action ) : <EOL> """<STR_LIT>""" <EOL> choices = [ ] <EOL> for model , _ in self . get_child_models ( ) : <EOL> perm_function_name = '<STR_LIT>' . format ( action ) <EOL> model_admin = self . _get_real_admin_by_model ( model ) <EOL> perm_function = getattr ( model_admin , perm_function_name ) <EOL> if not perm_function ( request ) : <EOL> continue <EOL> ct = ContentType . objects . get_for_model ( model , for_concrete_model = False ) <EOL> choices . append ( ( ct . id , model . _meta . verbose_name ) ) <EOL> return choices <EOL> def _get_real_admin ( self , object_id ) : <EOL> try : <EOL> obj = self . model . objects . non_polymorphic ( ) . values ( '<STR_LIT>' ) . get ( pk = object_id ) <EOL> except self . model . DoesNotExist : <EOL> raise Http404 <EOL> return self . _get_real_admin_by_ct ( obj [ '<STR_LIT>' ] ) <EOL> def _get_real_admin_by_ct ( self , ct_id ) : <EOL> try : <EOL> ct = ContentType . objects . get_for_id ( ct_id ) <EOL> except ContentType . DoesNotExist as e : <EOL> raise Http404 ( e ) <EOL> model_class = ct . model_class ( ) <EOL> if not model_class : <EOL> raise Http404 ( "<STR_LIT>" . format ( * ct . natural_key ( ) ) ) <EOL> return self . _get_real_admin_by_model ( model_class ) <EOL> def _get_real_admin_by_model ( self , model_class ) : <EOL> if model_class not in self . _child_models : <EOL> raise PermissionDenied ( "<STR_LIT>" . format ( model_class ) ) <EOL> try : <EOL> return self . _child_admin_site . _registry [ model_class ] <EOL> except KeyError : <EOL> raise ChildAdminNotRegistered ( "<STR_LIT>" . format ( model_class ) ) <EOL> def get_queryset ( self , request ) : <EOL> qs = super ( PolymorphicParentModelAdmin , self ) . get_queryset ( request ) <EOL> if not self . polymorphic_list : <EOL> qs = qs . non_polymorphic ( ) <EOL> return qs <EOL> def queryset ( self , request ) : <EOL> qs = super ( PolymorphicParentModelAdmin , self ) . queryset ( request ) <EOL> if not self . polymorphic_list : <EOL> qs = qs . non_polymorphic ( ) <EOL> return qs <EOL> def add_view ( self , request , form_url = '<STR_LIT>' , extra_context = None ) : <EOL> """<STR_LIT>""" <EOL> ct_id = int ( request . GET . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> if not ct_id : <EOL> return self . add_type_view ( request ) <EOL> else : <EOL> real_admin = self . _get_real_admin_by_ct ( ct_id ) <EOL> form_url = add_preserved_filters ( { <EOL> '<STR_LIT>' : urlencode ( { '<STR_LIT>' : ct_id } ) , <EOL> '<STR_LIT>' : self . model . _meta } , <EOL> form_url <EOL> ) <EOL> return real_admin . add_view ( request , form_url , extra_context ) <EOL> def change_view ( self , request , object_id , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> real_admin = self . _get_real_admin ( object_id ) <EOL> return real_admin . change_view ( request , object_id , * args , ** kwargs ) <EOL> if django . VERSION >= ( <NUM_LIT:1> , <NUM_LIT:7> ) : <EOL> def changeform_view ( self , request , object_id = None , * args , ** kwargs ) : <EOL> if object_id : <EOL> real_admin = self . _get_real_admin ( object_id ) <EOL> return real_admin . changeform_view ( request , object_id , * args , ** kwargs ) <EOL> else : <EOL> return super ( PolymorphicParentModelAdmin , self ) . changeform_view ( request , object_id , * args , ** kwargs ) <EOL> def history_view ( self , request , object_id , extra_context = None ) : <EOL> """<STR_LIT>""" <EOL> real_admin = self . _get_real_admin ( object_id ) <EOL> return real_admin . history_view ( request , object_id , extra_context = extra_context ) <EOL> def delete_view ( self , request , object_id , extra_context = None ) : <EOL> """<STR_LIT>""" <EOL> real_admin = self . _get_real_admin ( object_id ) <EOL> return real_admin . delete_view ( request , object_id , extra_context ) <EOL> def get_preserved_filters ( self , request ) : <EOL> if '<STR_LIT>' in request . GET : <EOL> request . GET = request . GET . copy ( ) <EOL> filters = request . GET . get ( '<STR_LIT>' ) <EOL> f = filters . split ( "<STR_LIT:&>" ) <EOL> for x in f : <EOL> c = x . split ( '<STR_LIT:=>' ) <EOL> request . GET [ c [ <NUM_LIT:0> ] ] = c [ <NUM_LIT:1> ] <EOL> del request . GET [ '<STR_LIT>' ] <EOL> return super ( PolymorphicParentModelAdmin , self ) . get_preserved_filters ( request ) <EOL> def get_urls ( self ) : <EOL> """<STR_LIT>""" <EOL> urls = super ( PolymorphicParentModelAdmin , self ) . get_urls ( ) <EOL> info = _get_opt ( self . model ) <EOL> if django . VERSION < ( <NUM_LIT:1> , <NUM_LIT:9> ) : <EOL> new_change_url = url ( <EOL> r'<STR_LIT>' . format ( self . pk_regex ) , <EOL> self . admin_site . admin_view ( self . change_view ) , <EOL> name = '<STR_LIT>' . format ( * info ) <EOL> ) <EOL> redirect_urls = [ ] <EOL> for i , oldurl in enumerate ( urls ) : <EOL> if oldurl . name == new_change_url . name : <EOL> urls [ i ] = new_change_url <EOL> else : <EOL> redirect_urls = [ pat for pat in urls if not pat . name ] <EOL> urls = [ pat for pat in urls if pat . name ] <EOL> custom_urls = [ <EOL> url ( r'<STR_LIT>' , self . admin_site . admin_view ( self . subclass_view ) ) <EOL> ] <EOL> self . _lazy_setup ( ) <EOL> dummy_urls = [ ] <EOL> for model , _ in self . get_child_models ( ) : <EOL> admin = self . _get_real_admin_by_model ( model ) <EOL> dummy_urls += admin . get_urls ( ) <EOL> return urls + custom_urls + dummy_urls + redirect_urls <EOL> def subclass_view ( self , request , path ) : <EOL> """<STR_LIT>""" <EOL> ct_id = int ( request . GET . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> if not ct_id : <EOL> try : <EOL> pos = path . find ( '<STR_LIT:/>' ) <EOL> if pos == - <NUM_LIT:1> : <EOL> object_id = long ( path ) <EOL> else : <EOL> object_id = long ( path [ <NUM_LIT:0> : pos ] ) <EOL> except ValueError : <EOL> raise Http404 ( "<STR_LIT>" . format ( path ) ) <EOL> ct_id = self . model . objects . values_list ( '<STR_LIT>' , flat = True ) . get ( pk = object_id ) <EOL> real_admin = self . _get_real_admin_by_ct ( ct_id ) <EOL> resolver = RegexURLResolver ( '<STR_LIT>' , real_admin . urls ) <EOL> resolvermatch = resolver . resolve ( path ) <EOL> if not resolvermatch : <EOL> raise Http404 ( "<STR_LIT>" . format ( path ) ) <EOL> return resolvermatch . func ( request , * resolvermatch . args , ** resolvermatch . kwargs ) <EOL> def add_type_view ( self , request , form_url = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not self . has_add_permission ( request ) : <EOL> raise PermissionDenied <EOL> extra_qs = '<STR_LIT>' <EOL> if request . META [ '<STR_LIT>' ] : <EOL> extra_qs = '<STR_LIT:&>' + request . META [ '<STR_LIT>' ] <EOL> choices = self . get_child_type_choices ( request , '<STR_LIT>' ) <EOL> if len ( choices ) == <NUM_LIT:1> : <EOL> return HttpResponseRedirect ( '<STR_LIT>' . format ( choices [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , extra_qs ) ) <EOL> form = self . add_type_form ( <EOL> data = request . POST if request . method == '<STR_LIT:POST>' else None , <EOL> initial = { '<STR_LIT>' : choices [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] } <EOL> ) <EOL> form . fields [ '<STR_LIT>' ] . choices = choices <EOL> if form . is_valid ( ) : <EOL> return HttpResponseRedirect ( '<STR_LIT>' . format ( form . cleaned_data [ '<STR_LIT>' ] , extra_qs ) ) <EOL> fieldsets = ( ( None , { '<STR_LIT>' : ( '<STR_LIT>' , ) } ) , ) <EOL> adminForm = AdminForm ( form , fieldsets , { } , model_admin = self ) <EOL> media = self . media + adminForm . media <EOL> opts = self . model . _meta <EOL> context = { <EOL> '<STR_LIT:title>' : _ ( '<STR_LIT>' ) % force_text ( opts . verbose_name ) , <EOL> '<STR_LIT>' : adminForm , <EOL> '<STR_LIT>' : ( "<STR_LIT>" in request . POST or <EOL> "<STR_LIT>" in request . GET ) , <EOL> '<STR_LIT>' : mark_safe ( media ) , <EOL> '<STR_LIT>' : AdminErrorList ( form , ( ) ) , <EOL> '<STR_LIT>' : opts . app_label , <EOL> } <EOL> return self . render_add_type_form ( request , context , form_url ) <EOL> def render_add_type_form ( self , request , context , form_url = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> opts = self . model . _meta <EOL> app_label = opts . app_label <EOL> context . update ( { <EOL> '<STR_LIT>' : self . has_change_permission ( request ) , <EOL> '<STR_LIT>' : mark_safe ( form_url ) , <EOL> '<STR_LIT>' : opts , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : self . save_on_top , <EOL> } ) <EOL> if hasattr ( self . admin_site , '<STR_LIT>' ) : <EOL> context [ '<STR_LIT>' ] = self . admin_site . root_path <EOL> context_instance = RequestContext ( request , current_app = self . admin_site . name ) <EOL> return render_to_response ( self . add_type_template or [ <EOL> "<STR_LIT>" % ( app_label , opts . object_name . lower ( ) ) , <EOL> "<STR_LIT>" % app_label , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] , context , context_instance = context_instance ) <EOL> @ property <EOL> def change_list_template ( self ) : <EOL> opts = self . model . _meta <EOL> app_label = opts . app_label <EOL> base_opts = self . base_model . _meta <EOL> base_app_label = base_opts . app_label <EOL> return [ <EOL> "<STR_LIT>" % ( app_label , opts . object_name . lower ( ) ) , <EOL> "<STR_LIT>" % app_label , <EOL> "<STR_LIT>" % ( base_app_label , base_opts . object_name . lower ( ) ) , <EOL> "<STR_LIT>" % base_app_label , <EOL> "<STR_LIT>" <EOL> ] <EOL> class PolymorphicChildModelAdmin ( admin . ModelAdmin ) : <EOL> """<STR_LIT>""" <EOL> base_model = None <EOL> base_form = None <EOL> base_fieldsets = None <EOL> extra_fieldset_title = _ ( "<STR_LIT>" ) <EOL> def get_form ( self , request , obj = None , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT>' , self . base_form or self . form ) <EOL> if not getattr ( self , '<STR_LIT>' , None ) : <EOL> kwargs . setdefault ( '<STR_LIT>' , None ) <EOL> return super ( PolymorphicChildModelAdmin , self ) . get_form ( request , obj , ** kwargs ) <EOL> @ property <EOL> def change_form_template ( self ) : <EOL> opts = self . model . _meta <EOL> app_label = opts . app_label <EOL> base_opts = self . base_model . _meta <EOL> base_app_label = base_opts . app_label <EOL> return [ <EOL> "<STR_LIT>" % ( app_label , opts . object_name . lower ( ) ) , <EOL> "<STR_LIT>" % app_label , <EOL> "<STR_LIT>" % ( base_app_label , base_opts . object_name . lower ( ) ) , <EOL> "<STR_LIT>" % base_app_label , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> @ property <EOL> def delete_confirmation_template ( self ) : <EOL> opts = self . model . _meta <EOL> app_label = opts . app_label <EOL> base_opts = self . base_model . _meta <EOL> base_app_label = base_opts . app_label <EOL> return [ <EOL> "<STR_LIT>" % ( app_label , opts . object_name . lower ( ) ) , <EOL> "<STR_LIT>" % app_label , <EOL> "<STR_LIT>" % ( base_app_label , base_opts . object_name . lower ( ) ) , <EOL> "<STR_LIT>" % base_app_label , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> @ property <EOL> def object_history_template ( self ) : <EOL> opts = self . model . _meta <EOL> app_label = opts . app_label <EOL> base_opts = self . base_model . _meta <EOL> base_app_label = base_opts . app_label <EOL> return [ <EOL> "<STR_LIT>" % ( app_label , opts . object_name . lower ( ) ) , <EOL> "<STR_LIT>" % app_label , <EOL> "<STR_LIT>" % ( base_app_label , base_opts . object_name . lower ( ) ) , <EOL> "<STR_LIT>" % base_app_label , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> def render_change_form ( self , request , context , add = False , change = False , form_url = '<STR_LIT>' , obj = None ) : <EOL> context . update ( { <EOL> '<STR_LIT>' : self . base_model . _meta , <EOL> } ) <EOL> return super ( PolymorphicChildModelAdmin , self ) . render_change_form ( request , context , add = add , change = change , form_url = form_url , obj = obj ) <EOL> def delete_view ( self , request , object_id , context = None ) : <EOL> extra_context = { <EOL> '<STR_LIT>' : self . base_model . _meta , <EOL> } <EOL> return super ( PolymorphicChildModelAdmin , self ) . delete_view ( request , object_id , extra_context ) <EOL> def history_view ( self , request , object_id , extra_context = None ) : <EOL> context = { <EOL> '<STR_LIT>' : self . base_model . _meta , <EOL> } <EOL> if extra_context : <EOL> context . update ( extra_context ) <EOL> return super ( PolymorphicChildModelAdmin , self ) . history_view ( request , object_id , extra_context = context ) <EOL> def get_fieldsets ( self , request , obj = None ) : <EOL> if ( hasattr ( self , '<STR_LIT>' ) and self . declared_fieldsets ) or not self . base_fieldsets : <EOL> return super ( PolymorphicChildModelAdmin , self ) . get_fieldsets ( request , obj ) <EOL> other_fields = self . get_subclass_fields ( request , obj ) <EOL> if other_fields : <EOL> return ( <EOL> self . base_fieldsets [ <NUM_LIT:0> ] , <EOL> ( self . extra_fieldset_title , { '<STR_LIT>' : other_fields } ) , <EOL> ) + self . base_fieldsets [ <NUM_LIT:1> : ] <EOL> else : <EOL> return self . base_fieldsets <EOL> def get_subclass_fields ( self , request , obj = None ) : <EOL> exclude = list ( self . exclude or [ ] ) <EOL> exclude . extend ( self . get_readonly_fields ( request , obj ) ) <EOL> form = self . get_form ( request , obj , exclude = exclude ) <EOL> subclass_fields = list ( six . iterkeys ( form . base_fields ) ) + list ( self . get_readonly_fields ( request , obj ) ) <EOL> for fieldset in self . base_fieldsets : <EOL> for field in fieldset [ <NUM_LIT:1> ] [ '<STR_LIT>' ] : <EOL> try : <EOL> subclass_fields . remove ( field ) <EOL> except ValueError : <EOL> pass <EOL> return subclass_fields <EOL> def _get_opt ( model ) : <EOL> try : <EOL> return model . _meta . app_label , model . _meta . model_name <EOL> except AttributeError : <EOL> return model . _meta . app_label , model . _meta . module_name </s>
