Пример #1
0
 def add_comment(self):
     ''' this method first checks if a parent exists and if it does, it adds an comment to it
     True is returned for success and False when it fails to add the comment '''
     pprint("adding comment...")
     pprint(self.json())
     if self.parent.lower() == 'question':
         # print(self.json())
         if Question.get_question_by_id(self.parent_id):
             try:
                 handler = CommentsHandler()
                 handler.insert_qn_comment(self.user_id, self.parent_id,
                                           self.comment)
             except:
                 return False
             return True
         return False
     elif self.parent.lower() == 'answer':
         if Answer.get_answer_by_ans_id(self.parent_id):
             try:
                 handler = CommentsHandler()
                 handler.insert_ans_comment(self.user_id, self.parent_id,
                                            self.comment)
             except:
                 return False
             return True
         return False
     return False
Пример #2
0
    def post(self, answerId):
        ''' method to add a comment to a question '''
        try:
            # check if the submitted questionId is in the expected format
            answerId = float(answerId)
        except:
            return {"message": "The answer id should be a float"}, 400

        # check if the answer exists
        if not Answer.get_answer_by_ans_id(answerId):
            return {"message": "Sorry, that answer doesn't exist"}, 400

        parser = reqparse.RequestParser()
        parser.add_argument('comment',
                            type=str,
                            required=True,
                            help="The comment field can't be empty")

        data = parser.parse_args()
        ''' validate data sent '''
        if not clean_input(data['comment']):
            return {'message': 'The comment should be a string'}, 400

        # ''' validate that the comment has been given before'''
        if Comment.check_for_repeated_comment(data['comment'], 'answer'):
            return {
                'message': 'Sorry, that comment has already been given'
            }, 400

        user_id = get_jwt_identity()

        comment = Comment(user_id, data['comment'], 'answer', answerId)
        try:
            pprint("adding answer comment...")
            pprint(comment.json())
            if comment.add_comment():
                return {'message': 'Your comment was successfully added'}, 201
        except:
            return {'message': 'There was a problem adding the comment'}, 500

        return {
            'message': 'There was a problem adding the comment to the answer'
        }, 500
Пример #3
0
    def get(self, answerId):
        ''' method to add a comment to a question '''
        try:
            # check if the submitted questionId is in the expected format
            answerId = float(answerId)
        except:
            return {"message": "The answer id should be a float"}, 400

        # check if the answer exists
        if not Answer.get_answer_by_ans_id(answerId):
            return {"message": "Sorry, that answer doesn't exist"}, 400

        try:
            comments = Comment.get_comments_by_parent_id("answer", answerId)
            pprint(comments)
            return {"comments": comments}, 200
        except:
            return {'message': 'There was a problem getting the comments'}, 500

        return {
            'message':
            "There was a problem getting the comments to that answer..."
        }, 500