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
Beispiel #2
0
    def post(self, questionId):
        ''' method to add an answer '''
        try:
            # check if the submitted questionId is in the expected format
            questionId = float(questionId)
        except:
            return {"message": "The question id should be a float"}, 400

        # check if the question exists
        if not Question.get_question_by_id(questionId):
            return {"message": "Sorry, that question doesn't exist"}, 400

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

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

        if not check_answer_length(body):
            return {'message': 'Answer is too short'}, 400
        ''' validate that the question hasn't been asked before '''
        if Answer.check_repeated_ans(body, questionId):
            return {
                'message': 'Sorry, that answer has already been given'
            }, 400

        user_id = get_jwt_identity()
        answer = Answer(body, questionId, user_id)
        try:
            if Answer.add_answer(answer) == True:
                return {'message': 'Your answer was successfully added'}, 201
        except:
            return {'message': 'There was a problem adding the answer'}, 500

        return {'message': 'There was a problem adding the answer'}, 500
    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
    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
 def get(self):
     """ this method gets questions asked by the user """
     return {"answers": Answer.get_answers_by_user_id(get_jwt_identity())}
Beispiel #6
0
    def put(self, questionId, answerId):
        ''' this method deletes a question only if the user is the author '''
        parser = reqparse.RequestParser()
        parser.add_argument('action',
                            type=str,
                            required=True,
                            help="The action field can't be empty")
        parser.add_argument('body', type=str)
        data = parser.parse_args()
        action = data['action'].strip()
        # print(action)

        if action.lower() != 'update' and action.lower() != 'accept':
            return {
                'message': 'The action value should be update or accept'
            }, 400

        user_id = get_jwt_identity()
        db_question = Question.get_question_by_id(questionId)
        if not db_question:
            return {'message': 'Question not found'}, 404

        answer = Answer.get_answer_by_qn_id(answerId, questionId)
        if answer:
            print(db_question.json())
            print(answer.json())
            if action.lower() == 'update':
                if answer.user_id == user_id:
                    body = data['body'].lower().strip()
                    if not body:
                        return {'message': 'The body should not be empty'}, 400
                    ''' validate data sent '''
                    if not clean_input(body):
                        return {'message': 'The body should be a string'}, 400

                    if not check_answer_length(body):
                        return {'message': 'Answer is too short'}, 400
                    ''' validate that the answer hasn't been given before '''
                    if Answer.check_repeated_ans(body, questionId):
                        return {
                            'message': 'Sorry, that answer with already exits'
                        }, 400
                    try:
                        if Answer.update_answer(answer.ans_id, body):
                            return {
                                "message": "Answer was successfully updated"
                            }, 201

                        return {
                            "message":
                            "There was a problem updating the answer, Please try again later..."
                        }, 500
                    except:
                        return {
                            'message': 'There was a problem adding the answer'
                        }, 500
                return {
                    'message':
                    "Sorry you don't have permission to update this answer"
                }, 401
            elif action.lower() == 'accept':
                if db_question.user_id == user_id:
                    try:
                        if Answer.accept_answer(answer.ans_id,
                                                db_question.qn_id):
                            return {
                                "message": "Answer was successfully accepted"
                            }, 200

                        return {
                            "message":
                            "There was a problem accepting the answer, Please try again later..."
                        }, 500
                    except:
                        return {
                            'message':
                            'There was a problem accepting the answer, please try again later...'
                        }, 500
                return {
                    'message':
                    "Sorry you don't have permission to accept this answer"
                }, 401

        return {'message': 'Answer not found'}, 404
Beispiel #7
0
 def get(self):
     ''' method get all the available answers '''
     return {'answers': Answer.get_answers()}