<s> from copy import deepcopy <EOL> from django . contrib import admin <EOL> from django . contrib . admin import site <EOL> from django . contrib . auth import get_user_model , get_permission_codename <EOL> from django . contrib . auth . admin import UserAdmin <EOL> from django . db import OperationalError <EOL> from django . utils . translation import ugettext , ugettext_lazy as _ <EOL> from cms . admin . forms import GlobalPagePermissionAdminForm , PagePermissionInlineAdminForm , ViewRestrictionInlineAdminForm <EOL> from cms . exceptions import NoPermissionsException <EOL> from cms . models import Page , PagePermission , GlobalPagePermission , PageUser <EOL> from cms . utils . conf import get_cms_setting <EOL> from cms . utils . helpers import classproperty <EOL> from cms . utils . permissions import get_user_permission_level <EOL> PERMISSION_ADMIN_INLINES = [ ] <EOL> user_model = get_user_model ( ) <EOL> admin_class = UserAdmin <EOL> for model , admin_instance in site . _registry . items ( ) : <EOL> if model == user_model : <EOL> admin_class = admin_instance . __class__ <EOL> class TabularInline ( admin . TabularInline ) : <EOL> pass <EOL> class PagePermissionInlineAdmin ( TabularInline ) : <EOL> model = PagePermission <EOL> form = PagePermissionInlineAdminForm <EOL> classes = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> exclude = [ '<STR_LIT>' ] <EOL> extra = <NUM_LIT:0> <EOL> @ classproperty <EOL> def raw_id_fields ( cls ) : <EOL> threshold = get_cms_setting ( '<STR_LIT>' ) <EOL> try : <EOL> threshold = threshold and get_user_model ( ) . objects . count ( ) > threshold <EOL> except OperationalError : <EOL> threshold = False <EOL> return [ '<STR_LIT:user>' ] if threshold else [ ] <EOL> def get_queryset ( self , request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> qs = self . model . objects . subordinate_to_user ( request . user ) <EOL> return qs . filter ( can_view = False ) <EOL> except NoPermissionsException : <EOL> return self . objects . get_empty_query_set ( ) <EOL> def get_formset ( self , request , obj = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> exclude = self . exclude or [ ] <EOL> if obj : <EOL> if not obj . has_add_permission ( request ) : <EOL> exclude . append ( '<STR_LIT>' ) <EOL> if not obj . has_delete_permission ( request ) : <EOL> exclude . append ( '<STR_LIT>' ) <EOL> if not obj . has_publish_permission ( request ) : <EOL> exclude . append ( '<STR_LIT>' ) <EOL> if not obj . has_advanced_settings_permission ( request ) : <EOL> exclude . append ( '<STR_LIT>' ) <EOL> if not obj . has_move_page_permission ( request ) : <EOL> exclude . append ( '<STR_LIT>' ) <EOL> formset_cls = super ( PagePermissionInlineAdmin , self <EOL> ) . get_formset ( request , obj = None , exclude = exclude , ** kwargs ) <EOL> qs = self . get_queryset ( request ) <EOL> if obj is not None : <EOL> qs = qs . filter ( page = obj ) <EOL> formset_cls . _queryset = qs <EOL> return formset_cls <EOL> class ViewRestrictionInlineAdmin ( PagePermissionInlineAdmin ) : <EOL> extra = <NUM_LIT:0> <EOL> form = ViewRestrictionInlineAdminForm <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> exclude = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> def get_formset ( self , request , obj = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> formset_cls = super ( PagePermissionInlineAdmin , self ) . get_formset ( request , obj , ** kwargs ) <EOL> qs = self . get_queryset ( request ) <EOL> if obj is not None : <EOL> qs = qs . filter ( page = obj ) <EOL> formset_cls . _queryset = qs <EOL> return formset_cls <EOL> def get_queryset ( self , request ) : <EOL> """<STR_LIT>""" <EOL> qs = self . model . objects . subordinate_to_user ( request . user ) <EOL> return qs . filter ( can_view = True ) <EOL> class GlobalPagePermissionAdmin ( admin . ModelAdmin ) : <EOL> list_display = [ '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> list_filter = [ '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> form = GlobalPagePermissionAdminForm <EOL> search_fields = [ ] <EOL> for field in admin_class . search_fields : <EOL> search_fields . append ( "<STR_LIT>" % field ) <EOL> search_fields . append ( '<STR_LIT>' ) <EOL> exclude = [ ] <EOL> list_display . append ( '<STR_LIT>' ) <EOL> list_filter . append ( '<STR_LIT>' ) <EOL> def get_list_filter ( self , request ) : <EOL> threshold = get_cms_setting ( '<STR_LIT>' ) <EOL> try : <EOL> threshold = threshold and get_user_model ( ) . objects . count ( ) > threshold <EOL> except OperationalError : <EOL> threshold = False <EOL> filter_copy = deepcopy ( self . list_filter ) <EOL> if threshold : <EOL> filter_copy . remove ( '<STR_LIT:user>' ) <EOL> return filter_copy <EOL> @ classproperty <EOL> def raw_id_fields ( cls ) : <EOL> threshold = get_cms_setting ( '<STR_LIT>' ) <EOL> try : <EOL> threshold = threshold and get_user_model ( ) . objects . count ( ) > threshold <EOL> except OperationalError : <EOL> threshold = False <EOL> return [ '<STR_LIT:user>' ] if threshold else [ ] <EOL> class GenericCmsPermissionAdmin ( object ) : <EOL> """<STR_LIT>""" <EOL> def update_permission_fieldsets ( self , request , obj = None ) : <EOL> """<STR_LIT>""" <EOL> fieldsets = deepcopy ( self . fieldsets ) <EOL> perm_models = ( <EOL> ( Page , ugettext ( '<STR_LIT>' ) ) , <EOL> ( PageUser , ugettext ( '<STR_LIT>' ) ) , <EOL> ( PagePermission , ugettext ( '<STR_LIT>' ) ) , <EOL> ) <EOL> for i , perm_model in enumerate ( perm_models ) : <EOL> model , title = perm_model <EOL> opts , fields = model . _meta , [ ] <EOL> name = model . __name__ . lower ( ) <EOL> for key in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> perm_code = '<STR_LIT>' % ( opts . app_label , get_permission_codename ( key , opts ) ) <EOL> if request . user . has_perm ( perm_code ) : <EOL> fields . append ( '<STR_LIT>' % ( key , name ) ) <EOL> if fields : <EOL> fieldsets . insert ( <NUM_LIT:2> + i , ( title , { '<STR_LIT>' : ( fields , ) } ) ) <EOL> return fieldsets <EOL> def _has_change_permissions_permission ( self , request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> get_user_permission_level ( request . user ) <EOL> except NoPermissionsException : <EOL> return False <EOL> return True <EOL> def has_add_permission ( self , request ) : <EOL> return self . _has_change_permissions_permission ( request ) and super ( self . __class__ , self ) . has_add_permission ( request ) <EOL> def has_change_permission ( self , request , obj = None ) : <EOL> return self . _has_change_permissions_permission ( request ) and super ( self . __class__ , self ) . has_change_permission ( request , obj ) <EOL> if get_cms_setting ( '<STR_LIT>' ) : <EOL> admin . site . register ( GlobalPagePermission , GlobalPagePermissionAdmin ) <EOL> PERMISSION_ADMIN_INLINES . extend ( [ <EOL> ViewRestrictionInlineAdmin , <EOL> PagePermissionInlineAdmin , <EOL> ] ) </s>
