<s> from __future__ import unicode_literals <EOL> import pdb <EOL> from django import __version__ as django_version <EOL> from django . apps import apps <EOL> from django . contrib . auth . models import Permission <EOL> from django . contrib . admin . templatetags . admin_static import static <EOL> from django . contrib . admin . widgets import FilteredSelectMultiple <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . template . loader import get_template <EOL> from django . utils . encoding import force_text <EOL> from django . utils . html import escapejs <EOL> from django . utils . safestring import mark_safe <EOL> from . app_settings import TABULAR_PERMISSIONS_EXCLUDE_FUNCTION , TABULAR_PERMISSIONS_EXCLUDE_APPS , TABULAR_PERMISSIONS_EXCLUDE_MODELS , TABULAR_PERMISSIONS_TEMPLATE , TABULAR_PERMISSIONS_USE_FOR_CONCRETE <EOL> from . helpers import get_perm_name <EOL> class TabularPermissionsWidget ( FilteredSelectMultiple ) : <EOL> class Media : <EOL> js = ( '<STR_LIT>' , ) <EOL> def __init__ ( self , verbose_name , is_stacked , input_name = '<STR_LIT>' , attrs = None , choices = ( ) ) : <EOL> super ( TabularPermissionsWidget , self ) . __init__ ( verbose_name , is_stacked , attrs , choices ) <EOL> self . managed_perms = [ ] <EOL> self . input_name = input_name <EOL> self . hide_original = True <EOL> def render ( self , name , value , attrs = None , choices = ( ) ) : <EOL> apps_available = [ ] <EOL> user_permissions = Permission . objects . filter ( id__in = value or [ ] ) . values_list ( '<STR_LIT:id>' , flat = True ) <EOL> all_perms = Permission . objects . all ( ) . values ( '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' ) . order_by ( '<STR_LIT>' ) <EOL> excluded_perms = set ( [ ] ) <EOL> codename_id_map = { } <EOL> for p in all_perms : <EOL> codename_id_map [ '<STR_LIT>' % ( p [ '<STR_LIT>' ] , p [ '<STR_LIT>' ] ) ] = p [ '<STR_LIT:id>' ] <EOL> reminder_perms = codename_id_map . copy ( ) <EOL> for app in apps . get_app_configs ( ) : <EOL> app_dict = { '<STR_LIT>' : force_text ( app . verbose_name ) , <EOL> '<STR_LIT>' : [ ] } <EOL> for model_name in app . models : <EOL> model = app . models [ model_name ] <EOL> ct_id = ContentType . objects . get_for_model ( model , for_concrete_model = TABULAR_PERMISSIONS_USE_FOR_CONCRETE ) . pk <EOL> add_perm_name = get_perm_name ( model_name , '<STR_LIT>' ) <EOL> change_perm_name = get_perm_name ( model_name , '<STR_LIT>' ) <EOL> delete_perm_name = get_perm_name ( model_name , '<STR_LIT>' ) <EOL> add_perm_id = codename_id_map . get ( '<STR_LIT>' % ( add_perm_name , ct_id ) , False ) <EOL> change_perm_id = codename_id_map . get ( '<STR_LIT>' % ( change_perm_name , ct_id ) , False ) <EOL> delete_perm_id = codename_id_map . get ( '<STR_LIT>' % ( delete_perm_name , ct_id ) , False ) <EOL> if add_perm_id and change_perm_id and delete_perm_id and not { add_perm_id , change_perm_id , <EOL> delete_perm_id } & excluded_perms : <EOL> excluded_perms . update ( [ add_perm_id , change_perm_id , delete_perm_id ] ) <EOL> reminder_perms . pop ( '<STR_LIT>' % ( add_perm_name , ct_id ) ) <EOL> reminder_perms . pop ( '<STR_LIT>' % ( change_perm_name , ct_id ) ) <EOL> reminder_perms . pop ( '<STR_LIT>' % ( delete_perm_name , ct_id ) ) <EOL> if app . label in TABULAR_PERMISSIONS_EXCLUDE_APPS or model_name in TABULAR_PERMISSIONS_EXCLUDE_MODELS or TABULAR_PERMISSIONS_EXCLUDE_FUNCTION ( model ) : <EOL> continue <EOL> app_dict [ '<STR_LIT>' ] . append ( { <EOL> '<STR_LIT>' : model_name , <EOL> '<STR_LIT>' : model , <EOL> '<STR_LIT>' : force_text ( model . _meta . verbose_name_plural ) , <EOL> '<STR_LIT>' : force_text ( model . _meta . verbose_name ) , <EOL> '<STR_LIT>' : add_perm_id , <EOL> '<STR_LIT>' : add_perm_name , <EOL> '<STR_LIT>' : change_perm_id , <EOL> '<STR_LIT>' : change_perm_name , <EOL> '<STR_LIT>' : delete_perm_id , <EOL> '<STR_LIT>' : delete_perm_name , <EOL> } ) <EOL> if app . models : <EOL> apps_available . append ( app_dict ) <EOL> request_context = { '<STR_LIT>' : apps_available , '<STR_LIT>' : user_permissions , <EOL> '<STR_LIT>' : codename_id_map , '<STR_LIT>' : self . input_name } <EOL> body = get_template ( TABULAR_PERMISSIONS_TEMPLATE ) . render ( request_context ) . encode ( "<STR_LIT:utf-8>" ) <EOL> self . managed_perms = excluded_perms <EOL> if reminder_perms : <EOL> self . hide_original = False <EOL> if attrs is None : <EOL> attrs = { } <EOL> attrs [ '<STR_LIT:class>' ] = '<STR_LIT>' <EOL> if self . is_stacked : <EOL> attrs [ '<STR_LIT:class>' ] += '<STR_LIT>' <EOL> output = [ super ( FilteredSelectMultiple , self ) . render ( name , value , attrs , choices ) ] <EOL> if reminder_perms : <EOL> output . append ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in django_version : <EOL> output . append ( '<STR_LIT>' <EOL> % ( name , self . verbose_name . replace ( '<STR_LIT:">' , '<STR_LIT>' ) , int ( self . is_stacked ) , static ( '<STR_LIT>' ) ) ) <EOL> else : <EOL> output . append ( '<STR_LIT>' <EOL> % ( name , escapejs ( self . verbose_name ) , int ( self . is_stacked ) ) ) <EOL> initial = mark_safe ( '<STR_LIT>' . join ( output ) ) <EOL> response = '<STR_LIT>' . join ( [ force_text ( body ) , force_text ( initial ) ] ) <EOL> return mark_safe ( response ) <EOL> def render_option ( self , selected_choices , option_value , option_label ) : <EOL> if option_value in self . managed_perms : <EOL> return '<STR_LIT>' <EOL> self . hide_original = False <EOL> return super ( TabularPermissionsWidget , self ) . render_option ( selected_choices , option_value , option_label ) <EOL> def build_attrs ( self , extra_attrs = None , ** kwargs ) : <EOL> if self . hide_original : <EOL> extra_attrs [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> return super ( TabularPermissionsWidget , self ) . build_attrs ( extra_attrs , ** kwargs ) </s>
