-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulator.py
96 lines (83 loc) · 3.39 KB
/
populator.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE','secretsapp.settings')
import django
django.setup()
import random
import string
from secretsmodules.models import *
import csv
from faker import Faker
from django.contrib.auth.models import User
from django.core.files import File
import string
from django.db.models import Q
fakegen = Faker()
def random_string_generator(size=10, chars=string.ascii_lowercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def add_countries():
dir = os.path.join(os.getcwd(),'tempdata','countries.csv')
with open(dir,'r') as csvFile:
reader = csv.reader(csvFile)
next(reader,None)
for row in reader:
c = Country.objects.get_or_create(name = row[0], code = row[1])[0]
def add_secrets(n):
countries = list(Country.objects.all())
dir = os.path.join(os.getcwd(),'tempdata','images')
images = [os.path.join(dir,x) for x in os.listdir(dir)]
for i in range(0,n):
title = fakegen.sentence()
description = fakegen.text()
price = float(random.randrange(500,10000))
imagePath = images[random.randint(0,len(images)-1)] #I don't know how to save it programically, doing empty for now
f = open(imagePath,'rb')
img =File(f)
date = fakegen.date_between(start_date="-1y", end_date="today")
country = countries[random.randint(0,len(countries)-1)]
rank = random.randint(1,5)
secret = Secret.objects.get_or_create(title = title, description = description,
price = price, date = date, country_of_origin = country, rank = rank)[0]
secret.picture.save(random_string_generator(size = 32)+'.jpg',img,True)
secret.save()
def add_users(n):
for i in range(0,n):
username = fakegen.user_name()
email = fakegen.email()
fname = fakegen.first_name()
lname = fakegen.last_name()
password = fakegen.word()
addr = fakegen.address()
phone = fakegen.phone_number()
user = User.objects.create_user(username=username,
email=email,
password=password,
first_name = fname,
last_name = lname)
user.save()
profile = UserProfile(address = addr, phone = phone, user = user)
profile.save()
def add_carts(n):
users = list(UserProfile.objects.all())
secrets = list(Secret.objects.all())
for i in range(0,n):
cart = Cart(user = users[random.randint(0,len(users)-1)])
cart.save()
for j in range(0, random.randint(1,5)):
secret = secrets[random.randint(0, len(secrets)-1)]
item = PurchasedItem(cart = cart, secret = secret, purchased_price = secret.price)
item.save()
def replace_images(secrets):
dir = os.path.join(os.getcwd(),'tempdata','images')
images = [os.path.join(dir,x) for x in os.listdir(dir)]
for item in secrets:
imagePath = images[random.randint(0,len(images)-1)] #I don't know how to save it programically, doing empty for now
f = open(imagePath,'rb')
img =File(f)
item.picture.save(random_string_generator(size = 32)+'.jpg',img,True)
item.save()
if __name__ == '__main__':
add_countries()
add_secrets(30)
add_users(8)
add_carts(7)
print('done')