Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,6 @@
|
|
| 1 |
from flask import Flask, render_template, request, redirect, url_for, session, flash
|
| 2 |
from pymongo import MongoClient
|
| 3 |
import bcrypt
|
| 4 |
-
import smtplib
|
| 5 |
-
from email.mime.text import MIMEText
|
| 6 |
-
from email.mime.multipart import MIMEMultipart
|
| 7 |
from datetime import datetime
|
| 8 |
from bson.objectid import ObjectId
|
| 9 |
|
|
@@ -13,83 +10,60 @@ 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 |
-
# Initialize db as None first
|
| 17 |
-
db = None
|
| 18 |
-
|
| 19 |
try:
|
| 20 |
-
client = MongoClient(MONGODB_URI
|
| 21 |
-
client.admin.command('ping')
|
| 22 |
db = client.carwala1
|
| 23 |
print("β
MongoDB connected successfully!")
|
| 24 |
except Exception as e:
|
| 25 |
print(f"β MongoDB connection failed: {e}")
|
| 26 |
db = None
|
| 27 |
|
| 28 |
-
# Email configuration
|
| 29 |
-
SMTP_EMAIL = "singharshbir76@gmail.com"
|
| 30 |
-
SMTP_PASSWORD = "wpiy tuxp pgpf wljz"
|
| 31 |
-
|
| 32 |
# Admin credentials
|
| 33 |
ADMIN_EMAIL = "singharshbir76@gmail.com"
|
| 34 |
ADMIN_PASSWORD = "arshbir"
|
| 35 |
|
| 36 |
def initialize_database():
|
| 37 |
-
"""Initialize database
|
| 38 |
-
if db is None:
|
| 39 |
-
print("β Database not available for initialization")
|
| 40 |
return
|
| 41 |
|
| 42 |
try:
|
|
|
|
| 43 |
users = db.users
|
| 44 |
cars = db.cars
|
| 45 |
appointments = db.appointments
|
| 46 |
|
| 47 |
-
# Create admin user
|
| 48 |
-
if
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
"name": "Admin User",
|
| 52 |
-
"email": ADMIN_EMAIL,
|
| 53 |
-
"password":
|
| 54 |
-
"role": "admin",
|
| 55 |
-
"
|
| 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
|
| 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
|
| 80 |
-
"
|
| 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 |
]
|
|
@@ -97,120 +71,115 @@ def initialize_database():
|
|
| 97 |
print("β
Sample cars added")
|
| 98 |
|
| 99 |
except Exception as e:
|
| 100 |
-
print(f"
|
| 101 |
|
|
|
|
| 102 |
initialize_database()
|
| 103 |
|
| 104 |
-
#
|
| 105 |
def format_price(price):
|
| 106 |
-
"""Format price safely - handle None and other types"""
|
| 107 |
try:
|
| 108 |
-
if price is None:
|
| 109 |
-
return "βΉ0"
|
| 110 |
return f"βΉ{int(price):,}"
|
| 111 |
-
except
|
| 112 |
return "βΉ0"
|
| 113 |
|
| 114 |
app.jinja_env.filters['format_price'] = format_price
|
| 115 |
|
| 116 |
@app.route('/')
|
| 117 |
def index():
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
try:
|
| 121 |
cars = list(db.cars.find({"status": "approved"}))
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
| 127 |
|
| 128 |
@app.route('/login', methods=['GET', 'POST'])
|
| 129 |
def login():
|
| 130 |
if request.method == 'POST':
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 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']):
|
| 151 |
-
if user['role'] == 'seller' and not user.get('approved', False):
|
| 152 |
-
flash('Seller account pending approval')
|
| 153 |
-
return redirect(url_for('login'))
|
| 154 |
-
|
| 155 |
session['user_id'] = str(user['_id'])
|
| 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 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
else:
|
| 166 |
-
flash('Invalid
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
| 172 |
|
| 173 |
return render_template('login.html')
|
| 174 |
|
| 175 |
@app.route('/register', methods=['GET', 'POST'])
|
| 176 |
def register():
|
| 177 |
if request.method == 'POST':
|
| 178 |
-
name = request.form.get('name', '').strip()
|
| 179 |
-
email = request.form.get('email', '').strip()
|
| 180 |
-
password = request.form.get('password', '')
|
| 181 |
-
role = request.form.get('role', 'buyer')
|
| 182 |
-
phone = request.form.get('phone', '').strip()
|
| 183 |
-
address = request.form.get('address', '').strip()
|
| 184 |
-
|
| 185 |
-
if not all([name, email, password, phone, address]):
|
| 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 |
-
|
| 193 |
try:
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 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 |
-
|
| 210 |
-
db.users.insert_one(user_data)
|
| 211 |
-
flash('Registration successful! Please login.' if role == 'buyer' else 'Registration pending approval')
|
| 212 |
-
return redirect(url_for('login'))
|
| 213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
except Exception as e:
|
| 215 |
print(f"Registration error: {e}")
|
| 216 |
flash('Registration failed')
|
|
@@ -226,35 +195,37 @@ def logout():
|
|
| 226 |
@app.route('/admin')
|
| 227 |
def admin_dashboard():
|
| 228 |
if session.get('role') != 'admin':
|
| 229 |
-
flash('
|
| 230 |
return redirect(url_for('login'))
|
| 231 |
|
| 232 |
-
|
| 233 |
-
|
| 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"}))
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
@app.route('/approve_seller/<user_id>')
|
| 247 |
def approve_seller(user_id):
|
| 248 |
if session.get('role') != 'admin':
|
| 249 |
return redirect(url_for('login'))
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
db.users.update_one({"_id": ObjectId(user_id)}, {"$set": {"approved": True}})
|
| 254 |
-
flash('Seller approved')
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
|
| 259 |
return redirect(url_for('admin_dashboard'))
|
| 260 |
|
|
@@ -263,51 +234,54 @@ def seller_dashboard():
|
|
| 263 |
if session.get('role') != 'seller':
|
| 264 |
return redirect(url_for('login'))
|
| 265 |
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
user = db.users.find_one({"_id": ObjectId(session['user_id'])})
|
| 270 |
-
if not user.get('approved', False):
|
| 271 |
-
flash('
|
| 272 |
return redirect(url_for('login'))
|
| 273 |
|
| 274 |
cars = list(db.cars.find({"seller_id": session['user_id']}))
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
|
| 280 |
@app.route('/add_car', methods=['POST'])
|
| 281 |
def add_car():
|
| 282 |
if session.get('role') != 'seller':
|
| 283 |
return redirect(url_for('login'))
|
| 284 |
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 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)
|
| 307 |
-
flash('Car added
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
return redirect(url_for('seller_dashboard'))
|
| 313 |
|
|
@@ -316,12 +290,13 @@ def approve_car(car_id):
|
|
| 316 |
if session.get('role') != 'admin':
|
| 317 |
return redirect(url_for('login'))
|
| 318 |
|
| 319 |
-
|
| 320 |
-
|
| 321 |
db.cars.update_one({"_id": ObjectId(car_id)}, {"$set": {"status": "approved"}})
|
| 322 |
-
flash('Car approved')
|
| 323 |
-
|
| 324 |
-
|
|
|
|
| 325 |
|
| 326 |
return redirect(url_for('admin_dashboard'))
|
| 327 |
|
|
@@ -330,90 +305,38 @@ def book_appointment(car_id):
|
|
| 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 |
-
|
| 337 |
try:
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
if request.method == 'POST':
|
| 344 |
-
name = request.form.get('name', '').strip()
|
| 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 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
|
|
|
|
|
|
| 368 |
|
| 369 |
-
|
| 370 |
-
|
|
|
|
| 371 |
return redirect(url_for('index'))
|
| 372 |
-
|
| 373 |
-
return render_template('appointment.html', car=car)
|
| 374 |
-
|
| 375 |
except Exception as e:
|
| 376 |
-
print(f"
|
| 377 |
-
flash('Error
|
| 378 |
return redirect(url_for('index'))
|
| 379 |
|
| 380 |
-
@app.route('/seller_appointments/<car_id>')
|
| 381 |
-
def seller_appointments(car_id):
|
| 382 |
-
if session.get('role') != 'seller':
|
| 383 |
-
return redirect(url_for('login'))
|
| 384 |
-
|
| 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 {}
|
| 392 |
-
except Exception as e:
|
| 393 |
-
print(f"Appointments error: {e}")
|
| 394 |
-
|
| 395 |
-
return render_template('seller.html', appointments=appointments, car=car)
|
| 396 |
-
|
| 397 |
-
@app.route('/approve_appointment/<appointment_id>', methods=['POST'])
|
| 398 |
-
def approve_appointment(appointment_id):
|
| 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",
|
| 406 |
-
"meeting_date": request.form.get('meeting_date', ''),
|
| 407 |
-
"meeting_time": request.form.get('meeting_time', ''),
|
| 408 |
-
"meeting_place": request.form.get('meeting_place', '')
|
| 409 |
-
}
|
| 410 |
-
db.appointments.update_one({"_id": ObjectId(appointment_id)}, {"$set": meeting_data})
|
| 411 |
-
flash('Appointment approved')
|
| 412 |
-
except Exception as e:
|
| 413 |
-
print(f"Approve appointment error: {e}")
|
| 414 |
-
flash('Approval failed')
|
| 415 |
-
|
| 416 |
-
return redirect(url_for('seller_dashboard'))
|
| 417 |
-
|
| 418 |
if __name__ == '__main__':
|
| 419 |
app.run(host='0.0.0.0', port=7860, debug=False)
|
|
|
|
| 1 |
from flask import Flask, render_template, request, redirect, url_for, session, flash
|
| 2 |
from pymongo import MongoClient
|
| 3 |
import bcrypt
|
|
|
|
|
|
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
from bson.objectid import ObjectId
|
| 6 |
|
|
|
|
| 10 |
# MongoDB configuration
|
| 11 |
MONGODB_URI = "mongodb+srv://arshbir:arshbir123@arshbir.9pulohe.mongodb.net/carwala1?retryWrites=true&w=majority"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
+
client = MongoClient(MONGODB_URI)
|
|
|
|
| 15 |
db = client.carwala1
|
| 16 |
print("β
MongoDB connected successfully!")
|
| 17 |
except Exception as e:
|
| 18 |
print(f"β MongoDB connection failed: {e}")
|
| 19 |
db = None
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# Admin credentials
|
| 22 |
ADMIN_EMAIL = "singharshbir76@gmail.com"
|
| 23 |
ADMIN_PASSWORD = "arshbir"
|
| 24 |
|
| 25 |
def initialize_database():
|
| 26 |
+
"""Initialize database with sample data"""
|
| 27 |
+
if db is None:
|
|
|
|
| 28 |
return
|
| 29 |
|
| 30 |
try:
|
| 31 |
+
# Create collections if they don't exist
|
| 32 |
users = db.users
|
| 33 |
cars = db.cars
|
| 34 |
appointments = db.appointments
|
| 35 |
|
| 36 |
+
# Create admin user if not exists
|
| 37 |
+
if users.find_one({"email": ADMIN_EMAIL}) is None:
|
| 38 |
+
hashed_password = bcrypt.hashpw(ADMIN_PASSWORD.encode('utf-8'), bcrypt.gensalt())
|
| 39 |
+
admin_data = {
|
| 40 |
+
"name": "Admin User",
|
| 41 |
+
"email": ADMIN_EMAIL,
|
| 42 |
+
"password": hashed_password,
|
| 43 |
+
"role": "admin",
|
| 44 |
+
"approved": True,
|
|
|
|
|
|
|
| 45 |
"created_at": datetime.now()
|
| 46 |
+
}
|
| 47 |
+
users.insert_one(admin_data)
|
| 48 |
print("β
Admin user created")
|
| 49 |
|
| 50 |
+
# Add sample cars if none exist
|
| 51 |
if cars.count_documents({}) == 0:
|
| 52 |
sample_cars = [
|
| 53 |
{
|
| 54 |
+
"name": "Maruti Suzuki Swift",
|
| 55 |
+
"year": 2022,
|
| 56 |
+
"price": 650000,
|
| 57 |
+
"description": "Excellent condition with low mileage",
|
|
|
|
|
|
|
| 58 |
"status": "approved",
|
| 59 |
"created_at": datetime.now()
|
| 60 |
},
|
| 61 |
{
|
| 62 |
"name": "Hyundai Creta",
|
| 63 |
+
"year": 2021,
|
| 64 |
+
"price": 1250000,
|
| 65 |
+
"description": "Well maintained SUV",
|
| 66 |
+
"status": "approved",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
"created_at": datetime.now()
|
| 68 |
}
|
| 69 |
]
|
|
|
|
| 71 |
print("β
Sample cars added")
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
+
print(f"Database initialization error: {e}")
|
| 75 |
|
| 76 |
+
# Initialize database
|
| 77 |
initialize_database()
|
| 78 |
|
| 79 |
+
# Price formatting filter
|
| 80 |
def format_price(price):
|
|
|
|
| 81 |
try:
|
|
|
|
|
|
|
| 82 |
return f"βΉ{int(price):,}"
|
| 83 |
+
except:
|
| 84 |
return "βΉ0"
|
| 85 |
|
| 86 |
app.jinja_env.filters['format_price'] = format_price
|
| 87 |
|
| 88 |
@app.route('/')
|
| 89 |
def index():
|
| 90 |
+
try:
|
| 91 |
+
if db is not None:
|
|
|
|
| 92 |
cars = list(db.cars.find({"status": "approved"}))
|
| 93 |
+
else:
|
| 94 |
+
cars = []
|
| 95 |
+
return render_template('index.html', cars=cars)
|
| 96 |
+
except Exception as e:
|
| 97 |
+
print(f"Error in index route: {e}")
|
| 98 |
+
return render_template('index.html', cars=[])
|
| 99 |
|
| 100 |
@app.route('/login', methods=['GET', 'POST'])
|
| 101 |
def login():
|
| 102 |
if request.method == 'POST':
|
| 103 |
+
try:
|
| 104 |
+
email = request.form.get('email')
|
| 105 |
+
password = request.form.get('password')
|
| 106 |
+
|
| 107 |
+
# Admin login
|
| 108 |
+
if email == ADMIN_EMAIL and password == ADMIN_PASSWORD:
|
| 109 |
+
session['user_id'] = 'admin'
|
| 110 |
+
session['role'] = 'admin'
|
| 111 |
+
session['email'] = email
|
| 112 |
+
flash('Admin login successful!')
|
| 113 |
+
return redirect(url_for('admin_dashboard'))
|
| 114 |
+
|
| 115 |
+
# Regular user login
|
| 116 |
+
if db is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
user = db.users.find_one({"email": email})
|
| 118 |
if user and bcrypt.checkpw(password.encode('utf-8'), user['password']):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
session['user_id'] = str(user['_id'])
|
| 120 |
session['role'] = user['role']
|
| 121 |
session['email'] = email
|
| 122 |
|
|
|
|
|
|
|
| 123 |
if user['role'] == 'buyer':
|
| 124 |
return redirect(url_for('index'))
|
| 125 |
+
elif user['role'] == 'seller':
|
| 126 |
+
if user.get('approved', False):
|
| 127 |
+
return redirect(url_for('seller_dashboard'))
|
| 128 |
+
else:
|
| 129 |
+
flash('Seller account pending approval')
|
| 130 |
+
return redirect(url_for('login'))
|
| 131 |
else:
|
| 132 |
+
flash('Invalid email or password')
|
| 133 |
+
else:
|
| 134 |
+
flash('Database connection issue')
|
| 135 |
+
|
| 136 |
+
except Exception as e:
|
| 137 |
+
print(f"Login error: {e}")
|
| 138 |
+
flash('Login failed')
|
| 139 |
|
| 140 |
return render_template('login.html')
|
| 141 |
|
| 142 |
@app.route('/register', methods=['GET', 'POST'])
|
| 143 |
def register():
|
| 144 |
if request.method == 'POST':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
try:
|
| 146 |
+
name = request.form.get('name')
|
| 147 |
+
email = request.form.get('email')
|
| 148 |
+
password = request.form.get('password')
|
| 149 |
+
role = request.form.get('role')
|
| 150 |
+
phone = request.form.get('phone')
|
| 151 |
+
address = request.form.get('address')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
+
if db is not None:
|
| 154 |
+
# Check if user exists
|
| 155 |
+
if db.users.find_one({"email": email}):
|
| 156 |
+
flash('Email already registered')
|
| 157 |
+
return redirect(url_for('register'))
|
| 158 |
+
|
| 159 |
+
# Create new user
|
| 160 |
+
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
| 161 |
+
user_data = {
|
| 162 |
+
"name": name,
|
| 163 |
+
"email": email,
|
| 164 |
+
"password": hashed_password,
|
| 165 |
+
"role": role,
|
| 166 |
+
"phone": phone,
|
| 167 |
+
"address": address,
|
| 168 |
+
"approved": role == 'buyer',
|
| 169 |
+
"created_at": datetime.now()
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
db.users.insert_one(user_data)
|
| 173 |
+
|
| 174 |
+
if role == 'buyer':
|
| 175 |
+
flash('Registration successful! Please login.')
|
| 176 |
+
else:
|
| 177 |
+
flash('Registration submitted. Waiting for admin approval.')
|
| 178 |
+
|
| 179 |
+
return redirect(url_for('login'))
|
| 180 |
+
else:
|
| 181 |
+
flash('Database connection issue')
|
| 182 |
+
|
| 183 |
except Exception as e:
|
| 184 |
print(f"Registration error: {e}")
|
| 185 |
flash('Registration failed')
|
|
|
|
| 195 |
@app.route('/admin')
|
| 196 |
def admin_dashboard():
|
| 197 |
if session.get('role') != 'admin':
|
| 198 |
+
flash('Please login as admin')
|
| 199 |
return redirect(url_for('login'))
|
| 200 |
|
| 201 |
+
try:
|
| 202 |
+
if db is not None:
|
|
|
|
|
|
|
|
|
|
| 203 |
pending_sellers = list(db.users.find({"role": "seller", "approved": False}))
|
| 204 |
pending_cars = list(db.cars.find({"status": "pending"}))
|
| 205 |
+
else:
|
| 206 |
+
pending_sellers = []
|
| 207 |
+
pending_cars = []
|
| 208 |
+
|
| 209 |
+
return render_template('admin.html',
|
| 210 |
+
pending_sellers=pending_sellers,
|
| 211 |
+
pending_cars=pending_cars)
|
| 212 |
+
except Exception as e:
|
| 213 |
+
print(f"Admin dashboard error: {e}")
|
| 214 |
+
flash('Error loading admin dashboard')
|
| 215 |
+
return render_template('admin.html', pending_sellers=[], pending_cars=[])
|
| 216 |
|
| 217 |
@app.route('/approve_seller/<user_id>')
|
| 218 |
def approve_seller(user_id):
|
| 219 |
if session.get('role') != 'admin':
|
| 220 |
return redirect(url_for('login'))
|
| 221 |
|
| 222 |
+
try:
|
| 223 |
+
if db is not None:
|
| 224 |
db.users.update_one({"_id": ObjectId(user_id)}, {"$set": {"approved": True}})
|
| 225 |
+
flash('Seller approved successfully')
|
| 226 |
+
except Exception as e:
|
| 227 |
+
print(f"Approve seller error: {e}")
|
| 228 |
+
flash('Error approving seller')
|
| 229 |
|
| 230 |
return redirect(url_for('admin_dashboard'))
|
| 231 |
|
|
|
|
| 234 |
if session.get('role') != 'seller':
|
| 235 |
return redirect(url_for('login'))
|
| 236 |
|
| 237 |
+
try:
|
| 238 |
+
if db is not None:
|
| 239 |
+
# Verify seller is approved
|
| 240 |
user = db.users.find_one({"_id": ObjectId(session['user_id'])})
|
| 241 |
+
if not user or not user.get('approved', False):
|
| 242 |
+
flash('Seller account not approved')
|
| 243 |
return redirect(url_for('login'))
|
| 244 |
|
| 245 |
cars = list(db.cars.find({"seller_id": session['user_id']}))
|
| 246 |
+
else:
|
| 247 |
+
cars = []
|
| 248 |
+
|
| 249 |
+
return render_template('seller_dashboard.html', cars=cars)
|
| 250 |
+
except Exception as e:
|
| 251 |
+
print(f"Seller dashboard error: {e}")
|
| 252 |
+
flash('Error loading seller dashboard')
|
| 253 |
+
return render_template('seller_dashboard.html', cars=[])
|
| 254 |
|
| 255 |
@app.route('/add_car', methods=['POST'])
|
| 256 |
def add_car():
|
| 257 |
if session.get('role') != 'seller':
|
| 258 |
return redirect(url_for('login'))
|
| 259 |
|
| 260 |
+
try:
|
| 261 |
+
name = request.form.get('name')
|
| 262 |
+
year = request.form.get('year')
|
| 263 |
+
price = request.form.get('price')
|
| 264 |
+
description = request.form.get('description')
|
| 265 |
+
|
| 266 |
+
if db is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
car_data = {
|
| 268 |
+
"name": name,
|
| 269 |
+
"year": int(year),
|
| 270 |
"price": int(price),
|
| 271 |
+
"description": description,
|
| 272 |
"seller_id": session['user_id'],
|
| 273 |
+
"seller_email": session.get('email', ''),
|
| 274 |
"status": "pending",
|
| 275 |
"created_at": datetime.now()
|
| 276 |
}
|
| 277 |
db.cars.insert_one(car_data)
|
| 278 |
+
flash('Car added successfully. Waiting for admin approval.')
|
| 279 |
+
else:
|
| 280 |
+
flash('Database connection issue')
|
| 281 |
+
|
| 282 |
+
except Exception as e:
|
| 283 |
+
print(f"Add car error: {e}")
|
| 284 |
+
flash('Error adding car')
|
| 285 |
|
| 286 |
return redirect(url_for('seller_dashboard'))
|
| 287 |
|
|
|
|
| 290 |
if session.get('role') != 'admin':
|
| 291 |
return redirect(url_for('login'))
|
| 292 |
|
| 293 |
+
try:
|
| 294 |
+
if db is not None:
|
| 295 |
db.cars.update_one({"_id": ObjectId(car_id)}, {"$set": {"status": "approved"}})
|
| 296 |
+
flash('Car approved successfully')
|
| 297 |
+
except Exception as e:
|
| 298 |
+
print(f"Approve car error: {e}")
|
| 299 |
+
flash('Error approving car')
|
| 300 |
|
| 301 |
return redirect(url_for('admin_dashboard'))
|
| 302 |
|
|
|
|
| 305 |
if session.get('role') != 'buyer':
|
| 306 |
return redirect(url_for('login'))
|
| 307 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
try:
|
| 309 |
+
if db is not None:
|
| 310 |
+
car = db.cars.find_one({"_id": ObjectId(car_id)})
|
| 311 |
+
if not car:
|
| 312 |
+
flash('Car not found')
|
| 313 |
+
return redirect(url_for('index'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
|
| 315 |
+
if request.method == 'POST':
|
| 316 |
+
appointment_data = {
|
| 317 |
+
"car_id": car_id,
|
| 318 |
+
"car_name": car['name'],
|
| 319 |
+
"buyer_id": session['user_id'],
|
| 320 |
+
"buyer_name": request.form.get('name'),
|
| 321 |
+
"buyer_email": request.form.get('email'),
|
| 322 |
+
"buyer_phone": request.form.get('phone'),
|
| 323 |
+
"buyer_address": request.form.get('address'),
|
| 324 |
+
"status": "pending",
|
| 325 |
+
"created_at": datetime.now()
|
| 326 |
+
}
|
| 327 |
+
db.appointments.insert_one(appointment_data)
|
| 328 |
+
flash('Appointment booked successfully!')
|
| 329 |
+
return redirect(url_for('index'))
|
| 330 |
|
| 331 |
+
return render_template('appointment.html', car=car)
|
| 332 |
+
else:
|
| 333 |
+
flash('Database connection issue')
|
| 334 |
return redirect(url_for('index'))
|
| 335 |
+
|
|
|
|
|
|
|
| 336 |
except Exception as e:
|
| 337 |
+
print(f"Book appointment error: {e}")
|
| 338 |
+
flash('Error booking appointment')
|
| 339 |
return redirect(url_for('index'))
|
| 340 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
if __name__ == '__main__':
|
| 342 |
app.run(host='0.0.0.0', port=7860, debug=False)
|