edit listing
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
{% extends 'base.html' %}
|
||||
{% block title %}Edit Listing{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-md-6">
|
||||
<h1 class="text-center">Edit Listing</h1>
|
||||
<form method="POST">
|
||||
<div class="mb-3">
|
||||
<label for="title" class="form-label">Title</label>
|
||||
<input type="text" class="form-control" id="title" name="title" required value="{{ listing.title }}">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="description" class="form-label">Description</label>
|
||||
<textarea class="form-control" id="description" name="description" rows="3" required>{{ listing.description }}</textarea>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="price" class="form-label">Price</label>
|
||||
<input type="number" class="form-control" id="price" name="price" required value="{{ listing.price }}">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="location" class="form-label">Location</label>
|
||||
<input type="text" class="form-control" id="location" name="location" required value="{{ listing.location }}">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="property_type" class="form-label">Property Type</label>
|
||||
<select class="form-select" id="property_type" name="property_type" required>
|
||||
{% for key, value in listing.PROPERTY_TYPES.items() %}
|
||||
<option value="{{ key }}" {% if key == listing.property_type %}selected{% endif %}>{{ value }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Amenities</label>
|
||||
<div class="row">
|
||||
{% for amenity in amenities_list %}
|
||||
<div class="col-md-6">
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="checkbox" name="amenities" value="{{ amenity.id }}" id="amenity_{{ amenity.id }}" {% if amenity.id in selected_amenities %}checked{% endif %}>
|
||||
<label class="form-check-label" for="amenity_{{ amenity.id }}">
|
||||
{{ amenity.amenity }}
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<label for="latitude" class="form-label">Latitude</label>
|
||||
<input type="text" class="form-control" id="latitude" name="latitude" value="{{ listing.latitude }}">
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label for="longitude" class="form-label">Longitude</label>
|
||||
<input type="text" class="form-control" id="longitude" name="longitude" value="{{ listing.longitude }}">
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-3 mb-3">
|
||||
<label for="guests" class="form-label">Guests</label>
|
||||
<input type="number" class="form-control" id="guests" name="guests" required value="{{ listing.guests }}">
|
||||
</div>
|
||||
<div class="col-md-3 mb-3">
|
||||
<label for="beds" class="form-label">Beds</label>
|
||||
<input type="number" class="form-control" id="beds" name="beds" required value="{{ listing.beds }}">
|
||||
</div>
|
||||
<div class="col-md-3 mb-3">
|
||||
<label for="rooms" class="form-label">Rooms</label>
|
||||
<input type="number" class="form-control" id="rooms" name="rooms" required value="{{ listing.rooms }}">
|
||||
</div>
|
||||
<div class="col-md-3 mb-3">
|
||||
<label for="bathrooms" class="form-label">Bathrooms</label>
|
||||
<input type="number" class="form-control" id="bathrooms" name="bathrooms" required value="{{ listing.bathrooms }}">
|
||||
</div>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary w-100">Save Changes</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user