Spaces:
Sleeping
Sleeping
File size: 2,544 Bytes
9e7b45f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | import os
import shutil
import csv
from pymongo import MongoClient
import bcrypt
from dotenv import load_dotenv
# Load env vars
load_dotenv()
# MongoDB setup
MONGO_URI = os.environ.get('MONGO_URI')
DB_NAME = os.environ.get('MONGODB_DB', 'attendance_db')
if not MONGO_URI:
print("Error: MONGO_URI not found in .env")
exit(1)
client = MongoClient(MONGO_URI)
db = client[DB_NAME]
def clean_db():
print("Cleaning MongoDB...")
collections = ['faculty', 'students', 'attendance', 'users', 'timetable']
for col_name in collections:
if col_name in db.list_collection_names() or col_name in ['faculty', 'students', 'attendance', 'users', 'timetable']:
# Check if collection exists implicitly or explicitly
try:
count = db[col_name].count_documents({})
db[col_name].delete_many({})
print(f"Deleted {count} documents from '{col_name}' collection.")
except Exception as e:
print(f"Error cleaning {col_name}: {e}")
def clean_encodings():
print("Cleaning split_encodings...")
split_dir = "split_encodings"
if os.path.exists(split_dir):
for filename in os.listdir(split_dir):
file_path = os.path.join(split_dir, filename)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception as e:
print(f"Failed to delete {file_path}: {e}")
def seed_db_and_csv():
print("Seeding Rahul...")
# 1. MongoDB
users = db['faculty']
email = "rahul@facemarkpro.com"
raw_password = "rahul@123"
hashed_password = bcrypt.hashpw(raw_password.encode('utf-8'), bcrypt.gensalt())
user_data = {
"email": email,
"password": hashed_password,
"name": "Rahul",
"role": "teacher",
"department": "CSE",
"created_at": "2024-01-01"
}
users.insert_one(user_data)
print(f"User {email} created in MongoDB.")
# 2. CSV
csv_file = 'faculty_users.csv'
header = ['faculty_name', 'faculty_email', 'password']
row = ['Rahul', email, raw_password] # Keeping raw password in CSV as per existing pattern
with open(csv_file, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(header)
writer.writerow(row)
print(f"User {email} added to {csv_file}.")
if __name__ == "__main__":
clean_db()
clean_encodings()
seed_db_and_csv()
print("Database reset complete.")
|