-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added env loader, changed datetime issue
- Loading branch information
1 parent
c1c445e
commit 2283be7
Showing
6 changed files
with
205 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import datetime | ||
import smtplib, ssl | ||
from dotenv import load_dotenv | ||
from os import environ, getenv | ||
from string import Template | ||
|
||
from templates.template_render import EmailTemplates | ||
|
||
|
||
class MailSender(): | ||
@staticmethod | ||
# def send_data(func): | ||
# def wrapper(*args, **kwargs): | ||
# smtp_server = getenv('SMTP_SERVER') | ||
# port = 465#('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 | ||
|
||
@staticmethod | ||
def send_data(func): | ||
def wrapper(*args, **kwargs): | ||
load_dotenv() | ||
smtp_server = getenv('SMTP_SERVER') | ||
port = 465 # or int(getenv('PORT')) | ||
sender_email = getenv('EMAIL') | ||
password = getenv('PASSWORD') | ||
|
||
context = ssl.create_default_context() | ||
|
||
# # If the wrapped function already has these arguments, use them | ||
# if 'smtp_server' in kwargs: | ||
# smtp_server = kwargs['smtp_server'] | ||
# if 'port' in kwargs: | ||
# port = kwargs['port'] | ||
# if 'sender_email' in kwargs: | ||
# sender_email = kwargs['sender_email'] | ||
# if 'password' in kwargs: | ||
# password = kwargs['password'] | ||
# if 'context' in kwargs: | ||
# context = kwargs['context'] | ||
|
||
kwargs.update({ | ||
'smtp_server': smtp_server, | ||
'port': port, | ||
'sender_email': sender_email, | ||
'password': password, | ||
'context': context | ||
|
||
}) | ||
result = func(*args, **kwargs) | ||
return result | ||
|
||
return wrapper | ||
|
||
def __init__(self, borrowers: list, template: EmailTemplates): | ||
self.borrowers = borrowers | ||
self.template = template | ||
|
||
def __str__(self): | ||
print(self.borrowers) | ||
|
||
@send_data | ||
def send_passed_return_date(self, *args, **kwargs): # smtp_server, port, sender_email, password, context): | ||
print('Before connected to SMTP server') | ||
try: | ||
print('connected to SMTP server') | ||
# | ||
with smtplib.SMTP_SSL(host=kwargs['smtp_server'], port=kwargs["port"], context=kwargs['context']) as server: | ||
for borrower in self.borrowers: | ||
today = datetime.datetime.today() | ||
today.strftime('%Y-%m-%d') | ||
result = borrower.return_at - today | ||
print(type(borrower.return_at)) | ||
mail = self.template.send_reminder_nearby().substitute(name=borrower.name, title=borrower.title, | ||
return_at=borrower.return_at, result=str(result)) | ||
#@TODO wysyla pustego maila | ||
print('To jest mail:', mail) | ||
print('Iterating threw self.borrowers') | ||
server.login(kwargs['sender_email'], kwargs['password']) | ||
print('logged in') | ||
server.sendmail(kwargs['sender_email'], borrower.email, mail) | ||
|
||
except Exception as e: | ||
print(f'Error: {e}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,16 @@ | ||
from collections import namedtuple | ||
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 | ||
|
||
from mail_sender import MailSender | ||
from database.db_service import DbManager, DataBaseService | ||
from templates.template_render import EmailTemplates | ||
|
||
connection = sqlite3.connect('database/sample_books.db') #być może trzeba będzie to zostawić | ||
|
||
db = DataBaseService(connection) | ||
|
||
# print(db.get_borrowed_books_by_today()) | ||
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) | ||
|
||
|
||
|
||
connection = sqlite3.connect('database/sample_books.db') | ||
|
||
data = DataBaseService(connection) | ||
templates = EmailTemplates() | ||
borrowers_list = data.get_borrowed_books_by_today() | ||
print(borrowers_list) | ||
|
||
email = MailSender(borrowers_list, templates) | ||
email.send_passed_return_date() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from string import Template | ||
|
||
|
||
class EmailTemplates(): | ||
|
||
@staticmethod | ||
def send_reminder_overdue(): | ||
mssg = Template('$name, You have borrowed $title and return date passed $return_at\n Please return it imidietly\nBest Regards\nPawel') | ||
return mssg | ||
|
||
@staticmethod | ||
def send_reminder_nearby(): | ||
mssg = Template('$name, You have borrowed $title and the return day is $return_at. Its about @result days from today.Please try not to overdue this dateBest Regards' | ||
'Pawel') | ||
return mssg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,46 @@ | ||
import pytest | ||
|
||
from database.db_service import DbManager, DataBaseService | ||
from database.db_service import DataBaseService | ||
import sqlite3 | ||
import datetime | ||
import unittest | ||
from freezegun import freeze_time | ||
|
||
|
||
@pytest.fixture | ||
def create_database(): | ||
connection = sqlite3.connect(':memory:') | ||
cursor = connection.cursor() | ||
cursor.execute('''CREATE TABLE borrowed_books( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
email TEXT, | ||
name TEXT, | ||
title TEXT NOT NULL, | ||
author TEXT NOT NULL, | ||
return_at DATE)''' | ||
) | ||
def create_connection(): | ||
with sqlite3.connect(':memory:') as connection: | ||
return connection | ||
|
||
|
||
@freeze_time('2024-01-08 00:00:00') | ||
def test_get_borrowed_books_for_today_reminder_purpose(create_connection, monkeypatch): | ||
db = DataBaseService(create_connection) | ||
db.create_database() | ||
query = 'INSERT INTO borrowed_books VALUES(?,?,?,?,?,?)' | ||
sample_data = [ | ||
(6, '[email protected]', 'John Smith', 'Lalka', 'Bolesław Prus', '2024-12-27 13:30:30'), | ||
(7, '[email protected]', 'Luiza Acante', 'Engine Manual for Smartasses', 'Mario Bro', '2023-12-27 13:30:30') | ||
] | ||
cursor.executemany('INSERT INTO borrowed_books VALUES(?,?,?,?,?,?)', sample_data) | ||
return cursor | ||
] | ||
for data in sample_data: | ||
db._execute_query(query, data) | ||
|
||
list_of_borrowers = db.get_borrowed_books_by_today() | ||
|
||
def test_get_borrowed_books_for_today_reminder_purpose(create_database): | ||
db = DataBaseService | ||
cursor = create_database | ||
list_of_borrowers = db.get_borrowed_books_by_today(cursor) | ||
assert len(list_of_borrowers) == 1 | ||
|
||
def test_get_borrowed_books_for_today_amount_of_records(create_database): | ||
db = DataBaseService | ||
cursor = create_database | ||
list_of_borrowers = db.get_borrowed_history(cursor) | ||
assert len(list_of_borrowers) == 2 | ||
|
||
@freeze_time('2024-01-08 00:00:00') | ||
def test_get_borrowed_books_for_today_amount_of_records(create_connection): | ||
db = DataBaseService(create_connection) | ||
db.create_database() | ||
query = 'INSERT INTO borrowed_books VALUES(?,?,?,?,?,?)' | ||
sample_data = [ | ||
(6, '[email protected]', 'John Smith', 'Lalka', 'Bolesław Prus', '2024-12-27 13:30:30'), | ||
(7, '[email protected]', 'Luiza Acante', 'Engine Manual for Smartasses', 'Mario Bro', '2023-12-27 13:30:30') | ||
] | ||
for data in sample_data: | ||
db._execute_query(query, data) | ||
|
||
list_of_borrowers = db.get_borrowed_history() | ||
assert len(list_of_borrowers) == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import pytest | ||
from string import Template | ||
from templates.template_render import EmailTemplates | ||
from mail_sender import MailSender | ||
from database.db_service import DbManager, DataBaseService | ||
import sqlite3 | ||
|
||
|
||
def test_email_send(): | ||
mssg = Template('This is test message') | ||
|
||
|
||
|