<s> """<STR_LIT>""" <EOL> from rest_framework . exceptions import APIException <EOL> from rest_framework import generics <EOL> from rest_framework import renderers <EOL> from rest_framework import permissions as drf_permissions <EOL> from modularodm import Q <EOL> from framework . auth import cas <EOL> from framework . auth . oauth_scopes import CoreScopes <EOL> from api . base . filters import ODMFilterMixin <EOL> from api . base . utils import get_object_or_error <EOL> from api . base . views import JSONAPIBaseView <EOL> from api . base import permissions as base_permissions <EOL> from api . tokens . serializers import ApiOAuth2PersonalTokenSerializer <EOL> from website . models import ApiOAuth2PersonalToken <EOL> class TokenList ( JSONAPIBaseView , generics . ListCreateAPIView , ODMFilterMixin ) : <EOL> """<STR_LIT>""" <EOL> permission_classes = ( <EOL> drf_permissions . IsAuthenticated , <EOL> base_permissions . OwnerOnly , <EOL> base_permissions . TokenHasScope , <EOL> ) <EOL> required_read_scopes = [ CoreScopes . TOKENS_READ ] <EOL> required_write_scopes = [ CoreScopes . TOKENS_WRITE ] <EOL> serializer_class = ApiOAuth2PersonalTokenSerializer <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> renderer_classes = [ renderers . JSONRenderer ] <EOL> def get_default_odm_query ( self ) : <EOL> owner = self . request . user . _id <EOL> return ( <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , owner ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , True ) <EOL> ) <EOL> def get_queryset ( self ) : <EOL> query = self . get_query_from_request ( ) <EOL> return ApiOAuth2PersonalToken . find ( query ) <EOL> def perform_create ( self , serializer ) : <EOL> """<STR_LIT>""" <EOL> serializer . validated_data [ '<STR_LIT>' ] = self . request . user <EOL> serializer . save ( ) <EOL> class TokenDetail ( JSONAPIBaseView , generics . RetrieveUpdateDestroyAPIView ) : <EOL> """<STR_LIT>""" <EOL> permission_classes = ( <EOL> drf_permissions . IsAuthenticated , <EOL> base_permissions . OwnerOnly , <EOL> base_permissions . TokenHasScope , <EOL> ) <EOL> required_read_scopes = [ CoreScopes . TOKENS_READ ] <EOL> required_write_scopes = [ CoreScopes . TOKENS_WRITE ] <EOL> serializer_class = ApiOAuth2PersonalTokenSerializer <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> renderer_classes = [ renderers . JSONRenderer ] <EOL> def get_object ( self ) : <EOL> obj = get_object_or_error ( ApiOAuth2PersonalToken , <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , self . kwargs [ '<STR_LIT>' ] ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , True ) ) <EOL> self . check_object_permissions ( self . request , obj ) <EOL> return obj <EOL> def perform_destroy ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> obj = self . get_object ( ) <EOL> try : <EOL> obj . deactivate ( save = True ) <EOL> except cas . CasHTTPError : <EOL> raise APIException ( "<STR_LIT>" ) <EOL> def perform_update ( self , serializer ) : <EOL> """<STR_LIT>""" <EOL> serializer . validated_data [ '<STR_LIT>' ] = self . request . user <EOL> serializer . save ( owner = self . request . user ) </s>
