优化代码健壮性,解决部分硬编码问题

This commit is contained in:
mgw
2024-04-15 11:39:10 +08:00
parent 9ac3ef7c93
commit 19156f5944
2 changed files with 16 additions and 2 deletions

View File

@@ -210,10 +210,14 @@ class ResMrpWorkOrder(models.Model):
local_dir_path = '/ftp/before' local_dir_path = '/ftp/before'
os.makedirs(local_dir_path, exist_ok=True) os.makedirs(local_dir_path, exist_ok=True)
local_filename = 'WH_MO_00099.xls' local_filename = self.save_name + '.xls'
local_file_path = os.path.join(local_dir_path, local_filename) local_file_path = os.path.join(local_dir_path, local_filename)
logging.info('local_file_path:%s' % local_file_path) logging.info('local_file_path:%s' % local_file_path)
remote_path = '/home/ftp/ftp_root/ThreeTest/XT/Before/WH_MO_00099.xls' remote_path = '/home/ftp/ftp_root/ThreeTest/XT/Before/' + local_filename
logging.info('remote_path:%s' % remote_path)
if not ftp.file_exists(remote_path):
raise UserError(f"文件不存在: {remote_path}")
with open(local_file_path, 'wb') as local_file: with open(local_file_path, 'wb') as local_file:
ftp.ftp.retrbinary('RETR ' + remote_path, local_file.write) ftp.ftp.retrbinary('RETR ' + remote_path, local_file.write)

View File

@@ -21,6 +21,16 @@ class FtpController():
except Exception: except Exception:
logging.info("ftp连接失败") logging.info("ftp连接失败")
def file_exists(self, path):
# 检查文件是否存在于FTP服务器上
try:
self.ftp.cwd(os.path.dirname(path))
files = self.ftp.nlst()
return os.path.basename(path) in files
except Exception as e:
logging.error(f"Error checking file: {e}")
return False
# 下载目录下的文件 # 下载目录下的文件
def download_file_tree(self, target_dir, serverdir): def download_file_tree(self, target_dir, serverdir):
if not os.path.exists(serverdir): if not os.path.exists(serverdir):