Beispiel #1
0
    def test_list(self):
        u1 = User(username='******', email='*****@*****.**', password='******')
        u2 = User(username='******', email='*****@*****.**', password='******')
        u3 = User(username='******', email='*****@*****.**', password='******')
        q1 = Question(answer='q1')
        q2 = Question(answer='q2')
        g1 = Game()
        q1.author = u2
        q2.author = u3
        q1.recipient = u1
        q2.recipient = u1
        q1.game = g1
        q2.game = g1

        db.session.add_all([u1, u2, u3, q1, q2, g1])
        db.session.commit()
        u1.last_question_read_time = datetime.utcnow()
        q2.timestamp = datetime.utcnow()
        db.session.commit()

        with self.client:
            self.client.post(url_for('auth.login'),
                             data={
                                 'email': '*****@*****.**',
                                 'password': '******'
                             })

            # test that only q2 appears (since q1.timestamp  < u1.last_question_read_time)
            response = self.client.get(url_for('questions.list'))
            data = response.get_data(as_text=True)
            self.assertTrue('Game Against three' in data)
            self.assertFalse('Game Against two' in data)
def questions():
	questions = current_user.questions.all()
	form = CreateQuestionForm()
	users = User.query.all()
	userlist = [(u.username, u.username) for u in users]
	form.assign_to.choices=userlist
	if form.validate_on_submit():
		question = Question(expression=form.expression.data)
		evalans = EvaluateExpression(form.expression.data)
		question.answer = evalans.evaluate()
		question.author = current_user.id 
		challenge = Challenge(question=question)
		username_to = []
		for name in form.assign_to.data:
			username_to.append(User.query.filter_by(username=name).first())

		challenge.to_user = username_to
		db.session.add(question)
		db.session.add(challenge)
		db.session.commit()
		flash('Congratulations, you have created a new question.')
		questions = current_user.questions.all()
		return render_template('questions.html', title='Questions', 
							user=current_user,
							questions=questions,
							form=form)
	return render_template('questions.html', title='Questions', 
							user=current_user,
							questions=questions,
							form=form)
Beispiel #3
0
    def save(self, commit=True):
        question = Question(title=self.cleaned_data.get('title'),
                            text=self.cleaned_data.get('text'))
        question.author = self.user
        if commit:
            question.save()

        question.tags.set(self.cleaned_data.get('tags'))
        if commit:
            question.save()
        return question
Beispiel #4
0
 def save(self, commit=True):
     question = Question(
         title=self.cleaned_data.get('title'),
         definition=self.cleaned_data.get('definition'),
     )
     question.author = self.author
     if commit:
         question.save()
     question.tags.set(self.cleaned_data.get('tags'))
     if commit:
         question.save()
     return question
    def save(self, commit=True):
        question = Question(title=self.cleaned_data['title'], text=self.cleaned_data['text'])
        question.author = self.auhtor
        question.dateTime = timezone.now()
        if commit:
            question.save()

        dis = DisLikeQuestion(question=question, dislikes=0)
        like = LikeQuestion(question=question, likes=0)
        dis.save()
        like.save()
        question.tags.set(self.cleaned_data['tags'])
        return question
Beispiel #6
0
def ask():
    getUser()
    form = AskForm()
    if session['qCount'] >= 5:
        return redirect('/')
    if form.validate_on_submit():
        q = Question(id=str(uuid.uuid4()),body=request.form['question'],optionOne=request.form['answerOne'],optionTwo=request.form['answerTwo'])
        q.author=db.session.query(User).filter_by(ip=session['user']).first()
        session['qCount']+=1
        db.session.add(q)
        db.session.commit()
        return redirect('/')
    return render_template('ask.html',form=form)
Beispiel #7
0
    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()
Beispiel #8
0
    def save(self, user):
        data = self.cleaned_data
        title = data.get('title', '')
        text = data.get('question', '')
        _tags = data.get('tags', '')

        q = Question()
        q.title = title
        q.text = text
        q.author = user
        q.save()

        if _tags != '':
            tags = re.split('\s+', _tags)
            print(tags)
            for tag in tags:
                print(tag)
                t = Tag.objects.get_or_create(title=tag)[0]
                q.tags.add(t)

        return q
