def get_queryset(self): # For bulk requests, queryset is formed from request body. if is_bulk_request(self.request): query = Q('_id', 'in', [coll['id'] for coll in self.request.data]) auth = get_user_auth(self.request) collections = Collection.find(query) for collection in collections: if not collection.can_edit(auth): raise PermissionDenied return collections else: query = self.get_query_from_request() return Collection.find(query)
def get_targets(): logger.info('Acquiring targets...') targets = [ u for u in OSFUser.find(Q('merged_by', 'eq', None)) if Collection.find( Q('is_bookmark_collection', 'eq', True) & Q('is_deleted', 'eq', False) & Q('creator', 'eq', u)).count() == 0 ] logger.info('Found {} target users.'.format(len(targets))) return targets
def migrate(): targets = get_targets() total = len(targets) for i, user in enumerate(targets): logger.info('({}/{}) Preparing to migrate User {}'.format(i + 1, total, user._id)) bookmarks = Collection.find(Q('is_bookmark_collection', 'eq', True) & Q('creator', 'eq', user)) if sum([bool(n.nodes) for n in bookmarks]) > 1: logger.warn('Expected no users to have more than one bookmark with .nodes, {} violated. Selecting first one'.format(user._id)) bookmark_to_keep = None for n in bookmarks: if n.nodes: bookmark_to_keep = n break bookmark_to_keep = bookmark_to_keep or bookmarks[0] logger.info('Marking Collection {} as primary Bookmark Collection for User {}, preparing to delete others'.format(bookmark_to_keep._id, user._id)) for n in bookmarks: if n._id != bookmark_to_keep._id: n.is_deleted = True n.save() logger.info('Successfully migrated User {}'.format(user._id)) logger.info('Successfully migrated {} users'.format(total))