def save(self, *args, **kwargs): self.uid = self.uid or util.get_uuid(8) self.max_upload_size = self.max_upload_size or self.set_upload_size() self.name = self.name or self.user.first_name or self.user.email.split( "@")[0] self.date_joined = self.date_joined or util.now() self.last_login = self.last_login or util.now() # - timedelta(days=1) self.token = self.token or util.get_uuid(16) super(Profile, self).save(*args, **kwargs)
def recently_joined(self): """ User that joined X amount of days are considered new. """ recent = (util.now() - self.date_joined).days > settings.RECENTLY_JOINED_DAYS return recent
def send_digests(days=1, subject=""): ''' Send digest emails to users ''' prefs_map = { 1: models.Profile.DAILY_DIGEST, 7: models.Profile.WEEKLY_DIGEST, 30: models.Profile.MONTHLY_DIGEST } # Send digests for posts within the last x days. delta = util.now() - timedelta(days=days) # Get users with the appropriate digest prefs. digest_prefs = prefs_map.get(days, models.Profile.DAILY_DIGEST) users = models.User.objects.filter(profile__digest_prefs=digest_prefs) # Fetch posts within the last x amount of days posts = Post.objects.filter(lastedit_date__gt=delta) email_template = loader.get_template("messages/digest.html") context = dict(subject=subject, posts=posts) # Queue and send digest emails. emails = users.values_list('email', flat=True) send_email(template_name=email_template, extra_context=context, recipient_list=emails) return
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_watched_tags(): ''' Get watched tags from the last week and send email to users. ''' # Get users with watched tags. users = models.User.objects.exclude(profile__watched_tags='') # Get posts made in the last week delta = util.now() - timedelta(days=7) posts = Post.objects.filter(lastedit_date__lte=delta) tags = Tag.objects.filter(post__lastedit_date__gt=delta, post__type__in=Post.TOP_LEVEL) tags = tags.values_list('name', flat=True) # Fetch users with the watched tags #TODO: this will probably need to be done in a for loop. users = users.filter(watched_tags__icontains=tags) print(users) return
def save(self, *args, **kwargs): self.uid = self.uid or util.get_uuid(10) self.sent_date = self.sent_date or util.now() super(Message, self).save(**kwargs)
def save(self, *args, **kwargs): self.sent_date = self.sent_date or util.now() super(Message, self).save(**kwargs)
def save(self, *args, **kwargs): self.date = self.date or util.now() super(UserLog, self).save(*args, **kwargs)
def is_new(self): is_new = (util.now() - self.date_joined).days > 30 return is_new
def save(self, *args, **kwargs): self.html = self.html or mistune.markdown(self.body) self.uid = self.uid or util.get_uuid(10) self.sent_date = self.sent_date or util.now() super(Message, self).save(**kwargs)