def get_choice(self): resp = proxy.get('/v1/support-plane-types/') if resp.status_code != 200: raise BackendServiceError('API has something wrong.') items = resp.json()['items'] if not items: raise BackendServiceError('API has something wrong.') result = [] for x in items: tmp_dict = tuple(dict(key=items[x], value=x).values()) result.append(tmp_dict) return result
def delete_serial_pn_from_boundeditems(self, material): """从入库的表单中获取某飞机的含有件号和序号的时控件或时寿件, 从数据库中获取它的boundedId,然后从绑定状态中删除该件和序号, 更新到期列表。 """ if self.model.instoreCategory == DisassembleSTORE and material.category in [ '时控件', '时寿件' ]: if material.partNumber and material.serialNum and material.planeNum: bounded_status = get_boundedid_by_plane_pn_serialnum( material.planeNum, material.partNumber, material.serialNum, material.category) if not bounded_status: raise ValueError('%s飞机的件号:%s、序号:%s,没有找到相应的绑定状态' % (material.planeNum, material.partNumber, material.serialNum)) # 判断该飞机号、件号、类型下绑定到飞机上的序号有几个,如果为1,增加一个绑定状态再删除 tmp = get_plane_infos_by_pn(material.partNumber, material.planeNum, material.category) if not tmp: raise ValueError('查询绑定状态失败,请联系管理员') if tmp <= 1: # 客户端增加时寿件或时控件绑定状态的处理逻辑部分 if not add_boundedstatus_by_mxp_mxtype_plane_num( bounded_status[1], bounded_status[2], material.planeNum): raise ValueError('添加绑定状态失败,请联系管理员') resp = proxy.delete( None, '/v1/mxp-binding/?boundedid=' + bounded_status[0]) if resp.status_code >= 400: raise BackendServiceError('调用api出错')
def get_choice(self): doc_type = [] url = '/v1/doc-type/' resp = proxy.get(url) if resp.status_code != 200: raise BackendServiceError('API has something wrong.') items = resp.json().get('items') if not items: raise BackendServiceError('API has something wrong.') for item in items: # if item.get('expanded'): # doc_type.append((item.get('id'), item.get('id'))) # else: # doc_type.append(("temporary", item.get('id'))) doc_type.append((item.get('id'), item.get('id'))) return doc_type
def index_view(self): _api_url = '/v1/mxp/' resp = self._api_proxy.get(_api_url) if resp.status_code != 200: raise BackendServiceError('Please contact the service maintainer.') # TODO: 根据用户授权的方案模型,全部显示 for item in resp.json().get('items'): if item.get('mxpName') == 'y5b': model_name = item.get('modelName') onlineTime = item.get('onlineTime') offlineTime = item.get('offlineTime') boundedCount = item.get('boundedCount') typeManufacturer = item.get('typeManufacturer') self._template_args.update({ 'modelName': model_name, 'onlineTime': timestamp_to_date(onlineTime), 'offlineTime': timestamp_to_date(offlineTime), 'boundedCount': boundedCount, 'typeManufacturer': typeManufacturer, 'not_on_top_search': True, }) return super(MxpBaseView, self).index_view()
def add_doctype(self): url = '/v1/doc-type/' resp = proxy.get(url) if resp.status_code != 200: flash('暂时无法更新支持的文档资料列表', 'error') return redirect(self.get_url('.index')) # 原来支持的 support_doctypes = resp.json()['items'] or [] # 现在更新的 doc_type = request.form.get('doc_type', None) if not doc_type: flash('文档资料类别不能为空。', 'error') return redirect(self.get_url('.index')) supported = set([item['id'] for item in support_doctypes]) update = set(doc_type.split(',')) if update < supported: flash('文档库资料类别只能新增或重置', 'error') return redirect(self.get_url('.index')) try: new_add = update - supported for item in new_add: doc_type = dict(id=item, value=item) proxy.create(doc_type, url) except: raise BackendServiceError( 'Update support doctype occured failure.') flash('文档资料类别更新成功。', 'success') return redirect(self.get_url('.index'))
def on_model_delete(self, model): file_str = model.fileResourceUrl files_list = json.loads(file_str) for file_obj in files_list: try: file_remove(key=file_obj.get('key'), name=file_obj.get('name')) except: raise BackendServiceError('RabbitMq has something wrong')
def generate_data(self, data): info = {"docType": "维修方案", "fileName": data.filename} url = "/v1/file-access/upload" resp = proxy.create(info, url) if resp.status_code != 200: raise BackendServiceError("file-access api has someting wrong") token = resp.json().get("token") key = resp.json().get("key") save_key = resp.json().get("saveKey") return {'data': data, 'token': token, 'key': key, 'save_key': save_key}
def _delete_file(self, filename): if self.object_data: object_data = set([json.dumps(f) for f in json.loads(self.object_data)]) old_file = set([json.dumps(f) for f in self.old_file]) for del_file in list(object_data - old_file): if del_file: item = json.loads(del_file) try: file_remove(key=item.get('key'), name=item.get('name')) except Exception as ex: logging.warn(ex) raise BackendServiceError("Please contact the service maintainer.")
def _delete_file(self, filename): if self.formData and self.object_data: object_data = set([json.dumps(f) for f in self.object_data]) old_file = set([json.dumps(f) for f in self.old_file]) del_files = list(object_data - old_file) for del_file in del_files: item = json.loads(del_file) file_name = item.get('name') key = item.get('key') try: file_remove(key=key, name=file_name) except: raise BackendServiceError("Please contact the service maintainer.")
def details_view(self): ao_id = request.args.get('id', '') inst_model = Storage.query.filter_by(id=ao_id).first() if not inst_model or not ao_id: raise BackendServiceError('') column = self.get_edit_details_view_colums(inst_model.instoreCategory) self._template_args.update({ 'table_columns': self.get_readonly_table(column), 'table_datas': self.get_table_data_from_db(inst_model), 'extra_table_js': 'js/inline_table_details.js' }) return super(_StorageView, self).details_view()
def details_view(self): ao_id = request.args.get('id', '') inst_model = PutOutStoreModel.query.filter_by(id=ao_id).first() if not ao_id or not inst_model: raise BackendServiceError('') column = self.get_edit_details_view_colums(inst_model.outStoreCategory) self._template_args.update({ 'table_columns': self.get_readonly_table(column[0]), 'table_datas': self.get_table_data_from_db(inst_model), 'extra_table_js': 'js/inline_table_details.js' }) self.creat_detail_view(exclude_list=column[2]) return super(_PutOutStoreView, self).details_view()
def reviewed_change(self, **kwargs): super(ApproveCanEdit, self).update_allowed_change(**kwargs) model = self.model file_type = model.fileResourceType file_str = model.fileResourceUrl if file_str: file_list = json.loads(file_str) res = [] for file_val in file_list: try: tmp = updateFileStr(file_val, file_type) res.append(tmp.get('update')) file_move(source=tmp.get('source'), target=tmp.get('target')) except: raise BackendServiceError('RabbitMq has something wrong') model.fileResourceUrl = json.dumps(res)
def _save_file(self, data, file_name): if file_name: info = self.get_info(file_name) url = "/v1/file-access/upload" resp = proxy.create(info, url) if resp.status_code != 200: raise BackendServiceError("file-access api has someting wrong") token = resp.json().get("token") key = resp.json().get("key") save_key = resp.json().get("saveKey") ret, info = put_data(token, key, bytes(data.stream.read())) if info.status_code != 200: logging.warn(token) logging.warn(save_key) logging.warn(ret) logging.warn(info) raise QiNiuServiceError("qiniu service have something wrong") self.store.append({'name': file_name, 'key': save_key}) return file_name
def reset_doctype(self): url = '/v1/doc-type/' resp = proxy.get(url) if resp.status_code != 200: flash('暂时无法更新支持的文档资料列表', 'error') return redirect(self.get_url('.index')) try: support_doctypes = resp.json()['items'] for item in support_doctypes: # 只有扩展的需要删除 if 'expanded' in item and item['expanded']: doc_type = dict(id=item['id'], value=item['id']) proxy.delete_with_data(doc_type, url, item['etag']) except: raise BackendServiceError( 'Update support doctype occured failure.') flash('文档库资料类别重置完毕。', 'success') return redirect(self.get_url('.index'))
def on_model_change(self, form, model, is_created): super(_TrainigMaterialView, self).on_model_change(form, model, is_created) if not is_created: res = [] new_type = form.trainFileResourceType.data old_type = form.trainFileResourceType.object_data file_str = model.trainFileResourceUrl if new_type != old_type and file_str: files_list = json.loads(file_str) for file_obj in files_list: try: tmp = updateFileStr(file_obj, new_type, unrelateDoc=True) res.append(tmp.get('update')) file_move(source=tmp.get('source'), target=tmp.get('target')) except: raise BackendServiceError( 'RabbitMq has something wrong') model.trainFileResourceUrl = json.dumps(res) self.session.commit()
def update_to_bounded_status(self): bounded_items = [] coll = current_app.mongodb type_dict = { '时控件': coll.time_control_unit_y5b, '时寿件': coll.life_control_unit_y5b, } mxType = { '时控件': 'timecontrol', '时寿件': 'lifecontrol', } for material in self.model.assembleApplicationList: if material.category in [ '时控件', '时寿件' ] and material.planeNum and material.serialNum: tmp = AirMaterialStorageList.query.filter( AirMaterialStorageList.partNumber == material.partNumber, AirMaterialStorageList.serialNum == material.serialNum).first() if tmp: raise ValueError('件号为%s,序号为%s的航材没有完成装机出库' % (material.partNumber, material.serialNum)) for material in self.model.assembleApplicationList: if material.category in [ '时控件', '时寿件' ] and material.planeNum and material.serialNum: # 获取维修方案编号 mx_p = type_dict[material.category].find_one( {'id': material.partNumber}) if not mx_p: raise ValueError('没有件号"%s"的维修方案' % (material.partNumber)) mxId = mx_p['id'] # 客户端增加时寿件或时控件绑定状态的处理逻辑部分 resp = proxy.create( { 'mxId': mxId, 'mxType': mxType[material.category], 'planeId': material.planeNum, }, '/v1/mxp-binding/duplicate') if resp.status_code == 200: bounded_id = resp.json()['boundedId'] bounded_item = { 'engineTime': convert_hh_mm_to_float(material.engineTime) if material.engineTime else 0, 'trace': True, 'serialNumber': material.serialNum, 'completeDate': str_to_time(material.lastCheckDate), 'nf': None, 'tc': None, 'ng': None, 'ellapsedTimes': material.flightTimes if material.flightTimes else 0, 'retiredIndex': None, 'ellapsedHours': convert_hh_mm_to_float(material.flyTime) if material.flyTime else 0, 'boundedId': bounded_id, } bounded_items.append(bounded_item) if len(bounded_items) > 0: resp = proxy.update(bounded_items, None, '/v1/mxp-binding/update?batch=1') if resp.status_code != 200: raise BackendServiceError('更新绑定状态失败')