<s> import warnings <EOL> from django . contrib import admin <EOL> from django . db . models import Q <EOL> from django . contrib . sites . models import Site <EOL> from django . conf import settings <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . contrib . admin . widgets import FilteredSelectMultiple <EOL> from opps . channels . models import Channel <EOL> from opps . core . models import Channeling , Publisher <EOL> from . models import Permission , PermissionGroup <EOL> FALLBACK_ON_CHANNEL = getattr ( settings , '<STR_LIT>' , <EOL> False ) <EOL> class AdminViewPermission ( admin . ModelAdmin ) : <EOL> _site_master = None <EOL> def _get_site_master ( self ) : <EOL> if self . _site_master : <EOL> return self . _site_master <EOL> self . _site_master = Site . objects . order_by ( '<STR_LIT:id>' ) [ <NUM_LIT:0> ] <EOL> return self . _site_master <EOL> @ property <EOL> def site_lookup ( self ) : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> return '<STR_LIT>' <EOL> def formfield_for_foreignkey ( self , db_field , request , ** kwargs ) : <EOL> if settings . OPPS_MULTISITE_ADMIN and not request . user . is_superuser : <EOL> obj = Permission . get_by_user ( request . user ) <EOL> if db_field . name in [ '<STR_LIT>' ] : <EOL> kwargs [ '<STR_LIT>' ] = Site . objects . filter ( <EOL> id__in = obj [ '<STR_LIT>' ] <EOL> ) <EOL> if db_field . name in [ '<STR_LIT>' ] : <EOL> if FALLBACK_ON_CHANNEL : <EOL> obj [ '<STR_LIT>' ] . add ( self . _get_site_master ( ) . pk ) <EOL> kwargs [ '<STR_LIT>' ] = Channel . objects . filter ( <EOL> Q ( id__in = obj [ '<STR_LIT>' ] ) | <EOL> Q ( site_id__in = obj [ '<STR_LIT>' ] ) <EOL> ) <EOL> return super ( AdminViewPermission , self ) . formfield_for_foreignkey ( <EOL> db_field , request , ** kwargs ) <EOL> def queryset ( self , request ) : <EOL> qs = super ( AdminViewPermission , self ) . queryset ( request ) <EOL> if not settings . OPPS_MULTISITE_ADMIN or request . user . is_superuser : <EOL> return qs <EOL> obj = Permission . get_by_user ( request . user ) <EOL> if self . __class__ . __name__ == '<STR_LIT>' : <EOL> if FALLBACK_ON_CHANNEL : <EOL> obj [ '<STR_LIT>' ] . add ( self . _get_site_master ( ) . pk ) <EOL> return qs . filter ( <EOL> Q ( site_id__in = obj [ '<STR_LIT>' ] ) | <EOL> Q ( id__in = obj [ '<STR_LIT>' ] ) <EOL> ) <EOL> filters = Q ( ) <EOL> if issubclass ( qs . model , Publisher ) : <EOL> filters |= Q ( site_iid__in = obj [ '<STR_LIT>' ] ) <EOL> if issubclass ( qs . model , Channeling ) : <EOL> filters |= Q ( channel_id__in = obj [ '<STR_LIT>' ] ) <EOL> return qs . filter ( filters ) <EOL> def get_form ( self , request , obj = None , ** kwargs ) : <EOL> form = super ( AdminViewPermission , self ) . get_form ( <EOL> request , obj , ** kwargs ) <EOL> obj = Permission . get_by_user ( request . user ) <EOL> try : <EOL> attrs_mirror = { } <EOL> if settings . OPPS_MULTISITE_ADMIN and not request . user . is_superuser : <EOL> qs_mirror = form . base_fields [ '<STR_LIT>' ] . queryset <EOL> form . base_fields [ '<STR_LIT>' ] . queryset = qs_mirror . filter ( <EOL> id__in = obj [ '<STR_LIT>' ] <EOL> ) <EOL> attrs_mirror = { '<STR_LIT>' : '<STR_LIT>' } <EOL> form . base_fields [ '<STR_LIT>' ] . widget = FilteredSelectMultiple ( <EOL> _ ( "<STR_LIT>" ) , is_stacked = False , attrs = attrs_mirror ) <EOL> except : <EOL> pass <EOL> try : <EOL> if settings . OPPS_MULTISITE_ADMIN and not request . user . is_superuser : <EOL> if FALLBACK_ON_CHANNEL : <EOL> obj [ '<STR_LIT>' ] . add ( self . _get_site_master ( ) . pk ) <EOL> qs_channel = form . base_fields [ '<STR_LIT>' ] . queryset <EOL> form . base_fields [ '<STR_LIT>' ] . queryset = qs_channel . filter ( <EOL> Q ( site_id__in = obj [ '<STR_LIT>' ] ) | <EOL> Q ( id__in = obj [ '<STR_LIT>' ] ) <EOL> ) <EOL> except : <EOL> pass <EOL> return form <EOL> class PermissionAdmin ( admin . ModelAdmin ) : <EOL> fields = ( '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> raw_id_fields = ( '<STR_LIT:user>' , ) <EOL> filter_horizontal = ( '<STR_LIT>' , ) <EOL> admin . site . register ( Permission , PermissionAdmin ) <EOL> class PermissionGroupAdmin ( admin . ModelAdmin ) : <EOL> fields = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> raw_id_fields = ( '<STR_LIT>' , ) <EOL> filter_horizontal = ( '<STR_LIT>' , ) <EOL> admin . site . register ( PermissionGroup , PermissionGroupAdmin ) </s>
