<s> from django import forms <EOL> from django . contrib import admin <EOL> from django . contrib . admin . helpers import AdminErrorList , AdminForm <EOL> from django . contrib . admin . widgets import AdminRadioSelect <EOL> from django . contrib . auth . admin import csrf_protect_m <EOL> from django . contrib . sites . models import Site <EOL> from django . core . exceptions import PermissionDenied , ValidationError <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect <EOL> from django . shortcuts import render_to_response <EOL> from django . template . context import RequestContext <EOL> from django . utils . encoding import force_text <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> class SelectSiteForm ( forms . Form ) : <EOL> site = forms . ChoiceField ( <EOL> label = _ ( '<STR_LIT>' ) , widget = AdminRadioSelect ( attrs = { '<STR_LIT:class>' : '<STR_LIT>' } ) ) <EOL> class SettingsAdmin ( admin . ModelAdmin ) : <EOL> list_display = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> select_site_form = SelectSiteForm <EOL> select_site_form_template = '<STR_LIT>' <EOL> def get_model_info ( self ) : <EOL> app_label = self . model . _meta . app_label <EOL> try : <EOL> return app_label , self . model . _meta . model_name <EOL> except AttributeError : <EOL> return app_label , self . model . _meta . module_name <EOL> def has_add_permission ( self , request ) : <EOL> sites = Site . objects . values_list ( '<STR_LIT:id>' , flat = True ) . order_by ( '<STR_LIT:id>' ) <EOL> settings = self . model . objects . values_list ( '<STR_LIT>' , flat = True ) . order_by ( '<STR_LIT:id>' ) <EOL> for site in sites : <EOL> if site not in settings : <EOL> return True <EOL> return False <EOL> def save_model ( self , request , obj , form , change ) : <EOL> if not change and '<STR_LIT>' in request . GET : <EOL> obj . site = Site . objects . get ( pk = request . GET [ '<STR_LIT>' ] ) <EOL> obj . user = request . user <EOL> super ( SettingsAdmin , self ) . save_model ( request , obj , form , change ) <EOL> @ csrf_protect_m <EOL> def add_view ( self , request , form_url = '<STR_LIT>' , extra_context = None ) : <EOL> site_id = request . GET . get ( '<STR_LIT>' , None ) <EOL> if not site_id : <EOL> return self . select_site_view ( request ) <EOL> else : <EOL> try : <EOL> site_id = self . model . _meta . pk . to_python ( site_id ) <EOL> site = Site . objects . get ( pk = site_id ) <EOL> except ( Site . DoesNotExist , ValidationError , ValueError ) : <EOL> return self . select_site_view ( request ) <EOL> else : <EOL> try : <EOL> obj = self . model . objects . get ( site = site ) <EOL> change_url = reverse ( <EOL> '<STR_LIT>' % self . get_model_info ( ) , args = ( obj . pk , ) , <EOL> current_app = self . admin_site . name ) <EOL> msg = _ ( '<STR_LIT>' ) . format ( self . opts . verbose_name , site . domain ) <EOL> self . message_user ( request , msg ) <EOL> return HttpResponseRedirect ( change_url ) <EOL> except self . model . DoesNotExist : <EOL> pass <EOL> return super ( SettingsAdmin , self ) . add_view ( request , form_url , extra_context ) <EOL> def get_site_choices ( self ) : <EOL> site_choices = [ ] <EOL> for site in Site . objects . all ( ) : <EOL> option_label = '<STR_LIT>' . format ( site . domain , site . name ) <EOL> site_choices . append ( ( site . id , option_label ) ) <EOL> return site_choices <EOL> def select_site_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> site_choices = self . get_site_choices ( ) <EOL> if len ( site_choices ) == <NUM_LIT:1> : <EOL> return HttpResponseRedirect ( '<STR_LIT>' . format ( site_choices [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , extra_qs ) ) <EOL> form = self . select_site_form ( <EOL> data = request . POST if request . method == '<STR_LIT:POST>' else None , <EOL> initial = { '<STR_LIT>' : site_choices [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] } <EOL> ) <EOL> form . fields [ '<STR_LIT>' ] . choices = site_choices <EOL> if form . is_valid ( ) : <EOL> return HttpResponseRedirect ( <EOL> '<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> context = { <EOL> '<STR_LIT:title>' : _ ( '<STR_LIT>' ) % force_text ( self . opts . verbose_name ) , <EOL> '<STR_LIT>' : adminForm , <EOL> '<STR_LIT>' : '<STR_LIT>' in request . REQUEST , <EOL> '<STR_LIT>' : mark_safe ( media ) , <EOL> '<STR_LIT>' : AdminErrorList ( form , ( ) ) , <EOL> '<STR_LIT>' : self . opts . app_label , <EOL> } <EOL> return self . render_select_site_form ( request , context , form_url ) <EOL> def render_select_site_form ( self , request , context , form_url = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> app_label = self . opts . app_label <EOL> context . update ( { <EOL> '<STR_LIT>' : self . has_change_permission ( request ) , <EOL> '<STR_LIT>' : mark_safe ( form_url ) , <EOL> '<STR_LIT>' : self . opts , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : self . save_on_top , <EOL> } ) <EOL> context_instance = RequestContext ( request , current_app = self . admin_site . name ) <EOL> return render_to_response ( self . select_site_form_template or [ <EOL> '<STR_LIT>' % ( app_label , self . opts . object_name . lower ( ) ) , <EOL> '<STR_LIT>' % app_label , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] , context , context_instance = context_instance ) <EOL> @ csrf_protect_m <EOL> def changelist_view ( self , request , extra_context = None ) : <EOL> sites_count = Site . objects . count ( ) <EOL> if sites_count == <NUM_LIT:1> : <EOL> site = Site . objects . get ( ) <EOL> try : <EOL> obj = self . model . objects . get ( site = site ) <EOL> change_url = reverse ( <EOL> '<STR_LIT>' % self . get_model_info ( ) , args = ( obj . pk , ) , <EOL> current_app = self . admin_site . name ) <EOL> return HttpResponseRedirect ( change_url ) <EOL> except self . model . DoesNotExist : <EOL> add_url = '<STR_LIT>' % ( <EOL> reverse ( '<STR_LIT>' % self . get_model_info ( ) , <EOL> current_app = self . admin_site . name ) , site . pk ) <EOL> return HttpResponseRedirect ( add_url ) <EOL> else : <EOL> return super ( SettingsAdmin , self ) . changelist_view ( request , extra_context ) </s>
