Coverage for girder/api/v1/folder : 81%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#!/usr/bin/env python # -*- coding: utf-8 -*-
############################################################################### # Copyright 2013 Kitware Inc. # # Licensed under the Apache License, Version 2.0 ( the "License" ); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ###############################################################################
"""API Endpoint for folders."""
""" Get a list of folders with given search parameters. Currently accepted search modes are:
1. Searching by parentId and parentType. 2. Searching with full text search.
To search with full text search, pass the "text" parameter. To search by parent, (i.e. list child folders) pass parentId and parentType, which must be one of ('folder' | 'collection' | 'user'). You can also pass limit, offset, sort, and sortdir paramters.
:param limit: The result set size limit, default=50. :param offset: Offset into the results, default=0. :param sort: The field to sort by, default=lowerName. :param sortdir: 1 for ascending, -1 for descending, default=1. """
return self.model('folder').textSearch( params['text'], user=user, limit=limit, project={ 'name': 1 }) ' or folder.')
id=params['parentId'], user=user, level=AccessType.READ, exc=True)
self.model('folder').childFolders( parentType=parentType, parent=parent, user=user, offset=offset, limit=limit, sort=sort)] else: Description('Search for folders by certain properties.') .responseClass('Folder') .param('parentType', """Type of the folder's parent: either user, folder, or collection (default='folder').""", required=False) .param('parentId', "The ID of the folder's parent.", required=False) .param('text', 'Pass to perform a text search.', required=False) .param('limit', "Result set size limit (default=50).", required=False, dataType='int') .param('offset', "Offset into result set (default=0).", required=False, dataType='int') .param('sort', "Field to sort the folder list by (default=name)", required=False) .param('sortdir', "1 for ascending, -1 for descending (default=1)", required=False, dataType='int') .errorResponse() .errorResponse('Read access was denied on the parent resource.', 403))
def downloadFolder(self, folder, params): """ Returns a generator function that will be used to stream out a zip file containing this folder's contents, filtered by permissions. """ 'attachment; filename="{}{}"'.format(folder['name'], '.zip')
Description('Download an entire folder as a zip archive.') .param('id', 'The ID of the folder.', paramType='path') .errorResponse('ID was invalid.') .errorResponse('Read access was denied for the folder.', 403))
""" Helper method to recurse through folders and download files in them. """ parent=folder, user=user, limit=0): path, sub['name'])): self.model('file') .download(file, headers=False), os.path.join( path, file['name'])):
def updateFolder(self, folder, params): """ Update the folder.
:param name: Name for the folder. :param description: Description for the folder. :param public: Public read access flag. :type public: bool """ folder['name'] = params.get('name', folder['name']).strip() folder['description'] = params.get( 'description', folder['description']).strip()
folder = self.model('folder').updateFolder(folder) return self.model('folder').filter(folder, self.getCurrentUser()) Description('Update a folder by ID.') .responseClass('Folder') .param('id', 'The ID of the folder.', paramType='path') .param('name', 'Name of the folder.') .param('description', 'Description for the folder.', required=False) .param('public', "Whether the folder should be public or private.", required=False, dataType='boolean') .errorResponse('ID was invalid.') .errorResponse('Write access was denied for the folder.', 403))
def updateFolderAccess(self, folder, params): self.requireParams(['access'], params)
public = params.get('public', 'false').lower() == 'true' self.model('folder').setPublic(folder, public)
try: access = json.loads(params['access']) return self.model('folder').setAccessList( folder, access, save=True) except ValueError: raise RestException('The access parameter must be JSON.') Description('Update the access control list for a folder.') .param('id', 'The ID of the folder.', paramType='path') .param('access', 'The JSON-encoded access control list.') .param('public', 'Public read access flag.', dataType='bool') .errorResponse('ID was invalid.') .errorResponse('Admin access was denied for the folder.', 403))
""" Create a new folder.
:param parentId: The _id of the parent folder. :type parentId: str :param parentType: The type of the parent of this folder. :type parentType: str - 'user', 'collection', or 'folder' :param name: The name of the folder to create. :param description: Folder description. :param public: Public read access flag. :type public: bool """
public = public.lower() == 'true'
raise RestException('Set parentType to collection, folder, ' 'or user.')
level=AccessType.WRITE, exc=True)
parent=parent, name=name, parentType=parentType, creator=user, description=description, public=public)
folder = self.model('folder').setUserAccess( folder, user=user, level=AccessType.ADMIN, save=True) # TODO set appropriate top-level community folder permissions pass Description('Create a new folder.') .responseClass('Folder') .param('parentType', """Type of the folder's parent: either user, folder', or collection (default='folder').""", required=False) .param('parentId', "The ID of the folder's parent.") .param('name', "Name of the folder.") .param('description', "Description for the folder.", required=False) .param('public', """Wheter the folder should be public or private. By default, inherits the value from parent folder, or in the case of user or collection parentType, defaults to False.""", required=False, dataType='boolean') .errorResponse() .errorResponse('Write access was denied on the parent', 403))
def getFolder(self, folder, params): """Get a folder by ID.""" Description('Get a folder by ID.') .responseClass('Folder') .param('id', 'The ID of the folder.', paramType='path') .errorResponse('ID was invalid.') .errorResponse('Read access was denied for the folder.', 403))
def getFolderAccess(self, folder, params): """ Get an access list for a folder. """ return self.model('folder').getFullAccessList(folder) Description('Get the access control list for a folder.') .responseClass('Folder') .param('id', 'The ID of the folder.', paramType='path') .errorResponse('ID was invalid.') .errorResponse('Admin access was denied for the folder.', 403))
def deleteFolder(self, folder, params): """ Delete a folder recursively. """ Description('Delete a folder by ID.') .param('id', 'The ID of the folder.', paramType='path') .errorResponse('ID was invalid.') .errorResponse('Admin access was denied for the folder.', 403)) |