<s> from modularodm import Q <EOL> from rest_framework import generics , permissions as drf_permissions <EOL> from rest_framework . exceptions import PermissionDenied , ValidationError , NotFound <EOL> from rest_framework . status import HTTP_204_NO_CONTENT <EOL> from rest_framework . response import Response <EOL> from framework . auth . oauth_scopes import CoreScopes <EOL> from api . base import generic_bulk_views as bulk_views <EOL> from api . base import permissions as base_permissions <EOL> from api . base . filters import ODMFilterMixin , ListFilterMixin <EOL> from api . base . views import JSONAPIBaseView <EOL> from api . base . parsers import JSONAPIOnetoOneRelationshipParser , JSONAPIOnetoOneRelationshipParserForRegularJSON <EOL> from api . base . pagination import CommentPagination <EOL> from api . base . utils import get_object_or_error , is_bulk_request , get_user_auth , is_truthy <EOL> from api . files . serializers import FileSerializer <EOL> from api . comments . serializers import CommentSerializer , CommentCreateSerializer <EOL> from api . comments . permissions import CanCommentOrPublic <EOL> from api . users . views import UserMixin <EOL> from api . nodes . serializers import ( <EOL> NodeSerializer , <EOL> NodeLinksSerializer , <EOL> NodeDetailSerializer , <EOL> NodeProviderSerializer , <EOL> NodeContributorsSerializer , <EOL> NodeContributorDetailSerializer , <EOL> NodeInstitutionRelationshipSerializer , <EOL> NodeAlternativeCitationSerializer , <EOL> NodeContributorsCreateSerializer <EOL> ) <EOL> from api . nodes . utils import get_file_object <EOL> from api . registrations . serializers import RegistrationSerializer <EOL> from api . institutions . serializers import InstitutionSerializer <EOL> from api . nodes . permissions import ( <EOL> AdminOrPublic , <EOL> ContributorOrPublic , <EOL> ContributorOrPublicForPointers , <EOL> ContributorDetailPermissions , <EOL> ReadOnlyIfRegistration , <EOL> ExcludeRetractions , <EOL> ) <EOL> from api . logs . serializers import NodeLogSerializer <EOL> from website . exceptions import NodeStateError <EOL> from website . util . permissions import ADMIN <EOL> from website . models import Node , Pointer , Comment , NodeLog , Institution <EOL> from website . files . models import FileNode <EOL> from framework . auth . core import User <EOL> from api . base . utils import default_node_list_query , default_node_permission_query <EOL> class NodeMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> serializer_class = NodeSerializer <EOL> node_lookup_url_kwarg = '<STR_LIT>' <EOL> def get_node ( self , check_object_permissions = True ) : <EOL> node = get_object_or_error ( <EOL> Node , <EOL> self . kwargs [ self . node_lookup_url_kwarg ] , <EOL> display_name = '<STR_LIT>' <EOL> ) <EOL> if node . is_collection or node . is_registration : <EOL> raise NotFound <EOL> if check_object_permissions : <EOL> self . check_object_permissions ( self . request , node ) <EOL> return node <EOL> class WaterButlerMixin ( object ) : <EOL> path_lookup_url_kwarg = '<STR_LIT:path>' <EOL> provider_lookup_url_kwarg = '<STR_LIT>' <EOL> def get_file_item ( self , item ) : <EOL> attrs = item [ '<STR_LIT>' ] <EOL> file_node = FileNode . resolve_class ( <EOL> attrs [ '<STR_LIT>' ] , <EOL> FileNode . FOLDER if attrs [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> else FileNode . FILE <EOL> ) . get_or_create ( self . get_node ( check_object_permissions = False ) , attrs [ '<STR_LIT:path>' ] ) <EOL> file_node . update ( None , attrs , user = self . request . user ) <EOL> self . check_object_permissions ( self . request , file_node ) <EOL> return file_node <EOL> def fetch_from_waterbutler ( self ) : <EOL> node = self . get_node ( check_object_permissions = False ) <EOL> path = self . kwargs [ self . path_lookup_url_kwarg ] <EOL> provider = self . kwargs [ self . provider_lookup_url_kwarg ] <EOL> return self . get_file_object ( node , path , provider ) <EOL> def get_file_object ( self , node , path , provider , check_object_permissions = True ) : <EOL> obj = get_file_object ( node = node , path = path , provider = provider , request = self . request ) <EOL> if provider == '<STR_LIT>' : <EOL> if check_object_permissions : <EOL> self . check_object_permissions ( self . request , obj ) <EOL> return obj <EOL> class NodeList ( JSONAPIBaseView , bulk_views . BulkUpdateJSONAPIView , bulk_views . BulkDestroyJSONAPIView , bulk_views . ListBulkCreateJSONAPIView , ODMFilterMixin , WaterButlerMixin ) : <EOL> """<STR_LIT>""" <EOL> permission_classes = ( <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> base_permissions . TokenHasScope , <EOL> ) <EOL> required_read_scopes = [ CoreScopes . NODE_BASE_READ ] <EOL> required_write_scopes = [ CoreScopes . NODE_BASE_WRITE ] <EOL> model_class = Node <EOL> serializer_class = NodeSerializer <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> ordering = ( '<STR_LIT>' , ) <EOL> def get_default_odm_query ( self ) : <EOL> user = self . request . user <EOL> base_query = default_node_list_query ( ) <EOL> permissions_query = default_node_permission_query ( user ) <EOL> return base_query & permissions_query <EOL> def get_queryset ( self ) : <EOL> if is_bulk_request ( self . request ) : <EOL> query = Q ( '<STR_LIT>' , '<STR_LIT>' , [ node [ '<STR_LIT:id>' ] for node in self . request . data ] ) <EOL> auth = get_user_auth ( self . request ) <EOL> nodes = Node . find ( query ) <EOL> if is_truthy ( self . request . query_params . get ( '<STR_LIT>' , False ) ) : <EOL> has_permission = [ ] <EOL> for node in nodes : <EOL> if node . can_edit ( auth ) : <EOL> has_permission . append ( node ) <EOL> query = Q ( '<STR_LIT>' , '<STR_LIT>' , [ node . _id for node in has_permission ] ) <EOL> return Node . find ( query ) <EOL> for node in nodes : <EOL> if not node . can_edit ( auth ) : <EOL> raise PermissionDenied <EOL> return nodes <EOL> else : <EOL> query = self . get_query_from_request ( ) <EOL> return Node . find ( query ) <EOL> def get_serializer_class ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . request . method in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return NodeDetailSerializer <EOL> else : <EOL> return NodeSerializer <EOL> def perform_create ( self , serializer ) : <EOL> """<STR_LIT>""" <EOL> user = self . request . user <EOL> serializer . save ( creator = user ) <EOL> def allow_bulk_destroy_resources ( self , user , resource_list ) : <EOL> """<STR_LIT>""" <EOL> if is_truthy ( self . request . query_params . get ( '<STR_LIT>' , False ) ) : <EOL> return any ( [ node . has_permission ( user , ADMIN ) for node in resource_list ] ) <EOL> return all ( [ node . has_permission ( user , ADMIN ) for node in resource_list ] ) <EOL> def bulk_destroy_skip_uneditable ( self , resource_object_list , user , object_type ) : <EOL> """<STR_LIT>""" <EOL> allowed = [ ] <EOL> skipped = [ ] <EOL> if not is_truthy ( self . request . query_params . get ( '<STR_LIT>' , False ) ) : <EOL> return None <EOL> for resource in resource_object_list : <EOL> if resource . has_permission ( user , ADMIN ) : <EOL> allowed . append ( resource ) <EOL> else : <EOL> skipped . append ( { '<STR_LIT:id>' : resource . _id , '<STR_LIT:type>' : object_type } ) <EOL> return { '<STR_LIT>' : skipped , '<STR_LIT>' : allowed } <EOL> def perform_destroy ( self , instance ) : <EOL> auth = get_user_auth ( self . request ) <EOL> try : <EOL> instance . remove_node ( auth = auth ) <EOL> except NodeStateError as err : <EOL> raise ValidationError ( err . message ) <EOL> instance . save ( ) <EOL> class NodeDetail ( JSONAPIBaseView , generics . RetrieveUpdateDestroyAPIView , NodeMixin , WaterButlerMixin ) : <EOL> """<STR_LIT>""" <EOL> permission_classes = ( <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> ContributorOrPublic , <EOL> ReadOnlyIfRegistration , <EOL> base_permissions . TokenHasScope , <EOL> ExcludeRetractions , <EOL> ) <EOL> required_read_scopes = [ CoreScopes . NODE_BASE_READ ] <EOL> required_write_scopes = [ CoreScopes . NODE_BASE_WRITE ] <EOL> serializer_class = NodeDetailSerializer <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> def get_object ( self ) : <EOL> return self . get_node ( ) <EOL> def perform_destroy ( self , instance ) : <EOL> auth = get_user_auth ( self . request ) <EOL> node = self . get_object ( ) <EOL> try : <EOL> node . remove_node ( auth = auth ) <EOL> except NodeStateError as err : <EOL> raise ValidationError ( err . message ) <EOL> node . save ( ) <EOL> class NodeContributorsList ( JSONAPIBaseView , bulk_views . BulkUpdateJSONAPIView , bulk_views . BulkDestroyJSONAPIView , bulk_views . ListBulkCreateJSONAPIView , ListFilterMixin , NodeMixin ) : <EOL> """<STR_LIT>""" <EOL> permission_classes = ( <EOL> AdminOrPublic , <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> ReadOnlyIfRegistration , <EOL> base_permissions . TokenHasScope , <EOL> ) <EOL> required_read_scopes = [ CoreScopes . NODE_CONTRIBUTORS_READ ] <EOL> required_write_scopes = [ CoreScopes . NODE_CONTRIBUTORS_WRITE ] <EOL> model_class = User <EOL> serializer_class = NodeContributorsSerializer <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> def get_default_queryset ( self ) : <EOL> node = self . get_node ( ) <EOL> visible_contributors = set ( node . visible_contributor_ids ) <EOL> contributors = [ ] <EOL> for contributor in node . contributors : <EOL> contributor . bibliographic = contributor . _id in visible_contributors <EOL> contributor . permission = node . get_permissions ( contributor ) [ - <NUM_LIT:1> ] <EOL> contributor . node_id = node . _id <EOL> contributors . append ( contributor ) <EOL> return contributors <EOL> def get_serializer_class ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . request . method == '<STR_LIT>' or self . request . method == '<STR_LIT>' or self . request . method == '<STR_LIT>' : <EOL> return NodeContributorDetailSerializer <EOL> elif self . request . method == '<STR_LIT:POST>' : <EOL> return NodeContributorsCreateSerializer <EOL> else : <EOL> return NodeContributorsSerializer <EOL> def get_queryset ( self ) : <EOL> queryset = self . get_queryset_from_request ( ) <EOL> if is_bulk_request ( self . request ) : <EOL> contrib_ids = [ item [ '<STR_LIT:id>' ] for item in self . request . data ] <EOL> queryset [ : ] = [ contrib for contrib in queryset if contrib . _id in contrib_ids ] <EOL> return queryset <EOL> def get_parser_context ( self , http_request ) : <EOL> """<STR_LIT>""" <EOL> res = super ( NodeContributorsList , self ) . get_parser_context ( http_request ) <EOL> res [ '<STR_LIT>' ] = True <EOL> return res <EOL> def perform_destroy ( self , instance ) : <EOL> auth = get_user_auth ( self . request ) <EOL> node = self . get_node ( ) <EOL> if len ( node . visible_contributors ) == <NUM_LIT:1> and node . get_visible ( instance ) : <EOL> raise ValidationError ( "<STR_LIT>" ) <EOL> if instance not in node . contributors : <EOL> raise NotFound ( '<STR_LIT>' ) <EOL> removed = node . remove_contributor ( instance , auth ) <EOL> if not removed : <EOL> raise ValidationError ( "<STR_LIT>" ) <EOL> class NodeContributorDetail ( JSONAPIBaseView , generics . RetrieveUpdateDestroyAPIView , NodeMixin , UserMixin ) : <EOL> """<STR_LIT>""" <EOL> permission_classes = ( <EOL> ContributorDetailPermissions , <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> ReadOnlyIfRegistration , <EOL> base_permissions . TokenHasScope , <EOL> ) <EOL> required_read_scopes = [ CoreScopes . NODE_CONTRIBUTORS_READ ] <EOL> required_write_scopes = [ CoreScopes . NODE_CONTRIBUTORS_WRITE ] <EOL> serializer_class = NodeContributorDetailSerializer <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> def get_object ( self ) : <EOL> node = self . get_node ( ) <EOL> user = self . get_user ( ) <EOL> self . check_object_permissions ( self . request , user ) <EOL> if user not in node . contributors : <EOL> raise NotFound ( '<STR_LIT>' . format ( user ) ) <EOL> user . permission = node . get_permissions ( user ) [ - <NUM_LIT:1> ] <EOL> user . bibliographic = node . get_visible ( user ) <EOL> user . node_id = node . _id <EOL> return user <EOL> def perform_destroy ( self , instance ) : <EOL> node = self . get_node ( ) <EOL> auth = get_user_auth ( self . request ) <EOL> if len ( node . visible_contributors ) == <NUM_LIT:1> and node . get_visible ( instance ) : <EOL> raise ValidationError ( "<STR_LIT>" ) <EOL> removed = node . remove_contributor ( instance , auth ) <EOL> if not removed : <EOL> raise ValidationError ( "<STR_LIT>" ) <EOL> class NodeRegistrationsList ( JSONAPIBaseView , generics . ListAPIView , NodeMixin ) : <EOL> """<STR_LIT>""" <EOL> permission_classes = ( <EOL> ContributorOrPublic , <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> base_permissions . TokenHasScope , <EOL> ExcludeRetractions <EOL> ) <EOL> required_read_scopes = [ CoreScopes . NODE_REGISTRATIONS_READ ] <EOL> required_write_scopes = [ CoreScopes . NODE_REGISTRATIONS_WRITE ] <EOL> serializer_class = RegistrationSerializer <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> def get_queryset ( self ) : <EOL> nodes = self . get_node ( ) . registrations_all <EOL> auth = get_user_auth ( self . request ) <EOL> registrations = [ node for node in nodes if node . can_view ( auth ) ] <EOL> return registrations <EOL> class NodeChildrenList ( JSONAPIBaseView , bulk_views . ListBulkCreateJSONAPIView , NodeMixin , ODMFilterMixin ) : <EOL> """<STR_LIT>""" <EOL> permission_classes = ( <EOL> ContributorOrPublic , <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> ReadOnlyIfRegistration , <EOL> base_permissions . TokenHasScope , <EOL> ExcludeRetractions <EOL> ) <EOL> required_read_scopes = [ CoreScopes . NODE_CHILDREN_READ ] <EOL> required_write_scopes = [ CoreScopes . NODE_CHILDREN_WRITE ] <EOL> serializer_class = NodeSerializer <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> def get_default_odm_query ( self ) : <EOL> return default_node_list_query ( ) <EOL> def get_queryset ( self ) : <EOL> node = self . get_node ( ) <EOL> req_query = self . get_query_from_request ( ) <EOL> query = ( <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , [ e . _id for e in node . nodes if e . primary ] ) & <EOL> req_query <EOL> ) <EOL> nodes = Node . find ( query ) <EOL> auth = get_user_auth ( self . request ) <EOL> return sorted ( [ each for each in nodes if each . can_view ( auth ) ] , key = lambda n : n . date_modified , reverse = True ) <EOL> def perform_create ( self , serializer ) : <EOL> user = self . request . user <EOL> serializer . save ( creator = user , parent = self . get_node ( ) ) <EOL> class NodeLinksList ( JSONAPIBaseView , bulk_views . BulkDestroyJSONAPIView , bulk_views . ListBulkCreateJSONAPIView , NodeMixin ) : <EOL> """<STR_LIT>""" <EOL> permission_classes = ( <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> ContributorOrPublic , <EOL> ReadOnlyIfRegistration , <EOL> base_permissions . TokenHasScope , <EOL> ExcludeRetractions , <EOL> ) <EOL> required_read_scopes = [ CoreScopes . NODE_LINKS_READ ] <EOL> required_write_scopes = [ CoreScopes . NODE_LINKS_WRITE ] <EOL> model_class = Pointer <EOL> serializer_class = NodeLinksSerializer <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> def get_queryset ( self ) : <EOL> return [ <EOL> pointer for pointer in <EOL> self . get_node ( ) . nodes_pointer <EOL> if not pointer . node . is_deleted <EOL> ] <EOL> def perform_destroy ( self , instance ) : <EOL> auth = get_user_auth ( self . request ) <EOL> node = self . get_node ( ) <EOL> try : <EOL> node . rm_pointer ( instance , auth = auth ) <EOL> except ValueError as err : <EOL> raise ValidationError ( err . message ) <EOL> node . save ( ) <EOL> def get_parser_context ( self , http_request ) : <EOL> """<STR_LIT>""" <EOL> res = super ( NodeLinksList , self ) . get_parser_context ( http_request ) <EOL> res [ '<STR_LIT>' ] = True <EOL> return res <EOL> class NodeLinksDetail ( JSONAPIBaseView , generics . RetrieveDestroyAPIView , NodeMixin ) : <EOL> """<STR_LIT>""" <EOL> permission_classes = ( <EOL> ContributorOrPublicForPointers , <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> base_permissions . TokenHasScope , <EOL> ReadOnlyIfRegistration , <EOL> ExcludeRetractions <EOL> ) <EOL> required_read_scopes = [ CoreScopes . NODE_LINKS_READ ] <EOL> required_write_scopes = [ CoreScopes . NODE_LINKS_WRITE ] <EOL> serializer_class = NodeLinksSerializer <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> def get_object ( self ) : <EOL> node_link_lookup_url_kwarg = '<STR_LIT>' <EOL> node_link = get_object_or_error ( <EOL> Pointer , <EOL> self . kwargs [ node_link_lookup_url_kwarg ] , <EOL> '<STR_LIT>' <EOL> ) <EOL> self . check_object_permissions ( self . request , node_link ) <EOL> return node_link <EOL> def perform_destroy ( self , instance ) : <EOL> auth = get_user_auth ( self . request ) <EOL> node = self . get_node ( ) <EOL> pointer = self . get_object ( ) <EOL> try : <EOL> node . rm_pointer ( pointer , auth = auth ) <EOL> except ValueError as err : <EOL> raise NotFound ( err . message ) <EOL> node . save ( ) <EOL> class NodeFilesList ( JSONAPIBaseView , generics . ListAPIView , WaterButlerMixin , ListFilterMixin , NodeMixin ) : <EOL> """<STR_LIT>""" <EOL> permission_classes = ( <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> base_permissions . PermissionWithGetter ( ContributorOrPublic , '<STR_LIT>' ) , <EOL> base_permissions . PermissionWithGetter ( ReadOnlyIfRegistration , '<STR_LIT>' ) , <EOL> base_permissions . TokenHasScope , <EOL> ExcludeRetractions <EOL> ) <EOL> ordering = ( '<STR_LIT>' , ) <EOL> serializer_class = FileSerializer <EOL> required_read_scopes = [ CoreScopes . NODE_FILE_READ ] <EOL> required_write_scopes = [ CoreScopes . NODE_FILE_WRITE ] <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> def get_default_queryset ( self ) : <EOL> files_list = self . fetch_from_waterbutler ( ) <EOL> if isinstance ( files_list , list ) : <EOL> return [ self . get_file_item ( file ) for file in files_list ] <EOL> if isinstance ( files_list , dict ) or getattr ( files_list , '<STR_LIT>' , False ) : <EOL> raise NotFound <EOL> return list ( files_list . children ) <EOL> def get_queryset ( self ) : <EOL> return self . get_queryset_from_request ( ) <EOL> class NodeFileDetail ( JSONAPIBaseView , generics . RetrieveAPIView , WaterButlerMixin , NodeMixin ) : <EOL> permission_classes = ( <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> base_permissions . PermissionWithGetter ( ContributorOrPublic , '<STR_LIT>' ) , <EOL> base_permissions . PermissionWithGetter ( ReadOnlyIfRegistration , '<STR_LIT>' ) , <EOL> base_permissions . TokenHasScope , <EOL> ExcludeRetractions <EOL> ) <EOL> serializer_class = FileSerializer <EOL> required_read_scopes = [ CoreScopes . NODE_FILE_READ ] <EOL> required_write_scopes = [ CoreScopes . NODE_FILE_WRITE ] <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> def get_object ( self ) : <EOL> fobj = self . fetch_from_waterbutler ( ) <EOL> if isinstance ( fobj , dict ) : <EOL> return self . get_file_item ( fobj ) <EOL> if isinstance ( fobj , list ) or not getattr ( fobj , '<STR_LIT>' , True ) : <EOL> raise NotFound <EOL> return fobj <EOL> class NodeProvider ( object ) : <EOL> def __init__ ( self , provider , node ) : <EOL> self . path = '<STR_LIT:/>' <EOL> self . node = node <EOL> self . kind = '<STR_LIT>' <EOL> self . name = provider <EOL> self . provider = provider <EOL> self . node_id = node . _id <EOL> self . pk = node . _id <EOL> class NodeProvidersList ( JSONAPIBaseView , generics . ListAPIView , NodeMixin ) : <EOL> """<STR_LIT>""" <EOL> permission_classes = ( <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> ContributorOrPublic , <EOL> ExcludeRetractions , <EOL> base_permissions . TokenHasScope , <EOL> ) <EOL> required_read_scopes = [ CoreScopes . NODE_FILE_READ ] <EOL> required_write_scopes = [ CoreScopes . NODE_FILE_WRITE ] <EOL> serializer_class = NodeProviderSerializer <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> def get_provider_item ( self , provider ) : <EOL> return NodeProvider ( provider , self . get_node ( ) ) <EOL> def get_queryset ( self ) : <EOL> return [ <EOL> self . get_provider_item ( addon . config . short_name ) <EOL> for addon <EOL> in self . get_node ( ) . get_addons ( ) <EOL> if addon . config . has_hgrid_files <EOL> and addon . configured <EOL> ] <EOL> class NodeProviderDetail ( JSONAPIBaseView , generics . RetrieveAPIView , NodeMixin ) : <EOL> permission_classes = ( <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> ContributorOrPublic , <EOL> ExcludeRetractions , <EOL> base_permissions . TokenHasScope , <EOL> ) <EOL> required_read_scopes = [ CoreScopes . NODE_FILE_READ ] <EOL> required_write_scopes = [ CoreScopes . NODE_FILE_WRITE ] <EOL> serializer_class = NodeProviderSerializer <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> def get_object ( self ) : <EOL> return NodeProvider ( self . kwargs [ '<STR_LIT>' ] , Node . load ( self . kwargs [ '<STR_LIT>' ] ) ) <EOL> class NodeAlternativeCitationsList ( JSONAPIBaseView , generics . ListCreateAPIView , NodeMixin ) : <EOL> """<STR_LIT>""" <EOL> permission_classes = ( <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> AdminOrPublic , <EOL> ReadOnlyIfRegistration , <EOL> base_permissions . TokenHasScope <EOL> ) <EOL> required_read_scopes = [ CoreScopes . NODE_CITATIONS_READ ] <EOL> required_write_scopes = [ CoreScopes . NODE_CITATIONS_WRITE ] <EOL> serializer_class = NodeAlternativeCitationSerializer <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> def get_queryset ( self ) : <EOL> return self . get_node ( ) . alternative_citations <EOL> class NodeAlternativeCitationDetail ( JSONAPIBaseView , generics . RetrieveUpdateDestroyAPIView , NodeMixin ) : <EOL> """<STR_LIT>""" <EOL> permission_classes = ( <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> AdminOrPublic , <EOL> ReadOnlyIfRegistration , <EOL> base_permissions . TokenHasScope <EOL> ) <EOL> required_read_scopes = [ CoreScopes . NODE_CITATIONS_READ ] <EOL> required_write_scopes = [ CoreScopes . NODE_CITATIONS_WRITE ] <EOL> serializer_class = NodeAlternativeCitationSerializer <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> def get_object ( self ) : <EOL> try : <EOL> return self . get_node ( ) . alternative_citations . find ( Q ( '<STR_LIT>' , '<STR_LIT>' , str ( self . kwargs [ '<STR_LIT>' ] ) ) ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> raise NotFound <EOL> def perform_destroy ( self , instance ) : <EOL> self . get_node ( ) . remove_citation ( get_user_auth ( self . request ) , instance , save = True ) <EOL> class NodeLogList ( JSONAPIBaseView , generics . ListAPIView , NodeMixin , ODMFilterMixin ) : <EOL> """<STR_LIT>""" <EOL> serializer_class = NodeLogSerializer <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> required_read_scopes = [ CoreScopes . NODE_LOG_READ ] <EOL> required_write_scopes = [ CoreScopes . NULL ] <EOL> log_lookup_url_kwarg = '<STR_LIT>' <EOL> ordering = ( '<STR_LIT>' , ) <EOL> permission_classes = ( <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> ContributorOrPublic , <EOL> base_permissions . TokenHasScope , <EOL> ExcludeRetractions <EOL> ) <EOL> def get_default_odm_query ( self ) : <EOL> auth = get_user_auth ( self . request ) <EOL> query = self . get_node ( ) . get_aggregate_logs_query ( auth ) <EOL> return query <EOL> def get_queryset ( self ) : <EOL> queryset = NodeLog . find ( self . get_query_from_request ( ) ) <EOL> return queryset <EOL> class NodeCommentsList ( JSONAPIBaseView , generics . ListCreateAPIView , ODMFilterMixin , NodeMixin ) : <EOL> """<STR_LIT>""" <EOL> permission_classes = ( <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> CanCommentOrPublic , <EOL> base_permissions . TokenHasScope , <EOL> ExcludeRetractions <EOL> ) <EOL> required_read_scopes = [ CoreScopes . NODE_COMMENTS_READ ] <EOL> required_write_scopes = [ CoreScopes . NODE_COMMENTS_WRITE ] <EOL> pagination_class = CommentPagination <EOL> serializer_class = CommentSerializer <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> ordering = ( '<STR_LIT>' , ) <EOL> def get_default_odm_query ( self ) : <EOL> return Q ( '<STR_LIT>' , '<STR_LIT>' , self . get_node ( ) ) & Q ( '<STR_LIT>' , '<STR_LIT>' , None ) <EOL> def get_queryset ( self ) : <EOL> comments = Comment . find ( self . get_query_from_request ( ) ) <EOL> for comment in comments : <EOL> if comment . root_target . referent . is_deleted : <EOL> comment . root_target = None <EOL> comment . save ( ) <EOL> return Comment . find ( self . get_query_from_request ( ) ) <EOL> def get_serializer_class ( self ) : <EOL> if self . request . method == '<STR_LIT:POST>' : <EOL> return CommentCreateSerializer <EOL> else : <EOL> return CommentSerializer <EOL> def get_parser_context ( self , http_request ) : <EOL> """<STR_LIT>""" <EOL> res = super ( NodeCommentsList , self ) . get_parser_context ( http_request ) <EOL> res [ '<STR_LIT>' ] = True <EOL> return res <EOL> def perform_create ( self , serializer ) : <EOL> node = self . get_node ( ) <EOL> serializer . validated_data [ '<STR_LIT:user>' ] = self . request . user <EOL> serializer . validated_data [ '<STR_LIT>' ] = node <EOL> serializer . save ( ) <EOL> class NodeInstitutionDetail ( JSONAPIBaseView , generics . RetrieveAPIView , NodeMixin ) : <EOL> """<STR_LIT>""" <EOL> permission_classes = ( <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> base_permissions . TokenHasScope , <EOL> AdminOrPublic <EOL> ) <EOL> required_read_scopes = [ CoreScopes . NODE_BASE_READ , CoreScopes . INSTITUTION_READ ] <EOL> required_write_scopes = [ CoreScopes . NULL ] <EOL> serializer_class = InstitutionSerializer <EOL> model = Institution <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> def get_object ( self ) : <EOL> node = self . get_node ( ) <EOL> if not node . primary_institution : <EOL> raise NotFound <EOL> return node . primary_institution <EOL> class NodeInstitutionRelationship ( JSONAPIBaseView , generics . RetrieveUpdateAPIView , NodeMixin ) : <EOL> """<STR_LIT>""" <EOL> permission_classes = ( <EOL> drf_permissions . IsAuthenticatedOrReadOnly , <EOL> base_permissions . TokenHasScope , <EOL> AdminOrPublic <EOL> ) <EOL> required_read_scopes = [ CoreScopes . NODE_BASE_READ , CoreScopes . INSTITUTION_READ ] <EOL> required_write_scopes = [ CoreScopes . NODE_BASE_WRITE ] <EOL> serializer_class = NodeInstitutionRelationshipSerializer <EOL> parser_classes = ( JSONAPIOnetoOneRelationshipParser , JSONAPIOnetoOneRelationshipParserForRegularJSON , ) <EOL> view_category = '<STR_LIT>' <EOL> view_name = '<STR_LIT>' <EOL> def get_object ( self ) : <EOL> return self . get_node ( ) <EOL> def update ( self , request , * args , ** kwargs ) : <EOL> partial = kwargs . pop ( '<STR_LIT>' , False ) <EOL> instance = self . get_object ( ) <EOL> serializer = self . get_serializer ( instance , data = request . data , partial = partial ) <EOL> serializer . is_valid ( raise_exception = True ) <EOL> self . perform_update ( serializer ) <EOL> inst = request . data . get ( '<STR_LIT:id>' , None ) <EOL> if not inst : <EOL> return Response ( status = HTTP_204_NO_CONTENT ) <EOL> return Response ( serializer . data ) </s>
