-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (51 loc) · 1.78 KB
/
Copy pathmain.py
File metadata and controls
67 lines (51 loc) · 1.78 KB
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
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
import getpass
from argparse import ArgumentParser
from os import getenv
from dotenv import load_dotenv
load_dotenv()
URI = getenv("MONGO_URI")
parser = ArgumentParser()
parser.add_argument('--reg', action='store_true', help="'--reg' for register new user")
parser.add_argument('--name','-n', type=str, default='', help='name')
parser.add_argument('--usr','-u', type=str, default='', help='username')
parser.add_argument('--email','-e', type=str, default='', help='email')
parser.add_argument('--list','-ls', action='store_true', help="list all registers user")
args = parser.parse_args()
# Create a new client and connect to the server
client = MongoClient(URI, server_api=ServerApi('1'))
db = client["db1"]
coll = db['items']
def register_user(collection):
name = args.name.strip()
email = args.email.strip()
username = args.usr.strip()
password = getpass.getpass("Password: ").strip()
# Check if username/email already exists
if collection.find_one({"$or": [{"username": username}, {"email": email}]}):
print("⚠️ Username or Email already exists. Try again.")
return
user_data = {
"name": name,
'username': username,
"email": email,
"password": password
}
if name and email and username and password:
collection.insert_one(user_data)
print("✅ Registration successful!")
else:
print("Invalid input!")
def list_users(collection):
for user in collection.find({}, {"_id": 0, "password": 0}): # Hide password
print(user)
def main():
if args.reg:
register_user(coll)
elif args.list:
list_users(coll)
else:
print("Invalis input")
# pass
main()