forked from pchojka/Py_Expense_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.py
43 lines (36 loc) · 1.32 KB
/
user.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
from PyInquirer import prompt
import csv
# Questions to be asked when user selects "New User"
user_questions = [
{
'type': 'input',
'name': 'name',
'message': '👋🏻 Welcome new user, \n What is your name ?'
}
]
# Function that makes sure that name isn't already taken in db
def is_user_new(name):
# Import csv that holds all the information on users
with open('users.csv', newline='') as csvfile:
fcc_data = csv.reader(csvfile, delimiter=' ', quotechar='|')
for user in fcc_data:
user_name = ''.join(user)
if user_name == name['name']:
return False
csvfile.close()
return True
def add_user():
# This function should create a new user, asking for its name
name = prompt(user_questions)
# Continues to ask for user's name whilst it's already taken
while not is_user_new(name):
print("🚨ERROR🚨: This name is already taken sorry darling, chose another one")
name = prompt(user_questions)
# Add to csv
with open('users.csv', 'a', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
print('Name is: ', name['name'])
writer.writerow([name['name']])
csvfile.close()
return True