Created
May 11, 2018 03:51
-
-
Save ppsirg/ddd28bfd84237903933185d4081c5ca2 to your computer and use it in GitHub Desktop.
does overbooking validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def validate_reservation_booking(initial_day, final_day, ship_object): | |
"""does reservation avoiding overbooking""" | |
response = False | |
message = [] | |
now_time = date.today() | |
# initial is less than final | |
if final_day < initial_day: | |
message.append('La fecha final no puede ser menor a la fecha inicial, por favor modifica las fechas de tu reserva.') | |
return response, ','.join(message) | |
# initial and final are in the future | |
if final_day < now_time or initial_day < now_time: | |
message.append('No es posible reservar en una fecha pasada, por favor modifica las fechas de tu reserva.') | |
return response, ','.join(message) | |
# for all reservations, check new one is not inside them or contain them | |
overbooking = False | |
# get all reservations that end in future from now | |
reservations = Reservation.objects.filter(end_day__gt=now_time, product=ship_object) | |
for item in reservations: | |
if (initial_day < final_day < item.start_day) or (item.end_day < initial_day < final_day): | |
pass | |
else: | |
overbooking = True | |
message.append('Hay una reserva de: <<{0.start_day}>> al <<{0.end_day}>>'.format(item)) | |
break | |
# if overbooking, dont accept add the reservation | |
if not overbooking: | |
message.append('not overbooking') | |
response = True | |
else: | |
message.append('overbooking, por favor modifica las fechas de tu reserva.') | |
print(message) | |
return response, ','.join(message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment