Skip to content

Commit 13d9253

Browse files
Day 20/
1 parent 0d20688 commit 13d9253

24 files changed

+603
-0
lines changed

Day 20/hungry/__init__.py

Whitespace-only changes.

Day 20/hungry/__main__.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from argparse import ArgumentParser
2+
3+
from data_class import UserManager
4+
from utils.templates import get_template, render_context
5+
6+
parser = ArgumentParser(prog="hungry")
7+
parser.add_argument("type", type=str, choices=['view', 'message'])
8+
#parser.add_argument("did_send", type=str, choices=['true', 'false'])
9+
parser.add_argument('-id', '--user_id', type=int)
10+
parser.add_argument('-e', '--email', type=str)
11+
12+
args = parser.parse_args()
13+
14+
15+
if args.type == "view":
16+
print(UserManager().get_user_data(user_id=args.user_id, email=args.email))
17+
elif args.type == "message":
18+
print(UserManager().message_user(user_id=args.user_id, email=args.email))
461 Bytes
Binary file not shown.
Binary file not shown.

Day 20/hungry/data.csv

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
id,name,email,amount,sent,date
2+
1,Justin,[email protected],231,adsd,2016-06-18 21:17:44.378612
3+
2,Justin,[email protected],231,adsd,2016-06-18 21:17:45.074490
4+
3,Justin,[email protected],231,adsd,2016-06-18 21:17:45.570860
5+
4,Justin,[email protected],231,adsd,2016-06-18 21:17:45.986075
6+
5,John,[email protected],asdf,adsd,2016-06-18 21:17:51.059165
7+
6,Justin,[email protected],231,adsd,2016-06-18 21:17:51.509586
8+
7,Justin,[email protected],231,adsd,2016-06-18 21:17:51.935921
9+
8,Justin,[email protected],231,adsd,2016-06-18 21:17:52.364994

Day 20/hungry/data_class.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import csv
2+
import datetime
3+
from email.mime.multipart import MIMEMultipart
4+
from email.mime.text import MIMEText
5+
import smtplib
6+
import shutil
7+
import os
8+
from tempfile import NamedTemporaryFile
9+
10+
from utils.templates import get_template, render_context
11+
#file_item_path = os.path.join(os.getcwd(), "data.csv")
12+
file_item_path = os.path.join(os.path.dirname(__file__), "data.csv")
13+
14+
15+
16+
host = "smtp.gmail.com"
17+
port = 587
18+
username = "[email protected]"
19+
password = "iamhungry2016day19"
20+
from_email = username
21+
to_list = ["[email protected]"]
22+
23+
class UserManager():
24+
def render_message(self, user_data):
25+
file_ = 'templates/email_message.txt'
26+
file_html = 'templates/email_message.html'
27+
template = get_template(file_)
28+
template_html = get_template(file_html)
29+
if isinstance(user_data, dict):
30+
context = user_data
31+
plain_ = render_context(template, context)
32+
html_ = render_context(template_html, context)
33+
return (plain_, html_)
34+
return (None, None)
35+
36+
def message_user(self, user_id=None, email=None, subject="Billing Update!"):
37+
user = self.get_user_data(user_id=user_id, email=email)
38+
if user:
39+
plain_, html_ = self.render_message(user)
40+
print(plain_, html_)
41+
user_email = user.get("email", "[email protected]")
42+
to_list.append(user_email)
43+
try:
44+
email_conn = smtplib.SMTP(host, port)
45+
email_conn.ehlo()
46+
email_conn.starttls()
47+
email_conn.login(username, password)
48+
the_msg = MIMEMultipart("alternative")
49+
the_msg['Subject'] = subject
50+
the_msg["From"] = from_email
51+
the_msg["To"] = user_email
52+
part_1 = MIMEText(plain_, 'plain')
53+
part_2 = MIMEText(html_, "html")
54+
the_msg.attach(part_1)
55+
the_msg.attach(part_2)
56+
email_conn.sendmail(from_email, to_list, the_msg.as_string())
57+
email_conn.quit()
58+
except smtplib.SMTPException:
59+
print("error sending message")
60+
return None
61+
62+
def get_user_data(self, user_id=None, email=None):
63+
filename = file_item_path
64+
with open(filename, "r") as csvfile:
65+
reader = csv.DictReader(csvfile)
66+
items = []
67+
unknown_user_id = None
68+
unknown_email = None
69+
for row in reader:
70+
if user_id is not None:
71+
if int(user_id) == int(row.get("id")):
72+
return row
73+
else:
74+
unknown_user_id = user_id
75+
if email is not None:
76+
if email == row.get("email"):
77+
return row
78+
else:
79+
unknown_email = email
80+
if unknown_user_id is not None:
81+
print("User id {user_id} not found".format(user_id=user_id))
82+
if unknown_email is not None:
83+
print("Email {email} not found".format(email=email))
84+
return None

