-
Notifications
You must be signed in to change notification settings - Fork 172
/
seeder.py
117 lines (109 loc) · 3.39 KB
/
seeder.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from polls.models import Choice, Poll, Vote
from django.contrib.auth.models import User
import datetime
import random
import time
from faker import Faker
fake = Faker()
def seed_users(num_entries=10, overwrite=False):
"""
Creates num_entries worth a new users
"""
if overwrite:
print("Overwriting Users")
User.objects.all().delete()
count = 0
for _ in range(num_entries):
first_name = fake.first_name()
last_name = fake.last_name()
u = User.objects.create_user(
first_name=first_name,
last_name=last_name,
email=first_name + "." + last_name + "@fakermail.com",
username=first_name + last_name,
password="password"
)
count += 1
percent_complete = count / num_entries * 100
print(
"Adding {} new Users: {:.2f}%".format(
num_entries, percent_complete),
end='\r',
flush=True
)
print()
def seed_polls(num_entries=10, choice_min=2, choice_max=5, overwrite=False):
"""
Seeds num_entries poll with random users as owners
Each poll will be seeded with # choices from choice_min to choice_max
"""
if overwrite:
print('Overwriting polls')
Poll.objects.all().delete()
users = list(User.objects.all())
count = 0
for _ in range(num_entries):
p = Poll(
owner=random.choice(users),
text=fake.paragraph(),
pub_date=datetime.datetime.now()
)
p.save()
num_choices = random.randrange(choice_min, choice_max + 1)
for _ in range(num_choices):
c = Choice(
poll=p,
choice_text=fake.sentence()
).save()
count += 1
percent_complete = count / num_entries * 100
print(
"Adding {} new Polls: {:.2f}%".format(
num_entries, percent_complete),
end='\r',
flush=True
)
print()
def seed_votes():
"""
Creates a new vote on every poll for every user
Voted for choice is selected random.
Deletes all votes prior to adding new ones
"""
Vote.objects.all().delete()
users = User.objects.all()
polls = Poll.objects.all()
count = 0
number_of_new_votes = users.count() * polls.count()
for poll in polls:
choices = list(poll.choice_set.all())
for user in users:
v = Vote(
user=user,
poll=poll,
choice=random.choice(choices)
).save()
count += 1
percent_complete = count / number_of_new_votes * 100
print(
"Adding {} new votes: {:.2f}%".format(
number_of_new_votes, percent_complete),
end='\r',
flush=True
)
print()
def seed_all(num_entries=10, overwrite=False):
"""
Runs all seeder functions. Passes value of overwrite to all
seeder function calls.
"""
start_time = time.time()
# run seeds
seed_users(num_entries=num_entries, overwrite=overwrite)
seed_polls(num_entries=num_entries, overwrite=overwrite)
seed_votes()
# get time
elapsed_time = time.time() - start_time
minutes = int(elapsed_time // 60)
seconds = int(elapsed_time % 60)
print("Script Execution took: {} minutes {} seconds".format(minutes, seconds))