Coverage for girder/models/folder : 89%

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. ###############################################################################
""" Folders are used to store items and can also store other folders in a hierarchical way, like a directory on a filesystem. Every folder has its own set of access control policies, but by default the access control list is inherited from the folder's parent folder, if it has one. Top-level folders are ones whose parent is a user or a collection. """
'name': 10, 'description': 1 })
""" Filter a folder document for display to the user. """ 'size', 'parentId', 'parentCollection', 'creatorId', 'baseParentType', 'baseParentId']
folder, user)
raise ValidationException('Folder name must not be empty.', 'name')
# Internal error; this shouldn't happen raise Exception('Invalid folder parent type: %s.' % doc['parentCollection'])
# Ensure unique name among sibling folders 'parentId': doc['parentId'], 'name': doc['name'], 'parentCollection': doc['parentCollection'] } 'exists here.', 'name')
# Ensure unique name among sibling items 'folderId': doc['parentId'], 'name': doc['name'] } raise ValidationException('An item with that name already ' 'exists here.', 'name')
force=False, fields=None, exc=False): """ We override load in order to ensure the folder has certain fields within it, and if not, we add them lazily at read time.
:param id: The id of the resource. :type id: string or ObjectId :param user: The user to check access against. :type user: dict or None :param level: The required access type for the object. :type level: AccessType :param force: If you explicity want to circumvent access checking on this resource, set this to True. :type force: bool """ self, id=id, objectId=objectId, level=level, fields=fields, exc=exc, force=force, user=user)
self.save(doc)
""" Delete a folder recursively.
:param folder: The folder document to delete. :type folder: dict """ # Delete all child items 'folderId': folder['_id'] }, limit=0)
# Delete all child folders 'parentId': folder['_id'], 'parentCollection': 'folder' }, limit=0)
# Delete pending uploads into this folder 'parentId': folder['_id'], 'parentType': 'folder' }, limit=0) self.model('upload').remove(upload)
# Delete this folder
""" Generator function that yields child items in a folder.
:param folder: The parent folder. :param limit: Result limit. :param offset: Result offset. :param sort: The sort structure to pass to pymongo. """ 'folderId': folder['_id'] }
q, limit=limit, offset=offset, sort=sort)
sort=None): """ This generator will yield child folders of a user, collection, or folder, with access policy filtering.
:param parent: The parent object. :type parentType: Type of the parent object. :param parentType: The parent type. :type parentType: 'user', 'folder', or 'collection' :param user: The user running the query. Only returns folders that this user can see. :param limit: Result limit. :param offset: Result offset. :param sort: The sort structure to pass to pymongo. """ raise ValidationException('The parentType must be folder, ' 'collection, or user.')
'parentId': parent['_id'], 'parentCollection': parentType }
# Perform the find; we'll do access-based filtering of the result set # afterward.
level=AccessType.READ, limit=limit, offset=offset):
public=None, creator=None): """ Create a new folder under the given parent.
:param parent: The parent document. Should be a folder, user, or collection. :type parent: dict :param name: The name of the folder. :type name: str :param description: Description for the folder. :type description: str :param parentType: What type the parent is: ('folder' | 'user' | 'collection') :type parentType: str :param public: Public read access flag. :type public: bool or None to inherit from parent :param creator: User document representing the creator of this folder. :type creator: dict :returns: The folder document that was created. """
raise ValidationException('The parentType must be folder, ' 'collection, or user.')
'parentCollection': parentType} pathFromRoot = self.parentsToRoot(parent, user=creator) parent['baseParentId'] = pathFromRoot[0]['object']['_id'] parent['baseParentType'] = pathFromRoot[0]['type'] else:
else:
'name': name, 'description': description, 'parentCollection': parentType, 'baseParentId': parent['baseParentId'], 'baseParentType': parent['baseParentType'], 'parentId': parent['_id'], 'creatorId': creatorId, 'created': now, 'updated': now, 'size': 0 }
# If this is a subfolder, default permissions are inherited from the # parent folder. Otherwise, the creator is granted admin access.
# Allow explicit public flag override if it's set.
# Now validate and save the folder.
""" Updates a folder.
:param folder: The folder document to update :type folder: dict :returns: The folder document that was edited. """ folder['updated'] = datetime.datetime.now()
# Validate and save the folder return self.save(folder)
level=AccessType.READ): """ Get the path to traverse to a root of the hierarchy.
:param item: The item whose root to find :type item: dict :returns: an ordered list of dictionaries from root to the current item """ curParentId, user=user, level=level, force=force) user) 'object': parentFiltered}] + curPath else: curParentId, user=user, level=level, force=force) 'object': self.filter(curParentObject, user)}] + curPath force=force) |