This commit is contained in:
2025-03-01 17:23:20 +08:00
parent f402baa984
commit eb2500a74d
2 changed files with 66 additions and 13 deletions
+39 -13
View File
@@ -1,4 +1,4 @@
from flask import render_template, request, redirect, url_for, send_from_directory
from flask import render_template, request, redirect, url_for, send_from_directory, flash
from flask_login import login_user, login_required, logout_user, current_user
from app import app, db, bcrypt, login_manager
from models import User, Listing, Booking, Image, Document, Amenities
@@ -227,10 +227,11 @@ def booking(listing_id):
user_document = Document.query.filter_by(user_id=current_user.id).first()
if not user_document or not user_document.verified:
return redirect(url_for('upload_document'))
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')
check_in = datetime.strptime(request.form['check_in'], '%Y-%m-%d').date()
check_out = datetime.strptime(request.form['check_out'], '%Y-%m-%d').date()
guests = int(request.form['guests'])
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 \
@@ -239,21 +240,46 @@ def booking(listing_id):
try:
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'))
total_days = (check_out - check_in).days
total_price = total_days * listing.price
return redirect(url_for('payment', listing_id=listing.id, check_in=check_in.isoformat(), check_out=check_out.isoformat(), guests=guests, total_price=total_price))
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('/payment', methods=['GET', 'POST'])
@login_required
def payment():
listing_id = request.args.get('listing_id', type=int)
check_in = request.args.get('check_in')
check_out = request.args.get('check_out')
guests = request.args.get('guests', type=int)
total_price = request.args.get('total_price', type=float)
if request.method == 'POST':
flash("Payment successful!", "success")
booking = Booking(
user_id=current_user.id,
listing_id=listing_id,
check_in=datetime.strptime(check_in, '%Y-%m-%d'),
check_out=datetime.strptime(check_out, '%Y-%m-%d'),
guests=guests
)
db.session.add(booking)
db.session.commit()
return redirect(url_for('home'))
return render_template('payment.html', total_price=total_price)
@app.route('/view/<int:listing_id>', methods=['GET'])
def view_listing(listing_id):
listing = Listing.query.get_or_404(listing_id)