<s> from __future__ import absolute_import , unicode_literals <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . shortcuts import get_object_or_404 <EOL> from rest_framework import generics <EOL> from rest_framework . response import Response <EOL> from acls . models import AccessControlList <EOL> from documents . models import Document <EOL> from documents . permissions import permission_document_view <EOL> from permissions import Permission <EOL> from rest_api . filters import MayanObjectPermissionsFilter <EOL> from rest_api . permissions import MayanPermission <EOL> from . models import Folder <EOL> from . permissions import ( <EOL> permission_folder_add_document , permission_folder_create , <EOL> permission_folder_delete , permission_folder_edit , <EOL> permission_folder_remove_document , permission_folder_view <EOL> ) <EOL> from . serializers import ( <EOL> FolderDocumentSerializer , FolderSerializer , NewFolderDocumentSerializer , <EOL> NewFolderSerializer <EOL> ) <EOL> class APIDocumentFolderListView ( generics . ListAPIView ) : <EOL> """<STR_LIT>""" <EOL> serializer_class = FolderSerializer <EOL> filter_backends = ( MayanObjectPermissionsFilter , ) <EOL> mayan_object_permissions = { '<STR_LIT:GET>' : ( permission_folder_view , ) } <EOL> def get_queryset ( self ) : <EOL> document = get_object_or_404 ( Document , pk = self . kwargs [ '<STR_LIT>' ] ) <EOL> try : <EOL> Permission . check_permissions ( <EOL> self . request . user , ( permission_document_view , ) <EOL> ) <EOL> except PermissionDenied : <EOL> AccessControlList . objects . check_access ( <EOL> permission_document_view , self . request . user , document <EOL> ) <EOL> queryset = document . document_folders ( ) . all ( ) <EOL> return queryset <EOL> class APIFolderListView ( generics . ListCreateAPIView ) : <EOL> filter_backends = ( MayanObjectPermissionsFilter , ) <EOL> mayan_object_permissions = { '<STR_LIT:GET>' : ( permission_folder_view , ) } <EOL> mayan_view_permissions = { '<STR_LIT:POST>' : ( permission_folder_create , ) } <EOL> permission_classes = ( MayanPermission , ) <EOL> queryset = Folder . objects . all ( ) <EOL> def get_serializer_class ( self ) : <EOL> if self . request . method == '<STR_LIT:GET>' : <EOL> return FolderSerializer <EOL> elif self . request . method == '<STR_LIT:POST>' : <EOL> return NewFolderSerializer <EOL> def get ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return super ( APIFolderListView , self ) . get ( * args , ** kwargs ) <EOL> def post ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return super ( APIFolderListView , self ) . post ( * args , ** kwargs ) <EOL> class APIFolderView ( generics . RetrieveUpdateDestroyAPIView ) : <EOL> filter_backends = ( MayanObjectPermissionsFilter , ) <EOL> mayan_object_permissions = { <EOL> '<STR_LIT:GET>' : ( permission_folder_view , ) , <EOL> '<STR_LIT>' : ( permission_folder_edit , ) , <EOL> '<STR_LIT>' : ( permission_folder_edit , ) , <EOL> '<STR_LIT>' : ( permission_folder_delete , ) <EOL> } <EOL> permission_classes = ( MayanPermission , ) <EOL> queryset = Folder . objects . all ( ) <EOL> serializer_class = FolderSerializer <EOL> def delete ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return super ( APIFolderView , self ) . delete ( * args , ** kwargs ) <EOL> def get ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return super ( APIFolderView , self ) . get ( * args , ** kwargs ) <EOL> def patch ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return super ( APIFolderView , self ) . patch ( * args , ** kwargs ) <EOL> def put ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return super ( APIFolderView , self ) . put ( * args , ** kwargs ) <EOL> class APIFolderDocumentListView ( generics . ListCreateAPIView ) : <EOL> """<STR_LIT>""" <EOL> filter_backends = ( MayanObjectPermissionsFilter , ) <EOL> mayan_object_permissions = { <EOL> '<STR_LIT:GET>' : ( permission_folder_view , ) , <EOL> '<STR_LIT:POST>' : ( permission_folder_add_document , ) <EOL> } <EOL> def get_serializer_class ( self ) : <EOL> if self . request . method == '<STR_LIT:GET>' : <EOL> return FolderDocumentSerializer <EOL> elif self . request . method == '<STR_LIT:POST>' : <EOL> return NewFolderDocumentSerializer <EOL> def get_serializer_context ( self ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT>' : self . get_folder ( ) , <EOL> '<STR_LIT>' : self . format_kwarg , <EOL> '<STR_LIT>' : self . request , <EOL> '<STR_LIT>' : self <EOL> } <EOL> def get_folder ( self ) : <EOL> return get_object_or_404 ( Folder , pk = self . kwargs [ '<STR_LIT>' ] ) <EOL> def get_queryset ( self ) : <EOL> folder = self . get_folder ( ) <EOL> documents = folder . documents . all ( ) <EOL> try : <EOL> Permission . check_permissions ( <EOL> self . request . user , ( permission_document_view , ) <EOL> ) <EOL> except PermissionDenied : <EOL> documents = AccessControlList . objects . filter_by_access ( <EOL> permission_document_view , self . request . user , documents <EOL> ) <EOL> return documents <EOL> def perform_create ( self , serializer ) : <EOL> serializer . save ( folder = self . get_folder ( ) ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return super ( APIFolderDocumentListView , self ) . post ( request , * args , ** kwargs ) <EOL> class APIFolderDocumentView ( generics . RetrieveDestroyAPIView ) : <EOL> filter_backends = ( MayanObjectPermissionsFilter , ) <EOL> mayan_object_permissions = { <EOL> '<STR_LIT:GET>' : ( permission_folder_view , ) , <EOL> '<STR_LIT>' : ( permission_folder_remove_document , ) <EOL> } <EOL> serializer_class = FolderDocumentSerializer <EOL> def delete ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return super ( APIFolderDocumentView , self ) . delete ( request , * args , ** kwargs ) <EOL> def get ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return super ( APIFolderDocumentView , self ) . get ( * args , ** kwargs ) <EOL> def get_folder ( self ) : <EOL> return get_object_or_404 ( Folder , pk = self . kwargs [ '<STR_LIT>' ] ) <EOL> def get_queryset ( self ) : <EOL> return self . get_folder ( ) . documents . all ( ) <EOL> def get_serializer_context ( self ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT>' : self . get_folder ( ) , <EOL> '<STR_LIT>' : self . format_kwarg , <EOL> '<STR_LIT>' : self . request , <EOL> '<STR_LIT>' : self <EOL> } <EOL> def perform_destroy ( self , instance ) : <EOL> self . get_folder ( ) . documents . remove ( instance ) <EOL> def retrieve ( self , request , * args , ** kwargs ) : <EOL> instance = self . get_object ( ) <EOL> try : <EOL> Permission . check_permissions ( <EOL> self . request . user , ( permission_document_view , ) <EOL> ) <EOL> except PermissionDenied : <EOL> AccessControlList . objects . check_access ( <EOL> permission_document_view , self . request . user , instance <EOL> ) <EOL> serializer = self . get_serializer ( instance ) <EOL> return Response ( serializer . data ) </s>
