/
Donor.py
358 lines (314 loc) · 12.5 KB
/
Donor.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
from datetime import datetime
import time
from random import randint
import date_handle
import csv
import os
import sys
import msvcrt
import main
class Donor_class:
# variables
name = ""
weight = ""
gender = ""
date_of_birth = ""
last_donation_date = ""
was_sick = ""
unique_id = ""
blood_type = ""
expiration_of_id = ""
email_address = ""
mobile_number = ""
date_format = ""
age=0
birth_date_string=""
hemoglobin=0
# lists
was_sick_list = ('yes', 'no')
gender_list = ('f', 'm')
blood_type_list = ('+a','-a','+b','-b','+ab','-ab','+0','-0')
# functions
def __init__(self, name, weight, date_of_birth, unique_id, blood_type, expiration_of_id, email_address, mobile_number, last_donation_date, was_sick, gender):
self.name = name
self.weight = weight
self.date_of_birth = date_of_birth
self.unique_id = unique_id
self.blood_type = blood_type
self.expiration_of_id = expiration_of_id
self.email_address = email_address
self.mobile_number = mobile_number
self.last_donation_date = last_donation_date
self.was_sick = was_sick
self.gender = gender
def parse_name(self):
splitted = self.name.split(" ")
return splitted
def valid_name(self):
name_parts = self.name.split(" ")
if not (len(name_parts) >= 2):
print("Your name is too short!")
return False
for i in name_parts:
if not (i.isalpha()):
print("Name should contains only letters!")
return False
return True
def get_name(self):
while self.name == "":
self.name = input("Name:")
if self.name == "":
print("Name field cannot be empty!")
elif not (self.valid_name()):
self.name = ""
def check_weight(self):
if not self.weight.isdigit():
print("Weight should be number!")
return False
return True
def valid_weight(self):
if not self.check_weight():
return False
elif int(self.weight) < 0:
print("Weight should be a positive integer!")
return False
elif int(self.weight) < 50:
self.not_suitable("You are too light!")
return True
return True
def get_weight(self):
while self.weight == "":
self.weight = input("Weight(kg):")
if self.weight == "":
print("Weight field cannot be empty!")
elif not (self.check_weight()):
self.weight = ""
elif not (self.valid_weight()):
self.weight = ""
def check_gender(self):
if self.gender.lower() not in self.gender_list:
print("Only F or M")
return False
return True
def get_gender(self):
while self.gender == "":
self.gender = input("Gender (f/m):")
if self.gender == "":
print("Gender field cannot be empty!")
elif not self.check_gender():
self.gender = ""
def check_birth_date(self):
self.birth_date_string=datetime.strptime(self.date_of_birth, '%Y.%m.%d')
self.age=(datetime.now()-self.birth_date_string).days//365
if self.birth_date_string>datetime.now():
print("I think you was born before now!")
return False
elif self.age<18:
self.not_suitable("You are too young, you need to be at least 18 years old.")
return True
else:
return True
def get_birth_of_date(self):
while self.date_of_birth == "":
self.date_of_birth = input("Date of birth(YYYY.MM.DD):")
if self.date_of_birth == "":
print("Date of birth field cannot be empty!")
elif not (date_handle.check_date(self.date_of_birth)and self.check_birth_date()):
self.date_of_birth = ""
def valid_last_donation_date(self): #Peter
donationdate=datetime.strptime(self.last_donation_date, '%Y.%m.%d')
if donationdate>datetime.now():
print("The donation must be earlier than the today date!")
return False
time_from_last_donation=(datetime.now()-donationdate).days
if time_from_last_donation<90:
self.not_suitable("You need at least 90 days from your last donation")
return True
return True
def get_last_donation_date(self):
while self.last_donation_date == "":
self.last_donation_date = input("Last donation date:")
if self.last_donation_date == "":
print("Last donation field cannot be empty!")
elif not (date_handle.check_date(self.last_donation_date) and self.valid_last_donation_date()):
self.last_donation_date = ""
def check_was_sick(self):
if self.was_sick.lower() not in self.was_sick_list:
print("You can choose between: yes/no")
return False
return True
def get_was_sick(self):
while self.was_sick == "":
self.was_sick = input("Was he/she sick in the last month?(yes/no):")
if self.was_sick == "":
print("Was he/she sick in the last month' cannot be empty!")
elif not (self.check_was_sick()):
self.was_sick = ""
if self.was_sick == "yes":
self.not_suitable("Sorry, you cannot be a donor!")
return True
return True
def add_unique_id_if_not_unique(self):
with open('Data\donor.csv', 'r+') as read:
reader = csv.reader(read)
for row in reader:
if row[7].lower() == self.unique_id:
print("This unique id is already living in database!\n")
print("1. Did you miss type? ID again.")
print("2. Add new Donor")
print("3. Back to main menu\n")
number=msvcrt.getwch()
if number=="1":
self.unique_id=""
self.get_unique_id()
elif number=="2":
donor_main()
elif number=="3":
main.creat_menu()
else:
self.bad_input()
def bad_input(self):
print("Choose from above!")
number=msvcrt.getwch()
if number=="1":
self.get_unique_id()
elif number=="2":
donor_main()
elif number=="3":
main.creat_menu()
else:
self.bad_input()
def check_unique_id(self):
if (len(self.unique_id)) < 8:
print("Unique id is too short!")
return False
elif len(self.unique_id) > 8:
print("Unique id is too long!")
return False
elif self.unique_id[:6].isdigit() and self.unique_id[6:].isalpha():
print("This is identity card!")
return True
elif self.unique_id[:6].isalpha() and self.unique_id[6:].isdigit():
print("This is passport!")
return True
return False
def get_unique_id(self):
while self.unique_id == "":
self.unique_id = input("Identity card(6 digit + 2 letter) or passport card(6 letter + 2 digit):").lower()
if self.unique_id == "":
print("ID field cannot be empty!")
elif not(self.check_unique_id()):
self.unique_id = ""
self.add_unique_id_if_not_unique()
def check_blood_type(self):
if self.blood_type.lower() not in self.blood_type_list:
print("You can choose: +a,-a,+b,-b,+ab,-ab,+0,-0!")
return False
return True
def get_blood_type(self):
while self.blood_type == "":
self.blood_type = input("Blood type(+a,-a,+b,-b,+ab,-ab,+0,-0):")
if self.blood_type == "":
print("Blood type field cannot be empty!")
elif not self.check_blood_type():
self.blood_type = ""
def valid_expiration_of_id(self):
expiration=datetime.strptime(self.expiration_of_id, '%Y.%m.%d')
if expiration<datetime.now():
self.not_suitable("Your ID has expired!")
return True
#exit()
return True
def get_expiration_of_id(self): # ide is majd hivd meg a valid_expiration_of_id-t
while self.expiration_of_id == "":
self.expiration_of_id = input("Expiration of id:")
if self.expiration_of_id == "":
print("Expiration of id filed cannot be empty!")
elif not (date_handle.check_date(self.expiration_of_id)and self.valid_expiration_of_id()):
self.expiration_of_id = ""
def valid_email_address(self):
if not (self.email_address.endswith('.hu') or self.email_address.endswith('.com')):
print("Email address must ends with .hu or .com!")
return False
if '@' not in self.email_address:
print("Email must contain a @!")
return False
if self.email_address[0]=="@":
print("This is not a valid e-mail address!")
return False
return True
def get_email_address(self):
while self.email_address == "":
self.email_address = input("Email address:")
if self.email_address == "":
print("Email address field cannot be empty!")
elif not self.valid_email_address():
self.email_address = ""
def valid_mobile_number(self):
if not (self.mobile_number.startswith("+36") or self.mobile_number.startswith("06")):
print("Mobile number must start with +36 or 06!")
return False
check_digit = 0
if self.mobile_number.startswith("+"):
check_digit = 1
mobile_nr_check = self.mobile_number[check_digit:]
if not mobile_nr_check.isdigit():
print("Mobile number should be integer!")
return False
if len(mobile_nr_check) < 11:
print("Mobile number is too short!")
return False
if len(mobile_nr_check) > 11:
print("Mobile number is too long!")
return False
if not (mobile_nr_check[2:4] == "30" or mobile_nr_check[2:4] == "20" or mobile_nr_check[2:4] == "70"):
print("Region number should be 30, 20, 70")
return False
return True
def get_mobile_number(self):
while self.mobile_number == "":
self.mobile_number = input("Mobile number (e.g 06301234567):")
if self.mobile_number == "":
print("Mobile number field cannot be empty!")
elif not self.valid_mobile_number():
self.mobile_number = ""
def get_hemoglobin(self):
self.hemoglobin=randint(80,201)
if self.hemoglobin > 110:
print("Cool ! You're ready for donation.")
else:
self.not_suitable("Your hemoglobin level is too low!")
return True
return True
def write_to_csv_file(self):
if "," in self.email_address:
self.email_address="\""+self.email_address+"\""
self.unique_id=self.unique_id.lower()
with open(os.path.join(os.path.dirname(sys.argv[0]), "Data/donor.csv"), 'a', newline='\n') as csvfile:
write_to_donor_csv = csv.writer(csvfile)
write_to_donor_csv.writerow([self.name,self.weight,
self.date_of_birth[0:4]+"."+self.date_of_birth[5:7]+"."+self.date_of_birth[8:10],
self.age,self.last_donation_date,self.was_sick,self.gender,self.unique_id,
self.expiration_of_id,self.email_address,self.blood_type,self.mobile_number,
self.hemoglobin])
csvfile.close()
def not_suitable(self,string):
print(string)
pass
def create_class():
return Donor_class("", "", "", "", "", "", "", "", "", "", "")
def donor_main():
don = Donor_class("", "", "", "", "", "", "", "", "", "", "")
don.get_name()
don.get_weight()
don.get_birth_of_date()
don.get_last_donation_date()
don.get_was_sick()
don.get_gender()
don.get_unique_id()
don.get_expiration_of_id()
don.get_blood_type()
don.get_hemoglobin()
don.get_email_address()
don.get_mobile_number()
don.write_to_csv_file()