Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,9 @@ app.secret_key = 'carwala_secret_key_2024'
|
|
| 13 |
# MongoDB configuration
|
| 14 |
MONGODB_URI = "mongodb+srv://arshbir:arshbir123@arshbir.9pulohe.mongodb.net/carwala1?retryWrites=true&w=majority"
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
client = MongoClient(MONGODB_URI, serverSelectionTimeoutMS=5000)
|
| 18 |
client.admin.command('ping')
|
|
@@ -32,7 +35,8 @@ ADMIN_PASSWORD = "arshbir"
|
|
| 32 |
|
| 33 |
def initialize_database():
|
| 34 |
"""Initialize database and create collections"""
|
| 35 |
-
if not db
|
|
|
|
| 36 |
return
|
| 37 |
|
| 38 |
try:
|
|
@@ -44,16 +48,50 @@ def initialize_database():
|
|
| 44 |
if not users.find_one({"email": ADMIN_EMAIL}):
|
| 45 |
hashed = bcrypt.hashpw(ADMIN_PASSWORD.encode('utf-8'), bcrypt.gensalt())
|
| 46 |
users.insert_one({
|
| 47 |
-
"name": "Admin
|
| 48 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
})
|
| 50 |
print("β
Admin user created")
|
| 51 |
|
| 52 |
# Add sample cars
|
| 53 |
if cars.count_documents({}) == 0:
|
| 54 |
sample_cars = [
|
| 55 |
-
{
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
]
|
| 58 |
cars.insert_many(sample_cars)
|
| 59 |
print("β
Sample cars added")
|
|
@@ -78,7 +116,7 @@ app.jinja_env.filters['format_price'] = format_price
|
|
| 78 |
@app.route('/')
|
| 79 |
def index():
|
| 80 |
cars = []
|
| 81 |
-
if db:
|
| 82 |
try:
|
| 83 |
cars = list(db.cars.find({"status": "approved"}))
|
| 84 |
except Exception as e:
|
|
@@ -106,7 +144,7 @@ def login():
|
|
| 106 |
return redirect(url_for('admin_dashboard'))
|
| 107 |
|
| 108 |
# Regular user login
|
| 109 |
-
if db:
|
| 110 |
try:
|
| 111 |
user = db.users.find_one({"email": email})
|
| 112 |
if user and bcrypt.checkpw(password.encode('utf-8'), user['password']):
|
|
@@ -118,6 +156,8 @@ def login():
|
|
| 118 |
session['role'] = user['role']
|
| 119 |
session['email'] = email
|
| 120 |
|
|
|
|
|
|
|
| 121 |
if user['role'] == 'buyer':
|
| 122 |
return redirect(url_for('index'))
|
| 123 |
else:
|
|
@@ -146,7 +186,7 @@ def register():
|
|
| 146 |
flash('Please fill all fields')
|
| 147 |
return render_template('register.html')
|
| 148 |
|
| 149 |
-
if
|
| 150 |
flash('Database unavailable')
|
| 151 |
return render_template('register.html')
|
| 152 |
|
|
@@ -157,8 +197,13 @@ def register():
|
|
| 157 |
|
| 158 |
hashed = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
| 159 |
user_data = {
|
| 160 |
-
"name": name,
|
| 161 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
"created_at": datetime.now()
|
| 163 |
}
|
| 164 |
|
|
@@ -187,7 +232,7 @@ def admin_dashboard():
|
|
| 187 |
pending_sellers = []
|
| 188 |
pending_cars = []
|
| 189 |
|
| 190 |
-
if db:
|
| 191 |
try:
|
| 192 |
pending_sellers = list(db.users.find({"role": "seller", "approved": False}))
|
| 193 |
pending_cars = list(db.cars.find({"status": "pending"}))
|
|
@@ -203,7 +248,7 @@ def approve_seller(user_id):
|
|
| 203 |
if session.get('role') != 'admin':
|
| 204 |
return redirect(url_for('login'))
|
| 205 |
|
| 206 |
-
if db:
|
| 207 |
try:
|
| 208 |
db.users.update_one({"_id": ObjectId(user_id)}, {"$set": {"approved": True}})
|
| 209 |
flash('Seller approved')
|
|
@@ -219,7 +264,7 @@ def seller_dashboard():
|
|
| 219 |
return redirect(url_for('login'))
|
| 220 |
|
| 221 |
cars = []
|
| 222 |
-
if db:
|
| 223 |
try:
|
| 224 |
user = db.users.find_one({"_id": ObjectId(session['user_id'])})
|
| 225 |
if not user.get('approved', False):
|
|
@@ -246,12 +291,16 @@ def add_car():
|
|
| 246 |
flash('Car name required')
|
| 247 |
return redirect(url_for('seller_dashboard'))
|
| 248 |
|
| 249 |
-
if db:
|
| 250 |
try:
|
| 251 |
car_data = {
|
| 252 |
-
"name": name,
|
| 253 |
-
"
|
| 254 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
"created_at": datetime.now()
|
| 256 |
}
|
| 257 |
db.cars.insert_one(car_data)
|
|
@@ -267,7 +316,7 @@ def approve_car(car_id):
|
|
| 267 |
if session.get('role') != 'admin':
|
| 268 |
return redirect(url_for('login'))
|
| 269 |
|
| 270 |
-
if db:
|
| 271 |
try:
|
| 272 |
db.cars.update_one({"_id": ObjectId(car_id)}, {"$set": {"status": "approved"}})
|
| 273 |
flash('Car approved')
|
|
@@ -281,7 +330,7 @@ def book_appointment(car_id):
|
|
| 281 |
if session.get('role') != 'buyer':
|
| 282 |
return redirect(url_for('login'))
|
| 283 |
|
| 284 |
-
if
|
| 285 |
flash('Database unavailable')
|
| 286 |
return redirect(url_for('index'))
|
| 287 |
|
|
@@ -296,15 +345,25 @@ def book_appointment(car_id):
|
|
| 296 |
email = request.form.get('email', '').strip()
|
| 297 |
address = request.form.get('address', '').strip()
|
| 298 |
phone = request.form.get('phone', '').strip()
|
|
|
|
|
|
|
| 299 |
|
| 300 |
if not all([name, email, address, phone]):
|
| 301 |
flash('Please fill all fields')
|
| 302 |
return render_template('appointment.html', car=car)
|
| 303 |
|
| 304 |
appointment_data = {
|
| 305 |
-
"car_id": car_id,
|
| 306 |
-
"
|
| 307 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
}
|
| 309 |
|
| 310 |
db.appointments.insert_one(appointment_data)
|
|
@@ -326,7 +385,7 @@ def seller_appointments(car_id):
|
|
| 326 |
appointments = []
|
| 327 |
car = {}
|
| 328 |
|
| 329 |
-
if db:
|
| 330 |
try:
|
| 331 |
appointments = list(db.appointments.find({"car_id": car_id}))
|
| 332 |
car = db.cars.find_one({"_id": ObjectId(car_id)}) or {}
|
|
@@ -340,7 +399,7 @@ def approve_appointment(appointment_id):
|
|
| 340 |
if session.get('role') != 'seller':
|
| 341 |
return redirect(url_for('login'))
|
| 342 |
|
| 343 |
-
if db:
|
| 344 |
try:
|
| 345 |
meeting_data = {
|
| 346 |
"status": "approved",
|
|
|
|
| 13 |
# MongoDB configuration
|
| 14 |
MONGODB_URI = "mongodb+srv://arshbir:arshbir123@arshbir.9pulohe.mongodb.net/carwala1?retryWrites=true&w=majority"
|
| 15 |
|
| 16 |
+
# Initialize db as None first
|
| 17 |
+
db = None
|
| 18 |
+
|
| 19 |
try:
|
| 20 |
client = MongoClient(MONGODB_URI, serverSelectionTimeoutMS=5000)
|
| 21 |
client.admin.command('ping')
|
|
|
|
| 35 |
|
| 36 |
def initialize_database():
|
| 37 |
"""Initialize database and create collections"""
|
| 38 |
+
if db is None: # Fixed: compare with None instead of using 'not db'
|
| 39 |
+
print("β Database not available for initialization")
|
| 40 |
return
|
| 41 |
|
| 42 |
try:
|
|
|
|
| 48 |
if not users.find_one({"email": ADMIN_EMAIL}):
|
| 49 |
hashed = bcrypt.hashpw(ADMIN_PASSWORD.encode('utf-8'), bcrypt.gensalt())
|
| 50 |
users.insert_one({
|
| 51 |
+
"name": "Admin User",
|
| 52 |
+
"email": ADMIN_EMAIL,
|
| 53 |
+
"password": hashed,
|
| 54 |
+
"role": "admin",
|
| 55 |
+
"address": "Delhi, India",
|
| 56 |
+
"phone": "9876543210",
|
| 57 |
+
"approved": True,
|
| 58 |
+
"created_at": datetime.now()
|
| 59 |
})
|
| 60 |
print("β
Admin user created")
|
| 61 |
|
| 62 |
# Add sample cars
|
| 63 |
if cars.count_documents({}) == 0:
|
| 64 |
sample_cars = [
|
| 65 |
+
{
|
| 66 |
+
"name": "Maruti Suzuki Swift",
|
| 67 |
+
"year": 2022,
|
| 68 |
+
"price": 650000,
|
| 69 |
+
"description": "Excellent condition, low mileage, fuel efficient.",
|
| 70 |
+
"seller_id": "admin",
|
| 71 |
+
"seller_email": ADMIN_EMAIL,
|
| 72 |
+
"status": "approved",
|
| 73 |
+
"created_at": datetime.now()
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"name": "Hyundai Creta",
|
| 77 |
+
"year": 2021,
|
| 78 |
+
"price": 1250000,
|
| 79 |
+
"description": "Well maintained, single owner, all services done.",
|
| 80 |
+
"seller_id": "admin",
|
| 81 |
+
"seller_email": ADMIN_EMAIL,
|
| 82 |
+
"status": "approved",
|
| 83 |
+
"created_at": datetime.now()
|
| 84 |
+
},
|
| 85 |
+
{
|
| 86 |
+
"name": "Toyota Fortuner",
|
| 87 |
+
"year": 2020,
|
| 88 |
+
"price": 3200000,
|
| 89 |
+
"description": "Powerful engine, premium SUV, 4x4 drive.",
|
| 90 |
+
"seller_id": "admin",
|
| 91 |
+
"seller_email": ADMIN_EMAIL,
|
| 92 |
+
"status": "approved",
|
| 93 |
+
"created_at": datetime.now()
|
| 94 |
+
}
|
| 95 |
]
|
| 96 |
cars.insert_many(sample_cars)
|
| 97 |
print("β
Sample cars added")
|
|
|
|
| 116 |
@app.route('/')
|
| 117 |
def index():
|
| 118 |
cars = []
|
| 119 |
+
if db is not None: # Fixed: compare with None
|
| 120 |
try:
|
| 121 |
cars = list(db.cars.find({"status": "approved"}))
|
| 122 |
except Exception as e:
|
|
|
|
| 144 |
return redirect(url_for('admin_dashboard'))
|
| 145 |
|
| 146 |
# Regular user login
|
| 147 |
+
if db is not None: # Fixed: compare with None
|
| 148 |
try:
|
| 149 |
user = db.users.find_one({"email": email})
|
| 150 |
if user and bcrypt.checkpw(password.encode('utf-8'), user['password']):
|
|
|
|
| 156 |
session['role'] = user['role']
|
| 157 |
session['email'] = email
|
| 158 |
|
| 159 |
+
flash(f'Welcome back, {user["name"]}!')
|
| 160 |
+
|
| 161 |
if user['role'] == 'buyer':
|
| 162 |
return redirect(url_for('index'))
|
| 163 |
else:
|
|
|
|
| 186 |
flash('Please fill all fields')
|
| 187 |
return render_template('register.html')
|
| 188 |
|
| 189 |
+
if db is None: # Fixed: compare with None
|
| 190 |
flash('Database unavailable')
|
| 191 |
return render_template('register.html')
|
| 192 |
|
|
|
|
| 197 |
|
| 198 |
hashed = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
| 199 |
user_data = {
|
| 200 |
+
"name": name,
|
| 201 |
+
"email": email,
|
| 202 |
+
"password": hashed,
|
| 203 |
+
"role": role,
|
| 204 |
+
"phone": phone,
|
| 205 |
+
"address": address,
|
| 206 |
+
"approved": role == 'buyer',
|
| 207 |
"created_at": datetime.now()
|
| 208 |
}
|
| 209 |
|
|
|
|
| 232 |
pending_sellers = []
|
| 233 |
pending_cars = []
|
| 234 |
|
| 235 |
+
if db is not None: # Fixed: compare with None
|
| 236 |
try:
|
| 237 |
pending_sellers = list(db.users.find({"role": "seller", "approved": False}))
|
| 238 |
pending_cars = list(db.cars.find({"status": "pending"}))
|
|
|
|
| 248 |
if session.get('role') != 'admin':
|
| 249 |
return redirect(url_for('login'))
|
| 250 |
|
| 251 |
+
if db is not None: # Fixed: compare with None
|
| 252 |
try:
|
| 253 |
db.users.update_one({"_id": ObjectId(user_id)}, {"$set": {"approved": True}})
|
| 254 |
flash('Seller approved')
|
|
|
|
| 264 |
return redirect(url_for('login'))
|
| 265 |
|
| 266 |
cars = []
|
| 267 |
+
if db is not None: # Fixed: compare with None
|
| 268 |
try:
|
| 269 |
user = db.users.find_one({"_id": ObjectId(session['user_id'])})
|
| 270 |
if not user.get('approved', False):
|
|
|
|
| 291 |
flash('Car name required')
|
| 292 |
return redirect(url_for('seller_dashboard'))
|
| 293 |
|
| 294 |
+
if db is not None: # Fixed: compare with None
|
| 295 |
try:
|
| 296 |
car_data = {
|
| 297 |
+
"name": name,
|
| 298 |
+
"year": int(year),
|
| 299 |
+
"price": int(price),
|
| 300 |
+
"description": description,
|
| 301 |
+
"seller_id": session['user_id'],
|
| 302 |
+
"seller_email": session.get('email', ''),
|
| 303 |
+
"status": "pending",
|
| 304 |
"created_at": datetime.now()
|
| 305 |
}
|
| 306 |
db.cars.insert_one(car_data)
|
|
|
|
| 316 |
if session.get('role') != 'admin':
|
| 317 |
return redirect(url_for('login'))
|
| 318 |
|
| 319 |
+
if db is not None: # Fixed: compare with None
|
| 320 |
try:
|
| 321 |
db.cars.update_one({"_id": ObjectId(car_id)}, {"$set": {"status": "approved"}})
|
| 322 |
flash('Car approved')
|
|
|
|
| 330 |
if session.get('role') != 'buyer':
|
| 331 |
return redirect(url_for('login'))
|
| 332 |
|
| 333 |
+
if db is None: # Fixed: compare with None
|
| 334 |
flash('Database unavailable')
|
| 335 |
return redirect(url_for('index'))
|
| 336 |
|
|
|
|
| 345 |
email = request.form.get('email', '').strip()
|
| 346 |
address = request.form.get('address', '').strip()
|
| 347 |
phone = request.form.get('phone', '').strip()
|
| 348 |
+
preferred_date = request.form.get('preferred_date', '')
|
| 349 |
+
preferred_time = request.form.get('preferred_time', '')
|
| 350 |
|
| 351 |
if not all([name, email, address, phone]):
|
| 352 |
flash('Please fill all fields')
|
| 353 |
return render_template('appointment.html', car=car)
|
| 354 |
|
| 355 |
appointment_data = {
|
| 356 |
+
"car_id": car_id,
|
| 357 |
+
"car_name": car['name'],
|
| 358 |
+
"buyer_id": session['user_id'],
|
| 359 |
+
"buyer_name": name,
|
| 360 |
+
"buyer_email": email,
|
| 361 |
+
"buyer_address": address,
|
| 362 |
+
"buyer_phone": phone,
|
| 363 |
+
"preferred_date": preferred_date,
|
| 364 |
+
"preferred_time": preferred_time,
|
| 365 |
+
"status": "pending",
|
| 366 |
+
"created_at": datetime.now()
|
| 367 |
}
|
| 368 |
|
| 369 |
db.appointments.insert_one(appointment_data)
|
|
|
|
| 385 |
appointments = []
|
| 386 |
car = {}
|
| 387 |
|
| 388 |
+
if db is not None: # Fixed: compare with None
|
| 389 |
try:
|
| 390 |
appointments = list(db.appointments.find({"car_id": car_id}))
|
| 391 |
car = db.cars.find_one({"_id": ObjectId(car_id)}) or {}
|
|
|
|
| 399 |
if session.get('role') != 'seller':
|
| 400 |
return redirect(url_for('login'))
|
| 401 |
|
| 402 |
+
if db is not None: # Fixed: compare with None
|
| 403 |
try:
|
| 404 |
meeting_data = {
|
| 405 |
"status": "approved",
|