-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_manage_vehicles.py
executable file
·64 lines (56 loc) · 1.49 KB
/
db_manage_vehicles.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
#!venv/bin/python3
from app import db
from app.models import Vehicle
import getpass
import os
def add_vehicle():
os.system('clear')
name = input('Vehicle name: ')
password = getpass.getpass('Password: ')
mileage = input('Mileage: ')
v = Vehicle(name=name, password=password, total_mileage=mileage)
db.session.add(v)
db.session.commit()
def delete_vehicle():
error = []
while True:
os.system('clear')
if error:
print('\n'.join(error))
error = []
vehicles = Vehicle.query.all()
for v in vehicles:
print('{}: {}'.format(v.id, v.name))
id = input('Vehicle # to delete: ')
password = getpass.getpass('Password: ')
try:
id = int(id)
except ValueError:
error.append('Enter a valid vehicle number')
v = Vehicle.query.get(id)
if v and v.verify_password(password):
print('Password Verified')
break
else:
error.append('Invalid password')
db.session.delete(v)
db.session.commit()
def main():
choice = 1
while True:
os.system('clear')
print('1: Add vehicle')
print('2: Delete vehicle')
try:
choice = int(input())
break
except ValueError:
pass
if choice == 1:
add_vehicle()
elif choice == 2:
delete_vehicle()
for v in Vehicle.query.all():
print(v)
if __name__ == '__main__':
main()