Skip to content

Commit

Permalink
Email notifier added,with date validation
Browse files Browse the repository at this point in the history
  • Loading branch information
hiral72 committed Apr 27, 2020
1 parent ad1c529 commit 9673d2f
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 52 deletions.
63 changes: 61 additions & 2 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,48 @@ def initialize_db(password):
print("PASSWORD IS INCORRECT. TRY AGAIN...")
sys.exit()

def create_table():
global conn
try:
#Creating a cursor object using the cursor() method
cursor = conn.cursor()
#Creating table tasks if it not exists...
sql ='''CREATE TABLE IF NOT EXISTS TASKS(
id int GENERATED ALWAYS AS IDENTITY primary key,
task_name text,
priority_of_task text,
category text,
is_done text,
deadline text
)'''

conn.commit()
print("Table Tasks loaded successfully........")

#Closing the connection
except:
print("Table Tasks is not loaded...")
conn.close()



def create_table2():
global conn
try:
#Creating table tasks if it not exists...
cursor = conn.cursor()
sql='''CREATE TABLE IF NOT EXISTS notification_tracker
(
id int,
notify_date text
) '''
cursor.execute(sql)
conn.commit()
print("Table notification_tracker loaded successfully........")
#Closing the connection
except:
print("Table notification_tracker is not loaded...")
conn.close()

def shutdown_db():
print('Exit.')
Expand All @@ -30,10 +72,26 @@ def add_task(values):

text = cursor.fetchone()[0]
conn.commit()
print("Records created successfully")
print("Task added successfully")
return text


def add_notify_date(values):
cursor = conn.cursor()
cursor.execute("INSERT INTO notification_tracker(id,notify_date) VALUES (%s, %s)",
(values[0], values[5]))

# text = cursor.fetchone()[0]
conn.commit()
print("Notifier date added to notification_tracker table")
# return text

def get_notified_tasks():
cursor = conn.cursor()
cursor.execute("SELECT id,notify_date from notification_tracker;")
rows_count = cursor.fetchall()
return rows_count

def get_tasks():
cursor = conn.cursor()
cursor.execute("SELECT id, task_name, priority_of_task, category, is_done,deadline from tasks;")
Expand All @@ -60,4 +118,5 @@ def read_from_db(var):
cursor = conn.cursor()
cursor.execute("SELECT task_name, priority_of_task, category, is_done,deadline from tasks where task_name=%s;", (var, ))
rows = cursor.fetchall()
return rows
return rows

165 changes: 115 additions & 50 deletions task_manager_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@
from tkinter import simpledialog
from ttkthemes import themed_tk as ttkt
import random

from datetime import datetime
from datetime import timedelta
from datetime import date
from dateutil.parser import parse

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

import tkcalendar
from tkcalendar import Calendar, DateEntry

import db


password_root = Tk()
password_root.withdraw()
password = simpledialog.askstring("Password", "Enter password:", show="*")
Expand All @@ -23,6 +29,9 @@
sys.exit()

db.initialize_db(password)
db.create_table()
db.create_table2()


root = ttkt.ThemedTk()
root.set_theme('radiance')
Expand All @@ -34,6 +43,48 @@
tree.grid(row=0, column=0, rowspan=2)
scrollbar.grid(row=0, column=1, rowspan=2, sticky=(W, N, E, S))

def already_notified(taskn):
for item in db.get_notified_tasks():
if taskn[0]==item[0]:
return True
return False

def notify():
for item in db.get_tasks():
if not already_notified(item):
dd=datetime.strptime(item[5], "%d-%m-%Y")
dd2= dd -timedelta(days=1)
dd3=dd2.strftime('%d-%m-%Y')
today=datetime.today().strftime('%d-%m-%Y')
if (dd3==today) and (item[4]=="false"):
email_notify(item)
db.add_notify_date(item)
print("Email notification sent sucessfully")


