wag-services-and-backend-la.../BankServices/RoutineEmailService/app.py

58 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import arrow
from Schemas import AccountRecords
from jinja2 import Environment, FileSystemLoader
from Services.EmailService.provider import send_email
def render_email_template(headers: list, rows: list):
template_dir = os.path.join(os.path.dirname(__file__), "templates")
env = Environment(loader=FileSystemLoader(template_dir)) # Load templates from the directory
template = env.get_template("template_accounts.html") # Load the specific template file
return template.render(headers=headers, rows=rows) # Render template with variables
def send_email_to_given_address(send_to: str, html_template: str):
today = arrow.now()
subject = f"{str(today.date())} Gunes Apt. Cari Durum Bilgilendirme Raporu"
try:
send_email(subject=subject, receivers=[send_to], html=html_template)
print(f"Email is sent to : {send_to}. BB")
return
except Exception as e:
print(f"Error: {e}")
print("Email is not sent")
return
def set_account_records_to_send_email():
account_records = (
AccountRecords.query.filter()
.order_by(
AccountRecords.bank_date.desc(), AccountRecords.bank_reference_code.desc()
)
.limit(3)
.all()
)
first_record, second_record, balance_error = account_records[0], account_records[1], False
second_balance = first_record.bank_balance - first_record.currency_value
if second_balance != second_record.bank_balance:
balance_error = True
rows = [{
"date": record.bank_date, "comment": record.bank_comment, "currency": record.currency_value,
} for record in account_records]
send_to = "karatay@mehmetkaratay.com.tr"
html_template = render_email_template(
headers=["Ulaştığı Tarih", "Banka Transaksiyonu Ek Bilgi", "Aktarım Değeri"],
rows=rows,
)
exit()
if __name__ == "__main__":
set_account_records_to_send_email()