-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram_guests.py
More file actions
171 lines (133 loc) · 5.07 KB
/
Copy pathprogram_guests.py
File metadata and controls
171 lines (133 loc) · 5.07 KB
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
import datetime
from dateutil import parser
from infrastructure.switchlang import switch
import program_hosts as hosts
import services.data_service as svc
from program_hosts import success_msg, error_msg
import infrastructure.state as state
def run():
print(' ****************** Welcome guest **************** ')
print()
show_commands()
while True:
action = hosts.get_action()
with switch(action) as s:
s.case('c', hosts.create_account)
s.case('l', hosts.log_into_account)
s.case('a', add_a_snake)
s.case('y', view_your_snakes)
s.case('b', book_a_cage)
s.case('v', view_bookings)
s.case('m', lambda: 'change_mode')
s.case('?', show_commands)
s.case('', lambda: None)
s.case(['x', 'bye', 'exit', 'exit()'], hosts.exit_app)
s.default(hosts.unknown_command)
state.reload_account()
if action:
print()
if s.result == 'change_mode':
return
def show_commands():
print('What action would you like to take:')
print('[C]reate an account')
print('[L]ogin to your account')
print('[B]ook a cage')
print('[A]dd a snake')
print('View [y]our snakes')
print('[V]iew your bookings')
print('[M]ain menu')
print('e[X]it app')
print('[?] Help (this info)')
print()
def add_a_snake():
print(' ****************** Add a snake **************** ')
if not state.active_account:
error_msg("You must log in first to add a snake")
return
name = input("What is your snake's name? ")
if not name:
error_msg('cancelled')
return
length = float(input('How long is your snake (in meters)? '))
species = input("Species? ")
is_venomous = input("Is your snake venomous [y]es, [n]o? ").lower().startswith('y')
snake = svc.add_snake(state.active_account, name, length, species, is_venomous)
state.reload_account()
success_msg('Created {} with id {}'.format(snake.name, snake.id))
def view_your_snakes():
print(' ****************** Your snakes **************** ')
if not state.active_account:
error_msg("You must log in first to view your snakes")
return
snakes = svc.get_snakes_for_user(state.active_account.id)
print("You have {} snakes.".format(len(snakes)))
for s in snakes:
print(" * {} is a {} that is {}m long and is {}venomous.".format(
s.name,
s.species,
s.length,
'' if s.is_venomous else 'not '
))
def book_a_cage():
print(' ****************** Book a cage **************** ')
if not state.active_account:
error_msg("You must log in first to book a cage")
return
snakes = svc.get_snakes_for_user(state.active_account.id)
if not snakes:
error_msg('You must first [a]dd a snake before you can book a cage.')
return
print("Let's start by finding available cages.")
start_text = input("Check-in date [yyyy-mm-dd]: ")
if not start_text:
error_msg('cancelled')
return
checkin = parser.parse(
start_text
)
checkout = parser.parse(
input("Check-out date [yyyy-mm-dd]: ")
)
if checkin >= checkout:
error_msg('Check in must be before check out')
return
print()
for idx, s in enumerate(snakes):
print('{}. {} (length: {}, venomous: {})'.format(
idx + 1,
s.name,
s.length,
'yes' if s.is_venomous else 'no'
))
snake = snakes[int(input('Which snake do you want to book (number)')) - 1]
cages = svc.get_available_cages(checkin, checkout, snake)
print("There are {} cages available in that time.".format(len(cages)))
for idx, c in enumerate(cages):
print(" {}. {} with {}m carpeted: {}, has toys: {}.".format(
idx + 1,
c.name,
c.square_meters,
'yes' if c.is_carpeted else 'no',
'yes' if c.has_toys else 'no'))
if not cages:
error_msg("Sorry, no cages are available for that date.")
return
cage = cages[int(input('Which cage do you want to book (number)')) - 1]
svc.book_cage(state.active_account, snake, cage, checkin, checkout)
success_msg('Successfully booked {} for {} at ${}/night.'.format(cage.name, snake.name, cage.price))
def view_bookings():
print(' ****************** Your bookings **************** ')
if not state.active_account:
error_msg("You must log in first to register a cage")
return
snakes = {s.id: s for s in svc.get_snakes_for_user(state.active_account.id)}
bookings = svc.get_bookings_for_user(state.active_account.email)
print("You have {} bookings.".format(len(bookings)))
for b in bookings:
print(' * Snake: {} is booked at {} from {} for {} days.'.format(
snakes.get(b.guest_snake_id).name,
b.cage.name,
datetime.date(b.check_in_date.year, b.check_in_date.month, b.check_in_date.day),
(b.check_out_date - b.check_in_date).days
))