我正在将 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'],)))
您可以看到,我正在使用celery
发送电子邮件(send_email
(。但是,最耗时的过程是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生成时间?这项任务需要将近三秒钟。
如果仅使用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'],)))
我希望它应该有效。
如果序列化错误存在任何问题(context
变量中的address
字段可能可能存在(,请将address = challan.recipient_address
更改为get
方法中的address = challan.recipient_address.pk
。然后,在芹菜任务内,用给定的PK检索地址(例如address = YourAddressModel.objects.get(pk=context['address'])
,然后用context['address'] = address
替换context['address']
。