使用 Django 发送电子邮件的服务

我正在尝试创建一项服务,用于在过期日期时间少于一周的情况下发送电子邮件。我想做的是只发送一次而不重复。我不知道我应该做什么


楷模:


class License(models.Model):


    PACKAGE_CHOISES = (

    ('Production', 'Production'),

    ('Evaluation', 'Evaluation'),

    )

    LICENSE_CHOISES = (

        ('js', 'Javascript_sdk'),

        ('ios', 'Ios_sdk'),

        ('android', 'Android_sdk'),

    )


    client = models.ForeignKey('Client', on_delete=models.CASCADE)

    package = models.CharField(max_length=15, choices=PACKAGE_CHOISES, blank=True, null=True)

    license_type = models.CharField(max_length=15, choices=LICENSE_CHOISES, blank=True, null=True)

    created_datetime = models.DateTimeField(auto_now=True)

    expiration_datetime = models.DateTimeField(default=get_default_license_expiration)

这是我正在尝试构建的服务:


def process_licenses():


    client = Client.objects.all()

    licesens = License.objects.all()

    clients_name = []

    hoje = datetime.today() - timedelta(days=7)

    for date in licesens:

        if hoje >= date.expiration_datetime:

            clients_name.append(date.client)


    for name in clients_name:

        if name in client:

            email = EmailMessage(

                'Test',

                'hi',

                settings.EMAIL_HOST_USER,

                [name.admin_poc],


            )

            email.fail_silently=False

            email.send()

        

    time.sleep(30)


狐的传说
浏览 99回答 1
1回答

翻阅古今

在许可证模型中添加此列is_send = models.BooleanField(default=False, blank=True, null=True)for date in licesens:     if hoje >= date.expiration_datetime and is_send is False:         clients_name.append(date.client)         license_instance = License.objects.get(pk=date.pk)         license_instance.is_send = True         license_instance.save()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python