Skip to content

Commit 0d20688

Browse files
Day 19/
1 parent 5b5391b commit 0d20688

24 files changed

+570
-0
lines changed

Day 19/hungry/__init__.py

Whitespace-only changes.

Day 19/hungry/__main__.py

Lines changed: 18 additions & 0 deletions
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())
461 Bytes
Binary file not shown.
935 Bytes
Binary file not shown.

Day 19/hungry/data.csv

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
id,name,email,amount,sent,date
2+
1,Justin,hello@teamcfe.com,231,adsd,2016-06-18 21:17:44.378612
3+
2,Justin,hello@teamcfe.com,231,adsd,2016-06-18 21:17:45.074490
4+
3,Justin,hello@teamcfe.com,231,adsd,2016-06-18 21:17:45.570860
5+
4,Justin,hello@teamcfe.com,231,adsd,2016-06-18 21:17:45.986075
6+
5,John,abc@teamcfe.com,asdf,adsd,2016-06-18 21:17:51.059165
7+
6,Justin,hello@teamcfe.com,231,adsd,2016-06-18 21:17:51.509586
8+
7,Justin,hello@teamcfe.com,231,adsd,2016-06-18 21:17:51.935921
9+
8,Justin,hello@teamcfe.com,231,adsd,2016-06-18 21:17:52.364994

Day 19/hungry/data_class.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import csv
2+
import datetime
3+
import shutil
4+
import os
5+
from tempfile import NamedTemporaryFile
6+
7+
from utils.templates import get_template, render_context
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+
13+
class UserManager():
14+
15+
def message_user(self):
16+
file_ = 'templates/email_message.txt'
17+
file_html = 'templates/email_message.html'
18+
template = get_template(file_)
19+
template_html = get_template(file_html)
20+
context = {
21+
"name": "Justin",
22+
"date": None,
23+
"total": None
24+
}
25+
print(render_context(template, context))
26+
print(render_context(template_html, context))
27+
return None
28+
29+
def get_user_data(self, user_id=None, email=None):
30+
filename = file_item_path
31+
with open(filename, "r") as csvfile:
32+
reader = csv.DictReader(csvfile)
33+
items = []
34+
unknown_user_id = None
35+
unknown_email = None
36+
for row in reader:
37+
if user_id is not None:
38+
if int(user_id) == int(row.get("id")):
39+
return row
40+
else:
41+
unknown_user_id = user_id
42+
if email is not None:
43+
if email == row.get("email"):
44+
return row
45+
else:
46+
unknown_email = email
47+
if unknown_user_id is not None:
48+
return "User id {user_id} not found".format(user_id=user_id)
49+
if unknown_email is not None:
50+
return "Email {email} not found".format(email=email)
51+
return None

Day 19/hungry/data_class.pyc

1.72 KB
Binary file not shown.

Day 19/hungry/data_manager.py

Lines changed: 34 additions & 0 deletions
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 19/hungry/data_manager.pyc

1.03 KB
Binary file not shown.

Day 19/hungry/old_src/custom.py

Lines changed: 46 additions & 0 deletions
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 = "hungrypy@gmail.com"
6+
password = "iamhungry2016"
7+
from_email = username
8+
to_list = ["hungrypy@gmail.com"]
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+

0 commit comments

Comments
 (0)