feat: migrate user management from json to mongodb

This commit is contained in:
Whispering Wind
2025-09-06 22:38:32 +03:30
committed by GitHub
parent 15eac57988
commit 80c21ccd07
17 changed files with 824 additions and 994 deletions

View File

@ -1,46 +1,34 @@
#!/usr/bin/env python3
import json
import sys
import os
import asyncio
from init_paths import *
from paths import *
def sync_remove_user(username):
if not os.path.isfile(USERS_FILE):
return 1, f"Error: Config file {USERS_FILE} not found."
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from db.database import db
def remove_user(username):
if db is None:
return 1, "Error: Database connection failed. Please ensure MongoDB is running."
try:
with open(USERS_FILE, 'r') as f:
try:
users_data = json.load(f)
except json.JSONDecodeError:
return 1, f"Error: {USERS_FILE} contains invalid JSON."
if username in users_data:
del users_data[username]
with open(USERS_FILE, 'w') as f:
json.dump(users_data, f, indent=4)
result = db.delete_user(username)
if result.deleted_count > 0:
return 0, f"User {username} removed successfully."
else:
return 1, f"Error: User {username} not found."
except Exception as e:
return 1, f"Error: {str(e)}"
return 1, f"An error occurred while removing the user: {e}"
async def remove_user(username):
return await asyncio.to_thread(sync_remove_user, username)
async def main():
def main():
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} <username>")
sys.exit(1)
username = sys.argv[1]
exit_code, message = await remove_user(username)
username = sys.argv[1].lower()
exit_code, message = remove_user(username)
print(message)
sys.exit(exit_code)
if __name__ == "__main__":
asyncio.run(main())
main()