def viewFileWithVersion(version, file): latest_file = FileDao.getFileById(file.id) #print(latest_file) fileList = HistoryDao.getHistory(latest_file.id, version) fileList.sort(key=attrgetter("version"), reverse=True) dmp = dmp_module.diff_match_patch() latest_file_json = json.dumps(latest_file.__dict__) for file in fileList: pathces = dmp.patch_fromText(file.patch_content) latest_file_json, _ = dmp.patch_apply(pathces, latest_file_json) #print("version" + str(file.version-1)+"file content: "+str(latest_file_json)) print(latest_file_json) file = MdFile("temp") file.__dict__ = json.loads(latest_file_json) file.version = version return file, latest_file
def getHistoryFileHandler(**kwargs): user = kwargs['userInfo'] fid = int(request.form['id']) version = int(request.form['version']) file = MdFile(id=fid) historyFile, currentFile = History.viewFileWithVersion(version=version, file=file) return { 'currentFile': currentFile.__dict__, 'historyFile': historyFile.__dict__ }
def addFileHandler(**kwargs): user = kwargs['userInfo'] file = MdFile(name=request.form['name'], ownerId=user.userId, date=request.form['date'], permission=request.form['permission'], status="OK", content=request.form['content'], version=1) FileDao.addFile(file) return None
def getFileById(fid): fid = int(fid) dbDriver = getDBDriver() try: res = dbDriver.getResult( "select id,name,date,ownerId,permission,status,content,version from FILE where id=?", [fid]) res = res[0] file = MdFile(id=res[0], name=res[1], date=res[2], ownerId=res[3], permission=res[4], status=res[5], content=res[6], version=res[7]) dbDriver.closeDB() return file except Exception as e: raise e
def getFileListByUid(uid): uid = int(uid) dbDriver = getDBDriver() try: resList = dbDriver.getResult( "select id,name,date,ownerId,permission,status,version from FILE where ownerId=?", [uid]) resultFile = list() for res in resList: file = MdFile(id=res[0], name=res[1], date=res[2], ownerId=res[3], permission=res[4], status=res[5], version=res[6]) resultFile.append(file) dbDriver.closeDB() return resultFile except Exception as e: raise e
def restoreFileHandler(**kwargs): user = kwargs['userInfo'] fid = int(request.form['id']) version = int(request.form['version']) History.restoreFile(version, MdFile(id=fid)) return None
ownerId=res[3], permission=res[4], status=res[5], content=res[6], version=res[7]) dbDriver.closeDB() return file except Exception as e: raise e if __name__ == '__main__': file = MdFile(name='hello1', date='1231233', ownerId=1, permission='private', status='OK', content='abcdefghijksdfsdflmnopqrstuvwxyz') addFile(file) fileList = getFileListByUid(1) for file in fileList: print(file) print("") file = getFileById(1) print(file) file.content += "gjwgjwgjw" updateFile(file) file = getFileById(1) print(file)
file.version = version return file, latest_file ''' 恢复到文件到指定版本 参数:version:指定的版本 file:文件对象,只要有id这一个有效参数即可 返回值: file:要求版本的文件 注:回复文件也会导致文件的版本+1 ''' def restoreFile(version, file): target_file, old_file = viewFileWithVersion(version, file) target_file.version = old_file.version + 1 saveFileWithPatch(target_file) if __name__ == '__main__': file = MdFile(name="1.md", content="1", version=0, id=1) FileDao.addFile(file) for i in range(10): file.content += str(i) file.version += 1 print("version: " + str(i) + " content" + str(file)) saveFileWithPatch(file) restoreFile(1, file) print("fin")