<s> from django import template <EOL> from django . db . models import get_models <EOL> register = template . Library ( ) <EOL> class AdminApplistNode ( template . Node ) : <EOL> def __init__ ( self , varname ) : <EOL> self . varname = varname <EOL> def render ( self , context ) : <EOL> from django . db import models <EOL> from django . utils . text import capfirst <EOL> app_list = [ ] <EOL> user = context [ '<STR_LIT:user>' ] <EOL> for app in models . get_apps ( ) : <EOL> app_models = get_models ( app ) <EOL> if not app_models : <EOL> continue <EOL> app_label = app_models [ <NUM_LIT:0> ] . _meta . app_label <EOL> has_module_perms = user . has_module_perms ( app_label ) <EOL> if has_module_perms : <EOL> model_list = [ ] <EOL> for m in app_models : <EOL> if m . _meta . admin : <EOL> perms = { <EOL> '<STR_LIT>' : user . has_perm ( "<STR_LIT>" % ( app_label , m . _meta . get_add_permission ( ) ) ) , <EOL> '<STR_LIT>' : user . has_perm ( "<STR_LIT>" % ( app_label , m . _meta . get_change_permission ( ) ) ) , <EOL> '<STR_LIT>' : user . has_perm ( "<STR_LIT>" % ( app_label , m . _meta . get_delete_permission ( ) ) ) , <EOL> } <EOL> if True in perms . values ( ) : <EOL> model_list . append ( { <EOL> '<STR_LIT:name>' : capfirst ( m . _meta . verbose_name_plural ) , <EOL> '<STR_LIT>' : '<STR_LIT>' % ( app_label , m . __name__ . lower ( ) ) , <EOL> '<STR_LIT>' : perms , <EOL> } ) <EOL> if model_list : <EOL> decorated = [ ( x [ '<STR_LIT:name>' ] , x ) for x in model_list ] <EOL> decorated . sort ( ) <EOL> model_list = [ x for key , x in decorated ] <EOL> app_list . append ( { <EOL> '<STR_LIT:name>' : app_label . title ( ) , <EOL> '<STR_LIT>' : has_module_perms , <EOL> '<STR_LIT>' : model_list , <EOL> } ) <EOL> context [ self . varname ] = app_list <EOL> return '<STR_LIT>' <EOL> def get_admin_app_list ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> tokens = token . contents . split ( ) <EOL> if len ( tokens ) < <NUM_LIT:3> : <EOL> raise template . TemplateSyntaxError , "<STR_LIT>" % tokens [ <NUM_LIT:0> ] <EOL> if tokens [ <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> raise template . TemplateSyntaxError , "<STR_LIT>" % tokens [ <NUM_LIT:0> ] <EOL> return AdminApplistNode ( tokens [ <NUM_LIT:2> ] ) <EOL> register . tag ( '<STR_LIT>' , get_admin_app_list ) </s>
