1

[希望我正确地为这篇文章命名]

我有一个(有点)“关注”推特的事情。用户可以关注company创建对象的配置文件follower对象。

class Follower(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    profile = models.ForeignKey(UserProfile)
    company = models.ForeignKey(Company)
    verified = models.BooleanField(default=False)
    from_user = models.BooleanField(default=False)

...

class Company(models.Model):
    owner = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL)
    name = models.CharField(max_length=200)
    ... and more fields that aren't relevant

我想做的是在 5 个新关注者之后向公司资料所有者发送更新电子邮件。'你有 5 个新的追随者!'。

截至目前,每当他们有新的追随者时,我都会向所有者发送一封电子邮件。有点多,我知道。

我猜我需要创建一个关注者列表,发送它,然后删除它以准备 5 个新关注者?我真的不知道该怎么做。非常感谢任何帮助或建议。

看法:

@login_required
# this is creating a follower. Maybe I shouldn't send the email through this?
def follow(request, id):
    company = Company.objects.get(id=id)
    profile = get_object_or_404(UserProfile, user__username=request.user.username)
    try:
        follower = Follower.objects.get(profile=profile, company=company)
        if not follower.verified:
            follower.verified = True
            follower.save() 
        messages.success(request, 'Now following %s\''%company.name)
        mes = Message(subject="New Follower", sender=profile, recipient=company.owner)
        mes.body = render_to_string('emails/email-message/new_follower.html', RequestContext(request, {
        'sender': profile,
        'receiver': company.owner,
        }))
    except ObjectDoesNotExist:
        messages.error(request, 'Failed to follow.')
4

1 回答 1

3

每次特定公司的关注者数量变为 5 的倍数时发送电子邮件,如下所示:

   if not (Follower.objects.filter(company=company).count() % 5):
        #send the email
于 2012-09-11T19:17:45.000 回答