Files
booking/routes.py
T

231 lines
9.6 KiB
Python
Raw Normal View History

2025-01-04 19:57:22 +08:00
from flask import render_template, request, redirect, url_for
from flask_login import login_user, login_required, logout_user, current_user
from app import app, db, bcrypt, login_manager
2025-01-21 23:44:54 +08:00
from models import User, Listing, Booking, Image, Document, Amenities
2025-01-04 19:57:22 +08:00
from datetime import datetime
2025-01-21 20:45:29 +08:00
from utils import generate_password, send_email, save_document
2025-01-04 19:57:22 +08:00
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
@app.route('/')
def home():
property_type = request.args.get('property_type')
2025-01-12 19:54:10 +08:00
listings = Listing.query.all()
2025-01-04 19:57:22 +08:00
if property_type:
2025-01-12 19:54:10 +08:00
listings = [l for l in listings if l.property_type == property_type]
return render_template('index.html', listings=listings)
2025-01-04 19:57:22 +08:00
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
user = User.query.filter_by(email=email).first()
if user and bcrypt.check_password_hash(user.password, password):
login_user(user)
2025-01-21 20:03:30 +08:00
return redirect(url_for('upload-document'))
2025-01-04 19:57:22 +08:00
return render_template('login.html')
2025-01-04 20:23:35 +08:00
@app.route('/logout/')
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
2025-01-04 19:57:22 +08:00
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
email = request.form['email']
password_plain = generate_password()
password = bcrypt.generate_password_hash(password_plain).decode('utf-8')
2025-01-04 19:57:22 +08:00
phone = request.form['phone']
user = User(email=email, password=password, phone=phone)
db.session.add(user)
db.session.commit()
send_email(email, 'Your Account Password', f'Your password is: {password_plain}')
2025-01-04 19:57:22 +08:00
login_user(user)
2025-01-21 20:03:30 +08:00
return redirect(url_for('upload_document'))
2025-01-04 19:57:22 +08:00
return render_template('register.html')
2025-01-21 18:46:15 +08:00
@app.route('/upload-document', methods=['GET', 'POST'])
@login_required
def upload_document():
document = Document.query.filter_by(user_id=current_user.id).first()
if document:
if not document.verified:
return render_template('upload_document.html', message="Your document is under verification.")
2025-01-21 20:03:30 +08:00
return redirect(url_for('dashboard'))
2025-01-21 18:46:15 +08:00
if request.method == 'POST':
file = request.files['document']
2025-01-21 20:45:29 +08:00
if not file:
return render_template('upload_document.html', message="No file selected.")
try:
saved_filename = save_document(file, 'documents')
new_document = Document(filename=saved_filename, user_id=current_user.id)
2025-01-21 18:46:15 +08:00
db.session.add(new_document)
db.session.commit()
return redirect(url_for('upload_document'))
2025-01-21 20:45:29 +08:00
except ValueError as e:
return render_template('upload_document.html', message=str(e))
2025-01-21 18:46:15 +08:00
return render_template('upload_document.html', message=None)
@app.route('/verify-document/<int:document_id>', methods=['POST'])
@login_required
def verify_document(document_id):
if current_user.is_host:
document = Document.query.get_or_404(document_id)
document.verified = True
db.session.commit()
return redirect(url_for('dashboard'))
return redirect(url_for('home'))
2025-01-22 08:33:57 +08:00
@app.route('/dashboard', methods=['GET', 'POST'])
2025-01-04 19:57:22 +08:00
@login_required
def dashboard():
if current_user.is_host:
listings = Listing.query.filter_by(host_id=current_user.id).all()
2025-01-22 08:33:57 +08:00
users = User.query.all()
documents = {doc.user_id: doc for doc in Document.query.all()}
return render_template('dashboard.html', listings=listings, users=users, documents=documents)
2025-01-04 19:57:22 +08:00
return redirect(url_for('home'))
@app.route('/create-listing', methods=['GET', 'POST'])
@login_required
def create_listing():
2025-01-08 15:17:39 +08:00
if not current_user.is_host:
return redirect(url_for('home'))
2025-01-21 23:44:54 +08:00
amenities_list = Amenities.query.all()
2025-01-04 19:57:22 +08:00
if request.method == 'POST':
title = request.form['title']
description = request.form['description']
price = request.form['price']
location = request.form['location']
property_type = request.form['property_type']
latitude = request.form.get('latitude')
longitude = request.form.get('longitude')
2025-01-16 18:22:38 +08:00
guests = request.form['guests']
2025-01-16 19:27:11 +08:00
rooms = request.form['rooms']
beds = request.form['beds']
bathrooms = request.form['bathrooms']
2025-01-21 23:44:54 +08:00
selected_amenities = request.form.getlist('amenities')
2025-01-16 19:17:22 +08:00
listing = Listing(title=title,
description=description,
price=price,
location=location,
property_type=property_type,
host_id=current_user.id,
latitude=latitude, longitude=longitude,
2025-01-16 19:27:11 +08:00
guests=guests,
rooms=rooms, beds=beds, bathrooms=bathrooms)
2025-01-21 23:44:54 +08:00
for amenity_id in selected_amenities:
amenity = Amenities.query.get(int(amenity_id))
if amenity:
listing.amenities.append(amenity)
2025-01-04 19:57:22 +08:00
db.session.add(listing)
db.session.flush()
image_urls = request.form.getlist('images')
for url in image_urls:
image = Image(url=url, listing_id=listing.id)
db.session.add(image)
db.session.commit()
return redirect(url_for('dashboard'))
2025-01-21 23:44:54 +08:00
return render_template('create_listing.html', property_types=Listing.PROPERTY_TYPES, amenities_list=amenities_list)
2025-01-04 19:57:22 +08:00
2025-01-28 13:32:29 +08:00
@app.route('/edit-listing/<int:listing_id>', methods=['GET', 'POST'])
@login_required
def edit_listing(listing_id):
listing = Listing.query.get_or_404(listing_id)
if listing.host_id != current_user.id:
return redirect(url_for('dashboard'))
amenities_list = Amenities.query.all()
selected_amenities = [amenity.id for amenity in listing.amenities]
if request.method == 'POST':
title = request.form['title']
description = request.form['description']
price = request.form['price']
location = request.form['location']
property_type = request.form['property_type']
latitude = request.form.get('latitude')
longitude = request.form.get('longitude')
guests = request.form['guests']
rooms = request.form['rooms']
beds = request.form['beds']
bathrooms = request.form['bathrooms']
selected_amenities_ids = request.form.getlist('amenities')
listing.title = title
listing.description = description
listing.price = price
listing.location = location
listing.property_type = property_type
listing.latitude = latitude
listing.longitude = longitude
listing.guests = guests
listing.rooms = rooms
listing.beds = beds
listing.bathrooms = bathrooms
listing.amenities = []
for amenity_id in selected_amenities_ids:
amenity = Amenities.query.get(int(amenity_id))
if amenity:
listing.amenities.append(amenity)
db.session.commit()
return redirect(url_for('dashboard'))
return render_template('edit_listing.html', listing=listing, amenities_list=amenities_list, selected_amenities=selected_amenities)
2025-01-04 19:57:22 +08:00
@app.route('/booking/<int:listing_id>', methods=['GET', 'POST'])
2025-01-12 20:22:56 +08:00
@login_required
2025-01-04 19:57:22 +08:00
def booking(listing_id):
listing = Listing.query.get_or_404(listing_id)
2025-01-12 20:08:54 +08:00
booked_dates = Booking.query.filter_by(listing_id=listing_id).all()
2025-01-23 12:10:08 +08:00
unavailable_dates = [(booking.check_in.strftime('%Y-%m-%d'), booking.check_out.strftime('%Y-%m-%d')) for booking in booked_dates]
2025-01-12 20:08:54 +08:00
2025-01-04 19:57:22 +08:00
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')
2025-01-23 12:10:08 +08:00
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.")
2025-01-12 20:22:56 +08:00
return render_template('booking.html', listing=listing, unavailable_dates=unavailable_dates)
2025-01-20 17:51:42 +08:00
@app.route('/view/<int:listing_id>', methods=['GET'])
def view_listing(listing_id):
listing = Listing.query.get_or_404(listing_id)
2025-01-20 18:01:15 +08:00
property_type_display = Listing.PROPERTY_TYPES.get(listing.property_type, 'Unknown')
2025-01-21 23:44:54 +08:00
amenities = list(listing.amenities)
return render_template('view.html', listing=listing, property_type_display=property_type_display, amenities=amenities)