예제 #1
0
 def test_create(self):
     created = Question.create(title='test question',
                               body='test body',
                               author='meetmangukiya',
                               tags=[],
                               pool=self.pool)
     retrieved = Question.from_qid(created._qid, pool=self.pool)
     # import pdb; pdb.set_trace()
     self.assertEqual(retrieved, created)
예제 #2
0
 def test_questions(self):
     tag = Tag.create(name='test_tag', description='..', pool=self.pool)
     question = Question.create(title='asa',
                                author='meetmangukiya',
                                body='',
                                tags=['test_tag'],
                                pool=self.pool)
     self.assertEqual(len(tag.questions()), 1)
예제 #3
0
 def test_as_dict(self):
     question = Question.from_qid(1, self.pool)
     self.assertEqual(
         {
             'title': 'Why should we use java for our backend?',
             'description': '',
             'upvotes': 10,
             'downvotes': 2,
             'author': 'meetmangukiya',
             'qid': 1,
             'tags': ['java', 'backend']
         }, question.as_dict())
예제 #4
0
파일: app.py 프로젝트: meetmangukiya/quast
def question_info(qid):
    """
    Return all question information.
    """
    question = Question.from_qid(qid, POOL)

    # For serving data as JSON
    #
    # answers = question.answers()
    # data = question.as_dict()
    # data['answers'] = list(map(lambda x: x.as_dict(), answers))
    # return jsonify(data)
    return render_template('question/details.html', question=question)
예제 #5
0
파일: app.py 프로젝트: meetmangukiya/quast
def create_answer(qid):
    """
    Adds an answer to a given question.
    """
    if request.method == 'POST':
        author = session['username']
        json = data_as_dict(request)
        body = json.get('body')
        answer = Answer.create(body=body, author=author, qid=qid, pool=POOL)
        return redirect(url_for("/question/{}#answer-{}".format(qid, author)))
    else:
        return render_template('answer/new.html',
                               question=Question.from_qid(qid, POOL))
예제 #6
0
    def questions(self):
        """
        Returns questions that are tagged with this tag.
        """
        conn = self._pool.getconn()
        with conn.cursor() as curs:
            curs.execute("SELECT qid FROM question_tags WHERE tag=%s",
                         (self._name, ))
            questions = []
            for qid in curs:
                questions.append(Question.from_qid(qid=qid, pool=self._pool))
        self._pool.putconn(conn)

        return questions
예제 #7
0
파일: app.py 프로젝트: meetmangukiya/quast
def create_question():
    """
    Create a new question.
    """
    if request.method == 'POST':
        author = session['username']
        json = data_as_dict(request)
        title = json.get('title')
        body = json.get('body')
        tags = list(map(lambda x: x.strip(), json.get('tags').split(',')))
        question = Question.create(author=author,
                                   title=title,
                                   body=body,
                                   tags=tags,
                                   pool=POOL)
        return jsonify(question.as_dict())
    else:
        return render_template('question/new.html')
예제 #8
0
파일: User.py 프로젝트: meetmangukiya/quast
 def get_questions(self):
     """
     Return a list of ``quast.models.Question`` objects representing
     questions asked by user ``username``.
     """
     conn = self._pool.getconn()
     questions = []
     with conn.cursor() as curs:
         curs.execute(
             "SELECT title, description, upvotes, downvotes, qid "
             "FROM questions WHERE author=%s", (self._username, ))
         for title, body, upvotes, downvotes, qid in curs.fetchmany():
             questions.append(
                 Question(title=title,
                          body=body,
                          upvotes=upvotes,
                          downvotes=downvotes,
                          qid=qid,
                          author=self._username))
     self._pool.putconn(conn)
     return questions
예제 #9
0
 def test_from_qid(self):
     question = Question.from_qid(1, self.pool)
     self.assertEqual(question._author, 'meetmangukiya')
예제 #10
0
 def test_repr(self):
     question = Question.from_qid(1, self.pool)
     self.assertEqual('<Question: (qid=1)>', repr(question))
예제 #11
0
 def test_delete(self):
     question = Question.from_qid(1, self.pool)
     assert question.delete()
     with self.assertRaises(TypeError):
         Question.from_qid(1, self.pool)
예제 #12
0
 def test_answers(self):
     question = Question.from_qid(1, self.pool)
     self.assertEqual(len(question.answers()), 2)