<s> from collections import defaultdict <EOL> from django . conf import settings <EOL> from django . conf . urls import url <EOL> from django . contrib . admin . sites import AdminSite <EOL> class RalphAdminSiteMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> site_header = settings . ADMIN_SITE_HEADER <EOL> site_title = settings . ADMIN_SITE_TITLE <EOL> index_template = '<STR_LIT>' <EOL> app_index_template = '<STR_LIT>' <EOL> object_history_template = '<STR_LIT>' <EOL> def _get_views ( self , admin ) : <EOL> return ( admin . change_views or [ ] ) + ( admin . list_views or [ ] ) <EOL> def register ( self , model_or_iterable , * args , ** kwargs ) : <EOL> super ( ) . register ( model_or_iterable , * args , ** kwargs ) <EOL> for model in model_or_iterable : <EOL> if model . _meta . swapped : <EOL> continue <EOL> admin_instance = self . _registry [ model ] <EOL> for view in self . _get_views ( admin_instance ) : <EOL> view . post_register ( self . name , model ) <EOL> def get_urls ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> urlpatterns = super ( RalphAdminSiteMixin , self ) . get_urls ( <EOL> * args , ** kwargs <EOL> ) <EOL> for model , model_admin in self . _registry . items ( ) : <EOL> for view in self . _get_views ( model_admin ) : <EOL> urlpatterns . insert ( <NUM_LIT:0> , url ( <EOL> view . get_url_pattern ( model ) , <EOL> view . as_view ( ) , <EOL> { <EOL> '<STR_LIT>' : model , <EOL> '<STR_LIT>' : getattr ( <EOL> model_admin , '<STR_LIT>' . format ( view . _type ) , [ ] <EOL> ) , <EOL> } , <EOL> name = view . url_to_reverse <EOL> ) ) <EOL> return urlpatterns <EOL> def get_extra_view_menu_items ( self ) : <EOL> """<STR_LIT>""" <EOL> items = defaultdict ( list ) <EOL> def get_item ( model , view , change_view = False ) : <EOL> url = view . url_with_namespace <EOL> if change_view : <EOL> url += '<STR_LIT>' <EOL> return { '<STR_LIT:title>' : view . label , '<STR_LIT:url>' : url } <EOL> for model , model_admin in self . _registry . items ( ) : <EOL> name = '<STR_LIT>' . format ( <EOL> model . _meta . app_label , model . _meta . model_name <EOL> ) <EOL> for view in model_admin . list_views or [ ] : <EOL> items [ name ] . append ( get_item ( model , view ) ) <EOL> for view in model_admin . change_views or [ ] : <EOL> items [ name ] . append ( get_item ( model , view , True ) ) <EOL> return items <EOL> def index ( self , request , extra_context = None ) : <EOL> from ralph . data_center . models import DataCenter <EOL> if extra_context is None : <EOL> extra_context = { } <EOL> extra_context [ '<STR_LIT>' ] = DataCenter . objects . filter ( <EOL> show_on_dashboard = True <EOL> ) <EOL> return super ( ) . index ( request , extra_context ) <EOL> class RalphAdminSite ( RalphAdminSiteMixin , AdminSite ) : <EOL> pass <EOL> ralph_site = RalphAdminSite ( ) </s>
