def fill_questions(self): fake = Factory.create() starts = ( 'How do I Sort a Multidimensional', 'What is Max?', 'SQL Server' ) for i in range(0, 40): q = Question() q.title = fake.sentence(nb_words=randint(2, 4), variable_nb_words=True) q.text = u"%s %s %s" % ( choice(starts), os.linesep, fake.paragraph(nb_sentences=randint(1, 4), variable_nb_sentences=True), ) q.author = Profile.objects.first() q.rating = randint(0, 1500) q.id = i q.save() self.stdout.write('add question [%d]' % (q.id))
def handle(self, *args, **options): users = list(User.objects.all()) for i in range(10): t = Topic() t.name = u'Topic Name {}'.format(i) t.description = u'Topic Description {}'.format(i) t.save() topics = list(Topic.objects.all()) for j in range(100): q = Question() q.author = random.choice(users) q.title = u'title {}'.format(j) q.text = u'text {}'.format(j) q.pub_date = datetime.datetime.now() q.is_published = True q.save() q.topics = random.sample(topics, random.randint(1, 6)) questions = list(Question.objects.all()) for k in range(100): c = Comment() c.author = random.choice(users) c.question = random.choice(questions) c.text = u'text {}'.format(k) c.pub_date = datetime.datetime.now() c.save()
def doimport(self, data): from django.contrib.auth.models import User from questions.models import Question, Answer from tagging.models import Tag try: question = Question() question.content = data['question'] question.tags = data['tags'] question.author = User.objects.get( pk=0) #FIXME: System smrtr user: use constant? question.save() # Save to allow m2m # Create correct answer c = Answer() c.content = data['correct'] c.is_correct = True c.question = question c.save() # Save incorrect answers data['incorrect'] = filter(lambda x: len(x) > 0, data['incorrect']) # Remove empty items for incorrect in data['incorrect']: ic = Answer() ic.content = incorrect ic.is_correct = False ic.question = question ic.save() except: print "Error importing:" + data['question']
def post(self, request): title = request.POST['title'] details = request.POST['details'].replace("'", ''') author = request.user question = Question(title=title, question_details=details) try: question.anonymous = request.POST['anonymous'] except: pass # if request.POST['anonymous']: # question.anonymous = True question.author = author question.save() if question.anonymous: AnonymousQuestionsWriters(question=question, user=request.user).save() write_profile = author.profile write_profile.no_of_questions += 1 write_profile.save() follow_question.delay(question.id, request.user.id) return redirect('/questions/' + str(question.slug))
def ask(request): ask_form = AskQuestion(request.POST or None) args = {} args['form'] = ask_form if request.POST and ask_form.is_valid(): question = Question(text=ask_form.cleaned_data['text'], title=ask_form.cleaned_data['title']) tags = ask_form.cleaned_data['tags'] g = Tag.objects.all() getTag = tags.split(', ') for tag in getTag: counter = 0 for l in g: if l.tag == tag: counter += 1 if counter == 0: t = Tag(tag=tag) t.save() user = auth.get_user(request) question.author = user question.save() a = g.filter(tag__in=getTag) question.tags.add(*a) return redirect('questionGet', question_id=question.id) else: return render(request, 'ask.html', args)
def doimport(self,data): from django.contrib.auth.models import User from questions.models import Question, Answer from tagging.models import Tag try: question = Question() question.content = data['question'] question.tags = data['tags'] question.author = User.objects.get(pk=0) #FIXME: System smrtr user: use constant? question.save() # Save to allow m2m # Create correct answer c = Answer() c.content = data['correct'] c.is_correct = True c.question = question c.save() # Save incorrect answers data['incorrect'] = filter(lambda x: len(x)>0, data['incorrect']) # Remove empty items for incorrect in data['incorrect']: ic = Answer() ic.content = incorrect ic.is_correct = False ic.question = question ic.save() except: print "Error importing:" + data['question']
def save(self, commit=True): question = Question() question.title = self.cleaned_data["title"] question.text = self.cleaned_data["text"] question.author = self.author question.save() for tag in self.cleaned_data["tags"]: try: question.tags.create(title=tag) except IntegrityError: question.tags.add(Tag.objects.get(title=tag)) return question
def handle(self, *args, **options): fake_factory = Factory.create('en_US') number = int(options['number']) users = User.objects.all()[1:] for i in range(number): question = Question() question.title = fake_factory.sentence(nb_words=randint(4, 6), variable_nb_words=True) question.text = fake_factory.text(max_nb_chars=500) question.author = choice(users) question.date = fake_factory.date() question.save() self.stdout.write('added question [%d]' % (question.id))
def handle(self, *args, **options): fake = Factory.create() number = int(options['number']) users = User.objects.all()[1:] for i in range(0, number): q = Question() q.title = fake.sentence(nb_words=randint(2, 4), variable_nb_words=True) q.text = fake.paragraph(nb_sentences=randint(4, 17), variable_nb_sentences=True) q.author = choice(users) q.save()
def create_questions(self): fake = Faker() tags_set = Tag.objects.all() author_set = User.objects.all() print() for i in range(100): post = Question() post.author = random.choice(author_set) post.title = fake.sentence(nb_words=6, variable_nb_words=True, ext_word_list=None) post.text = fake.text(max_nb_chars=100, ext_word_list=None) post.create_date = fake.date(pattern="%Y-%m-%d", end_datetime=None) post.id = i post.save() for j in range(3): t = random.choice(tags_set) post.tags.add(t)
def post(self, request): title = request.POST['title'] details = request.POST['details'] author = request.user topic = '' date_asked = date.today() time_asked = timezone.now().time().strftime('%H:%M') question = Question() question.title = title question.question_details = details question.author = author question.date_asked = date_asked question.time_asked = time_asked question.save() write_profile = UserOtherDetails.objects.get(user=author) write_profile.no_of_questions += 1 write_profile.save() topic = QuestionTopic.objects.filter(question=question) return redirect('/questions/' + str(question.slug))
def handle(self, *args, **options): User.objects.exclude(is_staff=True).delete() Question.objects.all().delete() Answer.objects.all().delete() password = "******" exp_questions = 50 exp_users = 20 avg_answers_per_question = 3 # avg_stars_per_question = 5 num_authors = User.objects.all().count() num_questions = Question.objects.all().count() num_answers = Answer.objects.all().count() faker = Faker() self.stdout.write("Defalt password:"******"Number of users:" + str(num_authors)) self.stdout.write("Number of questions:" + str(num_questions)) self.stdout.write("Number of answers:" + str(num_answers)) if(num_authors < exp_users): self.stdout.write( "Creating " + str(exp_users-num_authors) + "Authors") for x in range(num_authors, exp_users): username = faker.name().replace(" ", ".") email = username+"@test.com" User.objects.create(username=username, email=email, password=password) self.stdout.write(" " + username) num_authors = User.objects.all().count() if(num_questions < exp_questions): self.stdout.write( "Creating " + str(exp_questions-num_questions) + "Questions") for x in range(num_questions, exp_questions): u = self.randomAuthor() title = "Test Question " + str(x) self.stdout.write(" [" + u.username + "] "+title) rec = Question() rec.author = u rec.title = title rec.text = faker.text(225) rec.save() num_questions = Question.objects.all().count() self.stdout.write("Answers...") while(num_answers < avg_answers_per_question*num_questions): author = self.randomAuthor() question = self.randomQuestion() rec = Answer() rec.author = author rec.question = question rec.text = faker.text(250) rec.save() num_answers = Answer.objects.all().count() self.stdout.write(str(num_answers) + ": " + author.username + " gave an answer on [" + question.title + "]")