Skip to content

Commit

Permalink
Update database.py
Browse files Browse the repository at this point in the history
  • Loading branch information
sahildesai07 authored Aug 31, 2024
1 parent 807b54b commit 1005a14
Showing 1 changed file with 37 additions and 65 deletions.
102 changes: 37 additions & 65 deletions database/database.py
Original file line number Diff line number Diff line change
@@ -1,81 +1,53 @@
import pymongo
import time
from config import DB_URI, DB_NAME

# Database connection
client = pymongo.MongoClient(DB_URI)
db = client[DB_NAME]

# Users collection
users_collection = db['users']
verifications_collection = db['verifications']

async def add_user(user_id: int):
"""Add a new user to the database."""
users_collection.insert_one({'user_id': user_id, 'join_date': time.time()})
import motor.motor_asyncio
from config import DB_URI, DB_NAME

async def del_user(user_id: int):
"""Delete a user from the database."""
users_collection.delete_one({'user_id': user_id})
dbclient = motor.motor_asyncio.AsyncIOMotorClient(DB_URI)
database = dbclient[DB_NAME]

async def full_userbase():
"""Return the full list of users."""
return [user['user_id'] for user in users_collection.find()]
user_data = database['users']

async def present_user(user_id: int):
"""Check if a user is already in the database."""
return users_collection.find_one({'user_id': user_id})
default_verify = {
'is_verified': False,
'verified_time': 0,
'verify_token': "",
'link': ""
}

async def get_verify_status(user_id: int):
"""Retrieve the verification status of a user."""
user_data = users_collection.find_one({'user_id': user_id})
if not user_data:
return {"is_verified": False, "verify_token": None, "link": None, "verified_time": 0}
def new_user(id):
return {
"is_verified": user_data.get("is_verified", False),
"verify_token": user_data.get("verify_token", None),
"link": user_data.get("link", ""),
"verified_time": user_data.get("verified_time", 0)
}

async def update_verify_status(user_id: int, is_verified: bool = False, verify_token: str = None, verified_time: float = 0, link: str = ""):
"""Update the verification status of a user."""
update_data = {
"is_verified": is_verified,
"verified_time": verified_time,
"verify_token": verify_token,
"link": link
'_id': id,
'verify_status': {
'is_verified': False,
'verified_time': "",
'verify_token': "",
'link': ""
}
}
users_collection.update_one({"user_id": user_id}, {"$set": update_data}, upsert=True)

async def add_verification_data(user_id: int, token: str, verified_time: float):
"""Add a verification entry to the database."""
verifications_collection.insert_one({
"user_id": user_id,
"token": token,
"verified_time": verified_time
})

async def get_token_verification_stats():
"""Retrieve all verification statistics."""
return list(verifications_collection.find())

async def count_verified_users_24hr_and_today():
"""Count users who verified their tokens in the last 24 hours."""
now = time.time()
last_24_hours = now - 86400
today = time.time() - time.time() % 86400
return {
"last_24_hours": verifications_collection.count_documents({"verified_time": {"$gte": last_24_hours}}),
"today": verifications_collection.count_documents({"verified_time": {"$gte": today}})
}
async def present_user(user_id: int):
found = await user_data.find_one({'_id': user_id})
return bool(found)

async def add_user(user_id: int):
user = new_user(user_id)
await user_data.insert_one(user)
return

async def db_verify_status(user_id):
user = await users_collection.find_one({'_id': user_id})
user = await user_data.find_one({'_id': user_id})
if user:
return user.get('verify_status', default_verify)
return default_verify

async def db_update_verify_status(user_id, verify):
await users_collection.update_one({'_id': user_id}, {'$set': {'verify_status': verify}})
await user_data.update_one({'_id': user_id}, {'$set': {'verify_status': verify}})

async def full_userbase():
user_docs = user_data.find()
user_ids = [doc['_id'] async for doc in user_docs]
return user_ids

async def del_user(user_id: int):
await user_data.delete_one({'_id': user_id})
return

0 comments on commit 1005a14

Please sign in to comment.