コード例 #1
0
def query():
    feedback_file = './image_feedback/'
    feedback_file_name = os.listdir(feedback_file)[0]
    #replace function to remove the word jpg
    if request.method == 'GET':
        return render_template("feedback.html")
    classification = format(request.form['classification'])
    reason = format(request.form['reason'])
    mail = format(request.form['email'])
    feedback_folder = './image_feedback/'
    feedback_file = os.listdir(feedback_folder)[0]
    feedback_file_full = './image_feedback/' + feedback_file
    with open(feedback_file_full, 'rb') as f:
        data = f.read()
    encoded = base64.b64encode(data).decode()
    attachment = Attachment()
    attachment.content = encoded
    attachment.type = "application/jpg"
    if attachment.type != "application/jpg":  #allows only png and jpg
        attachment.type = "application/png"
        attachment.type = "application/png"
    attachment.filename = feedback_file_name
    attachment.disposition = "attachment"
    attachment.content_id = "image file"
    from_email = Email("*****@*****.**")
    sg = sendgrid.SendGridAPIClient(
        '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'
    )
    subject = "Concrete Surface misclassification query"
    to_email = Email(mail)
    cc_email = Email("*****@*****.**")
    p = Personalization()
    p.add_to(to_email)
    p.add_cc(cc_email)
    message = "your query:" + "" + classification + " " + reason + ". " + "This is the query receipt. The incorrectly classified image is attached to the email "
    #print(message)
    content = Content("text/plain", message)
    mail = Mail(from_email, subject, to_email, content)
    mail.add_personalization(p)  # allows you to send  CC = carbon copy
    mail.add_attachment(attachment)
    response = sg.client.mail.send.post(request_body=mail.get())
    print(response.status_code)
    print(response.body)
    print(response.headers)
    return render_template("finished_feedback.html")