unavailable dates
This commit is contained in:
@@ -144,19 +144,35 @@ def create_listing():
|
||||
def booking(listing_id):
|
||||
listing = Listing.query.get_or_404(listing_id)
|
||||
booked_dates = Booking.query.filter_by(listing_id=listing_id).all()
|
||||
unavailable_dates = [
|
||||
(booking.check_in.strftime('%Y-%m-%d'), booking.check_out.strftime('%Y-%m-%d'))
|
||||
for booking in booked_dates
|
||||
]
|
||||
unavailable_dates = [(booking.check_in.strftime('%Y-%m-%d'), booking.check_out.strftime('%Y-%m-%d')) for booking in booked_dates]
|
||||
|
||||
if request.method == 'POST':
|
||||
check_in = datetime.strptime(request.form['check_in'], '%Y-%m-%d')
|
||||
check_out = datetime.strptime(request.form['check_out'], '%Y-%m-%d')
|
||||
guests = request.form['guests']
|
||||
booking = Booking(user_id=current_user.id, listing_id=listing_id, check_in=check_in, check_out=check_out, guests=guests)
|
||||
db.session.add(booking)
|
||||
db.session.commit()
|
||||
return redirect(url_for('home'))
|
||||
|
||||
for start_date, end_date in unavailable_dates:
|
||||
if (check_in.strftime('%Y-%m-%d') >= start_date and check_in.strftime('%Y-%m-%d') <= end_date) or \
|
||||
(check_out.strftime('%Y-%m-%d') >= start_date and check_out.strftime('%Y-%m-%d') <= end_date):
|
||||
return render_template('booking.html', listing=listing, unavailable_dates=unavailable_dates, error="Selected dates are not available.")
|
||||
try:
|
||||
check_in = datetime.strptime(request.form['check_in'], '%Y-%m-%d')
|
||||
check_out = datetime.strptime(request.form['check_out'], '%Y-%m-%d')
|
||||
|
||||
if check_out <= check_in:
|
||||
return render_template('booking.html', listing=listing, unavailable_dates=unavailable_dates, error="Check-out date must be after check-in date.")
|
||||
|
||||
guests = int(request.form['guests'])
|
||||
if guests > listing.guests:
|
||||
return render_template('booking.html', listing=listing, unavailable_dates=unavailable_dates, error="Number of guests exceeds maximum allowed.")
|
||||
|
||||
booking = Booking(user_id=current_user.id, listing_id=listing_id, check_in=check_in, check_out=check_out, guests=guests)
|
||||
db.session.add(booking)
|
||||
db.session.commit()
|
||||
return redirect(url_for('home'))
|
||||
|
||||
except ValueError:
|
||||
return render_template('booking.html', listing=listing, unavailable_dates=unavailable_dates, error="Invalid date format.")
|
||||
|
||||
return render_template('booking.html', listing=listing, unavailable_dates=unavailable_dates)
|
||||
|
||||
@app.route('/view/<int:listing_id>', methods=['GET'])
|
||||
|
||||
+39
-2
@@ -5,10 +5,14 @@
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-md-6">
|
||||
<h1 class="text-center">Book {{ listing.title }}</h1>
|
||||
{% if error %}
|
||||
<div class="alert alert-danger">{{ error }}</div>
|
||||
{% endif %}
|
||||
<form method="POST">
|
||||
<div class="mb-3">
|
||||
<label for="check_in" class="form-label">Check-in Date</label>
|
||||
<input type="date" class="form-control" id="check_in" name="check_in" required>
|
||||
<input type="date" class="form-control" id="check_in" name="check_in" required
|
||||
oninput="setCheckOutMinDate(); disableUnavailableDates();">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="check_out" class="form-label">Check-out Date</label>
|
||||
@@ -22,4 +26,37 @@
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
<script>
|
||||
const unavailableDates = {{ unavailable_dates|tojson }};
|
||||
|
||||
function setCheckOutMinDate() {
|
||||
const checkIn = document.getElementById('check_in').value;
|
||||
const checkOut = document.getElementById('check_out');
|
||||
checkOut.min = checkIn;
|
||||
}
|
||||
|
||||
function disableUnavailableDates() {
|
||||
const checkIn = document.getElementById('check_in');
|
||||
const checkOut = document.getElementById('check_out');
|
||||
|
||||
checkIn.addEventListener('input', () => {
|
||||
unavailableDates.forEach(([start, end]) => {
|
||||
if (checkIn.value >= start && checkIn.value <= end) {
|
||||
alert('Selected date is unavailable. Please choose another date.');
|
||||
checkIn.value = '';
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
checkOut.addEventListener('input', () => {
|
||||
unavailableDates.forEach(([start, end]) => {
|
||||
if (checkOut.value >= start && checkOut.value <= end) {
|
||||
alert('Selected date is unavailable. Please choose another date.');
|
||||
checkOut.value = '';
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
</script>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user