<s> from django . db import models <EOL> from django . conf import settings <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> USER_MODEL_NAME = getattr ( settings , "<STR_LIT>" , '<STR_LIT>' ) <EOL> try : <EOL> from django . contrib . auth import get_user_model <EOL> except ImportError : <EOL> from django . contrib . auth . models import User <EOL> def get_user_model ( ) : <EOL> return User <EOL> class OwnerMixinManager ( models . Manager ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , owner_filter ) : <EOL> super ( OwnerMixinManager , self ) . __init__ ( ) <EOL> self . owner_filter = owner_filter <EOL> def _owned_by_multi ( self , users ) : <EOL> UserModel = get_user_model ( ) <EOL> user_pks = [ u . pk if isinstance ( u , UserModel ) else u for u in users ] <EOL> filter_ = { self . owner_filter + '<STR_LIT>' : user_pks } <EOL> return self . filter ( ** filter_ ) . distinct ( ) <EOL> def _owned_by_single ( self , user , include_staff , include_superuser ) : <EOL> UserModel = get_user_model ( ) <EOL> user_pk , user = ( user . pk , user ) if isinstance ( user , UserModel ) else ( user , None ) <EOL> if include_staff or include_superuser : <EOL> if not user : <EOL> user = UserModel . objects . only ( '<STR_LIT>' , '<STR_LIT>' ) . get ( pk = user_pk ) <EOL> if ( include_staff and user . is_staff ) or ( include_superuser and user . is_superuser ) : <EOL> return self . all ( ) <EOL> filter_ = { self . owner_filter : user_pk } <EOL> return self . filter ( ** filter_ ) <EOL> def owned_by ( self , user , include_staff = False , include_superuser = False ) : <EOL> """<STR_LIT>""" <EOL> if not user : <EOL> return [ ] <EOL> if isinstance ( user , ( list , tuple ) ) : <EOL> if len ( user ) == <NUM_LIT:1> : <EOL> user = user [ <NUM_LIT:0> ] <EOL> else : <EOL> if include_staff or include_superuser : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return self . _owned_by_multi ( user ) <EOL> return self . _owned_by_single ( user , include_staff , include_superuser ) <EOL> class OwnerMixinBase ( models . Model ) : <EOL> class Meta : <EOL> abstract = True <EOL> def get_owner_pks ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplemented <EOL> def is_owned_by ( self , user , include_staff = False , include_superuser = False ) : <EOL> """<STR_LIT>""" <EOL> UserModel = get_user_model ( ) <EOL> user_pk , user = ( user . pk , user ) if isinstance ( user , UserModel ) else ( user , None ) <EOL> if include_staff or include_superuser : <EOL> if not user : <EOL> user = UserModel . objects . only ( '<STR_LIT>' , '<STR_LIT>' ) . get ( pk = user_pk ) <EOL> if ( include_staff and user . is_staff ) or ( include_superuser and user . is_superuser ) : <EOL> return True <EOL> return user_pk in self . get_owner_pks ( ) <EOL> def is_not_owned_by ( self , user , include_staff = False , include_superuser = False ) : <EOL> """<STR_LIT>""" <EOL> return not self . is_owned_by ( user , include_staff , include_superuser ) <EOL> class SingleOwnerMixinManager ( OwnerMixinManager ) : <EOL> def __init__ ( self , owner_filter = '<STR_LIT>' ) : <EOL> super ( SingleOwnerMixinManager , self ) . __init__ ( owner_filter ) <EOL> class SingleOwnerMixin ( OwnerMixinBase ) : <EOL> """<STR_LIT>""" <EOL> owner = models . ForeignKey ( USER_MODEL_NAME , verbose_name = _ ( '<STR_LIT>' ) , <EOL> related_name = '<STR_LIT>' ) <EOL> objects = SingleOwnerMixinManager ( ) <EOL> class Meta : <EOL> abstract = True <EOL> def owner_list ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self . owner ] <EOL> def get_owner_pks ( self ) : <EOL> return [ self . owner_id ] <EOL> class MultipleOwnerMixinManager ( OwnerMixinManager ) : <EOL> def __init__ ( self , owner_filter = '<STR_LIT>' ) : <EOL> super ( MultipleOwnerMixinManager , self ) . __init__ ( owner_filter ) <EOL> class MultipleOwnerMixin ( OwnerMixinBase ) : <EOL> """<STR_LIT>""" <EOL> owners = models . ManyToManyField ( USER_MODEL_NAME , verbose_name = _ ( '<STR_LIT>' ) , <EOL> related_name = '<STR_LIT>' ) <EOL> objects = MultipleOwnerMixinManager ( ) <EOL> class Meta : <EOL> abstract = True <EOL> def owner_list ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . owners . all ( ) ) <EOL> def get_owner_pks ( self ) : <EOL> return self . owners . values_list ( '<STR_LIT:id>' , flat = True ) <EOL> class ParentOwnerMixin ( OwnerMixinBase ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> abstract = True <EOL> def get_owner_pks ( self ) : <EOL> assert isinstance ( self . objects , OwnerMixinManager ) <EOL> return getattr ( self , self . objects . owner_filter ) . get_owner_pks ( ) </s>
