📄 devicemanagement.py
字号:
page.errors.append('More serials than devices') else: count = 0 for i in range(0,len(serialList)): if len(serialList[i]) and (stateList[i] == CN_ARRIVED): # Update serial fields = {'serial': serialList[i], 'active': 'true'} try: updateFields(fields,'device','deviceid',deviceidList[i]) count += 1 # Send deviceactive event event = DeviceEvent('deviceActive') event.state = DeviceEvent.STATE_START event.deviceid = deviceidList[i] event.addVar('serial',serialList[i]) arrivaldate = form[CN_YEAR] + '-' +\ form[CN_MONTH] + '-' + \ form[CN_DAY] event.addVar('arrivaldate',arrivaldate) event.addVar('source','registered') event.post() # Send devicestate event event = DeviceEvent('deviceState','deviceOnShelf') event.state = DeviceEvent.STATE_START event.deviceid = deviceidList[i] event.addVar('username',username) event.post() except psycopg.IntegrityError: page.errors.append("A device with the serial '%s' " %\ serialList[i] +\ "already exists in the database") if count == 1: page.messages.append('Registered ' + str(count) + ' device') else: page.messages.append('Registered ' + str(count) + ' devices') else: page.errors.append('All devices from this order are already registered')def deleteOrder(deviceorderid,page): # Check if any of the devices are registered amountsql = "SELECT count(*) FROM device WHERE " +\ "deviceorderid='%s'" % (deviceorderid,) +\ "AND active=true" result = executeSQL(amountsql,fetch=True) arrived = result[0][0] if arrived > 0: # Only delete inactive devices and close order sql = "DELETE FROM device WHERE deviceorderid='%s' " % \ (deviceorderid,) +\ "AND active=false" executeSQL(sql) fields = {'arrived': mx.DateTime.now().strftime(TIMESTAMP)} updateFields(fields,'deviceorder','deviceorderid',deviceorderid) page.messages.append('Deleted inactive devices and closed order') else: # Delete entire order sql = "DELETE FROM deviceorder WHERE deviceorderid='%s' " \ % (deviceorderid,) executeSQL(sql) page.messages.append('Order deleted') return pagedef updateOrder(req,formData): form = req.form deviceorderid = form['deviceorderid'] # Add or delete devices amountsql = "SELECT count(*) FROM device WHERE " +\ "deviceorderid='%s'" % (deviceorderid,) +\ "AND device.active=false" result = executeSQL(amountsql,fetch=True) oldamount = result[0][0] amount = int(formData[CN_AMOUNT]) if amount > oldamount: # Make new devices fields = {} fields['productid'] = formData[CN_PRODUCT] fields['deviceorderid'] = deviceorderid fields['active'] = 'false' for i in range(0,amount-oldamount): insertFields(fields,'device') elif amount < oldamount: # Delete devices, first get all deviceids from this order deleteAmount = oldamount-amount sql = "SELECT deviceid FROM device WHERE " +\ "deviceorderid='%s' " % (deviceorderid,) +\ "AND device.active=false" result = executeSQL(sql,fetch=True) deviceidList = [] i = 1 for row in result: deviceidList.append(row[0]) if i == deleteAmount: break i += 1 sql = "DELETE FROM device WHERE " first = True for id in deviceidList: if not first: sql += "OR " sql += "deviceid='%d' " % (id,) first = False executeSQL(sql) # Update deviceorder orderdate = formData[CN_YEAR] + '-' + formData[CN_MONTH] + '-' + \ formData[CN_DAY] fields = {} now = mx.DateTime.now() if amount == 0: # Close the order fields['arrived'] = now.strftime(TIMESTAMP) fields['productid'] = formData[CN_PRODUCT] fields['updatedby'] = req.session['user'].login lastupdated = str(now.year) + '-' + str(now.month) + '-' + \ str(now.day) fields['lastupdated'] = lastupdated fields['orgid'] = formData[CN_ORG] orderdate = formData[CN_YEAR] + '-' + formData[CN_MONTH] + '-' + \ formData[CN_DAY] fields['ordered'] = orderdate if formData.has_key(CN_RETAILER): fields['retailer'] = formData[CN_RETAILER] if formData.has_key(CN_ORDERNUMBER): fields['ordernumber'] = formData[CN_ORDERNUMBER] if formData.has_key(CN_COMMENT): fields['comment'] = formData[CN_COMMENT] updateFields(fields,'deviceorder','deviceorderid',deviceorderid)def registerOrder(req,formData): # Create deviceorder fields = {} fields['productid'] = formData[CN_PRODUCT] fields['username'] = req.session['user'].login fields['orgid'] = formData[CN_ORG] orderdate = formData[CN_YEAR] + '-' + formData[CN_MONTH] + '-' + \ formData[CN_DAY] fields['ordered'] = orderdate if formData.has_key(CN_RETAILER): fields['retailer'] = formData[CN_RETAILER] if formData.has_key(CN_ORDERNUMBER): fields['ordernumber'] = formData[CN_ORDERNUMBER] if formData.has_key(CN_COMMENT): fields['comment'] = formData[CN_COMMENT] sequence = ('deviceorderid','public.deviceorder_deviceorderid_seq') orderid = insertFields(fields,'deviceorder',sequence) # Create devices fields = {} fields['productid'] = formData[CN_PRODUCT] fields['deviceorderid'] = orderid fields['active'] = 'false' for i in range(0,int(formData[CN_AMOUNT])): insertFields(fields,'device')def registerError(errorType,unitList,comment,username): result = [] olderrorType = errorType for unitid in unitList: if errorType == CN_DEVICE: where = "deviceid='%s'" % (unitid,) box = nav.db.manage.Netbox.getAll(where) module = nav.db.manage.Module.getAll(where) if box: errorType = CN_BOX unitid = box[0].netboxid elif module: errorType = CN_MODULE unitid = module[0].moduleid event = DeviceEvent('deviceNotice','deviceError') event.addVar('comment',comment) event.addVar('username',username) if errorType == CN_MODULE: module = nav.db.manage.Module(unitid) event.subid = module.moduleid event.netboxid = module.netbox.netboxid event.deviceid = module.device.deviceid event.addVar('unittype','module') event.addVar('roomid',module.netbox.room.roomid) event.addVar('locationid',module.netbox.room.location.locationid) result.append('Registered error on module ' + str(module.module) +\ ' in box ' + module.netbox.sysname) elif errorType == CN_BOX: netbox = nav.db.manage.Netbox(unitid) event.subid = None event.netboxid = netbox.netboxid event.deviceid = netbox.device.deviceid event.addVar('unittype','netbox') event.addVar('roomid',netbox.room.roomid) event.addVar('locationid',netbox.room.location.locationid) result.append('Registered error on box ' + netbox.sysname) elif errorType == CN_ROOM: room = nav.db.manage.Room(unitid) event.subid = None event.netboxid = None event.deviceid = None event.addVar('unittype','room') event.addVar('roomid',room.roomid) event.addVar('locationid',room.location.locationid) result.append('Registered error on room ' + room.roomid) elif errorType == CN_LOCATION: location = nav.db.manage.Location(unitid) event.subid = None event.netboxid = None event.deviceid = None event.addVar('unittype','location') event.addVar('locationid',location.locationid) result.append('Registered error on location '+location.locationid) elif errorType == CN_DEVICE: event.subid = None event.netboxid = None event.deviceid = unitid event.addVar('unittype','device') device = nav.db.manage.Device(unitid) serial = device.serial result.append("Registered error on device with serial '" +\ serial + "'") event.post() errorType = olderrorType return resultdef executeSQL(sql,fetch=False): connection = nav.db.getConnection('devicemanagement','manage') database = connection.cursor() # Clean sql string database.execute(sql) result = None if fetch: result = database.fetchall() return resultdef updateFields(fields,table,idfield,updateid): sql = 'UPDATE ' + table + ' SET ' first = True for field,value in fields.items(): if not first: sql += ',' sql += field + "='" + value + "'" first = False sql += ' WHERE ' + idfield + "='" + str(updateid) + "'" executeSQL(sql)def insertFields(fields,table,sequence=None): # Add a new entry using the dict fields which contain # key,value pairs # Sequence is a tuple (idfield,sequencename). If given, get # the nextval from sequence and set the idfield to this value nextid = None if sequence: idfield,seq = sequence sql = "SELECT nextval('%s')" % (seq,) result = executeSQL(sql,fetch=True) nextid = str(result[0][0]) fields[idfield] = nextid sql = 'INSERT INTO ' + table + ' (' first = True for field,value in fields.items(): if not first: sql += ',' sql += field first = False sql += ') VALUES (' first = True for field,value in fields.items(): if not first: sql += ',' sql += "'" + value + "'" first = False sql += ')' executeSQL(sql) return nextiddef makeMainMenu(selected): menu = [] i = 0 for item in MAIN_MENU: path = item[1] if i == selected: path = None menu.append([item[0],path,item[2]]) i += 1 return menudef makeHistory(form,historyType,unitList): boxList = [] for unitid in unitList: if historyType == CN_MODULE: boxList.append(ModuleHistoryBox(unitid)) elif historyType == CN_BOX: boxList.append(NetboxHistoryBox(unitid)) elif historyType == CN_ROOM: boxList.append(RoomHistoryBox(unitid)) elif historyType == CN_LOCATION: boxList.append(LocationHistoryBox(unitid)) elif historyType == CN_DEVICE: where = "deviceid='%s'" % (unitid,) box = nav.db.manage.Netbox.getAll(where=where) module = nav.db.manage.Module.getAll(where=where) if box: box = box[0] boxList.append(NetboxHistoryBox(box.netboxid)) elif module: module = module[0] boxList.append(ModuleHistoryBox(module.modulei
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -