firrst commit
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
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
|
||||
from datetime import datetime
|
||||
|
||||
@login_manager.user_loader
|
||||
def load_user(user_id):
|
||||
return User.query.get(int(user_id))
|
||||
|
||||
@app.route('/')
|
||||
def home():
|
||||
property_type = request.args.get('property_type')
|
||||
listings = Listing.query
|
||||
available_listings = []
|
||||
for listing in listings:
|
||||
bookings = Booking.query.filter_by(listing_id=listing.id).all()
|
||||
if all(datetime.strptime(booking.check_out, '%Y-%m-%d') < datetime.now() for booking in bookings):
|
||||
available_listings.append(listing)
|
||||
if property_type:
|
||||
available_listings = [l for l in available_listings if l.property_type == property_type]
|
||||
return render_template('index.html', listings=available_listings)
|
||||
|
||||
@app.route('/login', methods=['GET', 'POST'])
|
||||
def login():
|
||||
if request.method == 'POST':
|
||||
email = request.form['email']
|
||||
password = request.form['password']
|
||||
user = User.query.filter_by(email=email).first()
|
||||
if user and bcrypt.check_password_hash(user.password, password):
|
||||
login_user(user)
|
||||
return redirect(url_for('dashboard'))
|
||||
return render_template('login.html')
|
||||
|
||||
@app.route('/register', methods=['GET', 'POST'])
|
||||
def register():
|
||||
if request.method == 'POST':
|
||||
email = request.form['email']
|
||||
password = bcrypt.generate_password_hash(request.form['password']).decode('utf-8')
|
||||
phone = request.form['phone']
|
||||
user = User(email=email, password=password, phone=phone)
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
login_user(user)
|
||||
return redirect(url_for('dashboard'))
|
||||
return render_template('register.html')
|
||||
|
||||
@app.route('/dashboard')
|
||||
@login_required
|
||||
def dashboard():
|
||||
if current_user.is_host:
|
||||
listings = Listing.query.filter_by(host_id=current_user.id).all()
|
||||
return render_template('dashboard.html', listings=listings)
|
||||
return redirect(url_for('home'))
|
||||
|
||||
@app.route('/create-listing', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def create_listing():
|
||||
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')
|
||||
listing = Listing(title=title, description=description, price=price, location=location, property_type=property_type, host_id=current_user.id, latitude=latitude, longitude=longitude)
|
||||
db.session.add(listing)
|
||||
db.session.flush()
|
||||
|
||||
image_urls = request.form.getlist('images')
|
||||
for url in image_urls:
|
||||
image = Image(url=url, listing_id=listing.id)
|
||||
db.session.add(image)
|
||||
|
||||
db.session.commit()
|
||||
return redirect(url_for('dashboard'))
|
||||
return render_template('create_listing.html')
|
||||
|
||||
@app.route('/booking/<int:listing_id>', methods=['GET', 'POST'])
|
||||
def booking(listing_id):
|
||||
listing = Listing.query.get_or_404(listing_id)
|
||||
if request.method == 'POST':
|
||||
guest_name = request.form['name']
|
||||
email = request.form['email']
|
||||
phone = request.form['phone']
|
||||
check_in = datetime.strptime(request.form['check_in'], '%Y-%m-%d')
|
||||
check_out = datetime.strptime(request.form['check_out'], '%Y-%m-%d')
|
||||
guests = request.form['guests']
|
||||
booking = Booking(guest_name=guest_name, email=email, phone=phone, listing_id=listing_id, check_in=check_in, check_out=check_out, guests=guests)
|
||||
db.session.add(booking)
|
||||
db.session.commit()
|
||||
return redirect(url_for('home'))
|
||||
return render_template('booking.html', listing=listing)
|
||||
Reference in New Issue
Block a user