Esempio n. 1
0
    def put(self, questionid, answerid):
        """modify answer or mark it as preffered"""
        db = Database()
        valid = Validator()
        user_id = get_jwt_identity()
        answer = db.get_by_argument("answers", "question_id", questionid)
        if answer:
            if user_id == answer[2]:
                preffered = modify_arg.parse_args()
                preffer = preffered['preffered']
                if preffer != "True":
                    return {"message": "preffered has no value True"}, 400
                db.update_answer_record("answers", "preffered", preffer,
                                        "answer_id", answerid)
                return {"message": "answer marked as preffered"}, 200

            answer1 = db.get_by_argument("answers", "answer_id", answerid)
            if answer1:
                if user_id == answer1[2]:
                    modifyans = modify_arg.parse_args()
                    eddited_ans = modifyans["answer"]
                    if valid.q_validate(eddited_ans) is False:
                        return {"message": "answer should contain letters"}
                    db.update_answer_record("answers", "reply", eddited_ans,
                                            "answer_id", answerid)
                    return {"message": "answer updated sucessfully"}, 200
        return {"message": "warning you are not authorized"}, 403
Esempio n. 2
0
    def post(self):
        """user needs email used to signup
        and password to login"""
        valid = Validator()
        db = Database()
        user_data = login_arg.parse_args()
        email = user_data['email']
        password = user_data['password']

        if valid.valid_email(email) is False:
            return {"error": "invalid email"}, 400
        if valid.valid_password(password) is False:
            return {"error": "invalid password:check length"}, 400
        user = db.get_by_argument('users', 'email', email)
        if user == None:
            return{"error": "wrong email address"}, 404
        if(email == user[2] and
           bcrypt.check_password_hash(user[3], password) is False):
            return {"error": "wrong password"}, 400

        if(email != user[2] and
           bcrypt.check_password_hash(user[3], password) is False):
            return {"error": "invalid login credentials"}, 400

        if(email == user[2] and
           bcrypt.check_password_hash(user[3], password) is True):
            access_token = create_access_token(identity=user[0],
            expires_delta=False)
            return {"message": "login successful",
                    "username": user[1],
                    "userid": user[0],
                    "access_token": access_token}, 200
Esempio n. 3
0
    def get(self, questionid):
        """route to get a single question by id and
        all the available answers"""
        db = Database()
        questions = db.get_by_argument('questions', 'question_id', questionid)
        if questions:
            question = {
                "question_id": questions[0],
                "title": questions[2],
                "description": questions[3],
                "date posted": str(questions[4])
            }
            answers = db.query_all_where_id('answers', 'question_id',
                                            questionid)

            answerlist = []
            for ans in answers:
                answerdic = {
                    "answer_id": ans[0],
                    "question_id": ans[1],
                    "user_id": ans[2],
                    "answer": ans[3],
                    "preffered": ans[4],
                    "up_vote": ans[5],
                    "down_vote": ans[6]
                }
                answerlist.append(answerdic)
            return {"question": question, "answers": answerlist}, 200
        return {"message": "no question by that id"}, 400
Esempio n. 4
0
 def post(self, questionid):
     """method to post a question answer
     anybody can post an answer to a question"""
     valid = Validator()
     db = Database()
     user_id = get_jwt_identity()
     ans_input = ans_arg.parse_args()
     user_answer = ans_input['answer']
     if valid.q_validate(user_answer) is False:
         return {"message": "answer must contain leters"}, 400
     if db.get_by_argument("questions", "question_id", questionid):
         if db.get_by_argument('answers', 'reply', user_answer):
             return {'message': 'That answer already exists'}, 409
         db.insert_answer_data(questionid, user_answer, user_id)
         return {"message": "Your answer was posted successfully"}, 201
     return {"message": "question id does not exist"}, 404
Esempio n. 5
0
 def post(self, answerid):
     """this route will upvote an answer"""
     db = Database()
     votes = db.get_by_argument("answers", "answer_id", answerid)
     if votes:
         upvote = votes[5] + 1
         db.update_answer_record("answers", "up_vote", upvote, "answer_id",
                                 answerid)
         return {"message": "answer upvoted"}, 201
     return {"message": "No answer by that answer_id"}, 404
Esempio n. 6
0
    def post(self):
        """This routes post a new question if it does not exist"""
        valid = Validator()
        db = Database()
        qst_input = qst_arg.parse_args()
        title = qst_input['title']
        description = qst_input['description']
        if valid.q_validate(title) is False:
            return {"error": "title is invalid"}, 400
        if valid.q_validate(description) is False:
            return {"error": "description is invalid"}, 400
        if db.get_by_argument('questions', 'description', description):
            return {'message': 'question already exists'}, 409
        if db.get_by_argument('questions', 'title', title):
            return {'message': 'question already exists'}, 409

        user_id = get_jwt_identity()
        db.insert_question_data(user_id, title, description)
        return {"message": "Question created successfully"}, 201
Esempio n. 7
0
 def delete(self, questionid):
     """this routes delete a question and all answers provided"""
     db = Database()
     user_id = get_jwt_identity()
     questions = db.get_by_argument('questions', 'question_id', questionid)
     if questions:
         if user_id in questions:
             db.delete_question(questionid)
             return {
                 'message': 'Question deleted successfully and answers'
             }, 200
         return {
             'Warning': 'You have no rights to delete this question'
         }, 403
     return {'message': 'No question by that id'}, 404
Esempio n. 8
0
 def post(self):
     """user needs username, valid email and
        password to signup"""
     valid = Validator()
     db = Database()
     user_data = signup_arg.parse_args()
     username = user_data['username']
     email = user_data['email']
     password = user_data['password']
     if valid.valid_username(username) is False:
         return {"error": "invalid username"}, 400
     if valid.valid_email(email) is False:
         return {"error": "invalid email"}, 400
     if valid.valid_password(password) is False:
         return {"error": "invalid password"}, 400
     if db.get_by_argument('users', 'email', email):
         return {'message': 'user already exists'}, 409
     hashed_pass = bcrypt.generate_password_hash(password).decode('utf-8')
     db.insert_user_data(username, email, hashed_pass)
     return{"message": "Account created"}, 201