<s> from django . core . exceptions import PermissionDenied <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from . models import Organization , OrganizationUser <EOL> class OrganizationMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> org_model = Organization <EOL> org_context_name = '<STR_LIT>' <EOL> def get_org_model ( self ) : <EOL> return self . org_model <EOL> def get_context_data ( self , ** kwargs ) : <EOL> kwargs . update ( { self . org_context_name : self . get_organization ( ) } ) <EOL> return super ( OrganizationMixin , self ) . get_context_data ( ** kwargs ) <EOL> def get_object ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . organization <EOL> organization_pk = self . kwargs . get ( '<STR_LIT>' , None ) <EOL> self . organization = get_object_or_404 ( self . get_org_model ( ) , pk = organization_pk ) <EOL> return self . organization <EOL> get_organization = get_object <EOL> class OrganizationUserMixin ( OrganizationMixin ) : <EOL> """<STR_LIT>""" <EOL> user_model = OrganizationUser <EOL> org_user_context_name = '<STR_LIT>' <EOL> def get_user_model ( self ) : <EOL> return self . user_model <EOL> def get_context_data ( self , ** kwargs ) : <EOL> kwargs = super ( OrganizationUserMixin , self ) . get_context_data ( ** kwargs ) <EOL> kwargs . update ( { self . org_user_context_name : self . object , <EOL> self . org_context_name : self . object . organization } ) <EOL> return kwargs <EOL> def get_object ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . organization_user <EOL> organization_pk = self . kwargs . get ( '<STR_LIT>' , None ) <EOL> user_pk = self . kwargs . get ( '<STR_LIT>' , None ) <EOL> self . organization_user = get_object_or_404 ( <EOL> OrganizationUser . objects . select_related ( ) , <EOL> user__pk = user_pk , organization__pk = organization_pk ) <EOL> return self . organization_user <EOL> class MembershipRequiredMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> self . request = request <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> self . organization = self . get_organization ( ) <EOL> if not self . organization . is_member ( request . user ) and not request . user . is_superuser : <EOL> raise PermissionDenied ( _ ( "<STR_LIT>" ) ) <EOL> return super ( MembershipRequiredMixin , self ) . dispatch ( request , * args , <EOL> ** kwargs ) <EOL> class AdminRequiredMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> self . request = request <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> self . organization = self . get_organization ( ) <EOL> if not self . organization . is_admin ( request . user ) and not request . user . is_superuser : <EOL> raise PermissionDenied ( _ ( "<STR_LIT>" ) ) <EOL> return super ( AdminRequiredMixin , self ) . dispatch ( request , * args , <EOL> ** kwargs ) <EOL> class OwnerRequiredMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> self . request = request <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> self . organization = self . get_organization ( ) <EOL> if self . organization . owner . organization_user . user != request . user and not request . user . is_superuser : <EOL> raise PermissionDenied ( _ ( "<STR_LIT>" ) ) <EOL> return super ( OwnerRequiredMixin , self ) . dispatch ( request , * args , <EOL> ** kwargs ) </s>
