Amenities
This commit is contained in:
@@ -30,6 +30,7 @@ class Listing(db.Model):
|
|||||||
rooms = db.Column(db.Integer, nullable=False, default=1)
|
rooms = db.Column(db.Integer, nullable=False, default=1)
|
||||||
bathrooms = db.Column(db.Integer, nullable=False, default=1)
|
bathrooms = db.Column(db.Integer, nullable=False, default=1)
|
||||||
beds = db.Column(db.Integer, nullable=False, default=1)
|
beds = db.Column(db.Integer, nullable=False, default=1)
|
||||||
|
amenities = db.relationship('Amenities', secondary='listing_amenities', backref='listings', lazy='dynamic')
|
||||||
|
|
||||||
PROPERTY_TYPES = {
|
PROPERTY_TYPES = {
|
||||||
'house': 'Дом',
|
'house': 'Дом',
|
||||||
@@ -39,6 +40,15 @@ class Listing(db.Model):
|
|||||||
'hotel': 'Гостиница'
|
'hotel': 'Гостиница'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Amenities(db.Model):
|
||||||
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
amenity = db.Column(db.String(100), nullable=False, unique=True)
|
||||||
|
|
||||||
|
listing_amenities = db.Table('listing_amenities',
|
||||||
|
db.Column('listing_id', db.Integer, db.ForeignKey('listing.id'), primary_key=True),
|
||||||
|
db.Column('amenity_id', db.Integer, db.ForeignKey('amenities.id'), primary_key=True)
|
||||||
|
)
|
||||||
|
|
||||||
class Image(db.Model):
|
class Image(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
url = db.Column(db.String(300), nullable=False)
|
url = db.Column(db.String(300), nullable=False)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from flask import render_template, request, redirect, url_for
|
from flask import render_template, request, redirect, url_for
|
||||||
from flask_login import login_user, login_required, logout_user, current_user
|
from flask_login import login_user, login_required, logout_user, current_user
|
||||||
from app import app, db, bcrypt, login_manager
|
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 datetime import datetime
|
||||||
from utils import generate_password, send_email, save_document
|
from utils import generate_password, send_email, save_document
|
||||||
|
|
||||||
@@ -98,6 +98,7 @@ def create_listing():
|
|||||||
if not current_user.is_host:
|
if not current_user.is_host:
|
||||||
return redirect(url_for('home'))
|
return redirect(url_for('home'))
|
||||||
|
|
||||||
|
amenities_list = Amenities.query.all()
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
title = request.form['title']
|
title = request.form['title']
|
||||||
description = request.form['description']
|
description = request.form['description']
|
||||||
@@ -110,6 +111,7 @@ def create_listing():
|
|||||||
rooms = request.form['rooms']
|
rooms = request.form['rooms']
|
||||||
beds = request.form['beds']
|
beds = request.form['beds']
|
||||||
bathrooms = request.form['bathrooms']
|
bathrooms = request.form['bathrooms']
|
||||||
|
selected_amenities = request.form.getlist('amenities')
|
||||||
listing = Listing(title=title,
|
listing = Listing(title=title,
|
||||||
description=description,
|
description=description,
|
||||||
price=price,
|
price=price,
|
||||||
@@ -119,6 +121,10 @@ def create_listing():
|
|||||||
latitude=latitude, longitude=longitude,
|
latitude=latitude, longitude=longitude,
|
||||||
guests=guests,
|
guests=guests,
|
||||||
rooms=rooms, beds=beds, bathrooms=bathrooms)
|
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.add(listing)
|
||||||
db.session.flush()
|
db.session.flush()
|
||||||
|
|
||||||
@@ -129,7 +135,7 @@ def create_listing():
|
|||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return redirect(url_for('dashboard'))
|
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'])
|
@app.route('/booking/<int:listing_id>', methods=['GET', 'POST'])
|
||||||
@login_required
|
@login_required
|
||||||
@@ -155,4 +161,5 @@ def booking(listing_id):
|
|||||||
def view_listing(listing_id):
|
def view_listing(listing_id):
|
||||||
listing = Listing.query.get_or_404(listing_id)
|
listing = Listing.query.get_or_404(listing_id)
|
||||||
property_type_display = Listing.PROPERTY_TYPES.get(listing.property_type, 'Unknown')
|
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)
|
||||||
@@ -29,6 +29,21 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</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 }}">
|
||||||
|
<label class="form-check-label" for="amenity_{{ amenity.id }}">
|
||||||
|
{{ amenity.amenity }}
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-md-6">
|
<div class="col-md-6">
|
||||||
<label for="latitude" class="form-label">Latitude</label>
|
<label for="latitude" class="form-label">Latitude</label>
|
||||||
|
|||||||
@@ -16,6 +16,15 @@
|
|||||||
<p><strong>Bathrooms:</strong> {{ listing.bathrooms }}</p>
|
<p><strong>Bathrooms:</strong> {{ listing.bathrooms }}</p>
|
||||||
<p><strong>Beds:</strong> {{ listing.beds }}</p>
|
<p><strong>Beds:</strong> {{ listing.beds }}</p>
|
||||||
|
|
||||||
|
{% if amenities %}
|
||||||
|
<h4>Additional Amenities:</h4>
|
||||||
|
<ul>
|
||||||
|
{% for amenity in amenities %}
|
||||||
|
<li>{{ amenity.amenity }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<a href="/booking/{{ listing.id }}" class="btn btn-primary mt-3">Book This Listing</a>
|
<a href="/booking/{{ listing.id }}" class="btn btn-primary mt-3">Book This Listing</a>
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user