【问题标题】:Re-use the same template for a similar job为相似的工作重复使用相同的模板
【发布时间】:2017-02-01 12:19:41
【问题描述】:

我要求客户向我们提供一份文件清单,例如['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 作业以在 5 天内每 24 小时重新应用该过滤器。过滤器只会对提供的文档和客户端提供的剩余文档进行排序。

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

因为我刚开始编程几天,我需要你的帮助来解决我的问题。我认为这不是更好的解决方案。一开始我想用 slugs send-remaining-documents-day-one, send-remaining-documents-day-two, ... 创建五个不同的模板,但很明显,我猜不建议这样做。

谁能提出比这更好的解决方案?

【问题讨论】:

    标签: django filter cron django-templates


    【解决方案1】:

    您只能创建 2 个模板(不确定是否需要 html,因为您可以发送基本的 txt),例如:some_docs_missing.txtall_docs_found.txt,然后编写一个 python 脚本来检查您拥有哪些文件以及您需要什么你拿差价看看你用的是什么模板。

    这是一个sn-p:

    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
    

    【讨论】:

    • 您会使用 cron 作业来自动化该过程吗?
    • 我没有了解整个过程,但 cronjob 似乎是不错的选择,别忘了在第五天禁用它。
    猜你喜欢
    • 1970-01-01
    • 2022-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    相关资源
    最近更新 更多