(days=1, subject="")
return
Send digest emails to users
Send digest emails to users
Send digest emails to users
def send_digests(days=1, subject=""):
    '''
    Send digest emails to users
    '''

    mapper = {1: models.Profile.DAILY_DIGEST,
              7: models.Profile.WEEKLY_DIGEST,
              30: models.Profile.MONTHLY_DIGEST}

    # Get posts made within the given time range.
    trange = util.now() - timedelta(days=days)

    posts = Post.objects.filter(lastedit_date__gt=trange, is_toplevel=True).order_by('-lastedit_date')

    if not posts:
        logger.info(f'No new posts found in the last {days} days.')
        return

    # Total number of recipients allowed per batch,
    # AWS has limit of 50
    batch_size = 40

    # Get users with the appropriate digest preference.
    pref = mapper.get(days, models.Profile.DAILY_DIGEST)
    context = dict(subject=subject, posts=posts)
    users = models.User.objects.filter(profile__digest_prefs=pref)
    emails = users.values_list('email', flat=True)

    # Iterate through recipients and send emails in batches.
    for idx in range(0, len(emails), batch_size):
        # Get the next set of emails
        end = idx + batch_size
        rec_list = emails[idx:end]
        send_email(template_name="messages/digest.html", extra_context=context, recipient_list=rec_list)

    return
def send_digests ( days = 1 , subject = "" ) : mapper = { 1 : models . Profile . DAILY_DIGEST , 7 : models . Profile . WEEKLY_DIGEST , 30 : models . Profile . MONTHLY_DIGEST } # Get posts made within the given time range. trange = util . now ( ) - timedelta ( days = days ) posts = Post . objects . filter ( lastedit_date__gt = trange , is_toplevel = True ) . order_by ( '-lastedit_date' ) if not posts : logger . info ( f'No new posts found in the last {days} days.' ) return # Total number of recipients allowed per batch, # AWS has limit of 50 batch_size = 40 # Get users with the appropriate digest preference. pref = mapper . get ( days , models . Profile . DAILY_DIGEST ) context = dict ( subject = subject , posts = posts ) users = models . User . objects . filter ( profile__digest_prefs = pref ) emails = users . values_list ( 'email' , flat = True ) # Iterate through recipients and send emails in batches. for idx in range ( 0 , len ( emails ) , batch_size ) : # Get the next set of emails end = idx + batch_size rec_list = emails [ idx : end ] send_email ( template_name = "messages/digest.html" , extra_context = context , recipient_list = rec_list ) return