feat: migrate user management from json to mongodb
This commit is contained in:
@ -1,15 +1,15 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
from datetime import date
|
||||
from init_paths import *
|
||||
from paths import *
|
||||
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
|
||||
from db.database import db
|
||||
|
||||
def reset_user(username):
|
||||
"""
|
||||
Resets the data usage, status, and creation date of a user in the USERS_FILE.
|
||||
Resets the data usage, status, and creation date of a user in the database.
|
||||
|
||||
Args:
|
||||
username (str): The username to reset.
|
||||
@ -17,35 +17,34 @@ def reset_user(username):
|
||||
Returns:
|
||||
int: 0 on success, 1 on failure.
|
||||
"""
|
||||
if not os.path.isfile(USERS_FILE):
|
||||
print(f"Error: File '{USERS_FILE}' not found.")
|
||||
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
|
||||
user = db.get_user(username)
|
||||
if not user:
|
||||
print(f"Error: User '{username}' not found in the database.")
|
||||
return 1
|
||||
|
||||
if username not in users_data:
|
||||
print(f"Error: User '{username}' not found in '{USERS_FILE}'.")
|
||||
return 1
|
||||
updates = {
|
||||
'upload_bytes': 0,
|
||||
'download_bytes': 0,
|
||||
'status': 'Offline',
|
||||
'account_creation_date': date.today().strftime("%Y-%m-%d"),
|
||||
'blocked': False
|
||||
}
|
||||
|
||||
today = date.today().strftime("%Y-%m-%d")
|
||||
users_data[username]['upload_bytes'] = 0
|
||||
users_data[username]['download_bytes'] = 0
|
||||
users_data[username]['status'] = "Offline"
|
||||
users_data[username]['account_creation_date'] = today
|
||||
users_data[username]['blocked'] = False
|
||||
result = db.update_user(username, updates)
|
||||
if result.modified_count > 0:
|
||||
print(f"User '{username}' has been reset successfully.")
|
||||
return 0
|
||||
else:
|
||||
print(f"User '{username}' data was already in a reset state. No changes made.")
|
||||
return 0
|
||||
|
||||
try:
|
||||
with open(USERS_FILE, 'w') as f:
|
||||
json.dump(users_data, f, indent=4)
|
||||
print(f"User '{username}' has been reset successfully.")
|
||||
return 0
|
||||
except IOError:
|
||||
print(f"Error: Failed to reset user '{username}' in '{USERS_FILE}'.")
|
||||
except Exception as e:
|
||||
print(f"An error occurred while resetting the user: {e}")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
@ -53,6 +52,6 @@ if __name__ == "__main__":
|
||||
print(f"Usage: {sys.argv[0]} <username>")
|
||||
sys.exit(1)
|
||||
|
||||
username_to_reset = sys.argv[1]
|
||||
username_to_reset = sys.argv[1].lower()
|
||||
exit_code = reset_user(username_to_reset)
|
||||
sys.exit(exit_code)
|
||||
Reference in New Issue
Block a user