-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
339 lines (260 loc) · 9.6 KB
/
main.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
import firebase_admin
from firebase_admin import credentials, firestore
from PIL import Image, ImageFont, ImageDraw, ImageOps
try:
app = firebase_admin.get_app()
except ValueError as e:
cred = credentials.Certificate('serviceAccountKey.json')
firebase_admin.initialize_app(cred)
db = firestore.client()
rootRef = db.collection("employees")
################################################################
# Main Code -- Main Menu Loop, etc. #
################################################################
def main():
print("--------------------------------------")
print("Welcome to the Business Card Designer!")
print("--------------------------------------")
continueLoop = True
while continueLoop:
continueLoop = mainMenu()
print("\n~ Goodbye for now! ~\n")
return
################################################################
# MENUS #
################################################################
def mainMenu():
print("Select an option:")
print("(A) Add an employee")
print("(R) Remove an employee")
print("(V) View all employees")
print("(M) Modify an existing employee")
print("(B) Business card designer")
print("(E) Exit")
userInput = input("").lower()
if userInput == "a":
addEmployeeMenu()
elif userInput == "r":
removeEmployee(getEmpId())
elif userInput == "v":
printEmployees()
elif userInput == "m":
print()
print("------------------")
print("Update an Employee")
print("------------------")
empId = getEmpId()
continueLoop = True
while continueLoop:
continueLoop = modifyEmployeeMenu(empId)
elif userInput == "b":
print()
print("-------------")
print("Card Designer")
print("-------------")
empId = getEmpId()
continueLoop = True
while continueLoop:
continueLoop = businessCardMenu(empId)
elif userInput == "e":
return False
# if true, the menu will pop back up again
return True
def addEmployeeMenu():
print("\nOkay, you'll need to enter some information...")
empId = getEmpId()
name = input("Now enter his/her full name: ")
occupation = input("Now enter his/her occupation: ")
phoneNum = input("Now enter his/her phone number in the format 123-456-7890: ")
email = input("Lastly, enter his/her email in the format johnsmith@gmail.com: ")
addEmployeeToDB(empId, name, occupation, phoneNum, email)
return
def modifyEmployeeMenu(empId):
if not empExists(empId):
print("No employee with that ID!")
return False
doc = rootRef.document(empId).get()
print("\n" + doc.get("name") + "\'s profile")
print("-----------------------")
printEmployee(empId)
print("Select an option:")
print("(N) Update name")
print("(O) Update occupation")
print("(P) Update phone number")
print("(M) Update email")
print("(E) Exit")
userInput = input("").lower()
if userInput == "n":
rootRef.document(empId).update({"name": input("Enter the new name: ")})
elif userInput == "o":
rootRef.document(empId).update({"occupation": input("Enter the new occupcation: ")})
elif userInput == "p":
rootRef.document(empId).update({"phoneNum": input("Enter the new phone number: ")})
elif userInput == "m":
rootRef.document(empId).update({"email": input("Enter the new email: ")})
elif userInput == "e":
return False
return True
def businessCardMenu(empId):
doc = rootRef.document(empId).get()
borderSize = doc.get("borderSize")
color = doc.get("color")
font = doc.get("font")
if not empExists(empId):
print("No employee with that ID!")
return False
print("Change a setting, or print the business card:")
print("(B) Border size: " + str(borderSize))
print("(C) Color: " + color)
print("(F) Font: " + font)
print("(P) Print")
print("(E) Exit")
userInput = input().lower()
if userInput == "b":
rootRef.document(empId).update({"borderSize": changeBorderMenu()})
elif userInput == "c":
rootRef.document(empId).update({"color": changeColorMenu()})
elif userInput == "f":
rootRef.document(empId).update({"font": changeFontMenu()})
elif userInput == "p":
printCard(empId, borderSize, color, font)
elif userInput == "e":
return False
return True
def changeColorMenu():
print()
print("Select the main color:")
print("(B) Black")
print("(L) Blue")
print("(R) Red")
userInput = input().lower()
if userInput == "b":
return "black"
elif userInput == "l":
return "darkblue"
elif userInput == "r":
return "darkred"
else:
return "black"
def changeBorderMenu():
print()
print("Select a border width:")
print("(1) 15px")
print("(2) 30px")
userInput = input()
if userInput == "1":
return 15
elif userInput == "2":
return 30
else:
return 15
def changeFontMenu():
print()
print("Select a font:")
print("(A) Arial")
print("(G) Garamond")
userInput = input().lower()
if userInput == "a":
return "arial.ttf"
elif userInput == "g":
return "Garamond.ttf"
else:
return "arial.ttf"
################################################################
# BusinessCard Functions #
################################################################
def printCard(empId, borderSize, color, font):
doc = rootRef.document(empId).get()
name = doc.get("name")
occupation = doc.get("occupation")
phoneNum = doc.get("phoneNum")
email = doc.get("email")
firstLetter = name[0]
image = Image.new("RGBA", (600,400), "white")
draw = ImageDraw.Draw(image)
fontName = ImageFont.truetype(font, 35)
fontOccupation = ImageFont.truetype(font, 15)
fontPhoneNum = ImageFont.truetype(font, 15)
fontEmail = ImageFont.truetype(font, 15)
fontFirstLetter = ImageFont.truetype("Garamond.ttf", 300)
w,h = fontName.getsize(name)
# the line below the occupation should change according to length of name, not length of occupation;
# that's why I'm initializing a separate width and heigth here for "line"
lineW, lineH = fontName.getsize(name)
draw.multiline_text(((800-w)/2, (380-h)/2), name, font = fontName, fill = color, align = "center")
w,h = fontOccupation.getsize(occupation)
draw.multiline_text(((800-w)/2, (450-h)/2), occupation, font = fontOccupation, fill = color, align = "center")
# this next line is extremely obtuse, but all it's doing is drawing a line with the right length and position
draw.line([((800-lineW)/2 - lineW/8, (530-lineH)/2), ((800-lineW)/2 + 9 * lineW/8, (530-lineH)/2)], fill = color, width = 2, joint = "curve")
w,h = fontPhoneNum.getsize(phoneNum)
draw.multiline_text(((800-lineW)/2 - lineW/8, (590-lineH)/2), "Call: " + phoneNum, font = fontPhoneNum, fill = color, align = "center")
w,h = fontEmail.getsize(email)
draw.multiline_text(((800-lineW)/2 - lineW/8, (630-lineH)/2), "Mail to: " + email, font = fontEmail, fill = color, align = "center")
w,h = fontFirstLetter.getsize(firstLetter)
draw.multiline_text(((280-w)/2, (340-h)/2), firstLetter, font = fontFirstLetter, fill = color, align = "center")
image = ImageOps.expand(image, border = borderSize, fill = color)
image.save("businesscards/businesscard" + str(empId) + ".png")
print("\nSaved as " + "businesscard" + str(empId) + ".png\n")
return
################################################################
# Add to/Remove from Employee Database #
################################################################
def addEmployeeToDB(empId, name, occupation, phoneNum, email):
if not empExists(empId):
rootRef.document(empId).set({
"name" : name,
"occupation" : occupation,
"phoneNum" : phoneNum,
"email" : email,
"borderSize": 15,
"color": "black",
"font": "arial.ttf"
})
print("\n" + name + " was added to the database!\n")
else:
print("\nAn employee with that ID already exists!\n")
return
def removeEmployee(empId):
if empExists(empId):
rootRef.document(empId).delete()
print("\nEmployee #" + str(empId) + " was removed.\n")
else:
print("\nNo employee with that ID!\n")
return
################################################################
# Miscellaneous Functions #
################################################################
def printEmployees():
print()
print("-----------------")
print("List of Employees")
print("-----------------")
docs = rootRef.stream()
for doc in docs:
print(doc.get("name"))
print("ID: " + doc.id)
print("Occupation: " + doc.get("occupation"))
print("Phone Number: " + doc.get("phoneNum"))
print("Email: " + doc.get("email"))
print()
return
def printEmployee(empId):
doc = rootRef.document(empId).get()
print(doc.get("name"))
print("ID: " + doc.id)
print("Occupation: " + doc.get("occupation"))
print("Phone Number: " + doc.get("phoneNum"))
print("Email: " + doc.get("email"))
print()
return
def getEmpId():
empId = ""
while len(empId) != 6:
empId = input("Please enter the employee's 6-digit ID number: ")
return empId
def empExists(empId):
if rootRef.document(empId).get().exists:
return True
else:
return False
main()