Coverage for girder/models/item : 92%

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. ###############################################################################
""" Items are leaves in the data hierarchy. They can contain 0 or more files within them, and can also contain arbitrary metadata. """
'name': 10, 'description': 1 })
""" Filter an item document for display to the user. """ 'meta', 'creatorId', 'folderId', 'name', 'baseParentType', 'baseParentId']
# Ensure unique name among sibling items and folders. If the desired # name collides with an existing item or folder, we will append (n) # onto the end of the name, incrementing n until the name is unique. 'name': name, 'folderId': doc['folderId'] }
'parentId': doc['folderId'], 'name': name, 'parentCollection': 'folder' } else: n += 1 name = '%s (%d)' % (doc['name'], n)
force=False, fields=None, exc=False): """ We override Model.load to also do permission checking.
: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 """ exc=exc)
force, fields)
self.save(doc)
""" Generator function that yields child files in the item.
:param item: The parent item. :param limit: Result limit. :param offset: Result offset. :param sort: The sort structure to pass to pymongo. """ 'itemId': item['_id'] }
q, limit=limit, offset=offset, sort=sort)
""" Delete an item, and all references to it in the database.
:param item: The item document to delete. :type item: dict """
# Delete all files in this item 'itemId': item['_id'] }, limit=0) self.model('file').remove(file)
# Delete pending uploads into this item 'parentId': item['_id'], 'parentType': 'item' }, limit=0) self.model('upload').remove(upload)
# Delete the item itself
""" Custom override of Model.textSearch to filter items by permissions of the parent folder. """
# get the non-filtered search result from Model.textSearch
# list where we will store the filtered results
# cache dictionary mapping folderId's to read permission
# loop through all results in the non-filtered list # check if the folderId is cached
# if the folderId is not cached check for read permission # and set the cache folder, user=user, level=AccessType.READ)
'name': result['obj']['name'], '_id': result['obj']['_id'] })
# once we have hit the requested limit, return break
removeKeys=()): """ This method is provided as a convenience for filtering a result cursor of items by permissions, based on the parent folder. The results in the cursor must contain the folderId field. """ # Cache mapping folderIds -> access granted (bool)
folder, user=user, level=level)
skipped += 1 else: break
""" Create a new item. The creator will be given admin access to it.
:param name: The name of the item. :type name: str :param description: Description for the folder. :type description: str :param folder: The parent folder of the item. :param creator: User document representing the creator of the group. :type creator: dict :returns: The item document that was created. """
# Internal error -- this shouldn't be called without a user. raise Exception('Creator must be a user.')
creator)
'name': name, 'description': description, 'folderId': folder['_id'], 'creatorId': creator['_id'], 'baseParentType': folder['baseParentType'], 'baseParentId': folder['baseParentId'], 'created': now, 'updated': now, 'size': 0 })
""" Updates an item.
:param item: The item document to update :type item: dict :returns: The item document that was edited. """
# Validate and save the collection
""" Set metadata on an item. A rest exception is thrown in the cases where the metadata json object is badly formed, or if any of the metadata keys contains a period ('.').
:param item: The item to set the metadata on. :type item: dict :param metadata: A dictionary containing key-value pairs to add to the items meta field :type metadata: dict :returns: the item document """
# Add new metadata to existing metadata
# Remove metadata fields that were set to null (use items in py3)
# Validate and save the item
""" 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 """ item['folderId'], user=user, level=AccessType.READ, force=force) curFolder, user=user, level=AccessType.READ, force=force) |