def test_16_lantern_accounts(self): # Check that we can detect lantern accounts acc1 = MonitorUKAccount() acc1.email = "*****@*****.**" acc1.save() acc2 = MonitorUKAccount() acc2.email = "*****@*****.**" acc2.lantern_email = "*****@*****.**" acc2.lantern_api_key = "123456789" acc2.save() acc3 = MonitorUKAccount() acc3.email = "*****@*****.**" acc3.lantern_email = "*****@*****.**" acc3.lantern_api_key = "987654321" acc3.save(blocking=True) count = 0 gen = MonitorUKAccount.list_lantern_enabled() for acc in gen: if acc.email == "*****@*****.**": assert acc.lantern_api_key == "123456789" assert acc.lantern_email == "*****@*****.**" count += 1 elif acc.email == "*****@*****.**": assert acc.lantern_api_key == "987654321" assert acc.lantern_email == "*****@*****.**" count += 10 else: count += 100 assert count == 11
def make_new_jobs(cls): """ Send new requests to lantern, and record the jobs that are created. This method looks for accounts who have Lantern credentials, looks for PublicAPC records belonging to those accounts which could benefit from lookup in Lantern, :return: """ dao = PublicAPC() gen = MonitorUKAccount.list_lantern_enabled(keepalive="1h") for acc in gen: gen2 = dao.list_by_owner(acc.id) identifiers = [] for apc in gen2: if LanternApi._needs_lantern_data(apc): idents = LanternApi._get_identifiers(apc) if idents is not None: identifiers.append(idents) apc.lantern_lookup = dates.now() apc.save() # if there are no identifiers, no need to do any more if len(identifiers) == 0: continue # now check the user's quota lc = client.Lantern(api_key=acc.lantern_api_key) quota = lc.get_quota(acc.lantern_email) available = quota.get("data", {}).get("available", 0) if available == 0: continue if len(identifiers) > available: identifiers = identifiers[:available] batches = LanternApi._batch(identifiers) for batch in batches: resp = lc.create_job(acc.lantern_email, "monitor-uk", batch) if resp.get("status") == "success": job_id = resp.get("data", {}).get("job") lj = LanternJob() lj.job_id = job_id lj.account = acc.id lj.status = "active" lj.save()