feat: migrate user management from json to mongodb
This commit is contained in:
@ -4,12 +4,13 @@ import json
|
||||
import sys
|
||||
import os
|
||||
import getopt
|
||||
from init_paths import *
|
||||
from paths import *
|
||||
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
|
||||
from db.database import db
|
||||
|
||||
def get_user_info(username):
|
||||
"""
|
||||
Retrieves and prints information for a specific user from the USERS_FILE.
|
||||
Retrieves and prints information for a specific user from the database.
|
||||
|
||||
Args:
|
||||
username (str): The username to look up.
|
||||
@ -17,30 +18,20 @@ def get_user_info(username):
|
||||
Returns:
|
||||
int: 0 on success, 1 on failure.
|
||||
"""
|
||||
if not os.path.isfile(USERS_FILE):
|
||||
print(f"users.json file not found at {USERS_FILE}!")
|
||||
if db is None:
|
||||
print("Error: Database connection failed. Please ensure MongoDB is running.")
|
||||
return 1
|
||||
|
||||
|
||||
try:
|
||||
with open(USERS_FILE, 'r') as f:
|
||||
users_data = json.load(f)
|
||||
except json.JSONDecodeError:
|
||||
print(f"Error: {USERS_FILE} contains invalid JSON.")
|
||||
return 1
|
||||
|
||||
if username in users_data:
|
||||
user_info = users_data[username]
|
||||
print(json.dumps(user_info, indent=4)) # Print with indentation for readability
|
||||
# upload_bytes = user_info.get('upload_bytes', "No upload data available")
|
||||
# download_bytes = user_info.get('download_bytes', "No download data available")
|
||||
# status = user_info.get('status', "Status unavailable")
|
||||
# You can choose to print these individually as well, if needed
|
||||
# print(f"Upload Bytes: {upload_bytes}")
|
||||
# print(f"Download Bytes: {download_bytes}")
|
||||
# print(f"Status: {status}")
|
||||
return 0
|
||||
else:
|
||||
print(f"User '{username}' not found in {USERS_FILE}.")
|
||||
user_info = db.get_user(username)
|
||||
if user_info:
|
||||
print(json.dumps(user_info, indent=4))
|
||||
return 0
|
||||
else:
|
||||
print(f"User '{username}' not found in the database.")
|
||||
return 1
|
||||
except Exception as e:
|
||||
print(f"An error occurred while fetching user data: {e}")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
@ -54,7 +45,7 @@ if __name__ == "__main__":
|
||||
|
||||
for opt, arg in opts:
|
||||
if opt in ("-u", "--username"):
|
||||
username = arg
|
||||
username = arg.lower()
|
||||
|
||||
if not username:
|
||||
print(f"Usage: {sys.argv[0]} -u <username>")
|
||||
|
||||
Reference in New Issue
Block a user