def get_interfaces(): form = SearchForm(request.args) query = Interface.objects(is_deleted=False, is_template=False) # 简单搜索 if form.searchKey.data: search_key = form.searchKey.data query = query.filter( Q(name__contains=search_key) | Q(note__contains=search_key)) return build_order_by(query, form.orderBy.data), 200 # 高级搜索 if form.name.data: query = query.filter(name__contains=form.name.data) if form.note.data: query = query.filter(note__contains=form.note.data) if form.location.data: query = query.filter(location=form.location.data) if form.type.data: query = query.filter(type=form.type.data) if form.protocol.data: query = query.filter(protocol=form.protocol.data) if form.createUserId.data: query = query.filter(_create_user=form.createUserId.data) if form.parentEquipmentId.data: query = query.filter(_parent_equipment=form.parentEquipmentId.data) if form.parentDeviceId.data: query = query.filter(_parent_device=form.parentDeviceId.data) if form.createdAtStart.data: query = query.filter(created_at__gte=form.createdAtStart.data) if form.createdAtEnd.data: query = query.filter(created_at__lt=form.createdAtEnd.data) current_user.update(inc__search_num=1) return build_order_by(query, form.orderBy.data), 200
def get_child(oid): interface = Interface.objects(id=oid).count() if not interface: return jsonify(ok=True, msg='not found'), 404 child_cable = Cable.objects( Q(_start_interface=oid) | Q(_end_interface=oid)).count() return jsonify(ok=True, msg={'cable': child_cable})
def get_child(oid): device = Device.objects(id=oid).count() if not device: return jsonify(ok=True, msg='not found'), 404 child_interface = Interface.objects(_parent_device=oid).count() child_cable = Cable.objects(Q(_start_device=oid) | Q(_end_device=oid)).count() return jsonify(ok=True, msg={'interface': child_interface, 'cable': child_cable})
def load(self, cable): cable.name = self.name.data cable.signal_type = self.signalType.data cable.shield_type = self.shieldType.data cable.note = self.note.data cable.is_custom = self.isCustom.data cable.parameter_list = self.parameterList.data try: start_interface = Interface.objects().get( id=self.startInterfaceId.data) start_device = start_interface.parent_device start_equipment = start_interface.parent_equipment except Interface.DoesNotExist: raise FormLoadException('起始接口不存在,id: ' + str(self.startInterfaceId.data)) except Device.DoesNotExist: raise FormLoadException('起始接口所属的设备不存在,接口id: ' + str(self.startInterfaceId.data)) except Equipment.DoesNotExist: raise FormLoadException('起始接口所属设备的装置不存在,接口id: ' + str(self.startInterfaceId.data)) else: cable.start_interface = start_interface.id cable.start_device = start_device.id cable.start_equipment = start_equipment.id try: end_interface = Interface.objects().get( id=self.endInterfaceId.data) end_device = end_interface.parent_device end_equipment = end_interface.parent_equipment except Interface.DoesNotExist: raise FormLoadException('结束接口不存在,id: ' + str(self.endInterfaceId.data)) except Device.DoesNotExist: raise FormLoadException('结束接口所属的设备不存在,接口id: ' + str(self.endInterfaceId.data)) except Equipment.DoesNotExist: raise FormLoadException('结束接口所属设备的装置不存在,接口id: ' + str(self.endInterfaceId.data)) else: cable.end_interface = end_interface.id cable.end_device = end_device.id cable.end_equipment = end_equipment.id
def get_interfaces_child(): form = ObjectIdListForm(request.args) ids = form.ids.data interface = Interface.objects(id__in=ids).count() if not interface: return jsonify(ok=True, msg='not found'), 404 child_cable = Cable.objects( Q(_start_interface__in=ids) | Q(_end_interface__in=ids)).count() return jsonify(ok=True, msg={'cable': child_cable})
def get_equipments_child(): form = ObjectIdListForm(request.args) ids = form.ids.data equipment = Equipment.objects(id__in=ids).count() if not equipment: return jsonify(ok=True, msg='not found'), 404 child_device = Device.objects(_parent_equipment__in=ids).count() child_interface = Interface.objects(_parent_equipment__in=ids).count() child_cable = Cable.objects(Q(_start_equipment__in=ids) | Q(_end_equipment__in=ids)).count() return jsonify(ok=True, msg={'device': child_device, 'interface': child_interface, 'cable': child_cable})
def recover_cables_recycles(ids): cables = Cable.objects(Q(id__in=ids) & Q(is_template=False)) cables.update(is_deleted=False) # 恢复线缆自身 interfaces = [ interface.id for interface in cables.values_list( '_start_interface', '_end_interface')[0] ] devices = [ device.id for device in cables.values_list('_start_device', '_end_interface')[0] ] equipments = [ equipment.id for equipment in cables.values_list( '_start_equipment', '_end_interface')[0] ] Interface.objects(id__in=interfaces).update(is_deleted=False) Device.objects(id__in=devices).update(is_deleted=False) Equipment.objects(id__in=equipments).update(is_deleted=False) return jsonify(ok=True, msg='success')
def get_users_meta(): data = Interface.objects(is_deleted=False, is_template=False).fields(id=1, name=1, _parent_device=1).all() meta = [{ 'id': str(item.id), 'name': item.name, 'parentDeviceId': str(item._parent_device.id) if item._parent_device else None, } for item in data] return jsonify(ok=True, data=meta)
def recover_interfaces_recycles(ids): interfaces = Interface.objects(Q(id__in=ids) & Q(is_template=False)) interfaces.update(is_deleted=False) # 恢复接口自身 devices = [ device.id for device in interfaces.values_list('_parent_device') ] equipments = [ equipment.id for equipment in interfaces.values_list('_parent_equipment') ] Device.objects(id__in=devices).update(is_deleted=False) Equipment.objects(id__in=equipments).update(is_deleted=False) return jsonify(ok=True, msg='success')
def recover_interfaces__templates_recycles(ids): Interface.objects(Q(id__in=ids) & Q(is_template=True)).update(is_deleted=False) return jsonify(ok=True, msg='success')
def delete_equipment(oid, response): Device.objects(_parent_equipment=oid).update(is_deleted=True) Interface.objects(_parent_equipment=oid).update(is_deleted=True) Cable.objects(Q(_start_equipment=oid) | Q(_end_equipment=oid)).update(is_deleted=True) return response
def delete_device(oid, response): Interface.objects(_parent_device=oid).update(is_deleted=True) Cable.objects(Q(_start_device=oid) | Q(_end_device=oid)).update(is_deleted=True) return response
def update_device(new, old, response): if new.parent_equipment.id != old.parent_equipment.id: Interface.objects(_parent_device=new).update(_parent_equipment=new.parent_equipment) Cable.objects(_start_device=new).update(_start_equipment=new.parent_equipment) Cable.objects(_end_device=new).update(_end_equipment=new.parent_equipment) return response