def post(self, request): file_id = request.POST.get('file_id') folder_id = request.POST.get('folder_id') folder_copy = Folder.objects.filter(id=folder_id).first() file = File.objects.filter(id=file_id).first() # 原路径 path = getpath('media', file.url) # copy的路径 path_c = getpath('media', folder_copy.url, file.name) # 把copy之后的文件信息存入数据库 url = folder_copy.url + '/' + file.name File.objects.create(name=file.name, url=url, size=file.size, depth=folder_copy.depth + 1, creater_id=1, pfolder_id=folder_id, filetype=file.filetype) # 实际目录的拷贝 shutil.copyfile(path, path_c) data = { 'path': path, 'path_c': path_c, } return res(data=data)
def put(self, request): data = json.loads(request.body) file_id = data.get('file_id') new_name = data.get('new_name') file = File.objects.filter(id=file_id).first() foid = file.pfolder_id folder = Folder.objects.filter(id=foid).first() # 源文件路径 path = getpath('media', file.url) # 修改名字之后的路径 new_path = getpath('media', folder.url, new_name + file.filetype) try: file.name = new_name + file.filetype file.url = folder.url + '/' + file.name os.rename(path, new_path) file.save() except Exception: if os.path.exists(new_path): os.rename(new_path, path) raise Exception(u'重命名失败') data = { 'path': path, 'new_path': new_path, } return JsonResponse(data=data)
def delete(self, request): file_id = request.GET.get('file_id') file_lists = [] # 判断删除单个文件还是多个文件,并转化为list类型 if type(file_id) is int: file_lists.append(file_id) else: file_lists = list(file_id.split(',')) # 循环删除文件 for file_list in file_lists: try: file = File.objects.filter(id=file_list).first() # 文件目录 path = getpath('media', file.url) # 删除文件 os.remove(path) # 删除该文件的数据库信息 file.delete() except Exception: if os.path.exists(path): raise Exception(u'删除文件失败') data = { 'file_id': file_id, 'file_lists': file_lists, 'path': path, } return JsonResponse( data=data )
def put(self, request): # 用json的格式获取数据 data = json.loads(request.body) folder_id = data.get('folder_id') folder_name = data.get('folder_name') folder = Folder.objects.filter(id=folder_id).first() if folder is None: raise Exception(u"该文件不存在") # 实际文件 # 该文件夹地址 path = getpath('media', folder.url) # 文件的父目录,绝对路径 path_parent = os.path.split(path)[0] path_new = os.path.join(path_parent, folder_name) os.rename(path, path_new) # 在数据库中修改URL folder.url = os.path.split(folder.url)[0] + '/' + folder_name folder.name = folder_name print(folder.url, folder.name) folder.save() # 调用递归函数 self.getprante(folder_id=folder_id, folder_url=folder.url) data = { 'folder_id': folder_id, 'folder_name': folder_name, 'path': path, 'path_parent': path_parent, 'path_new': path_new, } return res(action='rename', data=data)
def delete(self, request): folder_id = request.GET.get('folder_id') # 文件夹数据库查询 folder = Folder.objects.filter(id=folder_id).first() if folder is None: raise Exception(u'文件夹不存在') # 目录地址 path = getpath('media', folder.url) # 查询该目录下的所有子文件 files = File.objects.filter(pfolder_id=folder_id) # 查询该目录下的所有子文件夹 folders = Folder.objects.filter(parent=folder_id) # 删除数据库中的子文件 for item in files: print('item1', item.id) item.delete() # 删除数据库中的所有子文件夹 for item in folders: print('item2', item.id) item.delete() # 数据库主目录删除 folder.delete() if folder.id: raise Exception(u'数据库文件夹删除失败') # 数据库信息删除完成,删除项目中的 print(path) folderdel(path) data = { 'path': path, # '文件夹id': folder.id, # '文件夹名': folder.name, } return res(data=data)
def __init__(self): err_log = getpath.getpath("logs","err_log") info_log = getpath.getpath("logs","info_log") if not os.path.isdir(err_log): os.mkdir(err_log) if not os.path.isdir(info_log): os.mkdir(info_log) time_name = time.strftime("%Y-%m-%d") self.err_log_time = os.path.join(err_log, time_name) self.info_log_time = os.path.join(info_log, time_name) if not os.path.isdir(self.err_log_time): os.mkdir(self.err_log_time) if not os.path.isdir(self.info_log_time): os.mkdir(self.info_log_time) self.th = logging.Formatter("%(levelname)s %(filename)s %(asctime)s %(lineno)s %(message)s") self.c = logging.StreamHandler() self.c.setFormatter(self.th)
def post(self, request): file_pid = request.POST.get('file_pid') # file_name = request.POST.get('file_name') file_ = request.FILES.get('file_') folder = Folder.objects.filter(id=file_pid).first() file = File.objects.filter(pfolder_id=file_pid, name=file_.name) if file.count() != 0: raise Exception(u'该文件已存在') if folder.id is None: raise ParamMissingException(u'父文件夹不存在') if file_.name is None: file_.name = u'新建文件' # 根目录 path_root = getpath('media', str(folder.url)) # 文件路径 path = os.path.join(path_root, file_.name) # 写入文件 f = open(path, 'w') f.close() # 文件大小 size = os.path.getsize(path) # 文件类型 file_type = os.path.splitext(path) file_type = file_type[-1] file_new = File.objects.create(name=file_.name, url=folder.url + '/' + file_.name, size=size, depth=folder.depth + 1, creater_id=1, pfolder_id=folder.id, filetype=file_type) if file_new.id is None: os.remove(path) data = { 'path_root': path_root, # 'file_': file_, 'file_id': file_new.id, 'file_name': file_new.name, } return res(action='add file', data=data)
def post(self, request): folder_name = request.POST.get('folder_name') folder_pid = request.POST.get('folder_pid') folder = Folder.objects.filter(id=folder_pid).first() # 文件根目录 path_root = getpath('media', folder.url) if '/' in folder_name: folder_name = folder_name.split('/')[-1] # 该文件是否在同级目录中重名 if folder_name in traversal(bool=False, path=path_root)[0]: raise Exception(u'该文件夹已存在!!!') if folder_name is None: folder_name = u'新建文件夹' # 文件夹路径 path = os.path.join(path_root, folder_name) try: # 添加数据 depth = folder.depth+1 Folder.objects.create(name=folder_name, parent_id=folder_pid, depth=depth, url=folder.url+'/'+folder_name, creater_id=1) # 创建文件夹 os.mkdir(path) except Exception: # 是否创建文件成功 if os.path.exists(path): os.remove(path) raise Exception(u'创建文件夹失败') # 测试数据 data = { 'code': 0, 'name': folder.name, 'url': folder.url, 'path_root': path_root, 'path': path, } return JsonResponse( data=data )
def runCase(): suite = unittest.TestSuite() tests = unittest.defaultTestLoader.discover(start_dir="testcase", pattern="web_test*") for x in tests: for i in x: suite.addTest(i) time_name = time.strftime("%Y-%m-%d") html_time_name = getpath.getpath("report", time_name) if not os.path.isdir(html_time_name): os.mkdir(html_time_name) # 时分秒 report_html = "report_" + time.strftime("%H-%M-%S") + "_.html" with open(os.path.join(html_time_name, report_html), "wb") as f: ht = HTMLTestRunner.HTMLTestRunner(f, title="谈欢的自动化测试报告") ht.run(suite)
#读取配置文件 import os import configparser from common import getpath path = getpath.getpath("config", "myconfig.ini") conf = configparser.ConfigParser() conf.read(path, encoding="utf8") url = conf.get("URL", "url") timeout = int(conf.get("TIMEOUT", "timeout")) timepl = float(conf.get("TIMEPL", "timepl")) sysurl = conf.get("URL", "sysurl")