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