<s> """<STR_LIT>""" <EOL> from fnmatch import fnmatch <EOL> from django . conf import settings <EOL> from django . contrib import admin <EOL> from django . core . urlresolvers import reverse <EOL> try : <EOL> from importlib import import_module <EOL> except ImportError : <EOL> from django . utils . importlib import import_module <EOL> import warnings <EOL> def uniquify ( value , seen_values ) : <EOL> """<STR_LIT>""" <EOL> id = <NUM_LIT:1> <EOL> new_value = value <EOL> while new_value in seen_values : <EOL> new_value = "<STR_LIT>" % ( value , id ) <EOL> id += <NUM_LIT:1> <EOL> seen_values . add ( new_value ) <EOL> return new_value <EOL> def get_admin_site ( context = None , request = None ) : <EOL> dashboard_cls = getattr ( <EOL> settings , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> if isinstance ( dashboard_cls , dict ) : <EOL> if context : <EOL> request = context . get ( '<STR_LIT>' ) <EOL> curr_url = request . path <EOL> for key in dashboard_cls : <EOL> mod , inst = key . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> mod = import_module ( mod ) <EOL> admin_site = getattr ( mod , inst ) <EOL> admin_url = reverse ( '<STR_LIT>' % admin_site . name ) <EOL> if curr_url . startswith ( admin_url ) : <EOL> return admin_site <EOL> else : <EOL> return admin . site <EOL> raise ValueError ( '<STR_LIT>' % dashboard_cls ) <EOL> def get_admin_site_name ( context ) : <EOL> return get_admin_site ( context ) . name <EOL> def get_avail_models ( request ) : <EOL> """<STR_LIT>""" <EOL> items = [ ] <EOL> admin_site = get_admin_site ( request = request ) <EOL> for model , model_admin in admin_site . _registry . items ( ) : <EOL> perms = model_admin . get_model_perms ( request ) <EOL> if True not in perms . values ( ) : <EOL> continue <EOL> items . append ( ( model , perms , ) ) <EOL> return items <EOL> def filter_models ( request , models , exclude ) : <EOL> """<STR_LIT>""" <EOL> items = get_avail_models ( request ) <EOL> included = [ ] <EOL> def full_name ( model ) : <EOL> return '<STR_LIT>' % ( model . __module__ , model . __name__ ) <EOL> if len ( models ) == <NUM_LIT:0> : <EOL> included = items <EOL> else : <EOL> for pattern in models : <EOL> wildcard_models = [ ] <EOL> for item in items : <EOL> model , perms = item <EOL> model_str = full_name ( model ) <EOL> if model_str == pattern : <EOL> included . append ( item ) <EOL> elif fnmatch ( model_str , pattern ) and item not in wildcard_models : <EOL> wildcard_models . append ( item ) <EOL> if wildcard_models : <EOL> wildcard_models . sort ( <EOL> key = lambda x : x [ <NUM_LIT:0> ] . _meta . verbose_name_plural <EOL> ) <EOL> included += wildcard_models <EOL> result = included [ : ] <EOL> for pattern in exclude : <EOL> for item in included : <EOL> model , perms = item <EOL> if fnmatch ( full_name ( model ) , pattern ) : <EOL> try : <EOL> result . remove ( item ) <EOL> except ValueError : <EOL> pass <EOL> return result <EOL> class AppListElementMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def _visible_models ( self , request ) : <EOL> if self . include_list : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning <EOL> ) <EOL> if self . exclude_list : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning <EOL> ) <EOL> included = self . models [ : ] <EOL> included . extend ( [ elem + "<STR_LIT:*>" for elem in self . include_list ] ) <EOL> excluded = self . exclude [ : ] <EOL> excluded . extend ( [ elem + "<STR_LIT:*>" for elem in self . exclude_list ] ) <EOL> if self . exclude_list and not included : <EOL> included = [ "<STR_LIT:*>" ] <EOL> return filter_models ( request , included , excluded ) <EOL> def _get_admin_app_list_url ( self , model , context ) : <EOL> """<STR_LIT>""" <EOL> app_label = model . _meta . app_label <EOL> return reverse ( '<STR_LIT>' % get_admin_site_name ( context ) , <EOL> args = ( app_label , ) ) <EOL> def _get_admin_change_url ( self , model , context ) : <EOL> """<STR_LIT>""" <EOL> app_label = model . _meta . app_label <EOL> return reverse ( '<STR_LIT>' % ( get_admin_site_name ( context ) , <EOL> app_label , <EOL> model . __name__ . lower ( ) ) ) <EOL> def _get_admin_add_url ( self , model , context ) : <EOL> """<STR_LIT>""" <EOL> app_label = model . _meta . app_label <EOL> return reverse ( '<STR_LIT>' % ( get_admin_site_name ( context ) , <EOL> app_label , <EOL> model . __name__ . lower ( ) ) ) </s>