Day 20/hungry/data_class.pyc

2.9 KB
Binary file not shown.

Day 20/hungry/data_manager.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import csv
2+
import datetime
3+
import shutil
4+
import os
5+
from tempfile import NamedTemporaryFile
6+
7+
8+
#file_item_path = os.path.join(os.getcwd(), "data.csv")
9+
file_item_path = os.path.join(os.path.dirname(__file__), "data.csv")
10+
11+
12+
def read_data(user_id=None, email=None):
13+
filename = file_item_path
14+
with open(filename, "r") as csvfile:
15+
reader = csv.DictReader(csvfile)
16+
items = []
17+
unknown_user_id = None
18+
unknown_email = None
19+
for row in reader:
20+
if user_id is not None:
21+
if int(user_id) == int(row.get("id")):
22+
return row
23+
else:
24+
unknown_user_id = user_id
25+
if email is not None:
26+
if email == row.get("email"):
27+
return row
28+
else:
29+
unknown_email = email
30+
if unknown_user_id is not None:
31+
return "User id {user_id} not found".format(user_id=user_id)
32+
if unknown_email is not None:
33+
return "Email {email} not found".format(email=email)
34+
return None

Day 20/hungry/data_manager.pyc

1.03 KB
Binary file not shown.

Day 20/hungry/old_src/custom.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import smtplib
2+
3+
host = "smtp.gmail.com"
4+
port = 587
5+
username = "[email protected]"
6+
password = "iamhungry2016"
7+
from_email = username
8+
to_list = ["[email protected]"]
9+
10+
email_conn = smtplib.SMTP(host, port)
11+
email_conn.ehlo()
12+
email_conn.starttls()
13+
email_conn.login(username, password)
14+
email_conn.sendmail(from_email, to_list, "Hello there this is an email message")
15+
email_conn.quit()
16+
17+
18+
from smtplib import SMTP
19+
20+
21+
ABC = SMTP(host, port)
22+
ABC.ehlo()
23+
ABC.starttls()
24+
ABC.login(username, password)
25+
ABC.sendmail(from_email, to_list, "Hello there this is an email message")
26+
ABC.quit()
27+
28+
29+
from smtplib import SMTP, SMTPAuthenticationError, SMTPException
30+
31+
32+
pass_wrong = SMTP(host, port)
33+
pass_wrong.ehlo()
34+
pass_wrong.starttls()
35+
try:
36+
pass_wrong.login(username, "wrong_password")
37+
pass_wrong.sendmail(from_email, to_list, "Hello there this is an email message")
38+
except SMTPAuthenticationError:
39+
print("Could not login")
40+
except:
41+
print("an error occured")
42+
43+
pass_wrong.quit()
44+
45+
46+

Day 20/hungry/old_src/data.csv

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
id,name,email,amount,sent,date
2+
1,Justin,[email protected],231,adsd,2016-06-18 21:17:44.378612
3+
2,Justin,[email protected],231,adsd,2016-06-18 21:17:45.074490
4+
3,Justin,[email protected],231,adsd,2016-06-18 21:17:45.570860
5+
4,Justin,[email protected],231,adsd,2016-06-18 21:17:45.986075
6+
5,John,[email protected],asdf,adsd,2016-06-18 21:17:51.059165
7+
6,Justin,[email protected],231,adsd,2016-06-18 21:17:51.509586
8+
7,Justin,[email protected],231,adsd,2016-06-18 21:17:51.935921
9+
8,Justin,[email protected],231,adsd,2016-06-18 21:17:52.364994
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from email.mime.multipart import MIMEMultipart
2+
from email.mime.text import MIMEText
3+
import smtplib
4+
5+
6+
host = "smtp.gmail.com"
7+
port = 587
8+
username = "[email protected]"
9+
password = "iamhungry2016"
10+
from_email = username
11+
to_list = ["[email protected]"]
12+
13+
try:
14+
email_conn = smtplib.SMTP(host, port)
15+
email_conn.ehlo()
16+
email_conn.starttls()
17+
email_conn.login(username, password)
18+
the_msg = MIMEMultipart("alternative")
19+
the_msg['Subject'] = "Hello there!"
20+
the_msg["From"] = from_email
21+
#the_msg["To"] = to_list[0]
22+
plain_txt = "Testing the message"
23+
html_txt = """\
24+
<html>
25+
<head></head>
26+
<body>
27+
<p>Hey!<br>
28+
Testing this email <b>message</b>. Made by <a href='http://joincfe.com'>Team CFE</a>.
29+
</p>
30+
</body>
31+
</html>
32+
"""
33+
part_1 = MIMEText(plain_txt, 'plain')
34+
part_2 = MIMEText(html_txt, "html")
35+
the_msg.attach(part_1)
36+
the_msg.attach(part_2)
37+
email_conn.sendmail(from_email, to_list, the_msg.as_string())
38+
email_conn.quit()
39+
except smtplib.SMTPException:
40+
print("error sending message")
41+
42+
43+
44+
45+
46+
47+

