好的,帶來一些背景關系,我有很大的特定功能,它作為多行程在后臺運行。又名第二個程序(可能稍后更多)。
這樣做的原因是它會重定向到一個 html 頁面,說請求成功。畢竟,在他的資料請求通過并轉換資料之前,客戶端不會在加載頁面上等待超過 7 分鐘。是的,這是我的應用程式所做的必不可少的。在你問為什么需要這么長時間之前,這與 JSON 資料的結構有關。
AKA 簡而言之,執行第一個請求以獲取資料點串列,并分別為每個資料點執行第二個請求,因為詳細資訊的請求 url 位于該資料點內。
在此后臺行程的最后一步,郵件將通過發送網格發送,根據檔案是否太大,它將發送帶有特殊“本地檔案”鏈接的附件以供下載。郵件的正文會根據情況有不同的內容,但您必須從中洗掉的主要內容是檔案是否作為附件,您將始終擁有本地檔案“下載位置”。
import os
import base64
from datetime import datetime
from flask import Flask
import Classified.background_script.converter.GIPOD_Converter as data_conv
from flask import send_file, send_from_directory, safe_join, abort, render_template, jsonify
from Classified import app
from celery import Celery
import random
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import (
Mail, Attachment, FileContent, FileName,
FileType, Disposition, ContentId)
from sendgrid import SendGridAPIClient
import Classified.background_script.sendgrid as mail_delivery
send_grid_mail = 'Classified'
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
app = Flask(__name__)
def main(file_name: str,file_location:str, attachment_bool: bool, download_link:str, recipient_email: str):
file_path = file_location
if os.getenv('SENDGRID_API_KEY') == None:
print("No API KEY FOUND")
else:
print("API KEY FOUND")
html_string = generate_html_content(attachment_bool,download_link)
message = Mail(
from_email='Classified',
to_emails=recipient_email,
subject='Uw data is gereed.',
html_content= html_string)
if attachment_bool is True:
with open(file_path, 'rb') as f:
data = f.read()
f.close()
encoded_file = base64.b64encode(data).decode()
attachment = Attachment()
attachment.file_content = FileContent(encoded_file)
attachment.file_type = FileType('application/zip')
attachment.file_name = FileName('{}.zip'.format(file_name))
attachment.disposition = Disposition('attachment')
attachment.content_id = ContentId('Example Content ID')
message.attachment = attachment
try:
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print("Failed scenario")
print("Scenerio checking")
# Generates the HTML variables for the email page.
def generate_html_content(attachment_bool: bool, download_link: str):
if attachment_bool is False:
Letter_1 = "Helaas was deze te groot om via mail door te sturen."
Letter_2 = "Klik hier om het bestand te downloaden."
if attachment_bool is True:
Letter_1 = "U vindt het bestand terug in de bijlage."
Letter_2 = "Is het bestand niet in de bijlage terug te vinden? Klik dan hier en download dan hier."
return render_template(
'email.html',
message_1 = Letter_1,
message_2 = Letter_2,
link = download_link,
title='YOU GOT MAIL'
)
if __name__ == "__main__":
main()
您可能會問為什么我使用 html 頁面渲染功能?那么它是因為以下
本質上,我是在渲染一個帶有變數的靜態模板
但是我遇到了一個特定的問題,當它不是多行程時我沒有遇到。
Traceback (most recent call last):
File "D:\IDE\Anaconda3\envs\PYTHONGDAL\lib\multiprocessing\process.py", line 315, in _bootstrap
self.run()
File "D:\IDE\Anaconda3\envs\PYTHONGDAL\lib\multiprocessing\process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "D:\Programmeer portfolio\FLASK\Classfiied\Classfiied\views.py", line 178, in execute_order66
Order66.main(stringpart,url,sper_year,email)
File "D:\Programmeer portfolio\FLASK\Classfiied\Classfiied\background_script\backgroundtask.py", line 53, in main
mail_delivery.main(file_name,requested_data,attachment_bool,requested_data,email)
File "D:\Programmeer portfolio\FLASK\Classfiied\Classfiied\background_script\sendgrid\send_mail.py", line 30, in main
html_string = generate_html_content(attachment_bool,download_link)
File "D:\Programmeer portfolio\FLASK\Classfiied\Classfiied\background_script\sendgrid\send_mail.py", line 67, in generate_html_content
return render_template(
File "D:\IDE\Anaconda3\envs\Classfiied\lib\site-packages\flask\templating.py", line 146, in render_template
ctx.app.update_template_context(context)
AttributeError: 'NoneType' object has no attribute 'app'
我不明白為什么我會收到這個錯誤是我需要做一些特別的事情還是我必須走不同的路線,無論如何都要撰寫完整的 HTML?
uj5u.com熱心網友回復:
Twilio SendGrid 開發人員布道者在這里。
跟蹤結束時的錯誤是:
File "D:\IDE\Anaconda3\envs\Classfiied\lib\site-packages\flask\templating.py", line 146, in render_template
ctx.app.update_template_context(context)
AttributeError: 'NoneType' object has no attribute 'app'
這告訴我ctx物件在NoneType這里,它不應該在這里。我認為問題在于您在 Flask 應用程式的背景關系之外運行此代碼,它沒有運行 Flask 路由來回應傳入的 HTTP 請求,您只是將其作為常規函式呼叫。當它不是多行程時,我猜這是在 Flask 路由的背景關系中運行的。
我查看了如何render_template在 Flask 路由之外使用,最終在 Full Stack Python 上遇到了這些示例,特別是示例 4,它用于render_template呈現 HTML,然后作為電子郵件發送。該示例來自flask-base,用于with app.app_context()在Flask 應用程式的背景關系中運行代碼,而不將其作為路由的一部分。
我通常不是 Python 程式員,但也許這樣的事情適用于您的應用程式:
def generate_html_content(attachment_bool: bool, download_link: str):
if attachment_bool is False:
Letter_1 = "Helaas was deze te groot om via mail door te sturen."
Letter_2 = "Klik hier om het bestand te downloaden."
if attachment_bool is True:
Letter_1 = "U vindt het bestand terug in de bijlage."
Letter_2 = "Is het bestand niet in de bijlage terug te vinden? Klik dan hier en download dan hier."
with app.app_context():
return render_template(
'email.html',
message_1 = Letter_1,
message_2 = Letter_2,
link = download_link,
title='YOU GOT MAIL'
)
讓我知道這是否有幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/380535.html
