<s> from __future__ import division , absolute_import , unicode_literals <EOL> from collections import namedtuple <EOL> import logging <EOL> import os <EOL> import sys <EOL> from django . core . urlresolvers import reverse <EOL> from django . conf . urls import patterns , url <EOL> from django . utils . six import with_metaclass <EOL> import extra_views <EOL> from . import apiviews <EOL> from . import settings <EOL> from . import views <EOL> from . import actions <EOL> from . import utils <EOL> from . forms import modelform_factory <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class ModelAdminBase2 ( type ) : <EOL> def __new__ ( cls , name , bases , attrs ) : <EOL> new_class = super ( ModelAdminBase2 , cls ) . __new__ ( cls , name , <EOL> bases , attrs ) <EOL> view_list = [ ] <EOL> for key , value in attrs . items ( ) : <EOL> if isinstance ( value , views . AdminView ) : <EOL> if not value . name : <EOL> value . name = key <EOL> view_list . append ( value ) <EOL> view_list . extend ( getattr ( new_class , '<STR_LIT>' , [ ] ) ) <EOL> new_class . views = view_list <EOL> return new_class <EOL> class ModelAdmin2 ( with_metaclass ( ModelAdminBase2 ) ) : <EOL> """<STR_LIT>""" <EOL> actions_selection_counter = True <EOL> date_hierarchy = False <EOL> list_display = ( '<STR_LIT>' , ) <EOL> list_display_links = ( ) <EOL> list_filter = ( ) <EOL> list_select_related = False <EOL> list_per_page = <NUM_LIT:100> <EOL> list_max_show_all = <NUM_LIT:200> <EOL> list_editable = ( ) <EOL> search_fields = ( ) <EOL> save_as = False <EOL> save_on_top = False <EOL> verbose_name = None <EOL> verbose_name_plural = None <EOL> model_admin_attributes = settings . MODEL_ADMIN_ATTRS <EOL> ordering = False <EOL> save_on_top = False <EOL> save_on_bottom = True <EOL> actions_on_bottom = False <EOL> actions_on_top = True <EOL> search_fields = [ ] <EOL> list_fields = [ ] <EOL> list_actions = [ actions . DeleteSelectedAction ] <EOL> document_actions = [ ] <EOL> field_actions = { } <EOL> field_renderers = { } <EOL> fields = None <EOL> exclude = None <EOL> fieldsets = None <EOL> form_class = None <EOL> filter_vertical = ( ) <EOL> filter_horizontal = ( ) <EOL> radio_fields = { } <EOL> prepopulated_fields = { } <EOL> formfield_overrides = { } <EOL> readonly_fields = ( ) <EOL> ordering = None <EOL> create_form_class = None <EOL> update_form_class = None <EOL> inlines = [ ] <EOL> index_view = views . AdminView ( r'<STR_LIT>' , views . ModelListView , name = '<STR_LIT:index>' ) <EOL> create_view = views . AdminView ( r'<STR_LIT>' , views . ModelAddFormView , name = '<STR_LIT>' ) <EOL> update_view = views . AdminView ( r'<STR_LIT>' , views . ModelEditFormView , name = '<STR_LIT>' ) <EOL> detail_view = views . AdminView ( r'<STR_LIT>' , views . ModelDetailView , name = '<STR_LIT>' ) <EOL> delete_view = views . AdminView ( r'<STR_LIT>' , views . ModelDeleteView , name = '<STR_LIT>' ) <EOL> history_view = views . AdminView ( r'<STR_LIT>' , views . ModelHistoryView , name = '<STR_LIT>' ) <EOL> views = [ ] <EOL> api_serializer_class = None <EOL> api_list_view = apiviews . ListCreateAPIView <EOL> api_detail_view = apiviews . RetrieveUpdateDestroyAPIView <EOL> def __init__ ( self , model , admin , name = None , ** kwargs ) : <EOL> self . name = name <EOL> self . model = model <EOL> self . admin = admin <EOL> model_options = utils . model_options ( model ) <EOL> self . app_label = model_options . app_label <EOL> self . model_name = model_options . object_name . lower ( ) <EOL> if self . name is None : <EOL> self . name = '<STR_LIT>' . format ( self . app_label , self . model_name ) <EOL> if self . verbose_name is None : <EOL> self . verbose_name = model_options . verbose_name <EOL> if self . verbose_name_plural is None : <EOL> self . verbose_name_plural = model_options . verbose_name_plural <EOL> def get_default_view_kwargs ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . app_label , <EOL> '<STR_LIT>' : self . model , <EOL> '<STR_LIT>' : self . model_name , <EOL> '<STR_LIT>' : immutable_admin_factory ( self ) , <EOL> } <EOL> def get_index_kwargs ( self ) : <EOL> kwargs = self . get_default_view_kwargs ( ) <EOL> kwargs . update ( { <EOL> '<STR_LIT>' : self . list_per_page , <EOL> } ) <EOL> return kwargs <EOL> def get_default_api_view_kwargs ( self ) : <EOL> kwargs = self . get_default_view_kwargs ( ) <EOL> kwargs . update ( { <EOL> '<STR_LIT>' : self . api_serializer_class , <EOL> } ) <EOL> return kwargs <EOL> def get_prefixed_view_name ( self , view_name ) : <EOL> return '<STR_LIT>' . format ( self . name , view_name ) <EOL> def get_create_kwargs ( self ) : <EOL> kwargs = self . get_default_view_kwargs ( ) <EOL> kwargs . update ( { <EOL> '<STR_LIT>' : self . inlines , <EOL> '<STR_LIT>' : ( self . create_form_class if <EOL> self . create_form_class else self . form_class ) , <EOL> } ) <EOL> return kwargs <EOL> def get_update_kwargs ( self ) : <EOL> kwargs = self . get_default_view_kwargs ( ) <EOL> form_class = ( self . update_form_class if <EOL> self . update_form_class else self . form_class ) <EOL> if form_class is None : <EOL> form_class = modelform_factory ( self . model , fields = '<STR_LIT>' ) <EOL> kwargs . update ( { <EOL> '<STR_LIT>' : self . inlines , <EOL> '<STR_LIT>' : form_class , <EOL> } ) <EOL> return kwargs <EOL> def get_index_url ( self ) : <EOL> return reverse ( '<STR_LIT>' . format ( <EOL> self . get_prefixed_view_name ( '<STR_LIT:index>' ) ) ) <EOL> def get_api_list_kwargs ( self ) : <EOL> kwargs = self . get_default_api_view_kwargs ( ) <EOL> kwargs . update ( { <EOL> '<STR_LIT>' : self . list_per_page , <EOL> } ) <EOL> return kwargs <EOL> def get_api_detail_kwargs ( self ) : <EOL> return self . get_default_api_view_kwargs ( ) <EOL> def get_urls ( self ) : <EOL> pattern_list = [ ] <EOL> for admin_view in self . views : <EOL> admin_view . model_admin = self <EOL> get_kwargs = getattr ( self , "<STR_LIT>" % admin_view . name , None ) <EOL> if not get_kwargs : <EOL> get_kwargs = admin_view . get_view_kwargs <EOL> try : <EOL> view_instance = admin_view . view . as_view ( ** get_kwargs ( ) ) <EOL> except Exception as e : <EOL> trace = sys . exc_info ( ) [ <NUM_LIT:2> ] <EOL> new_exception = TypeError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> self . __class__ . __name__ , admin_view . name , e ) ) <EOL> try : <EOL> raise new_exception . with_traceback ( trace ) <EOL> except AttributeError : <EOL> raise ( new_exception , None , trace ) <EOL> pattern_list . append ( <EOL> url ( <EOL> regex = admin_view . url , <EOL> view = view_instance , <EOL> name = self . get_prefixed_view_name ( admin_view . name ) <EOL> ) <EOL> ) <EOL> return patterns ( '<STR_LIT>' , * pattern_list ) <EOL> def get_api_urls ( self ) : <EOL> return patterns ( <EOL> '<STR_LIT>' , <EOL> url ( <EOL> regex = r'<STR_LIT>' , <EOL> view = self . api_list_view . as_view ( ** self . get_api_list_kwargs ( ) ) , <EOL> name = self . get_prefixed_view_name ( '<STR_LIT>' ) , <EOL> ) , <EOL> url ( <EOL> regex = r'<STR_LIT>' , <EOL> view = self . api_detail_view . as_view ( <EOL> ** self . get_api_detail_kwargs ( ) ) , <EOL> name = self . get_prefixed_view_name ( '<STR_LIT>' ) , <EOL> ) , <EOL> ) <EOL> @ property <EOL> def urls ( self ) : <EOL> return self . get_urls ( ) , None , None <EOL> @ property <EOL> def api_urls ( self ) : <EOL> return self . get_api_urls ( ) , None , None <EOL> def get_list_actions ( self ) : <EOL> actions_dict = { } <EOL> for cls in type ( self ) . mro ( ) [ : : - <NUM_LIT:1> ] : <EOL> class_actions = getattr ( cls , '<STR_LIT>' , [ ] ) <EOL> for action in class_actions : <EOL> actions_dict [ action . __name__ ] = { <EOL> '<STR_LIT:name>' : action . __name__ , <EOL> '<STR_LIT:description>' : actions . get_description ( action ) , <EOL> '<STR_LIT>' : action <EOL> } <EOL> return actions_dict <EOL> def get_ordering ( self , request ) : <EOL> return self . ordering <EOL> class Admin2Inline ( extra_views . InlineFormSet ) : <EOL> """<STR_LIT>""" <EOL> template = None <EOL> def construct_formset ( self ) : <EOL> """<STR_LIT>""" <EOL> formset = super ( Admin2Inline , self ) . construct_formset ( ) <EOL> formset . model = self . inline_model <EOL> formset . template = self . template <EOL> return formset <EOL> class Admin2TabularInline ( Admin2Inline ) : <EOL> template = os . path . join ( <EOL> settings . ADMIN2_THEME_DIRECTORY , '<STR_LIT>' ) <EOL> class Admin2StackedInline ( Admin2Inline ) : <EOL> template = os . path . join ( <EOL> settings . ADMIN2_THEME_DIRECTORY , '<STR_LIT>' ) <EOL> def immutable_admin_factory ( model_admin ) : <EOL> """<STR_LIT>""" <EOL> ImmutableAdmin = namedtuple ( '<STR_LIT>' , <EOL> model_admin . model_admin_attributes , <EOL> verbose = False ) <EOL> return ImmutableAdmin ( * [ getattr ( <EOL> model_admin , x ) for x in model_admin . model_admin_attributes ] ) </s>
