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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, redirect, url_for, session, flash, jsonify
|
| 2 |
+
from flask_pymongo import PyMongo
|
| 3 |
+
from pymongo import MongoClient
|
| 4 |
+
import bcrypt
|
| 5 |
+
import smtplib
|
| 6 |
+
from email.mime.text import MimeText
|
| 7 |
+
from email.mime.multipart import MimeMultipart
|
| 8 |
+
import os
|
| 9 |
+
from datetime import datetime
|
| 10 |
+
import uuid
|
| 11 |
+
|
| 12 |
+
app = Flask(__name__)
|
| 13 |
+
app.secret_key = 'carwala_secret_key_2024'
|
| 14 |
+
|
| 15 |
+
# MongoDB configuration
|
| 16 |
+
app.config["MONGO_URI"] = "mongodb+srv://arshbir:arshbir123@arshbir.9pulohe.mongodb.net/carwala?retryWrites=true&w=majority"
|
| 17 |
+
mongo = PyMongo(app)
|
| 18 |
+
|
| 19 |
+
# Email configuration
|
| 20 |
+
SMTP_EMAIL = "singharshbir76@gmail.com"
|
| 21 |
+
SMTP_PASSWORD = "wpiy tuxp pgpf wljz"
|
| 22 |
+
|
| 23 |
+
# Initialize collections
|
| 24 |
+
users = mongo.db.users
|
| 25 |
+
cars = mongo.db.cars
|
| 26 |
+
appointments = mongo.db.appointments
|
| 27 |
+
|
| 28 |
+
# Admin credentials
|
| 29 |
+
ADMIN_EMAIL = "singharshbir76@gmail.com"
|
| 30 |
+
ADMIN_PASSWORD = "arshbir"
|
| 31 |
+
|
| 32 |
+
# Routes
|
| 33 |
+
@app.route('/')
|
| 34 |
+
def index():
|
| 35 |
+
# Get all approved cars
|
| 36 |
+
car_list = list(cars.find({"status": "approved"}))
|
| 37 |
+
return render_template('index.html', cars=car_list)
|
| 38 |
+
|
| 39 |
+
@app.route('/login', methods=['GET', 'POST'])
|
| 40 |
+
def login():
|
| 41 |
+
if request.method == 'POST':
|
| 42 |
+
email = request.form.get('email')
|
| 43 |
+
password = request.form.get('password')
|
| 44 |
+
|
| 45 |
+
user = users.find_one({"email": email})
|
| 46 |
+
|
| 47 |
+
if user:
|
| 48 |
+
# Check if user is admin
|
| 49 |
+
if email == ADMIN_EMAIL and password == ADMIN_PASSWORD:
|
| 50 |
+
session['user_id'] = str(user['_id'])
|
| 51 |
+
session['role'] = 'admin'
|
| 52 |
+
session['email'] = email
|
| 53 |
+
return redirect(url_for('admin_dashboard'))
|
| 54 |
+
|
| 55 |
+
# Check password for regular users
|
| 56 |
+
if bcrypt.checkpw(password.encode('utf-8'), user['password']):
|
| 57 |
+
# Check if seller is approved
|
| 58 |
+
if user['role'] == 'seller' and not user.get('approved', False):
|
| 59 |
+
flash('Your seller account is pending approval by admin.')
|
| 60 |
+
return redirect(url_for('login'))
|
| 61 |
+
|
| 62 |
+
session['user_id'] = str(user['_id'])
|
| 63 |
+
session['role'] = user['role']
|
| 64 |
+
session['email'] = email
|
| 65 |
+
|
| 66 |
+
if user['role'] == 'buyer':
|
| 67 |
+
return redirect(url_for('index'))
|
| 68 |
+
elif user['role'] == 'seller':
|
| 69 |
+
return redirect(url_for('seller_dashboard'))
|
| 70 |
+
else:
|
| 71 |
+
flash('Invalid email or password')
|
| 72 |
+
else:
|
| 73 |
+
flash('User not found')
|
| 74 |
+
|
| 75 |
+
return render_template('login.html')
|
| 76 |
+
|
| 77 |
+
@app.route('/register', methods=['GET', 'POST'])
|
| 78 |
+
def register():
|
| 79 |
+
if request.method == 'POST':
|
| 80 |
+
name = request.form.get('name')
|
| 81 |
+
email = request.form.get('email')
|
| 82 |
+
password = request.form.get('password')
|
| 83 |
+
role = request.form.get('role')
|
| 84 |
+
address = request.form.get('address')
|
| 85 |
+
phone = request.form.get('phone')
|
| 86 |
+
|
| 87 |
+
# Check if user already exists
|
| 88 |
+
if users.find_one({"email": email}):
|
| 89 |
+
flash('Email already registered')
|
| 90 |
+
return redirect(url_for('register'))
|
| 91 |
+
|
| 92 |
+
# Hash password
|
| 93 |
+
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
| 94 |
+
|
| 95 |
+
# Create user document
|
| 96 |
+
user_data = {
|
| 97 |
+
"name": name,
|
| 98 |
+
"email": email,
|
| 99 |
+
"password": hashed_password,
|
| 100 |
+
"role": role,
|
| 101 |
+
"address": address,
|
| 102 |
+
"phone": phone,
|
| 103 |
+
"approved": True if role == 'buyer' else False,
|
| 104 |
+
"created_at": datetime.now()
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
users.insert_one(user_data)
|
| 108 |
+
|
| 109 |
+
if role == 'buyer':
|
| 110 |
+
flash('Registration successful! Please login.')
|
| 111 |
+
return redirect(url_for('login'))
|
| 112 |
+
else:
|
| 113 |
+
flash('Registration submitted. Waiting for admin approval.')
|
| 114 |
+
return redirect(url_for('login'))
|
| 115 |
+
|
| 116 |
+
return render_template('register.html')
|
| 117 |
+
|
| 118 |
+
@app.route('/logout')
|
| 119 |
+
def logout():
|
| 120 |
+
session.clear()
|
| 121 |
+
return redirect(url_for('index'))
|
| 122 |
+
|
| 123 |
+
@app.route('/admin')
|
| 124 |
+
def admin_dashboard():
|
| 125 |
+
if 'user_id' not in session or session.get('role') != 'admin':
|
| 126 |
+
return redirect(url_for('login'))
|
| 127 |
+
|
| 128 |
+
# Get pending seller approvals
|
| 129 |
+
pending_sellers = list(users.find({"role": "seller", "approved": False}))
|
| 130 |
+
|
| 131 |
+
# Get all cars for approval
|
| 132 |
+
pending_cars = list(cars.find({"status": "pending"}))
|
| 133 |
+
|
| 134 |
+
return render_template('admin.html',
|
| 135 |
+
pending_sellers=pending_sellers,
|
| 136 |
+
pending_cars=pending_cars)
|
| 137 |
+
|
| 138 |
+
@app.route('/approve_seller/<user_id>')
|
| 139 |
+
def approve_seller(user_id):
|
| 140 |
+
if 'user_id' not in session or session.get('role') != 'admin':
|
| 141 |
+
return redirect(url_for('login'))
|
| 142 |
+
|
| 143 |
+
users.update_one({"_id": user_id}, {"$set": {"approved": True}})
|
| 144 |
+
flash('Seller approved successfully')
|
| 145 |
+
return redirect(url_for('admin_dashboard'))
|
| 146 |
+
|
| 147 |
+
@app.route('/seller')
|
| 148 |
+
def seller_dashboard():
|
| 149 |
+
if 'user_id' not in session or session.get('role') != 'seller':
|
| 150 |
+
return redirect(url_for('login'))
|
| 151 |
+
|
| 152 |
+
# Check if seller is approved
|
| 153 |
+
user = users.find_one({"_id": session['user_id']})
|
| 154 |
+
if not user.get('approved', False):
|
| 155 |
+
flash('Your seller account is pending approval.')
|
| 156 |
+
return redirect(url_for('login'))
|
| 157 |
+
|
| 158 |
+
# Get seller's cars
|
| 159 |
+
seller_cars = list(cars.find({"seller_id": session['user_id']}))
|
| 160 |
+
return render_template('seller_dashboard.html', cars=seller_cars)
|
| 161 |
+
|
| 162 |
+
@app.route('/add_car', methods=['POST'])
|
| 163 |
+
def add_car():
|
| 164 |
+
if 'user_id' not in session or session.get('role') != 'seller':
|
| 165 |
+
return redirect(url_for('login'))
|
| 166 |
+
|
| 167 |
+
name = request.form.get('name')
|
| 168 |
+
year = request.form.get('year')
|
| 169 |
+
price = request.form.get('price')
|
| 170 |
+
description = request.form.get('description')
|
| 171 |
+
|
| 172 |
+
car_data = {
|
| 173 |
+
"name": name,
|
| 174 |
+
"year": year,
|
| 175 |
+
"price": price,
|
| 176 |
+
"description": description,
|
| 177 |
+
"seller_id": session['user_id'],
|
| 178 |
+
"seller_email": session['email'],
|
| 179 |
+
"status": "pending",
|
| 180 |
+
"created_at": datetime.now()
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
cars.insert_one(car_data)
|
| 184 |
+
flash('Car added successfully. Waiting for admin approval.')
|
| 185 |
+
return redirect(url_for('seller_dashboard'))
|
| 186 |
+
|
| 187 |
+
@app.route('/approve_car/<car_id>')
|
| 188 |
+
def approve_car(car_id):
|
| 189 |
+
if 'user_id' not in session or session.get('role') != 'admin':
|
| 190 |
+
return redirect(url_for('login'))
|
| 191 |
+
|
| 192 |
+
cars.update_one({"_id": car_id}, {"$set": {"status": "approved"}})
|
| 193 |
+
flash('Car approved successfully')
|
| 194 |
+
return redirect(url_for('admin_dashboard'))
|
| 195 |
+
|
| 196 |
+
@app.route('/book_appointment/<car_id>', methods=['GET', 'POST'])
|
| 197 |
+
def book_appointment(car_id):
|
| 198 |
+
if 'user_id' not in session or session.get('role') != 'buyer':
|
| 199 |
+
return redirect(url_for('login'))
|
| 200 |
+
|
| 201 |
+
car = cars.find_one({"_id": car_id})
|
| 202 |
+
|
| 203 |
+
if request.method == 'POST':
|
| 204 |
+
name = request.form.get('name')
|
| 205 |
+
email = request.form.get('email')
|
| 206 |
+
address = request.form.get('address')
|
| 207 |
+
phone = request.form.get('phone')
|
| 208 |
+
preferred_date = request.form.get('preferred_date')
|
| 209 |
+
preferred_time = request.form.get('preferred_time')
|
| 210 |
+
|
| 211 |
+
appointment_data = {
|
| 212 |
+
"car_id": car_id,
|
| 213 |
+
"car_name": car['name'],
|
| 214 |
+
"buyer_id": session['user_id'],
|
| 215 |
+
"buyer_name": name,
|
| 216 |
+
"buyer_email": email,
|
| 217 |
+
"buyer_address": address,
|
| 218 |
+
"buyer_phone": phone,
|
| 219 |
+
"preferred_date": preferred_date,
|
| 220 |
+
"preferred_time": preferred_time,
|
| 221 |
+
"status": "pending",
|
| 222 |
+
"created_at": datetime.now()
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
appointments.insert_one(appointment_data)
|
| 226 |
+
flash('Appointment booked successfully. Seller will contact you soon.')
|
| 227 |
+
return redirect(url_for('index'))
|
| 228 |
+
|
| 229 |
+
return render_template('appointment.html', car=car)
|
| 230 |
+
|
| 231 |
+
@app.route('/seller_appointments/<car_id>')
|
| 232 |
+
def seller_appointments(car_id):
|
| 233 |
+
if 'user_id' not in session or session.get('role') != 'seller':
|
| 234 |
+
return redirect(url_for('login'))
|
| 235 |
+
|
| 236 |
+
car_appointments = list(appointments.find({"car_id": car_id}))
|
| 237 |
+
car = cars.find_one({"_id": car_id})
|
| 238 |
+
return render_template('seller.html', appointments=car_appointments, car=car)
|
| 239 |
+
|
| 240 |
+
@app.route('/approve_appointment/<appointment_id>', methods=['POST'])
|
| 241 |
+
def approve_appointment(appointment_id):
|
| 242 |
+
if 'user_id' not in session or session.get('role') != 'seller':
|
| 243 |
+
return redirect(url_for('login'))
|
| 244 |
+
|
| 245 |
+
meeting_date = request.form.get('meeting_date')
|
| 246 |
+
meeting_time = request.form.get('meeting_time')
|
| 247 |
+
meeting_place = request.form.get('meeting_place')
|
| 248 |
+
|
| 249 |
+
appointment = appointments.find_one({"_id": appointment_id})
|
| 250 |
+
|
| 251 |
+
# Update appointment status
|
| 252 |
+
appointments.update_one(
|
| 253 |
+
{"_id": appointment_id},
|
| 254 |
+
{"$set": {
|
| 255 |
+
"status": "approved",
|
| 256 |
+
"meeting_date": meeting_date,
|
| 257 |
+
"meeting_time": meeting_time,
|
| 258 |
+
"meeting_place": meeting_place
|
| 259 |
+
}}
|
| 260 |
+
)
|
| 261 |
+
|
| 262 |
+
# Send email to buyer
|
| 263 |
+
send_meeting_email(
|
| 264 |
+
appointment['buyer_email'],
|
| 265 |
+
appointment['car_name'],
|
| 266 |
+
meeting_date,
|
| 267 |
+
meeting_time,
|
| 268 |
+
meeting_place
|
| 269 |
+
)
|
| 270 |
+
|
| 271 |
+
flash('Appointment approved and email sent to buyer')
|
| 272 |
+
return redirect(url_for('seller_appointments', car_id=appointment['car_id']))
|
| 273 |
+
|
| 274 |
+
def send_meeting_email(buyer_email, car_name, date, time, place):
|
| 275 |
+
try:
|
| 276 |
+
subject = f"Appointment Confirmation for {car_name}"
|
| 277 |
+
body = f"""
|
| 278 |
+
Dear Buyer,
|
| 279 |
+
|
| 280 |
+
Your appointment for {car_name} has been approved.
|
| 281 |
+
|
| 282 |
+
Meeting Details:
|
| 283 |
+
Date: {date}
|
| 284 |
+
Time: {time}
|
| 285 |
+
Place: {place}
|
| 286 |
+
|
| 287 |
+
Please arrive on time for the test drive.
|
| 288 |
+
|
| 289 |
+
Best regards,
|
| 290 |
+
Carwala Team
|
| 291 |
+
"""
|
| 292 |
+
|
| 293 |
+
msg = MimeMultipart()
|
| 294 |
+
msg['From'] = SMTP_EMAIL
|
| 295 |
+
msg['To'] = buyer_email
|
| 296 |
+
msg['Subject'] = subject
|
| 297 |
+
|
| 298 |
+
msg.attach(MimeText(body, 'plain'))
|
| 299 |
+
|
| 300 |
+
server = smtplib.SMTP('smtp.gmail.com', 587)
|
| 301 |
+
server.starttls()
|
| 302 |
+
server.login(SMTP_EMAIL, SMTP_PASSWORD)
|
| 303 |
+
text = msg.as_string()
|
| 304 |
+
server.sendmail(SMTP_EMAIL, buyer_email, text)
|
| 305 |
+
server.quit()
|
| 306 |
+
|
| 307 |
+
return True
|
| 308 |
+
except Exception as e:
|
| 309 |
+
print(f"Email error: {e}")
|
| 310 |
+
return False
|
| 311 |
+
|
| 312 |
+
if __name__ == '__main__':
|
| 313 |
+
app.run(host='0.0.0.0', port=5000, debug=True)
|