def get(self, id): """Retrieve the CAPTCHA with the given ID. If it's expired already, this will return None. A typical web application will need to new() a CAPTCHA when generating an html page, then get() it later when its images or sounds must be rendered. """ redis_captcha_dict_obj = RedisCaptchaDict.get_instance('0') self.storedInstances = redis_captcha_dict_obj.captcha_dict return self.storedInstances.get(id)
def new(self, cls, *args, **kwargs): """Create a new instance of our assigned BaseCaptcha subclass, passing it any extra arguments we're given. This stores the result for later testing. """ self.clean() inst = cls(*args, **kwargs) redis_captcha_dict_obj = RedisCaptchaDict.get_instance('0') self.storedInstances = redis_captcha_dict_obj.captcha_dict self.storedInstances[inst.id] = inst redis_captcha_dict_obj.put() return inst
def clean(self): """Removed expired tests""" expiredIds = [] now = time.time() redis_captcha_dict_obj = RedisCaptchaDict.get_instance('0') self.storedInstances = redis_captcha_dict_obj.captcha_dict for inst in self.storedInstances.itervalues(): if inst.creationTime + self.lifetime < now: expiredIds.append(inst.id) for id in expiredIds: del self.storedInstances[id] redis_captcha_dict_obj.put()
def test(self, id, solutions): """Test the given list of solutions against the BaseCaptcha instance created earlier with the given id. Returns True if the test passed, False on failure. In either case, the test is invalidated. Returns False in the case of an invalid id. """ self.clean() redis_captcha_dict_obj = RedisCaptchaDict.get_instance('0') self.storedInstances = redis_captcha_dict_obj.captcha_dict inst = self.storedInstances.get(id) if not inst: return False result = inst.testSolutions(solutions) return result