def update_item(self, item_id=None, item_path=None, new_name=None, new_description=None, new_parent_reference=None, new_file_system_info=None): """ Update the metadata of the specified item. :param str | None item_id: (Optional) ID of the target item. :param str | None item_path: (Optional) Path to the target item. :param str | None new_name: (Optional) If set, update the item metadata with the new name. :param str | None new_description: (Optional) If set, update the item metadata with the new description. :param onedrived.api.resources.ItemReference | None new_parent_reference: (Optional) If set, move the item. :param onedrived.api.facets.FileSystemInfoFacet | None new_file_system_info: (Optional) If set, update the client-wise timestamps. :rtype: onedrived.api.items.OneDriveItem """ if item_id is None and item_path is None: raise ValueError('Root is immutable. A specific item is required.') data = {} if new_name is not None: data['name'] = new_name if new_description is not None: data['description'] = new_description if new_parent_reference is not None: data['parentReference'] = new_parent_reference.data if new_file_system_info is not None: data['fileSystemInfo'] = new_file_system_info.data if len(data) == 0: raise ValueError('Nothing is to change.') uri = self.get_item_uri(item_id, item_path) request = self.root.account.session.patch(uri, data) return items.OneDriveItem(self, request.json())
def test_client_time(self): self.data['fileSystemInfo'] = get_data( 'facets/filesysteminfo_facet.json') self.assertNotEqual(self.data['fileSystemInfo']['createdDateTime'], self.data['createdDateTime']) self.assertNotEqual( self.data['fileSystemInfo']['lastModifiedDateTime'], self.data['lastModifiedDateTime']) item = items.OneDriveItem(get_sample_drive_object(), self.data) self.assert_timestamps(self.data['fileSystemInfo'], item) self.assert_timestamps(self.data['fileSystemInfo'], item.fs_info)
def setUp(self): self.data = get_data('image_item.json') self.data['audio'] = get_data('facets/audio_facet.json') self.data['deleted'] = get_data('facets/deleted_facet.json') self.data['photo'] = get_data('facets/photo_facet.json') self.data['video'] = get_data('facets/video_facet.json') self.data['specialFolder'] = get_data( 'facets/specialfolder_facet.json') self.data['location'] = get_data('facets/location_facet.json') self.data['parentReference'] = get_data('item_reference.json') self.item = items.OneDriveItem(drive=get_sample_drive_object(), data=self.data)
def get_item(self, item_id=None, item_path=None, list_children=True): """ Retrieve the metadata of an item from OneDrive server. :param str | None item_id: ID of the item. Required if item_path is None. :param str | None item_path: Path to the item relative to drive root. Required if item_id is None. :rtype: onedrived.api.items.OneDriveItem """ uri = self.get_item_uri(item_id, item_path) if list_children: uri += '?expand=children' request = self.root.account.session.get(uri) return items.OneDriveItem(self, request.json())
def test_update_item(self): data = self.all_items_data[0] self.assertNotEqual('12345767', data['file']['hashes']['crc32Hash']) data['file']['hashes']['crc32Hash'] = '12345767' self.assertNotEqual('/drive/root:/foo', data['parentReference']['path']) data['parentReference']['path'] = '/drive/root:/foo' self.assertNotEqual('2020-01-20T03:04:05.06Z', data['lastModifiedDateTime']) data['lastModifiedDateTime'] = '2020-01-20T03:04:05.06Z' item = items.OneDriveItem(self.drive, data) status = items_db.ItemRecordStatuses.MOVING self.itemdb.update_item(item, status) records = self.itemdb.get_items_by_id(item_id=item.id) self.assert_item_record(item, records, status)
def create_dir(self, name, parent_id=None, parent_path=None, conflict_behavior=options.NameConflictBehavior.DEFAULT): """ Create a new directory under the specified parent directory. :param str name: Name of the new directory. :param str | None parent_id: (Optional) ID of the parent directory item. :param str | None parent_path: (Optional) Path to the parent directory item. :param str conflict_behavior: (Optional) What to do if name exists. One value from options.nameConflictBehavior. :rtype: onedrived.api.items.OneDriveItem """ data = { 'name': name, 'folder': {}, '@name.conflictBehavior': conflict_behavior } uri = self.get_item_uri(parent_id, parent_path) + '/children' request = self.root.account.session.post( uri, json=data, ok_status_code=requests.codes.created) return items.OneDriveItem(self, request.json())
def put_file(self, filename, data, parent_id=None, parent_path=None, conflict_behavior=options.NameConflictBehavior.REPLACE): """ Use HTTP PUT to upload a file that is relatively small (less than 100M). :param str filename: Name of the remote file. :param file data: An opened file object available for reading. :param str | None parent_id: (Optional) ID of the parent directory. :param str | None parent_path: (Optional) Path to the parent directory. :param str conflict_behavior: (Optional) Specify the behavior to use if the file already exists. :rtype: onedrived.api.items.OneDriveItem """ if parent_id is not None: parent_id += ':' uri = self.get_item_uri(parent_id, parent_path) + '/' + filename + ':/content' if conflict_behavior != options.NameConflictBehavior.REPLACE: uri += '[email protected]=' + conflict_behavior request = self.root.account.session.put( uri, data=data, ok_status_code=requests.codes.created) return items.OneDriveItem(self, request.json())
def _add_item(self, filename): data = get_data(filename) item = items.OneDriveItem(self.drive, data) self.itemdb.update_item(item, items_db.ItemRecordStatuses.OK) self.all_items.append(item) self.all_items_data.append(data)
def test_time_fallback(self): item = items.OneDriveItem(get_sample_drive_object(), self.data) self.assert_timestamps(self.data, item)
def setUp(self): self.data = get_data('drive_root.json') self.item = items.OneDriveItem(drive=get_sample_drive_object(), data=self.data)
def build_item(self, data): return items.OneDriveItem(self, data)