예제 #1
0
def store_applications():
    stored_key = REDIS_KEY + "-stored"
    redis_client = redis()
    if redis_client.exists(stored_key):
        return

    applications = models.JobApplication.objects.filter(
        events__email__message_id__isnull=False,
        events__name="sent",
        events__created_at__lt="2019-03-07").values("candidate_email",
                                                    "siret").distinct()
    candidates = {}
    for application in applications:
        email = application["candidate_email"]
        if email not in candidates:
            candidates[email] = []
        candidates[email].append(application["siret"])

    redis_client = redis()
    for email, sirets in candidates.items():
        key_name = REDIS_KEY + ":" + email
        redis_client.delete(key_name)
        redis_client.lpush(key_name, *sirets)

    redis_client.set(stored_key, 1)
예제 #2
0
 def events(cls, key, minscore, maxscore):
     return redis().zrangebyscore(
         cls.key(key),
         minscore,
         maxscore,
         withscores=True,
     )
예제 #3
0
 def add(cls, key, score=None):
     """
     Add an event for key. This event will be used to apply rate limits.
     """
     score = score or time()
     client = redis()
     client.zadd(cls.key(key), {score: score})
     client.expire(cls.key(key), cls.max_delay())
예제 #4
0
def iter_applications():
    redis_client = redis()
    for key in redis_client.keys(REDIS_KEY + ":*"):
        candidate_email = key.decode().split(":")[1]
        if is_blacklisted(candidate_email):
            continue
        sirets = [siret.decode() for siret in redis_client.lrange(key, 0, -1)]
        yield candidate_email, sirets
예제 #5
0
def main():
    store_applications()
    redis_client = redis()
    for candidate_email, sirets in iter_applications():
        print(candidate_email, sirets)
        send_email(candidate_email, sirets)
        key_name = REDIS_KEY + ":" + candidate_email
        redis_client.delete(key_name)
예제 #6
0
 def expire_in(self):
     """
     Return:
         ttl (None/float): the delay in seconds before the email is removed
         from the blacklist. None if the key is not blacklisted.
     """
     ttl = redis().pttl(self.key)
     if ttl == -2:
         return None
     return ttl / 1000
예제 #7
0
 def flush(cls, key, score=None):
     score = score if score is not None else time()
     redis().zremrangebyscore(cls.key(key), 0, score)
예제 #8
0
 def setUp(self):
     redis().flushall()
     super().setUp()
예제 #9
0
 def setup_test_environment(self, **kwargs):
     redis().flushall()
     return super().setup_test_environment(**kwargs)
예제 #10
0
 def __init__(self, email):
     self.key = key(email)
     reason = redis().get(self.key)
     self.reason = reason.decode() if reason is not None else None
예제 #11
0
def remove(email):
    redis().delete(key(email))
예제 #12
0
def add(email, reason='', timestamp=None):
    duration = expire_in(timestamp)
    if duration > 0:
        redis().set(key(email), reason.encode())
        redis().expire(key(email), duration)