-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatafunc.py
163 lines (147 loc) · 5.17 KB
/
datafunc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import csv
from datetime import datetime, timedelta
def read_data(filename) -> list:
data =[]
with open(filename) as csv_file:
csv_data = csv.reader(csv_file, delimiter=',')
for line in csv_data:
data.append(line)
return data
def takeout(object, id, pid, due, filename) -> str:
data = read_data(filename)
flag = True
s = " "
for i in data:
if int(i[0]) == id:
s = f"ERROR: The following book is already signed out by {object.data_person[int(i[1])].name} on the date {i[2]}"
log(s)
return s
if len(data) == 0 or flag == True:
datenow = datetime.now().date()
duedate = datenow + timedelta(days=due)
with open(filename, mode='a+') as csv_file:
csv_data = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_data.writerow([id,pid,str(datenow), str(duedate)])
s = f"\n{object.data_person[pid].name} has taken out the following book:\n{object.data_books[id]}"
log(s)
return s
def bringback(object,id,pid,filename) -> str:
data = read_data(filename)
flag = True
for i in data:
if int(i[0]) == id:
flag = False
if flag == True:
s = (f"ERROR: The following book was not signed out!\n{object.data_books[id]}")
log(s)
return s
else:
for i in range(0,len(data)):
if int(data[i][0]) == id:
del data[i]
break
with open(filename, mode='w') as csv_file:
csv_data = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for i in data:
csv_data.writerow(i)
s = (f"\n{object.data_person[pid].name} has signed in the following book:\n{object.data_books[id]}")
log(s)
return s
def extend(object,id,pid, day,filename) -> str:
data = read_data(filename)
datenow = datetime.now().date()
duedate = datenow + timedelta(days=day)
flag = True
for i in data:
if int(i[0]) == id:
flag = False
if flag == True:
s = (f"ERROR: The following book was not signed out!\n{object.data_books[id]}")
log(s)
return s
else:
for i in range(0,len(data)):
if int(data[i][0]) == id:
data[i] = [id,pid,str(datenow), str(duedate)]
break
with open(filename, mode='w') as csv_file:
csv_data = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for i in data:
csv_data.writerow(i)
s = (f"\n{object.data_person[pid].name} has extended by {day} days the following book:\n{object.data_books[id]}")
log(s)
return s
def user_search_id(object, id, filename):
id = int(id)
data = read_data(filename)
books = []
dates = []
duedate = []
for i in data:
if int(i[1]) == id:
books.append(object.data_books[int(i[0])])
dates.append(i[2])
duedate.append(i[3])
if len(books) == 0:
print("No loans under this account")
else:
print(f"\n{object.data_person[id].name} has the following books out: ")
j = 0
for i in books:
print(i)
print(f"\tSigned out: {dates[j]}")
print(f"\tDue on: {duedate[j]}\n")
j += 1
def username_search(object, username, filename):
data = read_data(filename)
books = []
dates = []
id = None
for i in object.data_person:
if str(i.username) == str(username):
id = int(i.pID)
if id != None:
for i in data:
if int(i[1]) == id:
books.append(object.data_books[int(i[0])])
dates.append(i[2])
if len(books) == 0:
print("No loans under this account")
else:
print(f"\n{object.data_person[id].name} has the following books out: ")
j = 0
for i in books:
print(i)
print(f"\tOn: {dates[j]}\n")
j += 1
else:
print("User not found")
def book_search(object, bookname) -> list:
books =[]
for i in object.data_books:
if bookname.lower() in i.title.lower():
books.append(i)
return books
def log(logs):
f = open("LMS.log", "a")
datenow = datetime.now()
f.write(f"\nOn {datenow} the following took place:\n\t{logs}\n")
f.close()
# Function works, just awaits implementation
def sendemail(object,Remail,Semail,EmailPass, userid, bookid, date):
import smtplib, ssl
from email.message import EmailMessage
port = 465
smtp_server = "smtp.gmail.com"
sender_email = Semail
password = EmailPass
msg = EmailMessage()
msg.set_content(f"Hello {object.data_person[userid].name}, \nThe following book is due on {date}:\n{object.data_books[bookid]}")
msg['Subject'] = f'{object.data_books[bookid].title} is due!'
msg['From'] = Semail
msg['To'] = Remail
# Send the message via our own SMTP server.
server = smtplib.SMTP_SSL(smtp_server, port)
server.login(Semail, password)
server.send_message(msg)
server.quit()