def test_create_code_creates_unique(self): """Should be an entry in Unique to ensure unique code.""" user = User.create(email='*****@*****.**') user.put() response = self.testapp.post_json( '/api/codes', {'organization_id': 'triton', 'program_label': 'triton'}, headers=self.login_headers(user) ) pc = ProjectCohort.query().fetch(1) unique = Unique.query().fetch(1) self.assertIsNotNone(pc) self.assertIsNotNone(unique)
def queue_org_welcome_back(templates): """After joining a program for the first time, welcome them.""" # Orgs signing up for a program generate a new ProjectCohort every year. # Look for recently created ones, then exclude orgs that only have one PC # in this program. yesterday = datetime.datetime.now() - datetime.timedelta(hours=24) # Which programs have a template? to_prompt = programs_with_template(templates, ORG_WELCOME_BACK_SUFFIX) tasks = [] for program in to_prompt: # Can't use .get() from DatastoreModel because of the >= query query = ProjectCohort.query( ProjectCohort.created >= yesterday, ProjectCohort.deleted == False, ProjectCohort.program_label == program['label'], ) # Gather the project ids of all applicable project cohorts. By tracking # unique project ids, we only email each org in this program once. project_ids = set() for pc in query.iter(projection=[ProjectCohort.project_id]): if pc.project_id not in project_ids: if ProjectCohort.count(project_id=pc.project_id) > 1: project_ids.add(pc.project_id) # Now send an email for each org that qualifies. for project_id in project_ids: # Then this org has done this program before, they're returning # so welcome them back. url = '/task/email_project/{}/{}'.format( project_id, get_slug(program['label'], ORG_WELCOME_BACK_SUFFIX)) task = taskqueue.add(url=url) tasks.append(task) return tasks