Пример #1
0
def sign_up():
    try:
        schema = (user_model.schema_auth).copy()
        required_data = {}
        for key in schema:
            required_data[key] = schema[key].copy()
            required_data[key]['position'] = "body"

        # get request body for multipart form data body
        body_form = dict(request.form)

        # get request body for json body
        body_json = request.get_json()

        body = {}
        if body_form:
            body = body_form

        if body_json:
            body = body_json

        validate_body = user_model.validator.validate(body)
        if validate_body:
            username = body['username']
            password = body['password']
            role = body['role']

            check_username = user_model.UserModel.query.filter_by(
                username=username).first()
            if hasattr(check_username, '_id'):
                required_data['username']["unique"] = True
                required_data['username']["description"] = "username is exists"
                response = badrequest_response(required_data=required_data)
            else:
                data = user_model.UserModel(username=username,
                                            password=password,
                                            role=role)
                db.session.add(data)
                db.session.commit()
                response = created_response()
        else:
            schema = (user_model.schema).copy()
            required_data = {}
            for key in schema:
                required_data[key] = schema[key].copy()
                required_data[key]['position'] = "body"
            response = badrequest_response(required_data=required_data)
        return response
    except:
        print("Unexpected error:", sys.exc_info())
        response = server_error_response()
        return response


# /end routes
Пример #2
0
def update(_id):
    data_user = request.args.get('data_user')
    try:
        # get request body for multipart form data body
        body_form = dict(request.form)

        # get request body for json body
        body_json = request.get_json()

        body = {}
        if body_form:
            body = body_form

        if body_json:
            body = body_json

        validate_body = book_model.validator.validate(body)
        if validate_body:
            data = book_model.BookModel.query.get(_id)
            if hasattr(data, '_id'):
                data.title = body['title']
                data.description = body['description']
                data.updated_at = datetime
                data.updated_by = data_user['_id']

                db.session.add(data)
                db.session.commit()
                response = updated_response()
            else:
                required_data = {
                    "_id": {
                        "type": "integer",
                        "required": True,
                        "position": "query",
                        "description": f"data for _id ({_id}) not found"
                    }
                }
            response = badrequest_response(required_data=required_data)
        else:
            schema = (book_model.schema).copy()
            required_data = {}
            for key in schema:
                required_data[key] = schema[key].copy()
                required_data[key]['position'] = "body"
            response = badrequest_response(required_data=required_data)
        return response
    except:
        print("Unexpected error:", sys.exc_info())
        response = server_error_response()
        return response
Пример #3
0
def delete(_id):
    try:
        data = book_model.BookModel.query.get(_id)
        if data:
            db.session.delete(data)
            db.session.commit()
            response = deleted_response()
        else:
            required_data = {
                "_id": {
                    "type": "integer",
                    "required": True,
                    "position": "query",
                    "description": f"data for _id ({_id}) not found"
                }
            }
            response = badrequest_response(required_data=required_data)
        return response
    except:
        print("Unexpected error:", sys.exc_info())
        response = server_error_response()
        return response


# /end routes
Пример #4
0
def create():
    try:
        data_user = request.args.get('data_user')

        # get request body for multipart form data body
        body_form = dict(request.form)

        # get request body for json body
        body_json = request.get_json()

        body = {}
        if body_form:
            body = body_form

        if body_json:
            body = body_json

        validate_body = book_model.validator.validate(body)
        if validate_body:
            title = body['title']
            description = body['description']
            created_by = data_user['_id']

            data = book_model.BookModel(title=title,
                                        description=description,
                                        created_by=created_by)
            db.session.add(data)
            db.session.commit()
            response = created_response()
        else:
            schema = (book_model.schema).copy()
            required_data = {}
            for key in schema:
                required_data[key] = schema[key].copy()
                required_data[key]['position'] = "body"
            response = badrequest_response(required_data=required_data)
        return response
    except:
        print("Unexpected error:", sys.exc_info())
        response = server_error_response()
        return response
Пример #5
0
def create():
        try:
            # get request body for multipart form data body
            body_form = dict(request.form)

            # get request body for json body
            body_json = request.get_json()
            
            body = {}
            if body_form:
                body = body_form
            
            if body_json:
                body = body_json

            validate_body = user_model.validator.validate(body)
            if validate_body:
                username = body['username']
                password = body['password']

                data = user_model.UserModel(username=username, password=password)
                db.session.add(data)
                db.session.commit()
                response = created_response()
            else:
                schema = (user_model.schema).copy()
                required_data = {}
                for key in schema:
                    required_data[key] = schema[key].copy()
                    required_data[key]['position'] = "body"
                response = badrequest_response(required_data=required_data)
            return response
        except:
            print("Unexpected error:", sys.exc_info())
            response = server_error_response()
            return response
Пример #6
0
def sign_in():
    try:
        schema = (user_model.schema_auth).copy()
        required_data = {}
        for key in schema:
            required_data[key] = schema[key].copy()
            required_data[key]['position'] = "body"

        # get request body for multipart form data body
        body_form = dict(request.form)

        # get request body for json body
        body_json = request.get_json()

        body = {}
        if body_form:
            body = body_form

        if body_json:
            body = body_json

        validate_body = user_model.validator_auth.validate(body)
        if validate_body:
            username = body['username']
            password = body['password']

            data = user_model.UserModel.query.filter_by(
                username=username).first()
            if hasattr(data, '_id'):
                check_password = data.check_password(password)
                if check_password:
                    secretKey = os.getenv('JWT_TOKEN')
                    data_user = {
                        "_id": data._id,
                        "username": data.username,
                        "role": data.role
                    }
                    encrypt = encrypt_fernet(str(data_user))
                    payload = {
                        "exp": datetime.utcnow() + timedelta(days=365),
                        "iat": datetime.utcnow(),
                        "data_user": encrypt.decode()
                    }
                    encoded_jwt = jwt.encode(payload,
                                             secretKey,
                                             algorithm='HS256')
                    data_user['token'] = encoded_jwt
                    response = ok_response(data=data_user)
                else:
                    required_data['password'][
                        "description"] = "password is wrong"
                    response = badrequest_response(required_data=required_data)
            else:
                required_data['username']["description"] = "username is wrong"
                response = badrequest_response(required_data=required_data)
        else:
            response = badrequest_response(required_data=required_data)
        return response
    except:
        print("Unexpected error:", sys.exc_info())
        response = server_error_response()
        return response