edit listing

This commit is contained in:
2025-01-28 13:32:29 +08:00
parent d449acdbb5
commit 8aa9341be5
2 changed files with 128 additions and 0 deletions
+48
View File
@@ -139,6 +139,54 @@ def create_listing():
return redirect(url_for('dashboard'))
return render_template('create_listing.html', property_types=Listing.PROPERTY_TYPES, amenities_list=amenities_list)
@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)
@app.route('/booking/<int:listing_id>', methods=['GET', 'POST'])
@login_required
def booking(listing_id):