CHecks for availabiliy before creating reservation

This commit is contained in:
Ruidy Nemausat 2019-12-03 16:31:21 +01:00
parent be7bdec84f
commit 1527da9fd3
5 changed files with 100 additions and 29 deletions

View file

@ -79,8 +79,7 @@ Le visiteur doit pouvoir :
- Page service : navette + location
- Gestion du calendrier
.._ Google calendar pour afficher les disponibilités
.._ Check si les dates choisies sont libres
..\_ Google calendar pour afficher les disponibilités
- Envoyer devis réservation par mail et notification aux hôtes
- Ajout page/module de paiement
- ajouter les témoignages depuis Booking, AirBnb, ajouter le lien

17
rental/calendar.py Normal file
View file

@ -0,0 +1,17 @@
def get_bookings(place):
"""
returns a list of all related place reservations
"""
booked_dates = Reservation.objects.all()
return [booking for booking in booked_dates if booking.place.name == f"{place.name}"]
def check_availability(place, start_date, end_date):
"""
check if the related place is available during a given period
"""
bookings = get_bookings(place)
for booking in bookings:
if (booking.start <= start_date <= booking.end) or (booking.start <= end_date <= booking.end):
return False
return True

View file

@ -0,0 +1,25 @@
{% extends 'rental/base.html' %} {% block content %}
<section class="intro-single">
<div class="container">
<div class="row">
<div class="col-md-12 col-lg-8">
<div class="title-single-box">
<h1 class="title-single">
Calendrier {{place_name}}
</h1>
{% for booking in bookings %}
<h2 class="title-d">
{{booking.guest.name}}
</h2>
<ul>
<li>Réservation du {{booking.start}} au {{booking.end}}</li>
<li>Montant : {{booking.price}} €</li>
<li>Message : {{booking.message}}</li>
</ul>
{%endfor%}
</div>
</div>
</div>
</div>
</section>
{%endblock%}

View file

@ -12,6 +12,7 @@ urlpatterns = [
path('hebergements/', views.liste_location, name='list_place'),
# path('<place_name>/', views.Location.as_view(), name='detail_place'),
path('<place_name>/', views.location, name='detail_place'),
path('calendar/<place_name>/', views.calendar, name='calendar')
]
urlpatterns += staticfiles_urlpatterns()

View file

@ -6,6 +6,7 @@ from django.views.generic.base import TemplateView
from .forms import ReservationForm
from django.db import IntegrityError
from rental.pricing import get_reservation_price
from rental.calendar import check_availability
def index(request):
@ -54,25 +55,31 @@ def location(request, place_name='T2'):
if not guest.exists():
guest = Guest.objects.create(
email=email,
name=name
name=name,
phone=phone
)
else:
guest = guest.first()
place = get_object_or_404(Place, name=place_name)
price = get_reservation_price(place, start, end)
reservation = Reservation.objects.create(
guest=guest,
place=place,
message=message,
start=start,
end=end,
price=price
)
context = {
'reservation': reservation
}
return render(request, 'rental/merci.html', context)
available = check_availability(place, start, end)
if available:
price = get_reservation_price(place, start, end)
reservation = Reservation.objects.create(
guest=guest,
place=place,
message=message,
start=start,
end=end,
price=price
)
context = {
'reservation': reservation
}
return render(request, 'rental/merci.html', context)
else:
context = {'form': form}
return render(request, 'rental/reservation.html', context)
except IntegrityError:
form.errors['internal'] = "Une erreur interne est apparue. \
Merci de recommencer votre requête."
@ -100,23 +107,30 @@ def reservation(request):
if not guest.exists():
guest = Guest.objects.create(
email=email,
name=name
name=name,
phone=phone
)
else:
guest = guest.first()
place = get_object_or_404(Place, name=place_name)
reservation = Reservation.objects.create(
guest=guest,
place=place,
message=message,
start=start,
end=end
)
context = {
'guest': guest,
'place': place
}
return render(request, 'rental/merci.html', context)
available = check_availability(place, start, end)
price = get_reservation_price(place, start, end)
if available:
reservation = Reservation.objects.create(
guest=guest,
place=place,
message=message,
start=start,
end=end,
price=price
)
context = {
'reservation': reservation
}
return render(request, 'rental/merci.html', context)
else:
context = {'form': form}
return render(request, 'rental/reservation.html', context)
except IntegrityError:
form.errors['internal'] = "Une erreur interne est apparue. Merci de recommencer votre requête."
else:
@ -126,6 +140,21 @@ def reservation(request):
return render(request, 'rental/reservation.html', context)
def calendar(request, place_name):
"""
returns a list of all related place reservations
"""
booked_dates = Reservation.objects.all()
bookings = [
booking for booking in booked_dates if booking.place.name == place_name]
print(place_name)
context = {
'place_name': place_name,
'bookings': bookings
}
return render(request, 'rental/calendar.html', context)
class Contact(TemplateView):
template_name = 'rental/contact.html'