def _select_properties(properties, property_names): selected = dict() for property_name in property_names: property_value = properties.get(property_name) if property_value is not None: selected[property_name] = unicode_to_ascii(property_value) return selected
def copy_items(items, source, target, target_user, target_folder=None, relationships=None, work_dir=tempfile.gettempdir()): """ Copy items from the source portal to the target portal.""" if not target.is_logged_in(): raise PortalError('Must be logged into target portal to copy') # Make sure the folder exists (or gets created) on the target portal target_folder_id = None if target_folder: target_folder_id = _get_or_create_folder(target, target_user, target_folder) # Create a temporary folder to use for copying items. copy_dir = tempfile.mkdtemp(prefix='copy_items_', dir=unicode_to_ascii(work_dir)) try: # Copy the items copied_items = _copy_items(items, source, target, target_user, target_folder_id, None, copy_dir) # Copy the related items (if specified) if relationships: related_items = _copy_relationships(copied_items, source, target, target_user, target_folder_id, relationships, copy_dir) copied_items.update(related_items) finally: if clean_temp_files: shutil.rmtree(copy_dir) return copied_items
def copy_user_contents(source, source_user, target, target_user, ids=None, relationships=None, work_dir=tempfile.gettempdir()): """ Copy a user's items from the source portal to the target portal.""" if not source.is_logged_in(): raise PortalError('Must be logged into source portal to copy a '\ + 'user\'s contents') if not target.is_logged_in(): raise PortalError('Must be logged into target portal to copy a '\ + 'user\'s contents') # Get the user's content root_items, folders = source.user_contents(source_user) # Create a temporary folder to use for copying items. copy_dir = tempfile.mkdtemp(prefix='copy_user_content_', dir=unicode_to_ascii(work_dir)) try: # Copy the items in the root folder copied_items = _copy_items(root_items, source, target, target_user, None, ids, copy_dir) # Loop over all of the folders in the source portal, and get or create # the corresponding folder in the target portal for folder_id, folder_title, items in folders: target_folder_id = _get_or_create_folder(target, target_user, folder_title) copied_folder_items = _copy_items(items, source, target, target_user, target_folder_id, ids, copy_dir) copied_items.update(copied_folder_items) # Copy the related items (if specified) if relationships: related_items = _copy_relationships(copied_items, source, target, target_user, None, relationships, copy_dir) copied_items.update(related_items) finally: if clean_temp_files: shutil.rmtree(copy_dir) return copied_items
def _copy_item(item, source, target, target_user, target_folder_id, relationships, copy_dir): itemid = item['id'] item_dir = os.path.join(copy_dir, itemid) os.makedirs(item_dir) try: # Create a new items with the subset of properties we want to # copy to the target portal target_item = _select_properties(item, ITEM_COPY_PROPERTIES) # If its a text-based item, then read the text and # add it to the request. Otherwise treat it as a # file-based item, download it and add to the request # as a file data_file = None if item['type'] in TEXT_BASED_ITEM_TYPES: text = source.item_data(itemid) if text and len(text) > 0: target_item['text'] = text elif item['type'] in FILE_BASED_ITEM_TYPES: data_file = source.item_datad(itemid, item_dir, item.get('name')) # Handle the thumbnail (if one exists) thumbnail_file = None if 'thumbnail' in item: thumbnail_file = source.item_thumbnaild( itemid, item_dir, unicode_to_ascii(item['thumbnail'])) # Handle the metadata (if it exists) metadata_file = source.item_metadatad(itemid, item_dir) # Add the item to the target portal target_itemid = target.add_item( target_item, data_file, thumbnail_file, metadata_file, target_user, unicode_to_ascii(target_folder_id)) if target_itemid: _log.info('Copied item ' + itemid + ' in source portal ' + 'to ' + target_itemid + ' in target portal') # We're returning a mapping of source id to target (dict). # But before we return, handle the related items (if specified) copied_items = dict({itemid: target_itemid}) if relationships: related_items = _copy_relationships(copied_items, source, target, target_user, target_folder_id, relationships, copy_dir) copied_items.update(related_items) return target_itemid, copied_items else: _log.warning('Item ' + itemid + ' was not copied '\ + 'to target portal') # Just log IOErrors (includes HTTPErrors) except IOError as e: _log.warning('Item ' + itemid + ' was not copied to target portal ' \ + '(' + str(e) + ')') # Clean up the item directories as we go finally: if clean_temp_files: shutil.rmtree(item_dir) # Return an empty tuple, if for some reason the copy didn't happen return None, None