Amenities

This commit is contained in:
2025-01-21 23:44:54 +08:00
parent 95822c0bfb
commit 37ae72ceef
4 changed files with 44 additions and 3 deletions
+10 -3
View File
@@ -1,7 +1,7 @@
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
from models import User, Listing, Booking, Image, Document
from models import User, Listing, Booking, Image, Document, Amenities
from datetime import datetime
from utils import generate_password, send_email, save_document
@@ -98,6 +98,7 @@ def create_listing():
if not current_user.is_host:
return redirect(url_for('home'))
amenities_list = Amenities.query.all()
if request.method == 'POST':
title = request.form['title']
description = request.form['description']
@@ -110,6 +111,7 @@ def create_listing():
rooms = request.form['rooms']
beds = request.form['beds']
bathrooms = request.form['bathrooms']
selected_amenities = request.form.getlist('amenities')
listing = Listing(title=title,
description=description,
price=price,
@@ -119,6 +121,10 @@ def create_listing():
latitude=latitude, longitude=longitude,
guests=guests,
rooms=rooms, beds=beds, bathrooms=bathrooms)
for amenity_id in selected_amenities:
amenity = Amenities.query.get(int(amenity_id))
if amenity:
listing.amenities.append(amenity)
db.session.add(listing)
db.session.flush()
@@ -129,7 +135,7 @@ def create_listing():
db.session.commit()
return redirect(url_for('dashboard'))
return render_template('create_listing.html', property_types=Listing.PROPERTY_TYPES)
return render_template('create_listing.html', property_types=Listing.PROPERTY_TYPES, amenities_list=amenities_list)
@app.route('/booking/<int:listing_id>', methods=['GET', 'POST'])
@login_required
@@ -155,4 +161,5 @@ def booking(listing_id):
def view_listing(listing_id):
listing = Listing.query.get_or_404(listing_id)
property_type_display = Listing.PROPERTY_TYPES.get(listing.property_type, 'Unknown')
return render_template('view.html', listing=listing, property_type_display=property_type_display)
amenities = list(listing.amenities)
return render_template('view.html', listing=listing, property_type_display=property_type_display, amenities=amenities)