-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdb_populate_script.py
77 lines (71 loc) · 2.33 KB
/
db_populate_script.py
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
68
69
70
71
72
73
74
75
76
77
"""Module for populating MongoDB database"""
import sys
from json import load
from mongoengine import connect
from models.Product import Product
from models.User import User
from models.Review import Review
from models.Group import Group
## Replace it with you DB if you need to
MONGODB_URL = "mongodb+srv://capstone:Capstone123@wesourcecluster01.ctf3x.mongodb.net/WesourceDatabase?retryWrites=true&w=majority" # pylint: disable=line-too-long
connect(host=MONGODB_URL)
commands = {}
for arg in sys.argv:
if arg == "cu":
commands["create_users"] = True
elif arg == "cp":
commands["create_products"] = True
elif arg == "cr":
commands["create_reviews"] = True
elif arg == "cg":
commands["create_groups"] = True
elif arg == "du":
commands["delete_users"] = True
elif arg == "dp":
commands["delete_products"] = True
elif arg == "dr":
commands["delete_reviews"] = True
elif arg == "dg":
commands["delete_groups"] = True
elif arg == "delete_all":
commands["delete_all"] = True
USERS = None
with open(file="./data/users.json", encoding="utf-8") as reader:
USERS = load(reader)
PRODUCTS = None
with open("./data/products.json", encoding="utf-8") as reader:
PRODUCTS = load(reader)
REVIEWS = None
with open("./data/reviews.json", encoding="utf-8") as reader:
REVIEWS = load(reader)
GROUPS = None
with open("./data/groups.json", encoding="utf-8") as reader:
GROUPS = load(reader)
for key in commands:
if key == "create_users":
for user in USERS:
user = User(**user)
user.hash_password(user.password)
user.save()
if key == "create_products":
for product in PRODUCTS:
Product(**product).save()
if key == "create_reviews":
for review in REVIEWS:
Review(**review).save()
if key == "create_groups":
for group in GROUPS:
Group(**group).save()
if key == "delete_users":
User.drop_collection()
if key == "delete_products":
Product.drop_collection()
if key == "delete_reviews":
Review.drop_collection()
if key == "delete_groups":
Group.drop_collection()
if key == "delete_all":
User.drop_collection()
Product.drop_collection()
Review.drop_collection()
Group.drop_collection()