<s> from django . contrib . auth import get_user as _get_user <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . auth . backends import ModelBackend <EOL> from django . contrib . auth . models import AnonymousUser as DjangoAnonymousUser <EOL> from django . contrib . auth . models import Permission <EOL> from django . db . models import Q <EOL> from django . utils . functional import SimpleLazyObject <EOL> from rest_framework . authentication import BaseAuthentication <EOL> from . . core . config import config <EOL> class CustomizedModelBackend ( ModelBackend ) : <EOL> """<STR_LIT>""" <EOL> def get_group_permissions ( self , user_obj , obj = None ) : <EOL> """<STR_LIT>""" <EOL> if user_obj . is_anonymous ( ) or obj is not None : <EOL> return set ( ) <EOL> if not hasattr ( user_obj , '<STR_LIT>' ) : <EOL> if user_obj . is_superuser : <EOL> perms = Permission . objects . all ( ) <EOL> else : <EOL> user_groups_field = get_user_model ( ) . _meta . get_field ( '<STR_LIT>' ) <EOL> user_groups_query = '<STR_LIT>' % user_groups_field . related_query_name ( ) <EOL> query = Q ( ** { user_groups_query : user_obj } ) | Q ( group__pk = <NUM_LIT:2> ) <EOL> perms = Permission . objects . filter ( query ) <EOL> perms = perms . values_list ( '<STR_LIT>' , '<STR_LIT>' ) . order_by ( ) <EOL> user_obj . _group_perm_cache = set ( "<STR_LIT>" % ( ct , name ) for ct , name in perms ) <EOL> return user_obj . _group_perm_cache <EOL> class AnonymousUser ( DjangoAnonymousUser ) : <EOL> """<STR_LIT>""" <EOL> def get_all_permissions ( self , obj = None ) : <EOL> """<STR_LIT>""" <EOL> perms = Permission . objects . filter ( group__pk = <NUM_LIT:1> ) <EOL> if perms is None : <EOL> return set ( ) <EOL> perms = perms . values_list ( '<STR_LIT>' , '<STR_LIT>' ) . order_by ( ) <EOL> return set ( [ '<STR_LIT>' % ( content_type , codename ) for content_type , codename in perms ] ) <EOL> def has_perm ( self , perm , obj = None ) : <EOL> """<STR_LIT>""" <EOL> return ( perm in self . get_all_permissions ( ) ) <EOL> def has_module_perms ( self , app_label ) : <EOL> """<STR_LIT>""" <EOL> for perm in self . get_all_permissions ( ) : <EOL> if perm [ : perm . index ( '<STR_LIT:.>' ) ] == app_label : <EOL> return True <EOL> return False <EOL> class RESTFrameworkAnonymousAuthentication ( BaseAuthentication ) : <EOL> """<STR_LIT>""" <EOL> def authenticate ( self , request ) : <EOL> if config [ '<STR_LIT>' ] : <EOL> return ( AnonymousUser ( ) , None ) <EOL> return None <EOL> class AuthenticationMiddleware : <EOL> """<STR_LIT>""" <EOL> def process_request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> assert hasattr ( request , '<STR_LIT>' ) , ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> request . user = SimpleLazyObject ( lambda : get_user ( request ) ) <EOL> def get_user ( request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return_user = request . _cached_user <EOL> except AttributeError : <EOL> return_user = _get_user ( request ) <EOL> if config [ '<STR_LIT>' ] and isinstance ( return_user , DjangoAnonymousUser ) : <EOL> return_user = AnonymousUser ( ) <EOL> request . _cached_user = return_user <EOL> return return_user </s>