def email_notify(item):
email = '[email protected]'#add your gmail id from which u want to sent (only gmail account)
password = 'password'#add your email password
send_to_email = '[email protected]'#add your email id where u want to sent (any mail account)
subject = 'Task Notifier'# The subject line
message ='Category of task: '+item[3]+'\n\nYour Task:'+ item[1]
msg = MIMEMultipart()
msg['From'] = email
msg['To'] = send_to_email
msg['Subject'] = subject

# Attach the message to the MIMEMultipart object
msg.attach(MIMEText(message, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email, password)
text = msg.as_string() # You now need to convert the MIMEMultipart object to a string to send
server.sendmail(email, send_to_email, text)
server.quit()




def treeview_sort_column(treeview, column, reverse):
children_list = [(treeview.set(child, column), child) for child in treeview.get_children("")]
Expand All @@ -53,7 +104,7 @@ def treeview_sort_column(treeview, column, reverse):

width_task_name = int(width * 0.25)
tree.column("task_name", width=width_task_name, anchor="center")
tree.heading("task_name", text="task_name")
tree.heading("task_name", text="Tasks")

width_priority_of_task = int(width * 0.08)
tree.column("priority_of_task", width=width_priority_of_task, anchor="center")
Expand All @@ -69,7 +120,7 @@ def treeview_sort_column(treeview, column, reverse):

width_deadline = int(width * 0.11)
tree.column("deadline", width=width_is_done, anchor="center")
tree.heading("deadline", text="deadline")
tree.heading("deadline", text="Due date")

mainframe = ttk.Frame(root, padding="25 25 100 50")
mainframe.grid(row=0, column=2, sticky=(N, S, W, E))
Expand All @@ -82,7 +133,7 @@ def treeview_sort_column(treeview, column, reverse):
task_name_widget.grid(column=2, row=1, sticky=(W, E))

priority_of_task = StringVar()
ttk.Label(mainframe, text="priority of task \n(E.g 1 to 10):").grid(column=1, row=2, sticky=(W, E))
ttk.Label(mainframe, text="Priority of task \n(E.g 1 to 10):").grid(column=1, row=2, sticky=(W, E))
priority_of_task_widget = ttk.Entry(mainframe, width=20, textvariable=priority_of_task)
priority_of_task_widget.grid(column=2, row=2, sticky=(W, E))

Expand All @@ -92,7 +143,7 @@ def treeview_sort_column(treeview, column, reverse):
category_widget.grid(column=2, row=3, sticky=(W, E))

deadline = StringVar()
ttk.Label(mainframe, text="Deadline:").grid(column=1, row=4, sticky=(W, E))
ttk.Label(mainframe, text="Deadline: \n(Format:dd-mm-yyyy)").grid(column=1, row=4, sticky=(W, E))
deadline_widget = ttk.Entry(mainframe, width=20, textvariable=deadline)
deadline_widget.grid(column=2, row=4, sticky=(W, E))

Expand All @@ -103,6 +154,7 @@ def treeview_sort_column(treeview, column, reverse):
is_done_widget.grid(column=2, row=5, sticky=(W, E))


notify()

def create_task_item():
task_name_value = task_name.get()
Expand Down Expand Up @@ -134,19 +186,31 @@ def create_task_item():

create_button["state"] = "normal"
change_button["state"] = "disabled"
notify()


def inputs_validation():
task_name_value = task_name.get()
priority_of_task_value = priority_of_task.get()
category_value = category.get()
deadline_value=deadline.get()
dd=datetime.strptime(deadline_value,"%d-%m-%Y")
today=datetime.today().strftime('%d-%m-%Y')
today1=datetime.strptime(today,"%d-%m-%Y")




if not(len(task_name_value) > 0 and len(task_name_value) <= 40):
return False
if not(int(priority_of_task_value) > 0 and int(priority_of_task_value) <= 10):
print("Priority is not valid")
return False
if not(len(category_value) > 0 and len(category_value) <= 20):
return False
if (dd<today1):
print("Deadline is gone.")#pop-up box to be added
return False

return True

Expand All @@ -173,63 +237,63 @@ def change_theme():



# top = tk.Toplevel(root)
top = tk.Toplevel(root)
def calendar_events():
global top
global cal
top = tk.Toplevel(root)
cal = Calendar(top, selectmode='none')
for item in db.get_tasks():
dd=datetime.strptime(item[5], "%Y-%m-%d")
dd=datetime.strptime(item[5], "%d-%m-%Y")
cal.calevent_create(dd,item[1], 'reminder')
cal.tag_config('reminder', background='red', foreground='yellow')
cal.pack(fill="both", expand=True)
date = cal.datetime.today()
cal.calevent_create(date, 'Today', 'message')

# class ToolTip(object):

# def __init__(self, widget):
# self.widget = widget
# self.tipwindow = None
# self.id = None
# self.x = self.y = 0

# def showtip(self, text):
# "Display text in tooltip window"
# self.text = text
# if self.tipwindow or not self.text:
# return
# x, y, cx, cy = self.widget.bbox("insert")
# x = x + self.widget.winfo_rootx() + 57
# y = y + cy + self.widget.winfo_rooty() +27
# self.tipwindow = tw = Toplevel(self.widget)
# tw.wm_overrideredirect(1)
# tw.wm_geometry("+%d+%d" % (x, y))
# label = Label(tw, text=self.text, justify=LEFT,
# background="#ffffe0", relief=SOLID, borderwidth=1,
# font=("tahoma", "8", "normal"))
# label.pack(ipadx=1)

# def hidetip(self):
# tw = self.tipwindow
# self.tipwindow = None
# if tw:
# tw.destroy()

# def CreateToolTip(widget, text):
# toolTip = ToolTip(widget)
# def enter(event):
# toolTip.showtip(text)
# def leave(event):
# toolTip.hidetip()
# widget.bind('<Enter>', enter)
# widget.bind('<Leave>', leave)

# button = Button(top, text = 'Hover over me')
# button.pack()
# CreateToolTip(button, text = 'Hello you!\n'
# 'Have a nice day\n')
class ToolTip(object):

def __init__(self, widget):
self.widget = widget
self.tipwindow = None
self.id = None
self.x = self.y = 0

def showtip(self, text):
"Display text in tooltip window"
self.text = text
if self.tipwindow or not self.text:
return
x, y, cx, cy = self.widget.bbox("insert")
x = x + self.widget.winfo_rootx() + 57
y = y + cy + self.widget.winfo_rooty() +27
self.tipwindow = tw = Toplevel(self.widget)
tw.wm_overrideredirect(1)
tw.wm_geometry("+%d+%d" % (x, y))
label = Label(tw, text=self.text, justify=LEFT,
background="#ffffe0", relief=SOLID, borderwidth=1,
font=("tahoma", "8", "normal"))
label.pack(ipadx=1)

def hidetip(self):
tw = self.tipwindow
self.tipwindow = None
if tw:
tw.destroy()

def CreateToolTip(widget, text):
toolTip = ToolTip(widget)
def enter(event):
toolTip.showtip(text)
def leave(event):
toolTip.hidetip()
widget.bind('<Enter>', enter)
widget.bind('<Leave>', leave)

button = Button(top, text = 'Hover over me')
button.pack()
CreateToolTip(button, text = 'Hello you!\n'
'Have a nice day\n')
cal_events_btn=ttk.Button(mainframe, text='calendar with events', command=calendar_events)
cal_events_btn.grid(column=1, row=8, sticky=(W, E))

Expand Down Expand Up @@ -312,6 +376,7 @@ def change_item():

create_button["state"] = "normal"
change_button["state"] = "disabled"
notify()

change_button = ttk.Button(mainframe, text="Change Task", command=change_item)
change_button.grid(column=2, row=6, sticky=(W, E))
Expand Down

0 comments on commit 9673d2f

Please sign in to comment.