Created
April 23, 2016 11:01
-
-
Save jirfag/e89db4ddfa94c4b5e67b9e08fbbf80d8 to your computer and use it in GitHub Desktop.
Fast objects generation with bulk operations for Django app
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
from django.core.management.base import BaseCommand | |
from qa.models import Question, Answer | |
from lib.models import Liker, Tag | |
import random | |
#cd /Users/denis/track/env/lib/python2.7/site-packages/django && fgrep -lr "def " . | xargs -P8 -n1 cat | perl -lne '/^def (\w+)\(/ && print $1' >/tmp/all_django_funcs.txt | |
with open('/tmp/all_django_funcs.txt', 'r') as f: | |
all_django_funcs = f.read().splitlines() | |
question_text_templates = ( | |
'What is "{}" for?', | |
'How to use "{}"?', | |
'Is it error in "{}"?', | |
'Why Django has function "{}"?', | |
'Does RoR have analog of Django`s "{}"?', | |
'Where to find definition of "{}"?', | |
'Can you explain me Django function "{}"?', | |
) | |
answer_templates = ( | |
'Just open source code to see what "{}" does', | |
'Function "{}" does nothing', | |
'The function "{}" is deprecated, dont use it"', | |
'There is some error in function "{}"', | |
'What version of Django do you use?', | |
) | |
question_long_templates = ( | |
'I have googled it, searched through source code, but I cant find. Help me plz!', | |
'Thanks for your responses', | |
'Also I`d like to know what I need to read to easily understand all Django function. Give me an advice please.', | |
) | |
def create_questions(obj_n): | |
questions = [] | |
for idx in range(obj_n): | |
func_name = random.choice(all_django_funcs) | |
question_title = 'Django function "{}"'.format(func_name) | |
question_text = random.choice(question_text_templates).format(func_name) + ' ' + random.choice(question_long_templates) | |
q = Question(title=question_title, text=question_text, likes_n=random.randint(-3, 3)) | |
questions.append(q) | |
Question.objects.bulk_create(questions) | |
answers = [] | |
created_questions = Question.objects.all() | |
for q in created_questions: | |
for _ in range(random.randint(0, 10)): | |
answer_text = random.choice(answer_templates).format(func_name) | |
a = Answer(text=answer_text, question=q, likes_n=random.randint(-3, 3)) | |
answers.append(a) | |
Answer.objects.bulk_create(answers) | |
likes = [] | |
for a in Answer.objects.all(): | |
is_like = True if a.likes_n >= 0 else False | |
likes.extend([Liker(content_object=a, is_like=is_like) for _ in range(abs(a.likes_n))]) | |
for q in created_questions: | |
is_like = True if q.likes_n >= 0 else False | |
likes.extend([Liker(content_object=q, is_like=is_like) for _ in range(abs(q.likes_n))]) | |
Liker.objects.bulk_create(likes) | |
class Command(BaseCommand): | |
def add_arguments(self, parser): | |
parser.add_argument("-n", type=int, dest="num") | |
def handle(self, *args, **options): | |
Question.objects.all().delete() | |
Answer.objects.all().delete() | |
Liker.objects.all().delete() | |
Tag.objects.all().delete() | |
obj_n = options.get('num', 100) | |
create_questions(obj_n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment