diff --git a/routes.py b/routes.py index 2470e2c..0a07bc6 100644 --- a/routes.py +++ b/routes.py @@ -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/', 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/', methods=['GET', 'POST']) @login_required def booking(listing_id): diff --git a/templates/edit_listing.html b/templates/edit_listing.html new file mode 100644 index 0000000..eb76280 --- /dev/null +++ b/templates/edit_listing.html @@ -0,0 +1,80 @@ +{% extends 'base.html' %} +{% block title %}Edit Listing{% endblock %} + +{% block content %} +
+
+

Edit Listing

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+ {% for amenity in amenities_list %} +
+
+ + +
+
+ {% endfor %} +
+
+
+
+ + +
+
+ + +
+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+
+{% endblock %} \ No newline at end of file