Esempio n. 1
0
def article_details(id):
    """Checking the article existence"""
    article = Article.query.get(id)
    if not article:
        return response(message="Article not found", status=404)
    """Detail of the article"""
    if request.method == 'GET':
        article = article_schema.dump(article)
        return response(message="Data fetched successfully.",
                        data=article.data,
                        status=200)
    """Updating the  article"""
    if request.method == "PUT":
        article_serializer = article_update_schema.load(request.get_json()
                                                        or {},
                                                        instance=article,
                                                        partial=True)
        if article_serializer.errors:
            return response(message="Incorrect data were provided.",
                            data={**article_serializer.errors},
                            status=400)

        article = article_serializer.data
        article.save()
        article = article_schema.dump(article)
        return response(message=F"Article updated successfully.",
                        data=article.data,
                        status=200)

    if request.method == 'DELETE':
        article.delete()
        return response(message="Article deleted successfully.", status=200)
Esempio n. 2
0
def login():
    """View for login user"""
    login_data = user_login_schema.load(request.get_json() or {})

    if login_data.errors:
        return response(
            message="Incorrect data were provided",
            data={**login_data.errors},
            status=400
        )
    user = authenticate(**login_data.data)
    if not user:
        return response(
            message="Invalid credentials.",
            status=400
        )

    user_data = user_schema.dump(user).data
    payload = {
        'email': user.email,
        'id': user.id
    }
    access_token = create_access_token(identity=payload)

    res, status_code = response(
        data=user_data,
        message="User logged in successfully.",
        status=200
    )
    set_access_cookies(res, access_token)
    return res, status_code
Esempio n. 3
0
def users():
    """view for get users"""
    log.info("Users")
    # print(a)
    if request.method == "GET":
        users = users_schema.dump(User.query.all())
        return response(
            message="Data fetched successfully.",
            data=users.data,
            status=200
        )

    """View for Creating the user."""
    if request.method == 'POST':
        user_data = user_schema.load(request.get_json() or {})

        if user_data.errors:
            return response(
                message="Incorrect data were provided.",
                data={**user_data.errors},
                status=400
            )

        user = user_data.data
        user.save()
        user = user_schema.dump(user)
        return response(
            message="User created successfully.",
            data=user.data,
            status=201
        )
Esempio n. 4
0
def user_details():
    """get user details"""
    serializer = user_schema.dump(request.user)

    return response(data=serializer.data,
                    message="Data fetched successfully.",
                    status=200)
Esempio n. 5
0
def logout():
    jti = get_raw_jwt()['jti']
    blacklist.add(jti)
    res, status_code = response(
        message="Logged out successfully.",
        status=200
    )
    unset_jwt_cookies(res)
    return res, status_code
Esempio n. 6
0
def articles():
    """view for get articles"""
    if request.method == "GET":
        articles = articles_schema.dump(Article.query.all())
        return response(message="Data fetched successfully.",
                        data=articles.data,
                        status=200)
    """View for Creating the article."""
    if request.method == 'POST':
        article_data = article_create_schema.load(request.get_json() or {})

        if article_data.errors:
            return response(message="Incorrect data were provided.",
                            data={**article_data.errors},
                            status=401)

        article = article_data.data
        article.save()
        article = article_schema.dump(article)
        return response(message="Article created successfully.",
                        data=article.data,
                        status=201)
def token_in_blacklist_callback(message):
    return response(message=message, status=401)
def my_unauthorized_callback(reason):
    log.info(reason)
    return response(message="Unauthorized request", status=401)
def my_invalid_token_callback(invalid_token):
    return response(message="Invalid token.", status=401)
def my_expired_token_callback(expired_token):
    return response(message="Token has expired.", status=401)