def deRegisterUser(self, userName): #(long userID):    
        
        ''' To do:
                Remove the entries for the userID from the DB
                Remember to deregister from the jabber server as well apart from this. 
            '''
        try:
            u = User.get(User.username==userName)
            sensorObjects = SensorUserRel.select().where(SensorUserRel.user==u)
            for i in sensorObjects:
                i.delete_instance()
            queryObjects = Query.select().where(Query.username==u)
            for i in queryObjects:
                i.delete_instance()
            u.delete_instance()
        except User.DoesNotExist:
            print 'User ' + str(userName) + ' does not exist. Returning delete ACK anyway.'
        except:
            print 'Unknown error; returning!'
            return
        
        print str(userName) + " deleted successfully!"
        self.msgHandler.send_message(mto=str(self.rMessage['from']).split("/")[0], msubject="De-Registration Successful", mbody="Thank you!")

        return
 def registerUser(self, msgObject, userName): #(long userID,long password, string[] sensorsPresent): #msg contains userid+capabilities 
     ''' To do:
         Parse the incoming message and store the capabilities of a particular user in the DB
     '''
     print "Inside RegisterUser"
     foundFlag = False
     try:
         u = User.get(User.username==userName)
         foundFlag = True
     except User.DoesNotExist:
         if str(msgObject['ActivityRecognition'])=='present':
             activity = True
         else:
             activity = False
         if str(msgObject['DownloadAllowed'])=='yes':
             download = True
         else:
             download = False
         if 'Location' in msgObject:
             lAr = msgObject['Location']
             location = str(lAr[0])
             latitude = str(lAr[1])
             longitude = str(lAr[2])
         else:
             location = 'Hardcoded'
             latitude = 0.0
             longitude = 0.0
         u = User(username=str(userName), RegistrationDate=datetime.datetime.now(), ActivityRecognition=activity, DownloadAllowed=download, Location=location, Latitude=latitude, Longitude=longitude)
         u.save()
     #print "User Saved"
     
     '''Update location anyways, if the key is present '''
     if 'Location' in msgObject:
         lAr = msgObject['Location']
         location = str(lAr[0])
         latitude = str(lAr[1])
         longitude = str(lAr[2])
         u.Longitude = longitude
         u.Latitude = latitude
         u.Location = location
         u.save()
             
     
     if foundFlag:
         #User already exists. Should handle it differently.
         #Means it could be a updation message. Delete all his previous Sensor Relation entries and fill new ones for now.
         print "User already existed! Deleting previous relation entries!"
         surtoD = SensorUserRel.select().where(SensorUserRel.user==u)
         for s in surtoD:
             s.delete_instance()
                 
     
     sensorQueue = []
     numSensor = eval(str(msgObject['noSensors']))
     for i in range(1, numSensor+1):
         ''' TODO: Check for fields to be empty before parsing them with eval! '''
         sName = "sensor" + str(i)
         try:
             s = Sensor.get(Sensor.SensorType==str(msgObject[sName][0]), Sensor.maxRange==eval(str(msgObject[sName][1])), Sensor.minDelay==eval(str(msgObject[sName][2])), Sensor.power==eval(str(msgObject[sName][3])), Sensor.resolution==eval(str(msgObject[sName][4])))
         except Sensor.DoesNotExist:
             s = Sensor(name="temp", SensorType=str(msgObject[sName][0]), maxRange=eval(str(msgObject[sName][1])), minDelay = eval(str(msgObject[sName][2])), power = eval(str(msgObject[sName][3])), resolution=eval(str(msgObject[sName][4])))
             s.save()
         sensorQueue += [s]
         
         
     for s in sensorQueue:
         sur = SensorUserRel(user=u, sensor=s)
         sur.save()     
             
     print "Registered" + str(userName) + " Successfully!"
     
     self.msgHandler.send_message(mto=str(self.rMessage['from']).split("/")[0], msubject="Registration Successful!", mbody="Thank you! Registration/Updation Successful!")
     
     return