def removeSlotById(slotId): chargingSlot = ChargingSlot.query.filter( ChargingSlot.slotId == slotId).first() if chargingSlot == None: return 'No such charging slot' db_session.delete(chargingSlot) db_session.commit() return 'Success'
def findOrRegisterUser(userId,name): user = User.query.filter(User.userId == userId).first() if user == None: # Create a new Entry in the database for the user user = User(userId=userId, name=name, authToken='', tokenExpiryDate=datetime.min) db_session.add(user) db_session.commit() return user
def setUsernameAndPassword(user,username,password): # Make sure the username is unique clashes = User.query.filter(User.username == username).all() if not (clashes == [] or clashes == [user]): raise CustomError('username already taken', 600) user.username = username user.passwordHash = generate_password_hash(password) db_session.commit() return user
def removeSlotByDevice(deviceId, userId): chargingSlots = ChargingSlot.query.filter( ChargingSlot.deviceId == deviceId, ChargingSlot.userId == userId).all() if chargingSlots == []: return 'Nothing to delete for this device' for chargingSlot in chargingSlots: db_session.delete(chargingSlot) db_session.commit() return 'Success'
def addUnregistered(userId, deviceName, consumption, plugInTime, timeToCharge): chargingSlot = ChargingSlot(userId=userId, deviceName=deviceName, consumption=consumption, plugInTime=plugInTime, timeToCharge=timeToCharge) db_session.add(chargingSlot) db_session.commit() return chargingSlot
def get(self): userId = getUserId() userDevices = UserDevice.query.filter( UserDevice.userId == userId).all() #get all the devices for item in userDevices: # Delete associated charging slots ChargingSlotMethods.removeSlotByDevice(item.deviceId, userId) db_session.delete(item) db_session.commit() return "Devices deleted successfully"
def logUserIn(user, authToken): # Set how long the token is valid for tokenValidityDuration = timedelta(hours=12) tokenExpiryDate = datetime.now() + tokenValidityDuration # Update the user record in the database user.authToken = authToken user.tokenExpiryDate = tokenExpiryDate db_session.commit() return user
def get(self, deviceName): userId = getUserId() userDevice = UserDevice.query.filter( UserDevice.userId == userId, UserDevice.deviceName == deviceName).first() if userDevice != None: #check if the user device exists ChargingSlotMethods.removeSlotByDevice( userDevice.deviceId, userId) #delete any associated charging slots db_session.delete(userDevice) #then delete device db_session.commit() return "Device deleted successfully!" return "Device not found in the database!"
def post(self): userId = getUserId() #get the parameters from the form deviceName = request.form['deviceName'] consumption = request.form['consumption'] timeToCharge = request.form['timeToCharge'] userDevice = UserDevice.query.filter( UserDevice.userId == userId, UserDevice.deviceName == deviceName).first() if userDevice != None: return "used" userDevices = UserDevice(userId=userId, deviceName=deviceName, consumption=consumption, timeToCharge=timeToCharge) db_session.add(userDevices) db_session.commit() return "success"
def cleanUp(): # Log the time we execute the function slotCleanUp.content.append( datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M')) # Get all the charging slots allSlots = ChargingSlot.query.all() # For each slot check it's still optimal and update if not for slot in allSlots: # Currently always looks ahead 24 hours. result = schedulingMethods.bestTimeSlot(24, slot.consumption, slot.timeToCharge) optPlugInTime = datetime.strptime(result['data'][0]['plugInTime'], '%Y-%m-%dT%H:%MZ') # Check if optimal time is later than current optimal time if optPlugInTime > slot.plugInTime: slotCleanUp.content.append('Slot ' + str(slot.slotId) + ' updated from ' + str(slot.plugInTime) + ' to ' + str(optPlugInTime)) # Update slot to reflect new time slot.plugInTime = optPlugInTime db_session.commit()
def logUserOut(user): user.tokenExpiryDate = datetime.min db_session.commit() return user
def removePassword(user): user.passwordHash = '' db_session.commit()