Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seed data #195

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tools/cleanDB.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pymongo import MongoClient

client = MongoClient("mongodb://vhomesgroup:[email protected]:27017,cluster0-shard-00-01.rmikc.mongodb.net:27017,cluster0-shard-00-02.rmikc.mongodb.net:27017/VHomes?ssl=true&replicaSet=atlas-1wcpgc-shard-0&authSource=admin&retryWrites=true&w=majority")
mydb = client['Staging']
mycol = mydb['users']

mycol.remove()

print("db cleaned!")
51 changes: 51 additions & 0 deletions tools/seeder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import csv
from faker import Faker
import random
import time
import os
import pandas as pd
from pymongo import MongoClient
import json

def randomBool():
return random.randint(0, 1) == 1

def datagenerate(records, headers):
fake = Faker('en_US')
with open("Seed_data.csv", 'wt') as csvFile:
writer = csv.DictWriter(csvFile, fieldnames=headers)
writer.writeheader()
for i in range(records):
writer.writerow({
"name" : fake.name(),
"email" : fake.email(),
"password" : "$2a$10$F5XvrA99hPKWs11YZmsZ4.C7KFXcP.B1W0h9T4ACTg14Eyq/IXg4K",
"isHost" : randomBool(),
"friends" : "...",
"profileImg" : fake.image_url(),
"isVerified" : randomBool() if "isHost" else None,
"isPublic" : randomBool() if "isHost" else False,
"stripeId" : None
})

if __name__ == '__main__':
records = 100
headers = ["name", "email", "password", "isHost", "friends", "profileImg", "isVerified", "isPublic", "stripeId"]
datagenerate(records, headers)
print("CSV generation complete!")

time.sleep(3)

#This is where the database gets seeded

client = MongoClient("mongodb://vhomesgroup:[email protected]:27017,cluster0-shard-00-01.rmikc.mongodb.net:27017,cluster0-shard-00-02.rmikc.mongodb.net:27017/VHomes?ssl=true&replicaSet=atlas-1wcpgc-shard-0&authSource=admin&retryWrites=true&w=majority")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use the environment variables for this? The coordinates.py file has an example of how to pull from the .env file https://github.com/staynomad/Nomad-Back/blob/master/tools/coordinates.py. We should do the same for the cleanDB.py script

mydb = client['Staging']
mycol = mydb['users']

data = pd.read_csv("../tools/Seed_data.csv")
payload = json.loads(data.to_json(orient='records'))
mycol.remove()
mycol.insert_many(payload)

os.remove("Seed_data.csv")
print("csv Removed!")