为相似的作业重复使用相同的模板

时间:2017-01-31 14:11:57

标签: django filter cron django-templates

我要求客户向我们提供一份文件清单,例如['doc1', 'doc2', 'doc3', 'doc4', 'doc5']。从这时起,每隔24小时进行5天,我们会向客户发送一封电子邮件,通知他缺少文件:

Hello 'Mr or Ms',

We inform you that the following documents are missing to complete the request: 

'list of missing documents'

我认为我可以在该列表上使用过滤器并应用cron作业,每24小时重新应用该过滤器,持续5天。过滤器只会对提供的文档和客户提供的其余文档进行排序。

例如,如果客户从第一天起提供了所有文件,那么我们会向他发送一条消息,告诉他所有文件都已成功发送,并且他可以继续下一步。

在我编程以来的某些日子里,我需要你的帮助来解决我的问题。我认为这不是更好的解决方案。一开始我想创建五个不同的模板,包括slu send-remaining-documents-day-onesend-remaining-documents-day-two,...,但很明显,我猜不出来。

任何人都可以提出比这更好的解决方案吗?

1 个答案:

答案 0 :(得分:0)

您只能创建2个模板(不确定是否需要html,因为您可以发送基本的txt),如:some_docs_missing.txtall_docs_found.txt并编写一个python脚本来检查您拥有的文档和内容你需要那么你拿不同,看看你使用的模板。

这是一个片段:

from django.template.loader import render_to_string

# Let's say you have the 2 list of documents called: needed_docs and found_docs
missing_docs = list(set(needed_docs) - set(found_docs))
if missing_docs == []:  # All good
    message = render_to_string('all_docs_found.txt')
else:
    message = render_to_string('some_docs_missing.txt',
                               context={'missing_docs': missing_docs})

# Here send the message
相关问题