def localdrive(self, request): drives = get_mounted_drives_with_channel_info() # make sure everything is a dict, before converting to JSON assert isinstance(drives, dict) out = [mountdata._asdict() for mountdata in drives.values()] return Response(out)
def get_importable(self, instance): drive_id = self.context["request"].query_params.get( "importing_from_drive_id", None) # If node is from a remote source, assume it is importable. # Topics are annotated as importable by default, but client may disable importing # of the topic if it determines that the entire topic sub-tree is already on the device. if drive_id is None or instance.kind == content_kinds.TOPIC: return True # If non-topic ContentNode has no files, then it is not importable. content_files = instance.files.all() if not content_files.exists(): return False # Inspecting the external drive's files datafolder = cache.get(drive_id, None) if datafolder is None: drive_ids = get_mounted_drives_with_channel_info() if drive_id in drive_ids: datafolder = drive_ids[drive_id].datafolder cache.set(drive_id, datafolder, 60) # cache the datafolder for 1 minute else: raise serializers.ValidationError( "The external drive with given drive id {} does not exist." .format(drive_id)) importable = True for f in content_files: # Node is importable only if all of its Files are on the external drive try: file_path = get_content_storage_file_path( f.local_file.get_filename(), datafolder) importable = importable and os.path.exists(file_path) except InvalidStorageFilenameError: importable = False if not importable: break return importable