<s> import logging <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . auth . models import Permission <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . http import HttpResponseForbidden <EOL> from ralph . admin . sites import ralph_site <EOL> logger = logging . getLogger ( __name__ ) <EOL> _permission_views = [ ] <EOL> def view_permission_dispatch ( func ) : <EOL> """<STR_LIT>""" <EOL> def wraps ( self , request , * args , ** kwargs ) : <EOL> user_model = get_user_model ( ) <EOL> for model in [ kwargs . get ( '<STR_LIT>' ) , user_model ] : <EOL> if not model : <EOL> continue <EOL> perm_name = '<STR_LIT>' . format ( <EOL> model . _meta . app_label , self . permision_codename <EOL> ) <EOL> if request . user . has_perm ( perm_name ) : <EOL> return func ( self , request , * args , ** kwargs ) <EOL> logger . info ( '<STR_LIT>' . format ( <EOL> self . permision_codename , request . user <EOL> ) ) <EOL> return HttpResponseForbidden ( ) <EOL> return wraps <EOL> class PermissionViewMetaClass ( type ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , name , bases , attrs ) : <EOL> codename = '<STR_LIT>' . format ( name . lower ( ) ) <EOL> attrs [ '<STR_LIT>' ] = codename <EOL> new_class = super ( ) . __new__ ( cls , name , bases , attrs ) <EOL> dispatch = getattr ( new_class , '<STR_LIT>' , None ) <EOL> setattr ( new_class , '<STR_LIT>' , view_permission_dispatch ( dispatch ) ) <EOL> _permission_views . append ( ( new_class , codename ) ) <EOL> return new_class <EOL> def update_extra_view_permissions ( ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> logger . info ( '<STR_LIT>' ) <EOL> admin_classes = { } <EOL> for model , admin_class in ralph_site . _registry . items ( ) : <EOL> for change_view in admin_class . change_views : <EOL> admin_classes [ change_view ] = model <EOL> old_permission = Permission . objects . filter ( <EOL> codename__startswith = '<STR_LIT>' <EOL> ) . values_list ( '<STR_LIT:id>' , flat = True ) <EOL> current_permission = [ ] <EOL> for class_view , codename in _permission_views : <EOL> model = admin_classes . get ( class_view , None ) <EOL> if not model : <EOL> model = get_user_model ( ) <EOL> ct = ContentType . objects . get_for_model ( model ) <EOL> perm , _ = Permission . objects . get_or_create ( <EOL> content_type = ct , <EOL> codename = codename , <EOL> defaults = { '<STR_LIT:name>' : '<STR_LIT>' . format ( class_view . __name__ ) } <EOL> ) <EOL> current_permission . append ( perm . id ) <EOL> permission_ids = set ( old_permission ) - set ( current_permission ) <EOL> if permission_ids : <EOL> logger . warning ( <EOL> '<STR_LIT>' . format ( "<STR_LIT:U+002CU+0020>" . join ( <EOL> Permission . objects . filter ( id__in = permission_ids ) . values_list ( <EOL> '<STR_LIT>' , flat = True <EOL> ) <EOL> ) ) <EOL> ) <EOL> Permission . objects . filter ( id__in = permission_ids ) . delete ( ) </s>
