Esempio n. 1
0
    def post(self):
        '''method that saves a user resource'''
        parser = reqparse.RequestParser()
        parser.add_argument('username',
                            help='The username field cannot be blank',
                            required=True, type=str)
        parser.add_argument('email', help='The email field cannot be blank',
                            required=True, type=str)
        parser.add_argument('password',
                            help='The password field cannot be blank',
                            required=True, type=str)
        data = parser.parse_args()
        if Validate.validate_username_format(data['username']):
            return {'message': 'Invalid username username should be of form "username"'}, 400
        if not Validate.validate_length_username(data['username']):
            return {'message': 'The length of username should be atleast 4 characters'}, 400
        if not Validate.validate_password_length(data['password']):
            return {'message': 'the length of the password should be atleast 6 characters'}, 400
        if re.match(r"^[1-9]\d*(\.\d+)?$", data['password']):
            return {'message': 'the username and password should be of type string'}, 400
        if not Validate.validate_email_format(data['email']):
            return {'message': 'Invalid email.The email should be of type "*****@*****.**"'}, 400
        if User.find_by_username(data['username']):
            return {'message': 'This username is already taken,kindly try another username'}, 409
        if User.find_by_email(data['email']):
            return {'message': 'This email is already taken'}, 409

        user = User.save(self, username=request.json['username'],
                         email=request.json['email'],
                         password=User.generate_hash(request.json['password']),
                         date_created=datetime.now(),
                         date_modified=datetime.now())
        return {"message": "The user signed up successfully", "data": user}, 201
Esempio n. 2
0
    def post(self):
        '''Method that registers a user'''
        parser = reqparse.RequestParser()
        parser.add_argument('username', help='This field cannot be blank', required=True)
        parser.add_argument('password', help='This field cannot be blank', required=True)
        parser.add_argument('email', help='This field cannot be blank', required=True)
        data = parser.parse_args()
        if not Validate.validate_length_username(data['username']):
            return {'message': 'The length of username should be atleast 4 characters'}, 400
        if not Validate.validate_password_length(data['password']):
            return {'message': 'the length of the password should be atleast 6 characters'}, 400
        if re.match(r"^[1-9]\d*(\.\d+)?$", data['password']):
            return {'message': 'the username and password should be of type string'}, 400
        if not Validate.validate_email_format(data['email']):
            return {'message': 'Invalid email.The email should be of type "*****@*****.**"'}, 400
        if User.find_by_username(data['username']):
            return {'message': 'This username is already taken,kindly try another username'}, 409
        if User.find_by_email(data['email']):
            return {'message': 'This email is already taken'}, 409
        if Validate.validate_username_format(data['username']):
            return ({'message':'Invalid username.The username should be of type "Username"'}), 400
        user = User.save(username=data['username'], email=data['email'],
                         password=User.generate_hash(data['password']),date_created=datetime.now(),date_modified=datetime.now())

        return ({'message':'you have successfully registered','data':user}),201
Esempio n. 3
0
    def post(self):
        '''Create a question'''
        parser = reqparse.RequestParser()
        parser.add_argument('title', help='The title field cannot be blank',
                            required=True, type=str)
        parser.add_argument('body', help='The body field cannot be blank',
                            required=True, type=str)
        data = parser.parse_args()
        search_keys = ("title", "body")
        if all(key in data.keys() for key in search_keys):
            body = data.get("body").strip()
            does_qstn_exist = Validate.is_question_exist(body)
            if does_qstn_exist:
                return ({"message": "Question already exists, check it out for an answer"}), 409
            if re.match(r"^[1-9]\d*(\.\d+)?$", data['title']):
                return {'message': 'the title should be of type string'}, 400
            if len(data['title']) < 10:
                return {'message': 'The length of the title should be atleast 10 characters'}, 400
            if len(data['body']) < 20:
                return {'message': 'The length of your question content should be atleast 15 characters'}, 400

            question = Question.save(self, title=request.json['title'],
                                     body=request.json['body'],
                                     user_id=get_jwt_identity(),
                                     date_created=datetime.now(),
                                     date_modified=datetime.now())
            return {"message": "The question posted successfully",
                    "data": question}, 201
Esempio n. 4
0
 def put(self, id, answer_id):
     '''Accept an answer and update an answer'''
     question = Question.get_by_id(id=id)
     logged_in_user = get_jwt_identity()
     answer_to_update = Answer.get_by_id(answer_id)
     print(answer_to_update)
     if question:
         if answer_to_update:
             if answer_to_update['user_id'] == logged_in_user:
                 parser = reqparse.RequestParser()
                 parser.add_argument('body',
                                     help='The body field cannot be blank',
                                     required=True, type=str)
                 parser.parse_args()
                 answer_to_update = Answer.update(id=answer_id,
                                                  body=request.json['body'],
                                                  user_id=get_jwt_identity(),
                                                  accept=False)
                 return {"message": "You have successfully updated the answer",
                         "data": answer_to_update}, 201
             if question['user']["id"] == logged_in_user and answer_to_update['question_id'] == question["id"]:
                 if Validate.check_answer_accepted(question_id=question["id"]):
                     return {"message": "The question has an already acceped answer"}, 405
                 Answer.accept(id=answer_id)
                 return {"message": "You have successfully accepted the answer"}, 201
             else:
                 return {"message": "You are not authorized to modify the answer"}, 401
         return {"message": "Answer not found"}, 404
     return {"message": "No question found with that answer"}, 404
    def post(self):
        args = self.args.parse_args()
        validate = Validate.login(args['username'], args['password'])
        if validate['result'] == 'success':
            username = str(validate['data'].username)
            access_token = create_access_token(identity=validate['data'])
            validate = {"access_token": access_token}
            return validate

        else:
            return validate, 400
Esempio n. 6
0
    def post(self):
        args = self.args.parse_args()
        validate = Validate.login(args['username'], args['password'])
        if validate['result'] == 'success':
            login_user(validate['data'])
            validate = {
                "result": "success",
                "message": "Succesfully logged in"
            }
            return validate

        else:
            return validate, 400
Esempio n. 7
0
    def delete(self, user_id=None):
        if user_id is None:
            result = {"result": "failed", "message": "Requires user ID"}
        else:
            current_user = str(g.currentUser.id)
            validate_admin = Validate.admin(current_user)
            if validate_admin['result'] == "success":
                if user_id == current_user:
                    result = {
                        "result": "failed",
                        "message": "Can't delete own user"
                    }
                else:
                    result = User().delete(user_id)
            else:
                result = validate_admin

        return result
Esempio n. 8
0
    def post(self):
        '''Method to add an entry(POST request)'''
        parser = reqparse.RequestParser()
        parser.add_argument('title', help='This field cannot be blank', required=True)
        parser.add_argument('description', help='This field cannot be blank', required=True)
        data = parser.parse_args()
        search_keys = ("title", "description")
        if all(key in data.keys() for key in search_keys):
            description = data.get("description").strip()
            does_entry_exist = Validate.is_entry_exist(description)
            if does_entry_exist:
                return {"message": "This entry is already posted"},409
            if re.match(r"^[1-9]\d*(\.\d+)?$", data['title']):
                return {'message': 'the title should be of type string'}, 400
            if len(data['title']) < 10:
                return {'message': 'The length of the title should be atleast 10 characters'}, 400
            if len(data['description']) < 20:
                return {'message': 'The length of your question content should be atleast 15 characters'},400
            entry = Entry.save(user_id=get_jwt_identity(), date_created=str(datetime.now()),
                               date_modified=str(datetime.now()), title=data['title'],
                               description=data['description'])

            return {"status": "Success", "data": entry}, 201