def add(self, request_data, commit=True): if not request_data.files: return False, '没有上传文件!' file_upload = request_data.files['file'] if not file_upload: return False, '没有指定上传的文件!' file_name = file_upload.filename curr_app = current_app._get_current_object() file_path = curr_app.config.get("ATTACH_PATH_ROOT") # file_path = os.path.join('data', 'Astah') if not os.path.exists(file_path): os.mkdir(file_path) only_id = Utillity().get_nextval() new_file_name = Utillity.get_new_file_name(file_name, only_id) fname, ext_name = Utillity.split_ext_name(file_name) fname = fname.rstrip('.') file_upload.save(os.path.join(file_path, new_file_name)) file_url = os.path.join(file_path, new_file_name) file_url = Utillity.convert_url(curr_app.config.get('FILE_SRV_URL'), file_url) update_time = datetime.datetime.now() commit_time = update_time attach_info = { DSAttach.file_name.name: file_name, DSAttach.file_type.name: ext_name, DSAttach.file_url.name: file_url, DSAttach.update_time.name: update_time, DSAttach.commit_time.name: commit_time } attach = DSAttach(**attach_info) db.session.add(attach) if commit: db.session.commit() else: db.session.flush() return True, attach.to_dict()
def add(self, request_data, commit=True): if not request_data.files: return False, '没有上传文件!' file_upload = request_data.files['file'] proj_id = request_data.form.get('proj_id') spec_name = request_data.form.get('spec_name') spec_type = request_data.form.get('spec_type') spec_num = request_data.form.get('spec_num') spec_ver = str(request_data.form.get('spec_ver')) spec_id = request_data.form.get('spec_id') if request_data.form.get('replace') == 'true': replace = True else: replace = False if not file_upload: return False, '没有指定上传的文件!' file_name = file_upload.filename file_name = file_name.replace(' ', '').replace('(', '').replace(')', '') curr_app = current_app._get_current_object() file_path = curr_app.config.get("SPEC_PATH_TEMP") if not os.path.exists(file_path): os.mkdir(file_path) fname, ext_name = Utillity.split_ext_name(file_name) fname = fname.rstrip('.') if os.path.exists(os.path.join(file_path, file_name)): os.remove(os.path.join(file_path, file_name)) file_upload.save(os.path.join(file_path, file_name)) proj = db.session.query(Project).filter(Project.proj_id == proj_id).all() files = {'file': open(os.path.join(file_path, file_name), 'rb')} html_data = requests.post(curr_app.config.get('EXCEL_TO_HTM_URL'), files=files) if not html_data.status_code == 200: return False, 'EXCEL转换HTM调用失败' dd = json.loads(html_data.text) if dd.get("result") == "ok": html_url = dd.get("content") if not platform.system() == 'Windows': html_url = html_url.replace('\\', '/') html_url = html_url.lstrip('/') html_url = os.path.join(curr_app.config.get('SPEC_CHANGE_URL'), html_url) new_htm_url = os.path.join(curr_app.config.get('SPEC_PATH_ROOT'), proj[0].proj_name, spec_type, spec_ver, 'html') if not os.path.exists(new_htm_url): os.makedirs(new_htm_url) mv = 'mv %s %s' % (html_url, new_htm_url) os.system(mv) new_htm_url = self.convert_url(new_htm_url) # SpecSearch new_excel_url = os.path.join(curr_app.config.get('SPEC_PATH_ROOT'), proj[0].proj_name, spec_type, spec_ver, 'excel') file_url = os.path.join(file_path, file_name) if not os.path.exists(new_excel_url): os.makedirs(new_excel_url) cmd = 'cp -f %s %s' % (file_url, new_excel_url) os.system(cmd) pk_excel_url = os.path.join(new_excel_url, file_name) new_excel_url = self.convert_url(new_excel_url) update_time = datetime.datetime.now() # commit_time = update_time search_str = "[%s]-%s-%s" % (spec_type, fname, spec_name) data = {} inputs = db.session.query(Specification).filter(Specification.spec_id == spec_id).order_by( Specification.spec_id).all() search_obj = SpecSearch.instance() if not inputs: spec_info = {Specification.proj_id.name: proj_id, Specification.spec_name.name: spec_name, Specification.spec_file_name.name: file_name, Specification.spec_type.name: spec_type, Specification.spec_num.name: spec_num, Specification.search_str.name: search_str, } attach = Specification(**spec_info) db.session.add(attach) db.session.flush() ver_info = { SpecVersion.excel_url.name: os.path.join(new_excel_url, file_name), SpecVersion.html_url.name: os.path.join(new_htm_url, fname, fname+'.htm'), SpecVersion.spec_id.name: attach.spec_id, SpecVersion.spec_ver.name: spec_ver, SpecVersion.create_time.name: update_time, } attach2 = SpecVersion(**ver_info) db.session.add(attach2) data = attach2.to_dict() data['create_time'] = self.time_to_str(data.get('create_time')) db.session.commit() search_obj.add_document4excel(proj[0].proj_name, pk_excel_url, attach.spec_id, attach2.html_url) return True, data else: height_ver = self.search_height_version(spec_id, spec_ver) if not replace: version = db.session.query(SpecVersion).filter(SpecVersion.spec_id == spec_id, SpecVersion.spec_ver == spec_ver).order_by( SpecVersion.spec_id).all() if not version: ver_info = { SpecVersion.excel_url.name: os.path.join(new_excel_url, file_name), SpecVersion.html_url.name: os.path.join(new_htm_url, fname, fname+'.htm'), SpecVersion.spec_id.name: spec_id, SpecVersion.spec_ver.name: spec_ver, SpecVersion.create_time.name: update_time, } attach = SpecVersion(**ver_info) db.session.add(attach) data = attach.to_dict() data['create_time'] = self.time_to_str(data.get('create_time')) db.session.commit() if height_ver: search_obj.add_document4excel(proj[0].proj_name, pk_excel_url, spec_id, attach.html_url) return True, data else: return False, '已存在相同版本' else: if db.session.query(SpecVersion).filter(SpecVersion.spec_id == spec_id, SpecVersion.spec_ver == spec_ver).order_by( SpecVersion.create_time.desc()).all(): attach = db.session.query(SpecVersion).filter(SpecVersion.spec_id == spec_id, SpecVersion.spec_ver == spec_ver).order_by( SpecVersion.create_time.desc()).first() attach.excel_url = os.path.join(new_excel_url, file_name), attach.html_url = os.path.join(new_htm_url, fname, fname+'.htm'), attach.create_time = update_time, data = attach.to_dict() data['create_time'] = self.time_to_str(data.get('create_time')[0]) db.session.commit() if height_ver: search_obj.add_document4excel(proj[0].proj_name, pk_excel_url, spec_id, attach.html_url) return True, data else: return False, '没有此版本信息,无法更新' else: return False, dd.get("error")
def add_one_by_batch(self, curr_app, file_path, file_name, proj_id, spec_type, spec_ver): fname, ext_name = Utillity.split_ext_name(file_name) fname = fname.rstrip('.') spec_name = fname spec_num = re.search(r'(_[0-9]+_([0-9]+_)*)', fname, flags=0).group().strip("_") proj = db.session.query(Project).filter(Project.proj_id == proj_id).all() files = {'file': open(os.path.join(file_path, file_name), 'rb')} html_data = requests.post(curr_app.config.get('EXCEL_TO_HTM_URL'), files=files) if not html_data.status_code == 200: return False, file_name, 'EXCEL转换HTM调用失败' dd = json.loads(html_data.text) if dd.get("result") == "ok": html_url = dd.get("content") if not platform.system() == 'Windows': html_url = html_url.replace('\\', '/') html_url = html_url.lstrip('/') html_url = os.path.join(curr_app.config.get('SPEC_CHANGE_URL'), html_url) new_htm_url = os.path.join(curr_app.config.get('SPEC_PATH_ROOT'), proj[0].proj_name, spec_type, spec_ver, 'html') if not os.path.exists(new_htm_url): os.makedirs(new_htm_url) mv = 'mv %s %s' % (html_url, new_htm_url) os.system(mv) new_htm_url = self.convert_url(new_htm_url) new_excel_url = os.path.join(curr_app.config.get('SPEC_PATH_ROOT'), proj[0].proj_name, spec_type, spec_ver, 'excel') file_url = os.path.join(file_path, file_name) if not os.path.exists(new_excel_url): os.makedirs(new_excel_url) cmd = 'cp -f %s %s' % (file_url, new_excel_url) os.system(cmd) pk_excel_url = os.path.join(new_excel_url, file_name) new_excel_url = self.convert_url(new_excel_url) update_time = datetime.datetime.now() # commit_time = update_time search_str = "[%s]-%s-%s" % (spec_type, fname, spec_name) inputs = db.session.query(Specification).filter(Specification.proj_id == proj_id, Specification.spec_name == spec_name).order_by( Specification.spec_id).all() search_obj = SpecSearch.instance() if not inputs: spec_info = {Specification.proj_id.name: proj_id, Specification.spec_name.name: spec_name, Specification.spec_file_name.name: file_name, Specification.spec_type.name: spec_type, Specification.spec_num.name: spec_num, Specification.search_str.name: search_str, } attach = Specification(**spec_info) db.session.add(attach) db.session.flush() ver_info = { SpecVersion.excel_url.name: os.path.join(new_excel_url, file_name), SpecVersion.html_url.name: os.path.join(new_htm_url, fname, fname + '.htm'), SpecVersion.spec_id.name: attach.spec_id, SpecVersion.spec_ver.name: spec_ver, SpecVersion.create_time.name: update_time, } attach2 = SpecVersion(**ver_info) db.session.add(attach2) db.session.commit() search_obj.add_document4excel(proj[0].proj_name, pk_excel_url, attach.spec_id, attach2.html_url) return True, file_name, '' else: height_ver = self.search_height_version(inputs[0].spec_id, spec_ver) version = db.session.query(SpecVersion).filter(SpecVersion.spec_id == inputs[0].spec_id, SpecVersion.spec_ver == spec_ver).order_by( SpecVersion.spec_id).all() if not version: ver_info = { SpecVersion.excel_url.name: os.path.join(new_excel_url, file_name), SpecVersion.html_url.name: os.path.join(new_htm_url, fname, fname + '.htm'), SpecVersion.spec_id.name: inputs[0].spec_id, SpecVersion.spec_ver.name: spec_ver, SpecVersion.create_time.name: update_time, } attach = SpecVersion(**ver_info) db.session.add(attach) db.session.commit() if height_ver: search_obj.add_document4excel(proj[0].proj_name, pk_excel_url, inputs[0].spec_id, attach.html_url) return True, file_name, '' else: return False, file_name, '已存在相同版本' else: return False, file_name, 'EXCEL转换HTM调用失败'
def add(self, request_data): from app.ctrl.ctrl_ds_doc import CtrlDsDoc if not request_data.files: return False, '没有上传文件!' file_upload = request_data.files['file'] if not file_upload: return False, '没有指定上传的文件!' doc_id = request_data.form.get('doc_id') doc_data, error = CtrlDsDoc().ds_doc_exist(doc_id) if error: return False, error file_name = file_upload.filename curr_app = current_app._get_current_object() file_path = curr_app.config.get("ATTACH_PATH_ROOT") # file_path = os.path.join('data', 'Astah') if not os.path.exists(file_path): os.mkdir(file_path) fname, ext_name = Utillity.split_ext_name(file_name) # TODO@hcz: 所有都可以 # if not ext_name or ext_name.lower() not in (".asta", ".jude"): # return False, '文件扩展名不对,请上传".asta"或".jude文件"' fname = fname.rstrip('.') # 获取新的文件名称 only_id = Utillity().get_nextval() new_file_name = Utillity.get_new_file_name(file_name, only_id) file_upload.save(os.path.join(file_path, new_file_name)) file_url = os.path.join(file_path, new_file_name) file_url = Utillity.convert_url(curr_app.config.get('FILE_SRV_URL'), file_url) attach_id = request_data.form.get("attach_id") committer = request_data.form.get("committer") # 提交者' ver = request_data.form.get("ver") update_time = datetime.datetime.now() commit_time = update_time attach_info = { DSAttach.attach_id.name: attach_id, DSAttach.file_name.name: file_name, DSAttach.file_type.name: ext_name, DSAttach.file_url.name: file_url, DSAttach.update_time.name: update_time, DSAttach.committer.name: committer, DSAttach.ver.name: ver } commit_list = [] if attach_id: # 更新 log_dict, attach_info = CtrlDSAttach().update(attach_id, attach_info, commit=False) if log_dict: commit_list.append(log_dict) else: # 新增 # 添加附件 attach_info[DSAttach.commit_time.name] = commit_time attach_log = CtrlDSAttach().add2(attach_info, commit=False) attach_id = attach_log.get('key_id') attach_info[DSAttach.attach_id.name] = attach_id attach_info[DSAttach.commit_time.name] = self.time_to_str( commit_time) attach_info[DSAttach.update_time.name] = self.time_to_str( commit_time) astah_rel_info = { DSDocAstahRel.doc_id.name: doc_id, DSDocAstahRel.attach_id.name: attach_id } astah_rel_log = self.common_add(DSDocAstahRel, astah_rel_info, None, [], DSDocAstahRel.gid) commit_list.append(attach_log) commit_list.append(astah_rel_log) if commit_list: log_doc = CtrlDsDoc().update_ver(doc_id, committer) commit_list.append(log_doc) self.commit_log(commit_list, committer, update_time) db.session.commit() return True, attach_info