def _findContext(self, msg, errorOnMissing=True): """Find and return the context matching the msg - throw an exception on error""" ctxId = msg.getContextValue(CONTEXT_USER_CONTEXT, None) ctx = None if ctxId: #Note that the helper we call looks for ID *and* name ctx = UserContext.findContext(ctxId) if not ctx and errorOnMissing: raise ProcessingExcept("Error: No context with id/name = %s"%(ctxId,)) else: return ctx
def makeContext(self, msg): ctxName = msg.getContextValue(CONTEXT_USER_CONTEXT, None) ctx = UserContext.findContext(ctxName) hasContext = msg.getResult() if hasContext and ctx: return self.makeResponse(msg, True, CONFIRM_ACT) elif hasContext and not ctx: #Look by ID and name (we're expecting name) dt = datetime.now().isoformat() ctx = UserContext(name=ctxName) ctx.setPrefValue("ContextCreated", dt) ctx.setPrefValue("ContextUpdated", dt) ctx.save() return self.makeResponse(msg, True, CONFIRM_ACT) elif not hasContext and ctx: # Disallow deleting contexts from here return self.makeResponse(msg, False, DISCONFIRM_ACT) else: return self.makeResponse(msg, False, CONFIRM_ACT)
def testReadWriteRetrieveUsers(self): u1 = UserData.onLogin("user1", "User One", "*****@*****.**", "testing") u2 = UserData.onLogin("user2", "User Two", "*****@*****.**", "testing") self.assertNotEquals(u1.getId(), u2.getId()) ctx = UserContext( "WithUsers", [u1.getId(), u2.getId()], {"u1Name": u1.getUserName(), "u2Name": u2.getUserName()} ) self.assertReadWrite(ctx) #If read/write succeeded, check the indexes self.assertEquals([ctx], UserContext.objects(users=u1.getId())) self.assertEquals([ctx], UserContext.objects(users=u2.getId())) self.assertEquals([], UserContext.objects(users='notAPerson')) self.assertEquals([ctx], UserContext.objects(name='WithUsers')) self.assertEquals([], UserContext.objects(name='notAContext')) idList = sorted([u1.getId(), u2.getId()]) #Test users changes that shouldn't affect state copy = UserContext.read(ctx.getId()) self.assertSerialEquals(ctx, copy) ctx.addUser(u1.getId()) ctx.removeUser("user3") self.assertSerialEquals(ctx, copy) self.assertEquals(idList, copy.getUsers()) #Check user DB read users = ctx.readDBUsers() self.assertEquals(idList, sorted([u.getId() for u in users])) #What about empty users (via removal and then when hitting the DB) ctx.removeUser(u1.getId()) ctx.removeUser(u2.getId()) self.assertEquals([], ctx.getUsers()) self.assertEquals([], ctx.readDBUsers())
def testReadWriteFull(self): ctx = UserContext( "Full", ["user1", "user2"], {"Pref1": "Super", "Pref2": "Duper"} ) self.assertEquals("Full", ctx.getName()) self.assertEquals(["Pref1", "Pref2"], ctx.getPrefKeys()) self.assertReadWrite(ctx) #Get self.assertEquals("Super", ctx.getPrefValue("Pref1")) #Get missing self.assertIsNone(ctx.getPrefValue("NOPE")) self.assertEquals("MISSING", ctx.getPrefValue("StillNope", "MISSING")) #Pop missing self.assertIsNone(ctx.popPrefValue("NOPE")) self.assertEquals("MISSING", ctx.popPrefValue("StillNope", "MISSING")) #Pop and then re-pop now missing self.assertEquals("Duper", ctx.popPrefValue("Pref2", "WHAT?")) self.assertIsNone(ctx.popPrefValue("Pref2")) self.assertEquals("MISSING", ctx.popPrefValue("Pref2", "MISSING")) #Invasive state check ctx.setPrefValue("Pref1", "NewSuper") self.assertEquals({"Pref1": "NewSuper"}, ctx._prefs) self.assertEquals(["Pref1"], ctx.getPrefKeys())
def testReadWriteNoUsers(self): ctx = UserContext("NoUsers", [], {"Pref1": "Super"}) self.assertEquals("NoUsers", ctx.getName()) self.assertReadWrite(ctx)
def testReadWriteNoPrefs(self): ctx = UserContext("NoPrefs", ["user1"]) self.assertEquals("NoPrefs", ctx.getName()) self.assertReadWrite(ctx)
def testReadWriteSuperBlank(self): ctx = UserContext("SuperBlank") self.assertEquals("SuperBlank", ctx.getName()) self.assertReadWrite(ctx)
def testReadWriteRetrieveUsers(self): u1 = UserData.onLogin("user1", "User One", "*****@*****.**", "testing") u2 = UserData.onLogin("user2", "User Two", "*****@*****.**", "testing") self.assertNotEquals(u1.getId(), u2.getId()) ctx = UserContext("WithUsers", [u1.getId(), u2.getId()], { "u1Name": u1.getUserName(), "u2Name": u2.getUserName() }) self.assertReadWrite(ctx) #If read/write succeeded, check the indexes self.assertEquals([ctx], UserContext.objects(users=u1.getId())) self.assertEquals([ctx], UserContext.objects(users=u2.getId())) self.assertEquals([], UserContext.objects(users='notAPerson')) self.assertEquals([ctx], UserContext.objects(name='WithUsers')) self.assertEquals([], UserContext.objects(name='notAContext')) idList = sorted([u1.getId(), u2.getId()]) #Test users changes that shouldn't affect state copy = UserContext.read(ctx.getId()) self.assertSerialEquals(ctx, copy) ctx.addUser(u1.getId()) ctx.removeUser("user3") self.assertSerialEquals(ctx, copy) self.assertEquals(idList, copy.getUsers()) #Check user DB read users = ctx.readDBUsers() self.assertEquals(idList, sorted([u.getId() for u in users])) #What about empty users (via removal and then when hitting the DB) ctx.removeUser(u1.getId()) ctx.removeUser(u2.getId()) self.assertEquals([], ctx.getUsers()) self.assertEquals([], ctx.readDBUsers())
def testReadWriteFull(self): ctx = UserContext("Full", ["user1", "user2"], { "Pref1": "Super", "Pref2": "Duper" }) self.assertEquals("Full", ctx.getName()) self.assertEquals(["Pref1", "Pref2"], ctx.getPrefKeys()) self.assertReadWrite(ctx) #Get self.assertEquals("Super", ctx.getPrefValue("Pref1")) #Get missing self.assertIsNone(ctx.getPrefValue("NOPE")) self.assertEquals("MISSING", ctx.getPrefValue("StillNope", "MISSING")) #Pop missing self.assertIsNone(ctx.popPrefValue("NOPE")) self.assertEquals("MISSING", ctx.popPrefValue("StillNope", "MISSING")) #Pop and then re-pop now missing self.assertEquals("Duper", ctx.popPrefValue("Pref2", "WHAT?")) self.assertIsNone(ctx.popPrefValue("Pref2")) self.assertEquals("MISSING", ctx.popPrefValue("Pref2", "MISSING")) #Invasive state check ctx.setPrefValue("Pref1", "NewSuper") self.assertEquals({"Pref1": "NewSuper"}, ctx._prefs) self.assertEquals(["Pref1"], ctx.getPrefKeys())
def echo(self, msg): if msg.getSpeechAct() != "experiment": #Not our hack - just echo back resp = self.makeResponse(msg, None, INFORM_ACT) resp.setVerb("HOLLA BACK") return resp #Special, hacky case: create an experiment context dt = datetime.now().isoformat() #Look by ID and name (we're expecting name) ctx = UserContext.findContext("DemoExperiment") if not ctx: ctx = UserContext(name="DemoExperiment") ctx.setPrefValue("ContextCreated", dt) ctx.setPrefValue("ContextUpdated", dt) #Add all users (and set an experimental context variable for them) for user in UserData.objects(): user.setPrefValue(ctx.getName(), "UserAdded", dt) user.save() ctx.addUser(user.getId()) ctx.save() resp = self.makeResponse(msg, None, INFORM_ACT) resp.setVerb("HOLLA BACK") resp.setSpeechAct(resp.getSpeechAct() + " => Created context " + ctx.getId()) return resp