Skip to content

Commit

Permalink
Made decorator for sending email
Browse files Browse the repository at this point in the history
  • Loading branch information
Niepawowsky committed Jan 3, 2024
1 parent b12c346 commit c1c445e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion database/db_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def get_borrowed_books_by_today(self):
title,
return_at
FROM borrowed_books
WHERE return_at < ?''', (today,))
WHERE return_at > ?''', (today,))

for name, email, title, return_at in database.cursor.fetchall():
return_date = datetime.strptime(return_at, '%Y-%m-%d %H:%M:%S')
Expand Down
41 changes: 39 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,49 @@
from datetime import datetime
from database.db_service import DbManager, DataBaseService
from database.db_service import DbManager
import smtplib, ssl
from os import environ, getenv
import sqlite3


connection = sqlite3.connect('database/sample_books.db') #być może trzeba będzie to zostawić

db= DataBaseService(connection)
db = DataBaseService(connection)

# print(db.get_borrowed_books_by_today())
print(db.get_borrowed_for_remider())
print(db.get_borrowed_books_by_today())



class MailSender():
@staticmethod
def send_data(func):
def wrapper(*args, **kwargs):
#TODO create funcion that sends email using data from .env, than in different methods, sends emails with specific message
smtp_server = getenv('SMTP_SERVER')
port = getenv('PORT')
sender_email = getenv('EMAIL')
password = getenv('PASSWORD')

context = ssl.create_default_context()

result = func(*args, smtp_server = smtp_server, port = port, sender_email = sender_email, password = password, context = context)

return result

return wrapper

def __init__(self, user: namedtuple):
self.user = user

@send_data
def send_passed_return_date(self, smtp_server, port, sender_email, password, context, receiver_email, msg):
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, msg=msg)






0 comments on commit c1c445e

Please sign in to comment.