Example #1
0
    def test_herding(self):
        Question(title="Pickles").put()
        Question(title="Mr. Sparkles").put()

        r = self.testapp.get('/questions')

        assert "Pickles" in r
        assert "Mr. Sparkles" in r
Example #2
0
    def testQueries(self):
        # log in user one
        self.loginUser('*****@*****.**')

        # create two Questions
        Question1 = Question(title="Question 1")
        Question1.put()
        Question2 = Question(title="Question 2")
        Question2.put()

        # log in user two
        self.loginUser('*****@*****.**')

        # create two more Questions
        Question3 = Question(title="Question 3")
        Question3.put()
        Question4 = Question(title="Question 4")
        Question4.put()

        # Get all Questions
        all_Questions = list(Question.all_questions())

        # Make sure there are 4 Questions in total
        assert len(all_Questions) == 4

        # Make sure they're in the right order
        assert all_Questions == [Question4, Question3, Question2, Question1]

        # Make sure we only get two for user2, and that they're the right Questions
        user2_Questions = list(Question.all_questions_by_user())

        assert len(user2_Questions) == 2
        assert user2_Questions == [Question4, Question3]

        # Test all Questions shown up
        resp = self.testapp.get('/questions')
        assert 'Question 1' in resp.body
        assert 'Question 2' in resp.body
        assert 'Question 3' in resp.body
        assert 'Question 4' in resp.body

        # Test Questions_created_by_user
        resp = self.testapp.get('/questions?=mine')
        #         assert 'Question 1' not in resp.body
        #         assert 'Question 2' not in resp.body
        #         assert 'Question 3' in resp.body
        #         assert 'Question 4' in resp.body

        # Test 'edit' link exists
        assert 'Edit' in resp.body
Example #3
0
def bulk_create_question(db: Session, input: List[CreateQuestionInput]):
    questions = []
    for inp in input:
        questions.append(Question(**inp.dict()))

    db.add_all(questions)
    db.commit()
    return questions
Example #4
0
 def post(self):
     form = ReportForm()
     if form.validate_on_submit():
         question = Question(user_id=current_user.id,
                             title=form.title.data,
                             detail=form.detail.data)
         question.save()
         return redirect(url_for('user.report'))
     return render_template('user/question/report.html', form=form)
Example #5
0
def postInsertAlgoQuestion():

    requestData = request.get_json()

    # Error in parameters
    if "filename" not in dict(requestData):
        # Response
        return jsonify({
            'success':
            False,
            "message":
            "Please provide the 'filename' field in the body"
        })

    filename = requestData['filename']

    # Other question propertiess
    questionObj = deepcopy(requestData)
    del questionObj["filename"]

    try:
        questions = readProgrammingQuestions(filename)

        for question in questions:
            status, msg, id = Question(question,
                                       type=QuestionType.ALGO).insert_one()
            questionObj["body"] = question['body']

            if not status:
                Question({
                    "body": question['body']
                }, type=QuestionType.ALGO).update_one(questionObj)

            print(f"-- {msg} ---")

        return jsonify({"success": True, "message": "Questions are obtained!"})

    except Exception as e:

        raise e

        # Response
        return jsonify({"success": False, "message": str(e)})
Example #6
0
def postInsertQuestion():

    requestData = request.get_json()

    # Error in parameters
    if "filename" not in dict(requestData):
        # Response
        return jsonify({
            'success':
            False,
            "message":
            "Please provide the 'filename' field in the body"
        })

    filename = requestData['filename']

    # Other question propertiess
    questionObj = deepcopy(requestData)
    del questionObj["filename"]

    # Reading the questions from the given file
    questions = readQuestions(filename)

    try:
        for question in questions:
            questionObj["body"] = question

            status, msg, id = Question(questionObj).insert_one()

            if not status:
                Question({"body": question}).update_one(questionObj)

            print(f"-- {msg} ---")

        return jsonify({"success": True, "message": "Questions are obtained!"})

    except Exception as e:

        # Response
        return jsonify({"success": False, "message": str(e)})
