Advertisement
Guest User

fill

a guest
May 31st, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. import itertools
  2. import random
  3. from django.core.management.base import BaseCommand, CommandError
  4. from django.contrib.auth.hashers import make_password
  5. from django.db import IntegrityError
  6. from askEkkert.models import Profile, Question, Answer, Tag, Like
  7. from loremipsum import get_sentence, get_paragraph, get_sentences, get_paragraphs
  8. from random import randint, randrange
  9. from datetime import timedelta, datetime
  10.  
  11. users_count = 10001
  12. questions_count = 100001
  13. answers_count = 1000001
  14. tags_count = 10001
  15. likes_count = 2000001
  16.  
  17. class Command(BaseCommand):
  18. help = 'Fill the DB'
  19.  
  20. def handle(self, *args, **options):
  21. start_time = datetime.now()
  22.  
  23. for user_id in range(2, users_count + 1):
  24. if user_id % 10 == 0:
  25. self.stdout.write("user: " + str(user_id))
  26.  
  27. user = Profile(
  28. avatar='http://lorempixel.com/8%s/8%s' % (user_id % 10, user_id % 10),
  29. id=user_id,
  30. password=make_password("secret"),
  31. last_login=self.random_date(),
  32. is_superuser=False,
  33. username="user%s" % (user_id),
  34. first_name="",
  35. last_name="",
  36. email="",
  37. is_staff=False,
  38. is_active=True,
  39. date_joined=self.random_date())
  40. user.save()
  41.  
  42. for question_id in range(1, questions_count + 1):
  43. if question_id % 10 == 0:
  44. self.stdout.write("question: " + str(question_id))
  45.  
  46. text = ''
  47. for i in get_paragraphs(randint(1,3)):
  48. text += i
  49.  
  50. question = Question(
  51. id=question_id,
  52. title=get_sentence(),
  53. text=text,
  54. user=randint(1, users_count), #CustomUser.objects.get(pk=randint(1, users_count)),
  55. crated=self.random_date(),
  56. rating=0,
  57. )
  58. question.save()
  59.  
  60.  
  61. for answer_id in range(843888, answers_count + 1):
  62. if answer_id % 100 == 0:
  63. self.stdout.write("answer: " + str(answer_id))
  64.  
  65. text = ''
  66. for i in get_paragraphs(randint(1,2)):
  67. text += i
  68.  
  69. if (randint(0,1) == 0):
  70. correct = False
  71. else:
  72. correct = True
  73.  
  74. answer = Answer(
  75. id=answer_id,
  76. text=text,
  77. user=randint(1, users_count),
  78. crated=self.random_date(),
  79. question=randint(1, questions_count),
  80. correct=correct)
  81. answer.save()
  82.  
  83. words = open('ask/words', 'r')
  84. tag_id = 1
  85. for tag_id in range(1, tags_count + 1):
  86. self.stdout.write("tag: " + str(tag_id))
  87. tag = Tag(
  88. id=tag_id,
  89. name=words.readline()[:-1])
  90. tag.save()
  91. words.close()
  92.  
  93. for question_id in range(1, questions_count + 1):
  94. if question_id % 10 == 0:
  95. self.stdout.write('Linking tags with question:' + str(question_id))
  96.  
  97. question = Question.objects.get(pk=question_id)
  98. for i in range(1, 4):
  99. question.tags.add(Tag.objects.get(pk=randint(1, tags_count)))
  100.  
  101.  
  102. for like_id in range(1, likes_count + 1):
  103.  
  104. value = -1 if randint(0, 1) == 0 else 1
  105. like = Like(
  106. id=like_id,
  107. is_like=value,
  108. question=randint(1, questions_count),
  109. user=randint(1, users_count))
  110. try:
  111. like.save()
  112. if like_id % 10 == 0:
  113. self.stdout.write('like:' + str(like_id))
  114. except IntegrityError:
  115. pass
  116.  
  117.  
  118. for question in Question.objects.all():
  119. for like in question.like_set.all():
  120. question.rating += like.like_type
  121. if like.id % 10 == 0:
  122. self.stdout.write('question: ' + str(question.id) + ', like: ' + str(like.id))
  123. question.save()
  124.  
  125.  
  126. end_time = datetime.now()
  127.  
  128. self.stdout.write('Database filled successfully' + str(end_time - start_time))
  129.  
  130. def random_date(self):
  131.  
  132. start = datetime(2014, 1, 1, 1, 0, 0, 0, None)
  133. end = datetime(2015, 1, 1, 1, 0, 0, 0, None)
  134.  
  135. delta = end - start
  136. int_delta = (delta.days * 24 * 60 * 60) + delta.seconds
  137. random_second = randrange(int_delta)
  138. return start + timedelta(seconds=random_second)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement