forked from pungoyal/shadow-poll
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
script to add dummy responses in the database. USAGE: ./manage.py dum…
…mydata
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import random, os | ||
from django.core.management.base import NoArgsCommand | ||
|
||
from apps.poll.models import * | ||
from apps.charts.models import * | ||
from apps.reporters.models import * | ||
|
||
class Command(NoArgsCommand): | ||
USERS_TO_BE_CREATED = 100000 | ||
RESPONSES_PER_QUESTION = 1000000 | ||
|
||
def handle_noargs(self, **options): | ||
self.setUp() | ||
governorates = Governorate.objects.all() | ||
for i in range(0, self.USERS_TO_BE_CREATED): | ||
self.save_random_user(random.choice(governorates)) | ||
|
||
users = User.objects.all() | ||
for i in range(0, self.RESPONSES_PER_QUESTION): | ||
self.save_random_user_response(random.choice(users)) | ||
|
||
# os.system('/Users/Puneet/work/shadow-poll/manage.py dumpdata reporters poll --indent=4 > /Users/Puneet/work/shadow-poll/a.json') | ||
print "I just created %s Users and %s dummy UserResponses!" % (self.USERS_TO_BE_CREATED, self.RESPONSES_PER_QUESTION) | ||
|
||
def setUp(self): | ||
backends = PersistantBackend.objects.all() | ||
if len(backends) == 0: | ||
backend = PersistantBackend(slug="test", title="data") | ||
backend.save() | ||
else: | ||
backend = backends[0] | ||
|
||
connections = PersistantConnection.objects.all() | ||
if len(connections) == 0: | ||
self.connection = PersistantConnection(is_bot=False, identity="test", backend=backend) | ||
self.connection.save() | ||
else: | ||
self.connection = connections[0] | ||
|
||
def save_random_user(self, governorate): | ||
districts = District.objects.filter(governorate=governorate.id) | ||
user = User(connection = self.connection, | ||
age = random.randint(2,18), | ||
gender = random.choice('mf'), | ||
governorate = governorate.id, | ||
district = random.choice(districts).code) | ||
user.save() | ||
|
||
def save_random_user_response(self, user): | ||
for question_id in range(1,4): | ||
question = Question.objects.get(id=question_id) | ||
choices = Choice.objects.filter(question=question) | ||
user_response = UserResponse(question=question, user=user, choice=random.choice(choices)) | ||
user_response.save() | ||
|