Example #7
0
def create_question(cid):
    course = Course.query.filter_by(cid=cid).first_or_404()
    form = QuestionCreateForm().validate_for_api()
    with db.auto_commit():
        question = Question(title=form.title.data,
                            content=form.content.data,
                            course_id=course.cid,
                            author_gid=g.user['gid'],
                            update_time=int(datetime.now().timestamp()))
        history = History(root_question=question)
        for tag_name in form.tags.data:
            tag = Tag.get_or_create_tag(tag_name)
            question.tags.append(tag)
        db.session.add(question, history)
    return Success()
Example #8
0
def create_and_save_question(title, text, is_mcq, answers, room):
    '''
    Helper to create a question and save it to the graph
    :param name: the name of the question
    :param active: whether or not this question will be active by default
    :param admin_id: the user id representing the admin
    :param course_id: the course code / id representing the course that this question studies
    :return ValueError: raised if either the course or user cannot be found in the graph
    '''

    new_question = Question(title=title, text=text, is_multiple_choice=is_mcq)
    new_question.save()

    new_question.room.connect(room)

    for a in answers:
        temp_answ = Answer(text=a.get('text'), correct=a.get('correct'))
        temp_answ.save()
        temp_answ.question.connect(new_question)

    return new_question
Example #9
0
    def initdb():
        click.echo('Initializing the database...')
        User.drop_collection()
        Post.drop_collection()
        Question.drop_collection()


        click.echo('Generating the User..')
        # FIXME:
        for i in range(1, 10):
            u = User(
                email = fake.email(),
                username = fake.name(),
                avatar = f'http://192.168.1.106:8000/avatar/r{i}.png'
            )
            u.set_password('helloworld')
            u.save()
        click.echo('Done')

        click.echo('Generating the Post..')
        for i in range(10):
            p = Post(
                title = fake.sentence(),
                content = fake.text(2000),
                category = random.choices(['Python', 'JavaScript','Golang', 'Flask','Vue', 'Django'])[0],
                tags = random.choices(['Python', 'JavaScript','Golang', 'Flask','Vue', 'Django']),
            )
            p.save()
        click.echo('Done')
        
        click.echo('Generating the Comment..')
        for i in Post.objects.all():
            for j in range(5):
                c = Comment(
                    author = random.choice(User.objects.all()),
                    content = fake.text(200),
                    )
                i.comments.append(c)
                i.save()
        click.echo('Done')


        click.echo('Generating the Reply..')
        for i in Post.objects.all():
            for j in range(5):
                c = Comment(
                    author = random.choice(User.objects.all()),
                    content = fake.text(200),
                    reply = random.choice(i.comments).cid
                )
                i.comments.append(c)
                i.save()

        click.echo('Done')

        click.echo('Generating the Question..')
       
        for j in range(5):
            q = Question(
                title = fake.sentence(),
                author = random.choice(User.objects.all()),
                description = fake.text(200),
                tags = random.choices(['Python', 'JavaScript','Golang', 'Flask','Vue', 'Django']),
            )
            q.save()

        click.echo('Done')

        click.echo('Generating the Answer Comment..')
        for i in Question.objects.all():
            for j in range(5):
                c = Answer(
                    author = random.choice(User.objects.all()),
                    content = fake.text(200),
                    )
                i.answer.append(c)
                i.save()
        click.echo('Done')


        click.echo('Generating the Answer Reply..')
        for i in Question.objects.all():
            for j in range(5):
                c = Answer(
                    author = random.choice(User.objects.all()),
                    content = fake.text(200),
                    reply = random.choice(i.answer)._id
                )
                i.answer.append(c)
                i.save()

        click.echo('Done')
Example #10
0
def create_question(db: Session, input: CreateQuestionInput):
    c = Question(**input.dict())
    db.add(c)
    db.commit()
    db.refresh(c)
    return c