Update app.py
Browse files
app.py
CHANGED
|
@@ -7,14 +7,24 @@ from email.mime.multipart import MIMEMultipart
|
|
| 7 |
from datetime import datetime
|
| 8 |
import os
|
| 9 |
from bson.objectid import ObjectId
|
|
|
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
app.secret_key = 'carwala_secret_key_2024'
|
| 13 |
|
| 14 |
# MongoDB configuration - using carwala1 database
|
| 15 |
MONGODB_URI = "mongodb+srv://arshbir:arshbir123@arshbir.9pulohe.mongodb.net/carwala1?retryWrites=true&w=majority"
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Email configuration
|
| 20 |
SMTP_EMAIL = "singharshbir76@gmail.com"
|
|
@@ -26,101 +36,78 @@ ADMIN_PASSWORD = "arshbir"
|
|
| 26 |
|
| 27 |
def initialize_database():
|
| 28 |
"""Initialize database and create collections if they don't exist"""
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
appointments = db.appointments
|
| 33 |
-
|
| 34 |
-
# Create indexes
|
| 35 |
-
users.create_index("email", unique=True)
|
| 36 |
-
cars.create_index("seller_id")
|
| 37 |
-
appointments.create_index("car_id")
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
"
|
| 61 |
-
"
|
| 62 |
-
"price": 650000,
|
| 63 |
-
"description": "Excellent condition, low mileage, fuel efficient. Petrol engine, 5-speed manual.",
|
| 64 |
-
"seller_id": "admin",
|
| 65 |
-
"seller_email": ADMIN_EMAIL,
|
| 66 |
-
"status": "approved",
|
| 67 |
-
"created_at": datetime.now()
|
| 68 |
-
},
|
| 69 |
-
{
|
| 70 |
-
"name": "Hyundai Creta",
|
| 71 |
-
"year": 2021,
|
| 72 |
-
"price": 1250000,
|
| 73 |
-
"description": "Well maintained, single owner, all services done. Diesel automatic, sunroof.",
|
| 74 |
-
"seller_id": "admin",
|
| 75 |
-
"seller_email": ADMIN_EMAIL,
|
| 76 |
-
"status": "approved",
|
| 77 |
-
"created_at": datetime.now()
|
| 78 |
-
},
|
| 79 |
-
{
|
| 80 |
-
"name": "Toyota Fortuner",
|
| 81 |
-
"year": 2020,
|
| 82 |
-
"price": 3200000,
|
| 83 |
-
"description": "Powerful engine, premium SUV, 4x4 drive. Leather seats, well maintained.",
|
| 84 |
-
"seller_id": "admin",
|
| 85 |
-
"seller_email": ADMIN_EMAIL,
|
| 86 |
-
"status": "approved",
|
| 87 |
-
"created_at": datetime.now()
|
| 88 |
-
},
|
| 89 |
-
{
|
| 90 |
-
"name": "Honda City",
|
| 91 |
-
"year": 2023,
|
| 92 |
-
"price": 1450000,
|
| 93 |
-
"description": "Brand new condition, petrol automatic, top model with all features.",
|
| 94 |
-
"seller_id": "admin",
|
| 95 |
-
"seller_email": ADMIN_EMAIL,
|
| 96 |
-
"status": "approved",
|
| 97 |
-
"created_at": datetime.now()
|
| 98 |
-
},
|
| 99 |
-
{
|
| 100 |
-
"name": "Tata Nexon",
|
| 101 |
-
"year": 2022,
|
| 102 |
-
"price": 950000,
|
| 103 |
-
"description": "Compact SUV, diesel manual, 5-star safety rating, single owner.",
|
| 104 |
-
"seller_id": "admin",
|
| 105 |
-
"seller_email": ADMIN_EMAIL,
|
| 106 |
-
"status": "approved",
|
| 107 |
-
"created_at": datetime.now()
|
| 108 |
-
},
|
| 109 |
-
{
|
| 110 |
-
"name": "Mahindra Thar",
|
| 111 |
-
"year": 2021,
|
| 112 |
-
"price": 1550000,
|
| 113 |
-
"description": "4x4 off-roader, petrol manual, adventure ready, low mileage.",
|
| 114 |
-
"seller_id": "admin",
|
| 115 |
-
"seller_email": ADMIN_EMAIL,
|
| 116 |
-
"status": "approved",
|
| 117 |
"created_at": datetime.now()
|
| 118 |
}
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
# Initialize database when app starts
|
| 126 |
initialize_database()
|
|
@@ -136,92 +123,117 @@ app.jinja_env.filters['format_price'] = format_price
|
|
| 136 |
# Routes
|
| 137 |
@app.route('/')
|
| 138 |
def index():
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
@app.route('/login', methods=['GET', 'POST'])
|
| 144 |
def login():
|
| 145 |
if request.method == 'POST':
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
else:
|
| 181 |
-
flash('
|
| 182 |
-
|
| 183 |
-
|
|
|
|
| 184 |
|
| 185 |
return render_template('login.html')
|
| 186 |
|
| 187 |
@app.route('/register', methods=['GET', 'POST'])
|
| 188 |
def register():
|
| 189 |
if request.method == 'POST':
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
return render_template('register.html')
|
| 227 |
|
|
@@ -237,31 +249,49 @@ def admin_dashboard():
|
|
| 237 |
flash('Please login as admin to access this page.')
|
| 238 |
return redirect(url_for('login'))
|
| 239 |
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
@app.route('/approve_seller/<user_id>')
|
| 259 |
def approve_seller(user_id):
|
| 260 |
if 'user_id' not in session or session.get('role') != 'admin':
|
| 261 |
return redirect(url_for('login'))
|
| 262 |
|
| 263 |
-
|
| 264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
return redirect(url_for('admin_dashboard'))
|
| 266 |
|
| 267 |
@app.route('/seller')
|
|
@@ -269,39 +299,57 @@ def seller_dashboard():
|
|
| 269 |
if 'user_id' not in session or session.get('role') != 'seller':
|
| 270 |
return redirect(url_for('login'))
|
| 271 |
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
@app.route('/add_car', methods=['POST'])
|
| 283 |
def add_car():
|
| 284 |
if 'user_id' not in session or session.get('role') != 'seller':
|
| 285 |
return redirect(url_for('login'))
|
| 286 |
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
-
db.cars.insert_one(car_data)
|
| 304 |
-
flash('Car added successfully. Waiting for admin approval.')
|
| 305 |
return redirect(url_for('seller_dashboard'))
|
| 306 |
|
| 307 |
@app.route('/approve_car/<car_id>')
|
|
@@ -309,8 +357,17 @@ def approve_car(car_id):
|
|
| 309 |
if 'user_id' not in session or session.get('role') != 'admin':
|
| 310 |
return redirect(url_for('login'))
|
| 311 |
|
| 312 |
-
|
| 313 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
return redirect(url_for('admin_dashboard'))
|
| 315 |
|
| 316 |
@app.route('/book_appointment/<car_id>', methods=['GET', 'POST'])
|
|
@@ -318,77 +375,104 @@ def book_appointment(car_id):
|
|
| 318 |
if 'user_id' not in session or session.get('role') != 'buyer':
|
| 319 |
return redirect(url_for('login'))
|
| 320 |
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
email = request.form.get('email')
|
| 326 |
-
address = request.form.get('address')
|
| 327 |
-
phone = request.form.get('phone')
|
| 328 |
-
preferred_date = request.form.get('preferred_date')
|
| 329 |
-
preferred_time = request.form.get('preferred_time')
|
| 330 |
|
| 331 |
-
|
| 332 |
-
"car_id": car_id,
|
| 333 |
-
"car_name": car['name'],
|
| 334 |
-
"buyer_id": session['user_id'],
|
| 335 |
-
"buyer_name": name,
|
| 336 |
-
"buyer_email": email,
|
| 337 |
-
"buyer_address": address,
|
| 338 |
-
"buyer_phone": phone,
|
| 339 |
-
"preferred_date": preferred_date,
|
| 340 |
-
"preferred_time": preferred_time,
|
| 341 |
-
"status": "pending",
|
| 342 |
-
"created_at": datetime.now()
|
| 343 |
-
}
|
| 344 |
|
| 345 |
-
|
| 346 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
return redirect(url_for('index'))
|
| 348 |
-
|
| 349 |
-
return render_template('appointment.html', car=car)
|
| 350 |
|
| 351 |
@app.route('/seller_appointments/<car_id>')
|
| 352 |
def seller_appointments(car_id):
|
| 353 |
if 'user_id' not in session or session.get('role') != 'seller':
|
| 354 |
return redirect(url_for('login'))
|
| 355 |
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
@app.route('/approve_appointment/<appointment_id>', methods=['POST'])
|
| 361 |
def approve_appointment(appointment_id):
|
| 362 |
if 'user_id' not in session or session.get('role') != 'seller':
|
| 363 |
return redirect(url_for('login'))
|
| 364 |
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
{"
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
"
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 390 |
|
| 391 |
-
flash('Appointment approved and email sent to buyer')
|
| 392 |
return redirect(url_for('seller_appointments', car_id=appointment['car_id']))
|
| 393 |
|
| 394 |
def send_meeting_email(buyer_email, car_name, date, time, place):
|
|
@@ -435,5 +519,14 @@ def send_meeting_email(buyer_email, car_name, date, time, place):
|
|
| 435 |
print(f"Email error: {e}")
|
| 436 |
return False
|
| 437 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 438 |
if __name__ == '__main__':
|
| 439 |
app.run(host='0.0.0.0', port=7860, debug=False)
|
|
|
|
| 7 |
from datetime import datetime
|
| 8 |
import os
|
| 9 |
from bson.objectid import ObjectId
|
| 10 |
+
import traceback
|
| 11 |
|
| 12 |
app = Flask(__name__)
|
| 13 |
app.secret_key = 'carwala_secret_key_2024'
|
| 14 |
|
| 15 |
# MongoDB configuration - using carwala1 database
|
| 16 |
MONGODB_URI = "mongodb+srv://arshbir:arshbir123@arshbir.9pulohe.mongodb.net/carwala1?retryWrites=true&w=majority"
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
client = MongoClient(MONGODB_URI)
|
| 20 |
+
# Test the connection
|
| 21 |
+
client.admin.command('ping')
|
| 22 |
+
db = client.carwala1
|
| 23 |
+
print("✅ MongoDB connection successful!")
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"❌ MongoDB connection failed: {e}")
|
| 26 |
+
# Create a dummy client to prevent crashes
|
| 27 |
+
db = None
|
| 28 |
|
| 29 |
# Email configuration
|
| 30 |
SMTP_EMAIL = "singharshbir76@gmail.com"
|
|
|
|
| 36 |
|
| 37 |
def initialize_database():
|
| 38 |
"""Initialize database and create collections if they don't exist"""
|
| 39 |
+
if db is None:
|
| 40 |
+
print("❌ Database not available")
|
| 41 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
try:
|
| 44 |
+
# Get or create collections
|
| 45 |
+
users = db.users
|
| 46 |
+
cars = db.cars
|
| 47 |
+
appointments = db.appointments
|
| 48 |
+
|
| 49 |
+
# Create indexes
|
| 50 |
+
users.create_index("email", unique=True)
|
| 51 |
+
cars.create_index("seller_id")
|
| 52 |
+
appointments.create_index("car_id")
|
| 53 |
+
|
| 54 |
+
# Create admin user if not exists
|
| 55 |
+
admin_user = users.find_one({"email": ADMIN_EMAIL})
|
| 56 |
+
if not admin_user:
|
| 57 |
+
hashed_password = bcrypt.hashpw(ADMIN_PASSWORD.encode('utf-8'), bcrypt.gensalt())
|
| 58 |
+
admin_data = {
|
| 59 |
+
"name": "Admin User",
|
| 60 |
+
"email": ADMIN_EMAIL,
|
| 61 |
+
"password": hashed_password,
|
| 62 |
+
"role": "admin",
|
| 63 |
+
"address": "Delhi, India",
|
| 64 |
+
"phone": "9876543210",
|
| 65 |
+
"approved": True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
"created_at": datetime.now()
|
| 67 |
}
|
| 68 |
+
users.insert_one(admin_data)
|
| 69 |
+
print("✅ Admin user created successfully!")
|
| 70 |
+
|
| 71 |
+
# Add some sample cars with Indian prices if database is empty
|
| 72 |
+
if cars.count_documents({}) == 0:
|
| 73 |
+
sample_cars = [
|
| 74 |
+
{
|
| 75 |
+
"name": "Maruti Suzuki Swift",
|
| 76 |
+
"year": 2022,
|
| 77 |
+
"price": 650000,
|
| 78 |
+
"description": "Excellent condition, low mileage, fuel efficient. Petrol engine, 5-speed manual.",
|
| 79 |
+
"seller_id": "admin",
|
| 80 |
+
"seller_email": ADMIN_EMAIL,
|
| 81 |
+
"status": "approved",
|
| 82 |
+
"created_at": datetime.now()
|
| 83 |
+
},
|
| 84 |
+
{
|
| 85 |
+
"name": "Hyundai Creta",
|
| 86 |
+
"year": 2021,
|
| 87 |
+
"price": 1250000,
|
| 88 |
+
"description": "Well maintained, single owner, all services done. Diesel automatic, sunroof.",
|
| 89 |
+
"seller_id": "admin",
|
| 90 |
+
"seller_email": ADMIN_EMAIL,
|
| 91 |
+
"status": "approved",
|
| 92 |
+
"created_at": datetime.now()
|
| 93 |
+
},
|
| 94 |
+
{
|
| 95 |
+
"name": "Toyota Fortuner",
|
| 96 |
+
"year": 2020,
|
| 97 |
+
"price": 3200000,
|
| 98 |
+
"description": "Powerful engine, premium SUV, 4x4 drive. Leather seats, well maintained.",
|
| 99 |
+
"seller_id": "admin",
|
| 100 |
+
"seller_email": ADMIN_EMAIL,
|
| 101 |
+
"status": "approved",
|
| 102 |
+
"created_at": datetime.now()
|
| 103 |
+
}
|
| 104 |
+
]
|
| 105 |
+
cars.insert_many(sample_cars)
|
| 106 |
+
print("✅ Sample cars added successfully!")
|
| 107 |
+
|
| 108 |
+
print("✅ Database initialized successfully!")
|
| 109 |
+
except Exception as e:
|
| 110 |
+
print(f"❌ Database initialization failed: {e}")
|
| 111 |
|
| 112 |
# Initialize database when app starts
|
| 113 |
initialize_database()
|
|
|
|
| 123 |
# Routes
|
| 124 |
@app.route('/')
|
| 125 |
def index():
|
| 126 |
+
try:
|
| 127 |
+
if db is None:
|
| 128 |
+
flash('Database connection issue. Please try again later.')
|
| 129 |
+
return render_template('index.html', cars=[])
|
| 130 |
+
|
| 131 |
+
# Get all approved cars
|
| 132 |
+
car_list = list(db.cars.find({"status": "approved"}))
|
| 133 |
+
return render_template('index.html', cars=car_list)
|
| 134 |
+
except Exception as e:
|
| 135 |
+
print(f"Error in index route: {e}")
|
| 136 |
+
flash('Error loading cars. Please try again.')
|
| 137 |
+
return render_template('index.html', cars=[])
|
| 138 |
|
| 139 |
@app.route('/login', methods=['GET', 'POST'])
|
| 140 |
def login():
|
| 141 |
if request.method == 'POST':
|
| 142 |
+
try:
|
| 143 |
+
if db is None:
|
| 144 |
+
flash('Database connection issue. Please try again later.')
|
| 145 |
+
return redirect(url_for('login'))
|
| 146 |
+
|
| 147 |
+
email = request.form.get('email')
|
| 148 |
+
password = request.form.get('password')
|
| 149 |
+
|
| 150 |
+
# First check if it's the admin login
|
| 151 |
+
if email == ADMIN_EMAIL and password == ADMIN_PASSWORD:
|
| 152 |
+
admin_user = db.users.find_one({"email": ADMIN_EMAIL})
|
| 153 |
+
if admin_user:
|
| 154 |
+
session['user_id'] = str(admin_user['_id'])
|
| 155 |
+
session['role'] = 'admin'
|
| 156 |
+
session['email'] = ADMIN_EMAIL
|
| 157 |
+
flash('Admin login successful!')
|
| 158 |
+
return redirect(url_for('admin_dashboard'))
|
| 159 |
+
|
| 160 |
+
# Regular user login
|
| 161 |
+
user = db.users.find_one({"email": email})
|
| 162 |
+
|
| 163 |
+
if user:
|
| 164 |
+
# Check password
|
| 165 |
+
if bcrypt.checkpw(password.encode('utf-8'), user['password']):
|
| 166 |
+
# Check if seller is approved
|
| 167 |
+
if user['role'] == 'seller' and not user.get('approved', False):
|
| 168 |
+
flash('Your seller account is pending approval by admin.')
|
| 169 |
+
return redirect(url_for('login'))
|
| 170 |
+
|
| 171 |
+
session['user_id'] = str(user['_id'])
|
| 172 |
+
session['role'] = user['role']
|
| 173 |
+
session['email'] = email
|
| 174 |
+
|
| 175 |
+
flash(f'Welcome back, {user["name"]}!')
|
| 176 |
+
|
| 177 |
+
if user['role'] == 'buyer':
|
| 178 |
+
return redirect(url_for('index'))
|
| 179 |
+
elif user['role'] == 'seller':
|
| 180 |
+
return redirect(url_for('seller_dashboard'))
|
| 181 |
+
else:
|
| 182 |
+
flash('Invalid email or password')
|
| 183 |
else:
|
| 184 |
+
flash('User not found. Please register first.')
|
| 185 |
+
except Exception as e:
|
| 186 |
+
print(f"Error in login: {e}")
|
| 187 |
+
flash('Login failed. Please try again.')
|
| 188 |
|
| 189 |
return render_template('login.html')
|
| 190 |
|
| 191 |
@app.route('/register', methods=['GET', 'POST'])
|
| 192 |
def register():
|
| 193 |
if request.method == 'POST':
|
| 194 |
+
try:
|
| 195 |
+
if db is None:
|
| 196 |
+
flash('Database connection issue. Please try again later.')
|
| 197 |
+
return redirect(url_for('register'))
|
| 198 |
+
|
| 199 |
+
name = request.form.get('name')
|
| 200 |
+
email = request.form.get('email')
|
| 201 |
+
password = request.form.get('password')
|
| 202 |
+
role = request.form.get('role')
|
| 203 |
+
address = request.form.get('address')
|
| 204 |
+
phone = request.form.get('phone')
|
| 205 |
+
|
| 206 |
+
# Check if user already exists
|
| 207 |
+
if db.users.find_one({"email": email}):
|
| 208 |
+
flash('Email already registered')
|
| 209 |
+
return redirect(url_for('register'))
|
| 210 |
+
|
| 211 |
+
# Hash password
|
| 212 |
+
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
| 213 |
+
|
| 214 |
+
# Create user document
|
| 215 |
+
user_data = {
|
| 216 |
+
"name": name,
|
| 217 |
+
"email": email,
|
| 218 |
+
"password": hashed_password,
|
| 219 |
+
"role": role,
|
| 220 |
+
"address": address,
|
| 221 |
+
"phone": phone,
|
| 222 |
+
"approved": True if role == 'buyer' else False,
|
| 223 |
+
"created_at": datetime.now()
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
db.users.insert_one(user_data)
|
| 227 |
+
|
| 228 |
+
if role == 'buyer':
|
| 229 |
+
flash('Registration successful! Please login.')
|
| 230 |
+
return redirect(url_for('login'))
|
| 231 |
+
else:
|
| 232 |
+
flash('Registration submitted. Waiting for admin approval.')
|
| 233 |
+
return redirect(url_for('login'))
|
| 234 |
+
except Exception as e:
|
| 235 |
+
print(f"Error in register: {e}")
|
| 236 |
+
flash('Registration failed. Please try again.')
|
| 237 |
|
| 238 |
return render_template('register.html')
|
| 239 |
|
|
|
|
| 249 |
flash('Please login as admin to access this page.')
|
| 250 |
return redirect(url_for('login'))
|
| 251 |
|
| 252 |
+
try:
|
| 253 |
+
if db is None:
|
| 254 |
+
flash('Database connection issue. Please try again later.')
|
| 255 |
+
return render_template('admin.html', pending_sellers=[], pending_cars=[], total_users=0, total_cars=0, total_appointments=0)
|
| 256 |
+
|
| 257 |
+
# Get pending seller approvals
|
| 258 |
+
pending_sellers = list(db.users.find({"role": "seller", "approved": False}))
|
| 259 |
+
|
| 260 |
+
# Get all cars for approval
|
| 261 |
+
pending_cars = list(db.cars.find({"status": "pending"}))
|
| 262 |
+
|
| 263 |
+
# Get stats
|
| 264 |
+
total_users = db.users.count_documents({})
|
| 265 |
+
total_cars = db.cars.count_documents({})
|
| 266 |
+
total_appointments = db.appointments.count_documents({})
|
| 267 |
+
|
| 268 |
+
return render_template('admin.html',
|
| 269 |
+
pending_sellers=pending_sellers,
|
| 270 |
+
pending_cars=pending_cars,
|
| 271 |
+
total_users=total_users,
|
| 272 |
+
total_cars=total_cars,
|
| 273 |
+
total_appointments=total_appointments)
|
| 274 |
+
except Exception as e:
|
| 275 |
+
print(f"Error in admin dashboard: {e}")
|
| 276 |
+
flash('Error loading admin dashboard.')
|
| 277 |
+
return render_template('admin.html', pending_sellers=[], pending_cars=[], total_users=0, total_cars=0, total_appointments=0)
|
| 278 |
|
| 279 |
@app.route('/approve_seller/<user_id>')
|
| 280 |
def approve_seller(user_id):
|
| 281 |
if 'user_id' not in session or session.get('role') != 'admin':
|
| 282 |
return redirect(url_for('login'))
|
| 283 |
|
| 284 |
+
try:
|
| 285 |
+
if db is None:
|
| 286 |
+
flash('Database connection issue.')
|
| 287 |
+
return redirect(url_for('admin_dashboard'))
|
| 288 |
+
|
| 289 |
+
db.users.update_one({"_id": ObjectId(user_id)}, {"$set": {"approved": True}})
|
| 290 |
+
flash('Seller approved successfully')
|
| 291 |
+
except Exception as e:
|
| 292 |
+
print(f"Error approving seller: {e}")
|
| 293 |
+
flash('Error approving seller.')
|
| 294 |
+
|
| 295 |
return redirect(url_for('admin_dashboard'))
|
| 296 |
|
| 297 |
@app.route('/seller')
|
|
|
|
| 299 |
if 'user_id' not in session or session.get('role') != 'seller':
|
| 300 |
return redirect(url_for('login'))
|
| 301 |
|
| 302 |
+
try:
|
| 303 |
+
if db is None:
|
| 304 |
+
flash('Database connection issue. Please try again later.')
|
| 305 |
+
return render_template('seller_dashboard.html', cars=[])
|
| 306 |
+
|
| 307 |
+
# Check if seller is approved
|
| 308 |
+
user = db.users.find_one({"_id": ObjectId(session['user_id'])})
|
| 309 |
+
if not user.get('approved', False):
|
| 310 |
+
flash('Your seller account is pending approval.')
|
| 311 |
+
return redirect(url_for('login'))
|
| 312 |
+
|
| 313 |
+
# Get seller's cars
|
| 314 |
+
seller_cars = list(db.cars.find({"seller_id": session['user_id']}))
|
| 315 |
+
return render_template('seller_dashboard.html', cars=seller_cars)
|
| 316 |
+
except Exception as e:
|
| 317 |
+
print(f"Error in seller dashboard: {e}")
|
| 318 |
+
flash('Error loading seller dashboard.')
|
| 319 |
+
return render_template('seller_dashboard.html', cars=[])
|
| 320 |
|
| 321 |
@app.route('/add_car', methods=['POST'])
|
| 322 |
def add_car():
|
| 323 |
if 'user_id' not in session or session.get('role') != 'seller':
|
| 324 |
return redirect(url_for('login'))
|
| 325 |
|
| 326 |
+
try:
|
| 327 |
+
if db is None:
|
| 328 |
+
flash('Database connection issue.')
|
| 329 |
+
return redirect(url_for('seller_dashboard'))
|
| 330 |
+
|
| 331 |
+
name = request.form.get('name')
|
| 332 |
+
year = request.form.get('year')
|
| 333 |
+
price = request.form.get('price')
|
| 334 |
+
description = request.form.get('description')
|
| 335 |
+
|
| 336 |
+
car_data = {
|
| 337 |
+
"name": name,
|
| 338 |
+
"year": int(year),
|
| 339 |
+
"price": int(price),
|
| 340 |
+
"description": description,
|
| 341 |
+
"seller_id": session['user_id'],
|
| 342 |
+
"seller_email": session['email'],
|
| 343 |
+
"status": "pending",
|
| 344 |
+
"created_at": datetime.now()
|
| 345 |
+
}
|
| 346 |
+
|
| 347 |
+
db.cars.insert_one(car_data)
|
| 348 |
+
flash('Car added successfully. Waiting for admin approval.')
|
| 349 |
+
except Exception as e:
|
| 350 |
+
print(f"Error adding car: {e}")
|
| 351 |
+
flash('Error adding car. Please try again.')
|
| 352 |
|
|
|
|
|
|
|
| 353 |
return redirect(url_for('seller_dashboard'))
|
| 354 |
|
| 355 |
@app.route('/approve_car/<car_id>')
|
|
|
|
| 357 |
if 'user_id' not in session or session.get('role') != 'admin':
|
| 358 |
return redirect(url_for('login'))
|
| 359 |
|
| 360 |
+
try:
|
| 361 |
+
if db is None:
|
| 362 |
+
flash('Database connection issue.')
|
| 363 |
+
return redirect(url_for('admin_dashboard'))
|
| 364 |
+
|
| 365 |
+
db.cars.update_one({"_id": ObjectId(car_id)}, {"$set": {"status": "approved"}})
|
| 366 |
+
flash('Car approved successfully')
|
| 367 |
+
except Exception as e:
|
| 368 |
+
print(f"Error approving car: {e}")
|
| 369 |
+
flash('Error approving car.')
|
| 370 |
+
|
| 371 |
return redirect(url_for('admin_dashboard'))
|
| 372 |
|
| 373 |
@app.route('/book_appointment/<car_id>', methods=['GET', 'POST'])
|
|
|
|
| 375 |
if 'user_id' not in session or session.get('role') != 'buyer':
|
| 376 |
return redirect(url_for('login'))
|
| 377 |
|
| 378 |
+
try:
|
| 379 |
+
if db is None:
|
| 380 |
+
flash('Database connection issue. Please try again later.')
|
| 381 |
+
return redirect(url_for('index'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
+
car = db.cars.find_one({"_id": ObjectId(car_id)})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
|
| 385 |
+
if request.method == 'POST':
|
| 386 |
+
name = request.form.get('name')
|
| 387 |
+
email = request.form.get('email')
|
| 388 |
+
address = request.form.get('address')
|
| 389 |
+
phone = request.form.get('phone')
|
| 390 |
+
preferred_date = request.form.get('preferred_date')
|
| 391 |
+
preferred_time = request.form.get('preferred_time')
|
| 392 |
+
|
| 393 |
+
appointment_data = {
|
| 394 |
+
"car_id": car_id,
|
| 395 |
+
"car_name": car['name'],
|
| 396 |
+
"buyer_id": session['user_id'],
|
| 397 |
+
"buyer_name": name,
|
| 398 |
+
"buyer_email": email,
|
| 399 |
+
"buyer_address": address,
|
| 400 |
+
"buyer_phone": phone,
|
| 401 |
+
"preferred_date": preferred_date,
|
| 402 |
+
"preferred_time": preferred_time,
|
| 403 |
+
"status": "pending",
|
| 404 |
+
"created_at": datetime.now()
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
db.appointments.insert_one(appointment_data)
|
| 408 |
+
flash('Appointment booked successfully. Seller will contact you soon.')
|
| 409 |
+
return redirect(url_for('index'))
|
| 410 |
+
|
| 411 |
+
return render_template('appointment.html', car=car)
|
| 412 |
+
except Exception as e:
|
| 413 |
+
print(f"Error booking appointment: {e}")
|
| 414 |
+
flash('Error booking appointment. Please try again.')
|
| 415 |
return redirect(url_for('index'))
|
|
|
|
|
|
|
| 416 |
|
| 417 |
@app.route('/seller_appointments/<car_id>')
|
| 418 |
def seller_appointments(car_id):
|
| 419 |
if 'user_id' not in session or session.get('role') != 'seller':
|
| 420 |
return redirect(url_for('login'))
|
| 421 |
|
| 422 |
+
try:
|
| 423 |
+
if db is None:
|
| 424 |
+
flash('Database connection issue. Please try again later.')
|
| 425 |
+
return render_template('seller.html', appointments=[], car={})
|
| 426 |
+
|
| 427 |
+
car_appointments = list(db.appointments.find({"car_id": car_id}))
|
| 428 |
+
car = db.cars.find_one({"_id": ObjectId(car_id)})
|
| 429 |
+
return render_template('seller.html', appointments=car_appointments, car=car)
|
| 430 |
+
except Exception as e:
|
| 431 |
+
print(f"Error loading appointments: {e}")
|
| 432 |
+
flash('Error loading appointments.')
|
| 433 |
+
return render_template('seller.html', appointments=[], car={})
|
| 434 |
|
| 435 |
@app.route('/approve_appointment/<appointment_id>', methods=['POST'])
|
| 436 |
def approve_appointment(appointment_id):
|
| 437 |
if 'user_id' not in session or session.get('role') != 'seller':
|
| 438 |
return redirect(url_for('login'))
|
| 439 |
|
| 440 |
+
try:
|
| 441 |
+
if db is None:
|
| 442 |
+
flash('Database connection issue.')
|
| 443 |
+
return redirect(url_for('seller_dashboard'))
|
| 444 |
+
|
| 445 |
+
meeting_date = request.form.get('meeting_date')
|
| 446 |
+
meeting_time = request.form.get('meeting_time')
|
| 447 |
+
meeting_place = request.form.get('meeting_place')
|
| 448 |
+
|
| 449 |
+
appointment = db.appointments.find_one({"_id": ObjectId(appointment_id)})
|
| 450 |
+
|
| 451 |
+
# Update appointment status
|
| 452 |
+
db.appointments.update_one(
|
| 453 |
+
{"_id": ObjectId(appointment_id)},
|
| 454 |
+
{"$set": {
|
| 455 |
+
"status": "approved",
|
| 456 |
+
"meeting_date": meeting_date,
|
| 457 |
+
"meeting_time": meeting_time,
|
| 458 |
+
"meeting_place": meeting_place
|
| 459 |
+
}}
|
| 460 |
+
)
|
| 461 |
+
|
| 462 |
+
# Send email to buyer
|
| 463 |
+
send_meeting_email(
|
| 464 |
+
appointment['buyer_email'],
|
| 465 |
+
appointment['car_name'],
|
| 466 |
+
meeting_date,
|
| 467 |
+
meeting_time,
|
| 468 |
+
meeting_place
|
| 469 |
+
)
|
| 470 |
+
|
| 471 |
+
flash('Appointment approved and email sent to buyer')
|
| 472 |
+
except Exception as e:
|
| 473 |
+
print(f"Error approving appointment: {e}")
|
| 474 |
+
flash('Error approving appointment.')
|
| 475 |
|
|
|
|
| 476 |
return redirect(url_for('seller_appointments', car_id=appointment['car_id']))
|
| 477 |
|
| 478 |
def send_meeting_email(buyer_email, car_name, date, time, place):
|
|
|
|
| 519 |
print(f"Email error: {e}")
|
| 520 |
return False
|
| 521 |
|
| 522 |
+
# Error handler
|
| 523 |
+
@app.errorhandler(500)
|
| 524 |
+
def internal_error(error):
|
| 525 |
+
return render_template('error.html'), 500
|
| 526 |
+
|
| 527 |
+
@app.errorhandler(404)
|
| 528 |
+
def not_found_error(error):
|
| 529 |
+
return render_template('error.html'), 404
|
| 530 |
+
|
| 531 |
if __name__ == '__main__':
|
| 532 |
app.run(host='0.0.0.0', port=7860, debug=False)
|