Day 20/hungry/old_src/hungry_data.py

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import csv
2+
import datetime
3+
import shutil
4+
from tempfile import NamedTemporaryFile
5+
6+
def read_data(user_id=None, email=None):
7+
filename = "data.csv"
8+
with open(filename, "r") as csvfile:
9+
reader = csv.DictReader(csvfile)
10+
items = []
11+
unknown_user_id = None
12+
unknown_email = None
13+
for row in reader:
14+
if user_id is not None:
15+
if int(user_id) == int(row.get("id")):
16+
return row
17+
else:
18+
unknown_user_id = user_id
19+
if email is not None:
20+
if email == row.get("email"):
21+
return row
22+
else:
23+
unknown_email = email
24+
if unknown_user_id is not None:
25+
return "User id {user_id} not found".format(user_id=user_id)
26+
if unknown_email is not None:
27+
return "Email {email} not found".format(email=email)
28+
return None
29+
30+
31+
32+
33+
34+
def get_length(file_path):
35+
with open("data.csv", "r") as csvfile:
36+
reader = csv.reader(csvfile)
37+
reader_list = list(reader)
38+
return len(reader_list)
39+
40+
def append_data(file_path, name, email, amount):
41+
fieldnames = ['id', 'name', 'email', 'amount', 'sent', 'date']
42+
#the number of rows?
43+
next_id = get_length(file_path)
44+
with open(file_path, "a") as csvfile:
45+
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
46+
writer.writerow({
47+
"id": next_id,
48+
"name": name,
49+
"email": email,
50+
"sent": "",
51+
"amount": amount,
52+
"date": datetime.datetime.now()
53+
})
54+
55+
#append_data("data.csv", "Justin", "[email protected]", 123.22)
56+
57+
def edit_data(edit_id=None, email=None, amount=None, sent=None):
58+
filename = "data.csv"
59+
temp_file = NamedTemporaryFile(delete=False)
60+
61+
with open(filename, "rb") as csvfile, temp_file:
62+
reader = csv.DictReader(csvfile)
63+
fieldnames = ['id', 'name', 'email', 'amount', 'sent', 'date']
64+
writer = csv.DictWriter(temp_file, fieldnames=fieldnames)
65+
writer.writeheader()
66+
for row in reader:
67+
#print(row['id'] == 4)
68+
if edit_id is not None:
69+
if int(row['id']) == int(edit_id):
70+
row['amount'] = amount
71+
row['sent'] = sent
72+
elif email is not None and edit_id is None:
73+
if str(row['email']) == str(email):
74+
row['amount'] = amount
75+
row['sent'] = sent
76+
else:
77+
pass
78+
writer.writerow(row)
79+
80+
shutil.move(temp_file.name, filename)
81+
return True
82+
return False
83+
84+
85+
#edit_data(8, 9992.32, "")
86+
#edit_data(email='[email protected]', amount=99.99, sent='')
87+
88+
89+
def new_edit_data(new_data):
90+
filename = "data.csv"
91+
temp_file = NamedTemporaryFile(delete=False)
92+
93+
with open(filename, "rb") as csvfile, temp_file:
94+
reader = csv.DictReader(csvfile)
95+
fieldnames = ['id', 'name', 'email', 'amount', 'sent', 'date']
96+
writer = csv.DictWriter(temp_file, fieldnames=fieldnames)
97+
writer.writeheader()
98+
for row in reader:
99+
#print(row['id'] == 4)
100+
if isinstance(new_data, dict):
101+
print(new_data)
102+
id_ = new_data.get("id", None)
103+
email_ = new_data.get("email", None)
104+
if id_ and int(row['id']) == int(id_):
105+
for key, value in new_data.items():
106+
if key in fieldnames:
107+
row[key] = value
108+
elif str(row['email']) == str(email_) and not id_:
109+
for key, value in new_data.items():
110+
if key in fieldnames:
111+
row[key] = value
112+
writer.writerow(row)
113+
114+
shutil.move(temp_file.name, filename)
115+
return True
116+
return False
117+
118+
119+
#edit_data(8, 9992.32, "")
120+
121+
new_edit_data({"amount": 231, "email": "[email protected]", "sent": "adsd", "house": "NOne"})
122+
123+

0 commit comments

Comments
 (0)