如何在Django项目中将请求传递给Celery任务参数?

时间:2018-04-26 10:19:23

标签: django task celery

我正在使用wkhtmltopdf与Django生成PDF文件并通过电子邮件发送给某人。以下是我的观点:

class ChallanEmail(AtomicMixin, View, LoginRequiredMixin):
    template = "europarts/challan/email_template.html"

    def get(self, request, **kwargs):
        challan = Challan.objects.get(pk=kwargs['pk'])
        ref_no = challan.ref_no
        date = challan.created
        recipient = challan.recipient
        address = challan.recipient_address
        challan_rows = ChallanRow.objects.filter(challan=challan)

        context = {
            "ref_no": ref_no,
            "date": date,
            "recipient": recipient,
            "address": address,
            "challan_rows": challan_rows,
        }

        response = PDFTemplateResponse(
            request=request,
            template=self.template,
            filename='challan_email.pdf',
            context=context,
            show_content_in_browser=True,
            cmd_options={'margin-top': 10,
                         'zoom': 1,
                         'viewport-size': '1366 x 513',
                         'javascript-delay': 1000,
                         'no-stop-slow-scripts': True},
        )

        file_path = os.path.join(settings.BASE_DIR, settings.MEDIA_ROOT, 'challan_email.pdf')
        with open(file_path, 'wb') as f:
            f.write(response.rendered_content)

        subject = 'From Design Ace Limited'
        body = self.request.GET.get('email_body', '')
        from_email = 'Sorower Hossain <sorower@europartsbd.com>'
        to = ['{}'.format(self.request.GET.get('to_address'))]
        attachment = os.path.join(settings.MEDIA_ROOT, 'challan_email.pdf')

        send_email(subject, body, from_email, to, attachment)

        return HttpResponseRedirect(reverse('europarts:challan_details', args=(kwargs['pk'],)))

正如您可以看到的那样,我正在使用send_email向电子邮件发送包含任务(celery)的电子邮件。但是,最耗时的过程是PDF创建过程。以下是执行此操作的代码部分:

response = PDFTemplateResponse(
    request=request,
    template=self.template,
    filename='challan_email.pdf',
    context=context,
    show_content_in_browser=True,
    cmd_options={'margin-top': 10,
                 'zoom': 1,
                 'viewport-size': '1366 x 513',
                 'javascript-delay': 1000,
                 'no-stop-slow-scripts': True},
    )

我遇到的问题是每当我尝试将所有参数传递给芹菜任务时,它都会显示错误,因为request不可序列化。如何将请求转移到我的芹菜任务并节省PDF生成的时间?这项任务花了差不多三秒钟。

1 个答案:

答案 0 :(得分:1)

如果您仅使用PDFTemplateResponse生成pdf文件并通过电子邮件发送(而非http响应),则可以在芹菜任务中模拟它。

您的芹菜任务,在<app_name>/tasks.py档案中:

from django.test.client import RequestFactory
# other necessary imports
...

@shared_task
def create_pdf_and_send_email(template, context, email_body, to_address):
    request = RequestFactory().get('') # any valid url
    challan_rows = ChallanRow.objects.filter(pk__in=context['challan_rows'])
    context['challan_rows'] = challan_rows
    response = PDFTemplateResponse(
            request=request,
            template=template,
            filename='challan_email.pdf',
            context=context,
            show_content_in_browser=True,
            cmd_options={'margin-top': 10,
                         'zoom': 1,
                         'viewport-size': '1366 x 513',
                         'javascript-delay': 1000,
                         'no-stop-slow-scripts': True},
        )

        file_path = os.path.join(settings.BASE_DIR, settings.MEDIA_ROOT, 'challan_email.pdf')
        with open(file_path, 'wb') as f:
            f.write(response.rendered_content)

        subject = 'From Design Ace Limited'
        body = email_body
        from_email = 'Sorower Hossain <sorower@europartsbd.com>'
        to = ['{}'.format(to_address)]
        attachment = os.path.join(settings.MEDIA_ROOT, 'challan_email.pdf')
        send_email(subject, body, from_email, to, attachment)

您重构的get方法:

from .tasks import create_pdf_and_send_email
...
def get(self, request, **kwargs):
    challan = Challan.objects.get(pk=kwargs['pk'])
    ref_no = challan.ref_no
    date = challan.created
    recipient = challan.recipient
    address = challan.recipient_address
    challan_rows = ChallanRow.objects.filter(challan=challan).values_list('pk', flat=True)

    context = {
        "ref_no": ref_no,
        "date": date,
        "recipient": recipient,
        "address": address,
        "challan_rows": challan_rows,
    }

    email_body = self.request.GET.get('email_body', '')
    to_address = self.request.GET.get('to_address')
    create_pdf_and_send_email.delay(template, context, email_body, to_address)
    return HttpResponseRedirect(reverse('europarts:challan_details', args=(kwargs['pk'],)))

我希望它应该有效。

如果序列化错误出现任何问题(address变量中的context字段可能存在问题),请在address = challan.recipient_address方法中将address = challan.recipient_address.pk更改为get。 然后,在芹菜任务中,使用给定的pk检索地址(例如address = YourAddressModel.objects.get(pk=context['address']),并将context['address']替换为context['address'] = address