Beispiel #9
0
    def test_send_hint(self):
        u1 = User(username='******', email='*****@*****.**', password='******')
        q1 = Question(answer='q1')
        q2 = Question(answer='q2')
        d1 = Drawing(filename='d1')
        q1.drawing = d1
        q1.author = u1
        db.session.add_all([q1, q2, u1, d1])
        db.session.commit()

        with self.client:
            self.client.post(url_for('auth.login'),
                             data={
                                 'email': '*****@*****.**',
                                 'password': '******'
                             })
            # invalid question
            response = self.client.get(
                url_for('questions.send_hint', question_id=100))
            self.assertEqual(response.status_code, 404)

            # question does not belong to user
            response = self.client.get(
                url_for('questions.send_hint', question_id=q2.id))
            self.assertEqual(response.status_code, 403)

            # successful post
            response = self.client.post(url_for('questions.send_hint',
                                                question_id=q1.id),
                                        data={'body': 'hint1'},
                                        follow_redirects=True)
            data = response.get_data(as_text=True)
            self.assertTrue('Hint Successfully Submitted' in data)
            hint1 = Hint.query.get(1)
            self.assertEqual(hint1.body, 'hint1')
            self.assertEqual(hint1.question, q1)
Beispiel #10
0
    def test_question(self):
        u1 = User(username='******', email='*****@*****.**', password='******')
        u2 = User(username='******', email='*****@*****.**', password='******')
        q1 = Question(answer='q1')
        q2 = Question(answer='q2')
        q3 = Question(answer='q3')
        g1 = Game(author=u1,
                  guest=u2,
                  max_points=2,
                  status='waiting_author_answer')
        d1 = Drawing(filename='d1')
        q1.drawing = d1
        q1.author = u2
        q1.recipient = u1
        q1.game = g1
        q1.max_tries = 2
        q3.drawing = d1
        q3.author = u2
        q3.recipient = u1
        q3.game = g1
        q3.max_tries = 2
        q3.status = 'lost'
        db.session.add_all([u1, u2, q1, q2, q3, g1])
        db.session.commit()

        with self.client:
            self.client.post(url_for('auth.login'),
                             data={
                                 'email': '*****@*****.**',
                                 'password': '******'
                             })

            # invalid question
            response = self.client.get(url_for('questions.question', id=100))
            self.assertEqual(response.status_code, 404)

            # question does not belong to user
            response = self.client.get(url_for('questions.question', id=q2.id))
            self.assertEqual(response.status_code, 403)

            # question not in progress
            self.assertEqual(q3.num_of_tries, 0)
            response = self.client.get(url_for('questions.question', id=q3.id))
            self.assertEqual(q3.num_of_tries, 0)

            # if question answered incorrectly (but no loss or win)
            response = self.client.post(url_for('questions.question',
                                                id=q1.id),
                                        data={'answer': 'incorrect'},
                                        follow_redirects=True)
            data = response.get_data(as_text=True)
            self.assertTrue('Incorrect. Please Try Again.' in data)
            self.assertEqual(q1.num_of_tries, 1)

            # if user runs out of tries on question
            response = self.client.post(url_for('questions.question',
                                                id=q1.id),
                                        data={'answer': 'incorrect'},
                                        follow_redirects=True)
            data = response.get_data(as_text=True)
            self.assertTrue(
                'Incorrect. You Have Run Out of Tries On This Question' in
                data)
            self.assertEqual(q1.num_of_tries, 2)
            self.assertEqual(q1.status, 'lost')
            # check that game was updated correctly
            self.assertEqual(g1.status, 'author_turn_to_ask')

            # if user answers question correctly
            g1.status = 'waiting_author_answer'
            q1.status = 'in_progress'
            q1.num_of_tries = 1
            db.session.commit()
            response = self.client.post(url_for('questions.question',
                                                id=q1.id),
                                        data={'answer': 'q1'},
                                        follow_redirects=True)
            data = response.get_data(as_text=True)
            self.assertTrue('Question Answered Correctly' in data)
            self.assertEqual(q1.num_of_tries, 2)
            self.assertEqual(q1.status, 'complete')
            # check that game was updated correctly
            self.assertEqual(g1.status, 'author_turn_to_ask')

            # if user answers question correctly (leading to game win)
            g1.status = 'waiting_author_answer'
            q1.status = 'in_progress'
            q1.num_of_tries = 1
            db.session.commit()
            response = self.client.post(url_for('questions.question',
                                                id=q1.id),
                                        data={'answer': 'q1'},
                                        follow_redirects=True)
            data = response.get_data(as_text=True)
            self.assertTrue('Question Answered Correctly' in data)
            self.assertEqual(q1.num_of_tries, 2)
            self.assertEqual(q1.status, 'complete')
            # check that game was updated correctly
            self.assertEqual(g1.status, 'author_win')